knex having code example
Example 1: knex.js count
knex('users').count('active', {as: 'a'})Outputs:select count(`active`) as `a` from `users`
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')