add knexfile and first migration file for timeslots table

This commit is contained in:
Kfir Dayan 2023-04-25 22:45:47 +03:00
parent e52c9b5493
commit fee7f1a266
4 changed files with 65 additions and 61 deletions

View file

@ -21,64 +21,5 @@ install pg MacOS -
knex for migration files knex for migration files
npx knex migrate:make create_addresses_table npx knex migrate:make create_addresses_table
npx knex migrate:latest npx knex migrate:latest --env development
npx knex migrate:rollback npx knex migrate:rollback --env development
npx knex seed:make 01_addresses
npx knex seed:run
npx knex migrate:make create_addresses_table
npx knex migrate:latest
npx knex migrate:rollback
npx knex seed:make 01_addresses
npx knex seed:run
npx knex migrate:make create_addresses_table
npx knex migrate:latest
npx knex migrate:rollback
npx knex seed:make 01_addresses
npx knex seed:run
npx knex migrate:make create_addresses_table
npx knex migrate:latest
npx knex migrate:rollback
npx knex seed:make 01_addresses
npx knex seed:run
npx knex migrate:make create_addresses_table
npx knex migrate:latest
npx knex migrate:rollback
npx knex seed:make 01_addresses
npx knex seed:run
npx knex migrate:make create_addresses_table
npx knex migrate:latest
npx knex migrate:rollback
npx knex seed:make 01_addresses
npx knex seed:run
npx knex migrate:make create_addresses_table
npx knex migrate:latest
npx knex migrate:rollback
npx knex seed:make 01_addresses
npx knex seed:run
npx knex migrate:make create_addresses_table
npx knex migrate:latest
npx knex migrate:rollback
npx knex seed:make 01_addresses
npx knex seed:run
npx knex migrate:make create_addresses_table
npx knex migrate:latest
npx knex migrate:rollback
npx knex seed:make 01_addresses
npx knex seed:run

20
knexfile.js Normal file
View file

@ -0,0 +1,20 @@
const env = require('dotenv').config();
module.exports = {
development: {
client: 'postgresql',
connection: {
database: env.parsed.PGDATABASENAME,
user: env.parsed.PGUSER,
password: env.parsed.PGPASSWORD
},
migrations: {
directory: './src/migrations'
},
seeds: {
directory: './src/seeds'
}
}
}

View file

@ -0,0 +1,13 @@
exports.up = function(knex) {
return knex.schema.createTable('timeslots', function(table) {
table.increments('id').primary();
table.timestamp('start_time').notNullable();
table.timestamp('end_time').notNullable();
table.boolean('is_reserved').notNullable().defaultTo(false);
table.timestamps(true, true);
});
};
exports.down = function(knex) {
return knex.schema.dropTable('timeslots');
};

30
src/seed/load-data.js Normal file
View file

@ -0,0 +1,30 @@
exports.seed = async function(knex) {
// Delete all existing data from the tables
// check is table exists first
const hasTable = await knex.schema.hasTable('timeslots');
if (hasTable) {
await knex('timeslots').del();
}
// Load data from the JSON files
const holidays = require('../data/holidays.json');
const timeslots = require('../data/timeslots.json');
// Insert the holiday dates into the holidays table
for (const date of holidays) {
await knex('holidays').insert({ date });
}
// Insert the time slots into the timeslots table
for (const { date, slots } of timeslots) {
for (const slot of slots) {
await knex('timeslots').insert({
date,
start_time: slot.start_time,
end_time: slot.end_time
});
}
}
};