knex update example

Example 1: knex datatypes

table.increments('id')
  table.string('account_name')
  table.integer('age')
  table.float('age')
  table.decimal('balance', 8, 2)
  table.boolean('is_admin')
  table.date('birthday')
  table.time('created_at')
  table.timestamp('created_at').defaultTo(knex.fn.now())
  table.json('profile')
  table.jsonb('profile')
  table.uuid('id').primary()

Example 2: knex js join schema

exports.up = (knex) => {
  knex.schema.createTable('bookings', (table) => {
    table.increment('booking_id').primary()
    table.foreign('user_id').references('user_id').inTable('users').notNullable().onDelete('CASCADE').index()
    table.foreign('room_id').references('room_id').inTable('rooms').notNullable().onDelete('CASCADE').index()
    table.integer('total_person').notNullable().defaultTo(0)
    table.datetime('booking_time').notNullable().defaultTo(knex.fn.now())
    table.string('noted').notNullable()
    table.datetime('check_in_time').defaultTo(knex.fn.now())
    table.datetime('check_out_time').defaultTo(knex.fn.now())
    table.timestamp('created_at').defaultTo(knex.fn.now())
    table.timestamp('update_at').defaultTo(knex.fn.now())
    table.timestamp('delete_at').defaultTo(knex.fn.now())
  })
}

exports.down = (knex) => knex.schema.dropTable('bookings')

Tags:

Misc Example