knex.schema.createTable("contacts", tbl => { tbl.increments("id").primary(); tbl.text("first_name", 30); tbl.text("last_name", 30); tbl.text("email", 30).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 .dropTableIfExists("contacts") .createTable("contacts", tbl => { tbl.increments().primary(); tbl.text("first_name", 30); tbl.text("last_name", 30); tbl .text("email", 30) .unique() .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(); } } }
// Prompt the user for connection details prompt.get(FIELDS, async (err, config) => { if (err) { console.error(err); return; } // Connect to the database const knex = Knex({client: 'pg', connection: config}); // Create the "visits" table try { await knex.schema.createTable('visits', table => { table.increments(); table.timestamp('timestamp'); table.string('userIp'); }); console.log(`Successfully created 'visits' table.`); return knex.destroy(); } catch (err) { console.error(`Failed to create 'visits' table:`, err); if (knex) { knex.destroy(); } } });
.dropTableIfExists('users') .createTable('users', function(table) { table.increments('id'); table.string('user_name'); }) .createTable('accounts', function(table) { table.increments('id'); table.string('account_name'); table .integer('user_id') .unsigned() .references('users.id');
// Prompt the user for connection details prompt.get(FIELDS, async (err, config) => { if (err) { console.error(err); return; } // Connect to the database const knex = Knex({client: 'mysql', connection: config}); // Create the "visits" table try { await knex.schema.createTable('visits', table => { table.increments(); table.timestamp('timestamp'); table.string('userIp'); }); console.log(`Successfully created 'visits' table.`); } catch (err) { console.error(`Failed to create 'visits' table:`, err); } finally { if (knex) { knex.destroy(); } } });