knex-pg-builder
v1.2.2
Published
A small utility to automate the postgres's table creation for knex
Downloads
3
Readme
knex-pg-builder homepage
A small utility to automate the table creation for knex.
Why?
I don't want to use ORMs like bookshelf, but I still miss the one-command database build process.
Installation
npm i -D knex-pg-builder
Basic Usage
const { createSchemas, clearSchemas } = require('knex-pg-builder');
db.transaction(async function (trx) {
await createSchemas(trx, new Map([
['tableA', {
id: t => t.increments('id'),
created_at: (t, trx) => t.datetime('create_at').defaultTo(trx.fn.now(6))
}]
]), 'awesome_schema')
})
check __tests__/test.ts
for a complete example.
Docs
Tips
- why use Map instead of object?
When a table A has a reference to another table B, table A must be created after
tableB, but object.keys
doesn't ensure the order, eg.
object.keys({ a: 1, b: 2 }) // may be ['a', 'b'] or ['b', 'a']
but Map will keep the same order when items are set, eg.
var m = new Map()
m.set('a', 1)
m.set('b', 2)
Array.from(m) // generate [['a', 1], ['b', 1]]