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(); } } });
// 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(); } } });