-
Notifications
You must be signed in to change notification settings - Fork 1
/
adapter.js
51 lines (49 loc) · 1.45 KB
/
adapter.js
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
/**
* _query - makes the actual database calls
* @param (Object) connection - a promise based database connection
* @return {function}
* @param {string} query the actual query to perform
* @param {[string]} values - values that will be escaped and inserted in the query
* @param {function} cb - the callback
* @return {function}
*/
export const _query = (connection) => (query, values, cb) => {
console.log('query');
console.log(query);
console.log('values');
console.log(values);
console.log('cb');
console.log(cb);
const funcType = typeof(function() {});
let callback = false;
if (cb && typeof(cb) == funcType) {
callback = cb;
}
if (!callback && values && typeof(values) === funcType) {
callback = values;
}
return connection.query(query, values)
.then(([rows, fields]) => callback(null, rows, fields))
.catch((err) => callback(err, null, null));
};
/**
* adapter - the actual adapter takes in the promise
* and outputs an object that can handle session data
* @param {Object} db - a promise
* @return {Object}
*/
export const adapter = (db) => {
// if the user already resolved the intitial connection then use it
if(db.query) {
return {
query: (query, values, cb) => _query(db)(query, values, cb)
}
}
// else make the initial connection and get started
return {
query: (query, values, cb) => {
return db.then((connection) => _query(connection)(query, values, cb))
}
}
};
export default adapter;