This repository was archived by the owner on Jul 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.js
More file actions
46 lines (39 loc) · 1.3 KB
/
db.js
File metadata and controls
46 lines (39 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// create the actual db connection
const knex = require('knex')(require('./knexfile.js').development);
// config for transaction DB
const config = {
client: 'postgresql',
connection: process.env.TRANSACTION_DATABASE_URL
};
// create connection to transaction db
const db2 = require('knex')(config);
// test db connection
db2.raw('select 1+1 as result').then(() => {
console.log('valid connection....to transaction db');
});
// whenever there is a query, save result in second db
knex.on('query-response', function(one, two, three) {
const obj = {
bindings: three.toSQL().bindings,
query: three.toSQL().sql
};
return db2('transactions').insert(obj).then();
});
const fs = require('fs');
// registry plugin to handle models that reference each other
// require all models and pass in db connection
const modelObj = {};
const modelDirectories = fs.readdirSync('./models');
modelDirectories.forEach(function(folder) {
const isFolder = fs.lstatSync(`./models/${folder}`).isDirectory();
if (isFolder) {
const bookshelf = require('bookshelf')(knex);
bookshelf.plugin('registry');
const models = fs.readdirSync(`./models/${folder}`);
models.forEach(model => {
require(`./models/${folder}/${model}`)(bookshelf);
});
modelObj[folder] = bookshelf;
}
});
module.exports = modelObj;