knex.schema .dropTableIfExists("contacts") .createTable("contacts", tbl => { tbl.increments().primary(); tbl.text("first_name", 30); tbl.text("last_name", 30); tbl .text("email", 30) .unique() .notNullable(); })
describe('Initializing', function () { it('Should create a database table for caching if it does not exist or empty existing one', async () => { if (await knex.schema.hasTable('cache')) { await knex.table('cache').del() } else { await knex.schema.createTable('cache', function (table) { table.string('key').unique() table.text('value') table.integer('expiration') }) } }) })
knex.schema.createTable("contacts", tbl => { tbl.increments("id").primary(); tbl.text("first_name", 30); tbl.text("last_name", 30); tbl.text("email", 30).notNullable(); })
const createTable = async config => { // Connect to the database config.host = `/cloudsql/${config.connectionName}`; const knex = Knex({client: 'pg', connection: config}); // Create the "votes" table try { await knex.schema.createTable('votes', table => { table.bigIncrements('vote_id').notNull(); table.timestamp('time_cast').notNull(); table.specificType('candidate', 'CHAR(6) NOT NULL'); }); console.log(`Successfully created 'votes' table.`); return knex.destroy(); } catch (err) { console.error(`Failed to create 'votes' table:`, err); if (knex) { knex.destroy(); } } }
table .integer('user_id') .unsigned() .references('users.id'); }) .then(function() {