Skip to content

Commit

Permalink
Create, deploy & resolvers & tests
Browse files Browse the repository at this point in the history
Implementing:

- Schema creation script
- Deploy instance script
- Initial table creation migration

- Resolvers:
  - Queries CRUD
  - Messages CRUD

- Unit tests:
  - Queries
  - Messages
  • Loading branch information
Andrew Isherwood committed Apr 16, 2020
1 parent f544641 commit 2a112d1
Show file tree
Hide file tree
Showing 16 changed files with 5,438 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { Pool, types } = require('pg');

// Do not parse TIMESTAMP{TZ} columns into JS Date objects,
// it messes with the OpenAPI validator
const TYPE_TIMESTAMP = 1114;
const TYPE_TIMESTAMPTZ = 1184;
const noParse = (v) => v;
types.setTypeParser(TYPE_TIMESTAMP, noParse);
types.setTypeParser(TYPE_TIMESTAMPTZ, noParse);

const pool = new Pool();

module.exports = pool;
33 changes: 33 additions & 0 deletions database.json.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"super": {
"driver": "pg",
"user": "ems",
"password": "",
},
"test": {
"driver": "pg",
"database": "ems_test",
"user": {
"ENV": "PGUSER"
},
"password": {
"ENV": "PGPASSWORD"
},
"schema": {
"ENV": "SCHEMA"
}
},
"dev": {
"driver": "pg",
"database": "ems",
"user": {
"ENV": "PGUSER"
},
"password": {
"ENV": "PGPASSWORD"
},
"schema": {
"ENV": "SCHEMA"
}
}
}
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const DBMigrate = require('db-migrate');
const resolvers = require('./resolvers');

const mig = DBMigrate.getInstance(true, {
cwd: __dirname,
config: __dirname + '/database.json'
});

const emsDb = {
reset: () => mig.reset(),
update: () => mig.up(),
resolvers
};

module.exports = emsDb;
53 changes: 53 additions & 0 deletions migrations/20200406100555-init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20200406100555-init-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20200406100555-init-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports._meta = {
"version": 1
};
9 changes: 9 additions & 0 deletions migrations/sqls/20200406100555-init-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DROP TABLE querylabel;
DROP TABLE userquery;
DROP TABLE attachment;
DROP TABLE message;
DROP TABLE query;
DROP TABLE ems_user;
DROP TABLE role;
DROP TABLE label;
DROP TABLE folder;
70 changes: 70 additions & 0 deletions migrations/sqls/20200406100555-init-up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
CREATE TABLE folder (
id SERIAL PRIMARY KEY,
name TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ
);

CREATE TABLE label (
id SERIAL PRIMARY KEY,
name TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ
);

CREATE TABLE role (
id SERIAL PRIMARY KEY,
name TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ
);

CREATE TABLE ems_user (
id SERIAL PRIMARY KEY,
name TEXT,
role_id INTEGER REFERENCES role (id) ON DELETE SET NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ
);

CREATE TABLE query (
id SERIAL PRIMARY KEY,
title TEXT,
folder_id INTEGER REFERENCES folder (id) ON DELETE SET NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ
);

CREATE TABLE message (
id SERIAL PRIMARY KEY,
query_id INTEGER REFERENCES query (id) ON DELETE CASCADE,
creator_id INTEGER REFERENCES ems_user (id) ON DELETE CASCADE,
content TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ
);

CREATE TABLE attachment (
id SERIAL PRIMARY KEY,
query_id INTEGER REFERENCES query (id) ON DELETE CASCADE,
creator_id INTEGER REFERENCES ems_user (id) ON DELETE CASCADE,
filename TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ
);

CREATE TABLE userquery (
query_id INTEGER REFERENCES query (id) ON DELETE CASCADE,
user_id INTEGER REFERENCES ems_user (id) ON DELETE CASCADE,
filename TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
PRIMARY KEY(query_id, user_id)
);

CREATE TABLE querylabel (
query_id INTEGER REFERENCES query (id) ON DELETE CASCADE,
label_id INTEGER REFERENCES label (id) ON DELETE CASCADE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
Loading

0 comments on commit 2a112d1

Please sign in to comment.