Setup table migration and update schemas/endpoints

This commit is contained in:
2019-07-31 20:38:48 +02:00
parent effa3ef442
commit 6c562ee7f6
6 changed files with 171 additions and 22 deletions

8
data/dbConfig.js Normal file
View File

@@ -0,0 +1,8 @@
require("dotenv").config();
const knex = require('knex');
const config = require('../knexfile');
const environment = process.env.DB_ENV || 'development';
module.exports = knex(config[environment]);

View File

@@ -0,0 +1,73 @@
exports.up = function(knex) {
return knex.schema
.createTable('users', tbl => {
tbl
.increments();
tbl
.string('email')
.notNullable()
.unique();
tbl
.string('username')
.notNullable()
.unique();
tbl
.string('password')
.notNullable();
})
.createTable('sessions', tbl => {
tbl
.increments();
tbl
.integer('user_id')
.unsigned()
.notNullable()
.references('users.id')
.onUpdate('CASCADE')
.onDelete('CASCADE');
tbl
.datetime('bed_time')
.notNullable();
tbl
.datetime('wake_time');
tbl
.integer('bed_tiredness')
.unsigned();
tbl
.integer('wake_mood')
.unsigned();
})
.createTable('dailyAverages', tbl => {
tbl
.increments();
tbl
.integer('session_user')
.unsigned()
.notNullable()
.references('sessions.user_id')
.onUpdate('CASCADE')
.onDelete('CASCADE');
tbl
.datetime('session_date')
.notNullable()
.references('sessions.wake_time')
.onUpdate('CASCADE')
.onDelete('CASCADE');
tbl
.integer('sleep_duration')
.unsigned();
tbl
.float('average_mood');
})
};
exports.down = function(knex) {
return knex.schema
.dropTableIfExists('dailyAverages')
.dropTableIfExists('sessions')
.dropTableIfExists('users');
};