1.0.1 • Published 5 years ago

@graft/knex v1.0.1

Weekly downloads
4
License
BSD-2-Clause
Repository
github
Last release
5 years ago

@graft/knex

Installation

yarn add @graft/knex

Usage

primaryKey

import {primaryKey} from '@graft/knex'

primaryKey = (
  table: Knex.CreateTableBuilder,
  name = 'id'
): Knex.ColumnBuilder

A Knex column builder that uses an incrementing integer primary key.

import {knex, primaryKey} from '@graft/knex'

await knex.schema.createTable('items', table => {
  primaryKey(table)
})

primaryUuid

import {primaryUuid} from '@graft/knex'

primaryUuid = (
  knex: Knex,
  table: Knex.CreateTableBuilder,
  column = 'id'
): Knex.ColumnBuilder

A Knex column builder that uses a uuid as your primary key. It's often more efficient to use integers for your primary keys, but if you need something non-sequential for privacy or security Postgres has great uuid support through the uuid-ossp extension. This utility creates a unique uuid field defaulting to the column name 'id', using uuid_generate_v4() to generate default values.

import {knex, primaryUuid} from '@graft/knex'

await knex.schema.createTable('orders', table => {
  primaryUuid(knex, table)
})

To use, make sure to include this in your initial database migration:

import {knex} from '@graft/knex'

await knex.raw('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";')

foreignUuid

import {foreignUuid} from '@graft/knex'

foreignUuid = (
  table: CreateTableBuilder,
  column: string,
  reference: {column: string; table: string},
  required?: boolean
): Knex.ColumnBuilder

This tool makes it easy to reference a uuid column in another table.

import {foreignUuid} from '@graft/knex'

foreignUuid(table, 'address', {
  column: 'id',
  table: 'address',
}).comment(`The Order''s Address.`)

updateTimestamp

Defines a trigger on the given table to keep the updated_at field up to date.

import {updateTimestamp} from '@graft/knex'

await updateTimestamp(knex, 'vehicles')