-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdbconnection.js
More file actions
78 lines (67 loc) · 2.1 KB
/
Copy pathdbconnection.js
File metadata and controls
78 lines (67 loc) · 2.1 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const {Pool, Client} = require('pg');
const pg = require('pg');
const dotenv = require('dotenv').config()
const logger = require('./logging').logger;
// SQL Query builder library for ease of searching
var knex = require('knex')({
client: 'pg',
connection: {
host : process.env.DB_HOST,
user : process.env.DB_USER,
password : process.env.DB_PW,
database : process.env.DB_DATABASE
}
});
const pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_DATABASE,
password: process.env.DB_PW,
port: 5432,
ssl: true
});
const connection = new Client({
connectionString: process.env.DATABASE_URL,
ssl: true,
});
module.exports.itemHistoryInsert = function(item, username,histText){
// TODO: Triggers?
// this function inserts the newly modified item into the history table
const data = {
inv_id: item[0].inv_id,
description: item[0].description,
category: item[0].category,
date_modified: 'NOW()',
quantity: item[0].quantity,
storage_location: item[0].storage_location,
history: `${username.trim()} ${histText}.`
}
knex('inventory_history')
.insert(data)
.catch(err => console.log(err, 'Error inserting into history db.'))
}
module.exports.insertQuery = function(item,user) {
// this function inserts a new item into the inventory table, then
// calls the function to keep a record of the new item in the history table
logger(item);
let data = {
description: item.description,
category: item.category,
date_recieved: 'NOW()',
quantity: item.quantity,
available: item.quantity,
storage_location: item.storage_location,
}
knex('inventory')
.insert(data)
// for some reason, this is needed.
.returning('*')
.then(result => {
module.exports.itemHistoryInsert(result, user,'created item')
})
.catch(err => console.log(err, 'Error in item creation'))
}
connection.connect();
module.exports.connection = connection;
module.exports.knex = knex;
module.exports.pool = pool;