js knex code example
Example 1: is knex built into node js
$ npm install knex --save
# Then add one of the following (adding a --save) flag:
$ npm install pg
$ npm install sqlite3
$ npm install mysql
$ npm install mysql2
$ npm install oracledb
$ npm install mssql
Example 2: 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 3: 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')