-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
16 changed files
with
5,438 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); |
Oops, something went wrong.