1.0.1 • Published 6 years ago
@graft/knex v1.0.1
@graft/knex
Installation
yarn add @graft/knexUsage
primaryKey
import {primaryKey} from '@graft/knex'
primaryKey = (
table: Knex.CreateTableBuilder,
name = 'id'
): Knex.ColumnBuilderA 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.ColumnBuilderA 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.ColumnBuilderThis 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')