Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing configurable migrations table name #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ createDb("database-name", {
password: "password",
host: "localhost",
port: 5432,
}, "path/to/migration/files")
}, "path/to/migration/files", {
migrationTableName: "migrations",
logger: console.log,
})
})
.then(() => {/* ... */})
.catch((err) => {
Expand Down Expand Up @@ -133,27 +136,54 @@ Note that file names cannot be changed later.

### Javascript Migrations

By using `.js` extension on your migration file you gain access to all NodeJS features and only need to export a method called `generateSql` that returns a `string` literal like:
We support `.js` files as your migration file, there are five kind of supported exports, all of them needing to return a `string` literal:

#### Plain string literal
```js
// ./migrations/helpers/create-main-table.js
module.exports = `
CREATE TABLE main (
id int primary key
);`
```

// ./migrations/helpers/create-secondary-table.js
module.exports = `
CREATE TABLE secondary (
#### Anonymous function
```js
module.exports = () => `
CREATE TABLE main (
id int primary key
);`
```

#### Async anonymous function
```js
module.exports = async () => new Promise((resolve) => {
setTimeout(() => {
resolve(`
CREATE TABLE main (
id int primary key
);`)
}, 1000)
})
```

// ./migrations/1-init.js
const createMainTable = require('./create-main-table')
const createSecondaryTable = require('./create-secondary-table')
#### generateSql function
```js
module.exports.generateSql = () => `
CREATE TABLE main (
id int primary key
);`
```

module.exports.generateSql = () => `${createMainTable}
${createSecondaryTable}`
#### Async generateSql function
```js
module.exports.generateSql = async () => new Promise((resolve) => {
setTimeout(() => {
resolve(`
CREATE TABLE main (
id int primary key
);`)
}, 1000)
})
```

## Tips
Expand Down
Loading