1.2.2 • Published 5 years ago

knex-pg-builder v1.2.2

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

knex-pg-builder homepage

npm The MIT License GitHub David manpm Type definitions code style: prettier semantic-release Commitizen friendly Coverage Status Travis CI Known Vulnerabilities Greenkeeper badge

A small utility to automate the table creation for knex.

Why? start with 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]]
1.2.2

5 years ago

1.2.1

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago