Setup table migration and update schemas/endpoints
This commit is contained in:
parent
effa3ef442
commit
6c562ee7f6
6
.gitignore
vendored
6
.gitignore
vendored
@ -61,4 +61,8 @@ typings/
|
|||||||
.next
|
.next
|
||||||
|
|
||||||
# VS Code settings
|
# VS Code settings
|
||||||
.vscode
|
.vscode
|
||||||
|
|
||||||
|
# Databases
|
||||||
|
data/sleep-tracker.db3
|
||||||
|
data/test.db3
|
38
README.md
38
README.md
@ -18,7 +18,7 @@ Sleep Tracker is intended for anyone interested in improving their sleep and hea
|
|||||||
|
|
||||||
## Database Schemas
|
## Database Schemas
|
||||||
|
|
||||||
### Users `users`
|
### Users Schema `users`
|
||||||
| field | data type | metadata |
|
| field | data type | metadata |
|
||||||
| :--------| :--------------- | :-------------------------------------------------- |
|
| :--------| :--------------- | :-------------------------------------------------- |
|
||||||
| id | unsigned integer | primary key, auto-increments, generated by database |
|
| id | unsigned integer | primary key, auto-increments, generated by database |
|
||||||
@ -26,35 +26,35 @@ Sleep Tracker is intended for anyone interested in improving their sleep and hea
|
|||||||
| username | string | required |
|
| username | string | required |
|
||||||
| password | string | required |
|
| password | string | required |
|
||||||
|
|
||||||
### Sleep Sessions `sessions`
|
### Sleep Sessions Schema `sessions`
|
||||||
|
|
||||||
| field | data type | metadata |
|
| field | data type | metadata |
|
||||||
| :------------ | :--------------- | :-------------------------------------------------- |
|
| :------------ | :--------------- | :-------------------------------------------------- |
|
||||||
| id | unsigned integer | primary key, auto-increments, generated by database |
|
| id | unsigned integer | primary key, auto-increments, generated by database |
|
||||||
| user_id | unsigned integer | foreign key referencing `users.id`, required |
|
| user_id | unsigned integer | foreign key referencing `users.id`, required |
|
||||||
| bed_time | unsigned integer | timestamp with date, required |
|
| bed_time | unsigned integer | timestamp with date, required |
|
||||||
| wake_time | unsigned integer | timestamp with date, required |
|
| wake_time | unsigned integer | timestamp with date, required |
|
||||||
| bed_tiredness | unsigned integer | between 1-5, |
|
| bed_tiredness | unsigned integer | |
|
||||||
| wake_mood | unsigned integer | between 1-5, |
|
| wake_mood | unsigned integer | |
|
||||||
| day_mood | unsigned integer | between 1-5, |
|
| day_mood | unsigned integer | |
|
||||||
|
|
||||||
### Average Mood per Sleep Session `moods`
|
### Average Mood per Sleep Session Schema `dailyAverages`
|
||||||
| field | data type | metadata |
|
| field | data type | metadata |
|
||||||
| :------------- | :--------------- | :--------------------------------------------------- |
|
| :------------- | :--------------- | :-------------------------------------------------- |
|
||||||
| id | unsigned integer | primary key, auto-increments, generated by database |
|
| id | unsigned integer | primary key, auto-increments, generated by database |
|
||||||
| session_date | unsigned integer | foreign key referencing `sessions.wake_time` |
|
| session_user | unsigned integer | foreign key referencing `sessions.user_id` |
|
||||||
| sleep_duration | unsigned integer | |
|
| session_date | unsigned integer | foreign key referencing `sessions.wake_time` |
|
||||||
| average_mood | float | average of bed_tiredness/wake_mood/day_mood |
|
| sleep_duration | unsigned integer | |
|
||||||
|
| average_mood | float | |
|
||||||
|
|
||||||
## API Endpoints
|
## API Endpoints
|
||||||
- `POST /api/users` - add a new user
|
- `POST /api/users` - add a new user
|
||||||
- `GET /api/users/:id` - fetch a user
|
- `GET /api/users/:id` - fetch a user
|
||||||
- `PUT /api/users/:id` - update a user
|
- `PUT /api/users/:id` - update a user
|
||||||
|
|
||||||
- `GET /api/sessions` - fetch list of all sessions
|
- `GET /api/users/:id/sessions` - fetch list of all sessions by user id
|
||||||
- `POST /api/sessions` - add a session
|
- `POST /api/users/:id/sessions` - add a session y user id
|
||||||
- `GET /api/sessions/:id` - get a single session by id
|
- `GET /api/users/:id/sessions/:id` - fetch a single session by id
|
||||||
- `PUT /api/sessions/:id` - update a session by user id
|
- `PUT /api/users/:id/sessions/:id` - update a session by id
|
||||||
- `DELETE /api/sessions/:id` - delete a session by user id
|
- `DELETE /api/users/:id/sessions/:id` - delete a session by id
|
||||||
|
|
||||||
- `GET /api/moods/:id` - fetch a list of last 30 moods by user id
|
- `GET /api/dailyaverages/:id` - fetch a list of last 30 daily averages by user id
|
8
data/dbConfig.js
Normal file
8
data/dbConfig.js
Normal 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]);
|
@ -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');
|
||||||
|
};
|
4
index.js
4
index.js
@ -7,8 +7,8 @@ server.get("/", (req, res) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const PORT = process.env.PORT;
|
const PORT = process.env.PORT || 4000;
|
||||||
|
|
||||||
server.listen(PORT, () => {
|
server.listen(PORT, () => {
|
||||||
console.log(`Listening on port ${PORT}...`);
|
console.log(`Listening on port ${PORT}...`);
|
||||||
});
|
});
|
||||||
|
64
knexfile.js
Normal file
64
knexfile.js
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
require("dotenv").config();
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
|
||||||
|
development: {
|
||||||
|
client: "sqlite3",
|
||||||
|
useNullAsDefault: true,
|
||||||
|
connection: {
|
||||||
|
filename: "./data/sleep-tracker.db3"
|
||||||
|
},
|
||||||
|
pool: {
|
||||||
|
afterCreate: (conn, done) => {
|
||||||
|
conn.run("PRAGMA foreign_keys = ON", done);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
migrations: {
|
||||||
|
directory: "./data/migrations"
|
||||||
|
},
|
||||||
|
seeds: {
|
||||||
|
directory: "./data/seeds"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
testing: {
|
||||||
|
client: "sqlite3",
|
||||||
|
useNullAsDefault: true,
|
||||||
|
connection: {
|
||||||
|
filename: "./data/test.db3"
|
||||||
|
},
|
||||||
|
pool: {
|
||||||
|
afterCreate: (conn, done) => {
|
||||||
|
conn.run("PRAGMA foreign_keys = ON", done);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
migrations: {
|
||||||
|
directory: "./data/migrations"
|
||||||
|
},
|
||||||
|
seeds: {
|
||||||
|
directory: "./data/seeds"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
production: {
|
||||||
|
client: "postgresql",
|
||||||
|
useNullAsDefault: true,
|
||||||
|
connection: {
|
||||||
|
database: process.env.PROD_DB,
|
||||||
|
user: process.env.PROD_DB_USERNAME,
|
||||||
|
password: process.env.PROD_DB_PASSWORD
|
||||||
|
},
|
||||||
|
pool: {
|
||||||
|
afterCreate: (conn, done) => {
|
||||||
|
conn.run("PRAGMA foreign_keys = ON", done);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
migrations: {
|
||||||
|
directory: "./data/migrations"
|
||||||
|
},
|
||||||
|
seeds: {
|
||||||
|
directory: "./data/seeds"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user