Merge branch 'knex-refactor'

This commit is contained in:
rui hildt 2020-05-02 21:32:40 +02:00
commit 0f11632263
16 changed files with 2552 additions and 1923 deletions

View File

@ -0,0 +1,14 @@
const db = require('../../data/db');
module.exports = {
addUser,
};
function addUser(userData) {
// TODO Complete query without providing id
// right now if ID is not provided, pg-promise send an erro fo mising column
return db.one(
'INSERT INTO account VALUES(emptyUpdate, ${username}, ${email}, ${password}, ${timezone}, ${earliest_time}, ${latest_time}) RETURNING *',
userData,
);
}

View File

@ -0,0 +1,18 @@
let express = require('express');
let router = express.Router();
let Account = require('../models/accountModel');
// Add a user
router.post('/', async (req, res) => {
const userData = { ...req.body };
try {
const user = await Account.addUser(userData);
res.status(201).json(user);
} catch (error) {
res.status(500).json({ message: 'Failed to add new user', error });
}
});
module.exports = router;

View File

@ -1,9 +0,0 @@
var express = require('express');
var router = express.Router();
// define the home page route
router.get('/', function (req, res) {
res.send('Accounts home page');
});
module.exports = router;

View File

@ -1,12 +0,0 @@
const express = require("express");
const accountsRoute = require('./routes/accountsRoute');
const server = express();
server.use('/api/accounts', accountsRoute);
server.get('/', (req, res) =>
res.status(200).send('<h2>Welcome to Meeting Planner Backend API service.</h2>'),
);
module.exports = server;

8
data/db.js Normal file
View File

@ -0,0 +1,8 @@
const knex = require('knex');
const knexfile = require('../knexfile');
const env = process.env.NODE_ENV || 'development';
const configOptions = knexfile[env];
module.exports = knex(configOptions);

View File

@ -1,5 +1,5 @@
## How to do a migration
- Create `.env` to the root folder
- Add the database url to the `.env` file:
`DATABASE_URL=postgres://db_user:password@server_url:PORT/db_name`
- Run `npm run migrate up`
`DATABASE_URL=postgres://username:password@host:port/database`
- Run `npx knex migrate:latest`

View File

@ -0,0 +1,16 @@
exports.up = (knex) => {
return knex.schema.createTable('account', (table) => {
table.increments('id').primary();
table.string('username').notNullable();
table.string('email').notNullable().unique();
table.string('password').notNullable();
table.string('timezone');
table.time('earliest_time');
table.time('latest_time');
table.timestamps(true, true);
});
};
exports.down = (knex) => {
return knex.schema.dropTable('account');
};

View File

@ -0,0 +1,17 @@
exports.up = (knex) => {
return knex.schema.createTable('meeting', (table) => {
table.uuid('id').primary();
table.string('title').notNullable();
table.string('description');
table.time('start_time');
table.string('timezone').notNullable();
table.integer('duration').notNullable();
table.boolean('status').notNullable();
table.string('password');
table.timestamps(true, true);
});
};
exports.down = (knex) => {
return knex.schema.dropTable('meeting');
};

View File

@ -0,0 +1,29 @@
exports.up = (knex) => {
return knex.schema.createTable('participant', (table) => {
table.increments('id').primary();
table
.integer('account_id')
.unsigned()
.notNullable()
.references('account.id')
.onDelete('cascade');
table
.uuid('meeting_id')
.unsigned()
.notNullable()
.references('meeting.id')
.onDelete('cascade');
table.time('earliest_time');
table.time('latest_time');
table.boolean('quorum');
table.boolean('mandatory');
table.boolean('host');
table.boolean('answered');
table.string('timezone').notNullable();
table.timestamps(true, true);
});
};
exports.down = (knex) => {
return knex.schema.dropTable('participant');
};

View File

@ -0,0 +1,17 @@
exports.up = (knex) => {
return knex.schema.createTable('possible_date', (table) => {
table.increments('id').primary();
table
.uuid('meeting_id')
.unsigned()
.notNullable()
.references('meeting.id')
.onDelete('cascade');
table.date('possible_date').notNullable();
table.timestamps(true, true);
});
};
exports.down = (knex) => {
return knex.schema.dropTable('possible_date');
};

View File

@ -0,0 +1,26 @@
exports.up = (knex) => {
return knex.schema.createTable('availibility', (table) => {
table.increments('id').primary();
table
.integer('participant_id')
.unsigned()
.notNullable()
.references('participant.id')
.onDelete('cascade');
table
.integer('possible_date_id')
.unsigned()
.notNullable()
.references('possible_date.id')
.onDelete('cascade');
table.boolean('preference').notNullable();
table.time('start_time').notNullable();
table.time('end_time').notNullable();
table.string('timezone').notNullable();
table.timestamps(true, true);
});
};
exports.down = (knex) => {
return knex.schema.dropTable('availibility');
};

View File

@ -1,7 +1,28 @@
require('dotenv').config()
const app = require("./api/server");
const PORT = process.env.PORT || 3000;
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
require('dotenv').config();
app.listen(PORT, () =>
console.log(`Meeting Planner Backend listening at http://localhost:${PORT}`),
const accountsRoute = require('./api/routes/accountRoute');
const server = express();
server.use(express.json());
server.use(cors());
server.use(helmet());
server.use('/api/accounts', accountsRoute);
server.get('/', (req, res) =>
res
.status(200)
.send('<h2>Welcome to Meeting Planner Backend API service.</h2>'),
);
const PORT = process.env.PORT || 3001;
server.listen(PORT, () =>
console.log(
`Meeting Planner Backend listening at http://localhost:${PORT}`,
),
);

30
knexfile.js Normal file
View File

@ -0,0 +1,30 @@
require('dotenv').config();
module.exports = {
development: {
client: 'pg',
connection: process.env.DATABASE_URL,
migrations: {
directory: './data/migrations',
},
seeds: { directory: './data/seeds' },
},
testing: {
client: 'pg',
connection: process.env.DATABASE_URL,
migrations: {
directory: './data/migrations',
},
seeds: { directory: './data/seeds' },
},
production: {
client: 'pg',
connection: process.env.DATABASE_URL,
migrations: {
directory: './data/migrations',
},
seeds: { directory: './data/seeds' },
},
};

View File

@ -1,108 +0,0 @@
/* eslint-disable camelcase */
exports.shorthands = {
id: { type: 'serial', primaryKey: true },
varchar: { type: 'varchar(128)' },
varchar_req: { type: 'varchar(128)', notNull: true }
};
exports.up = (pgm) => {
pgm.createTable('account', {
id: 'id',
username: 'varchar_req',
email: 'varchar_req',
password: 'varchar_req',
timezone: 'varchar',
earliest_time: 'time',
latest_time: 'time',
createdAt: {
type: 'timestamp',
notNull: true,
default: pgm.func('current_timestamp'),
},
});
pgm.createTable('meeting', {
id: { type: 'uuid', primaryKey: true },
title: 'varchar_req',
description: 'varchar',
start_time: 'time',
timezone: 'varchar',
duration: 'int',
status: 'boolean',
password: 'varchar',
createdAt: {
type: 'timestamp',
notNull: true,
default: pgm.func('current_timestamp'),
},
});
pgm.createTable('possible_date', {
id: 'id',
meeting_id: {
type: 'uuid',
references: 'meeting(id)',
notNull: true,
onDelete: 'cascade',
},
possible_date: { type: 'date', notNull: true },
});
pgm.createTable('participant', {
id: 'id',
account_id: {
type: 'int',
references: 'account(id)',
notNull: true,
onDelete: 'cascade',
},
meeting_id: {
type: 'uuid',
references: 'meeting(id)',
notNull: true,
onDelete: 'cascade',
},
earliest_time: 'time',
latest_time: 'time',
quorum: 'boolean',
mandatory: 'boolean',
host: 'boolean',
answered: 'boolean',
timezone: 'varchar',
createdAt: {
type: 'timestamp',
notNull: true,
default: pgm.func('current_timestamp'),
},
});
pgm.createTable('availibility', {
id: 'id',
participant_id: {
type: 'id',
references: 'participant(id)',
notNull: true,
onDelete: 'cascade',
},
possible_date_id: {
type: 'id',
references: 'possible_date(id)',
notNull: true,
onDelete: 'cascade',
},
preference: { type: 'boolean', notNull: true },
start_time: { type: 'timestamp', notNull: true },
end_time: { type: 'timestamp', notNull: true },
timezone: 'varchar_req',
createdAt: {
type: 'timestamp',
notNull: true,
default: pgm.func('current_timestamp'),
},
});
};
// exports.down = (pgm) => {
// pgm.dropTable('accounts', { ifExists: true });
// };

4092
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,23 +1,25 @@
{
"name": "backend",
"version": "1.0.0",
"description": "Backend for Meeting Planner",
"main": "index.js",
"scripts": {
"dev": "nodemon index.js",
"prod": "node index.js",
"migrate": "node-pg-migrate"
},
"repository": {
"type": "git",
"url": "ssh://git@git.armada.digital:29418/meeting-planner/backend.git"
},
"author": "rui hildt",
"license": "AGPL-3.0-or-later",
"dependencies": {
"dotenv": "^8.2.0",
"express": "^4.17.1",
"knex": "^0.21.1",
"pg": "^8.0.3"
}
"name": "backend",
"version": "1.0.0",
"description": "Backend for Meeting Planner",
"main": "index.js",
"scripts": {
"dev": "nodemon index.js",
"prod": "node index.js"
},
"repository": {
"type": "git",
"url": "ssh://git@git.armada.digital:29418/meeting-planner/backend.git"
},
"author": "rui hildt",
"license": "AGPL-3.0-or-later",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"helmet": "^3.22.0",
"knex": "^0.21.1",
"pg": "^8.0.3",
"pgtools": "^0.3.0"
}
}