-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseModel.js
executable file
·118 lines (111 loc) · 3.13 KB
/
baseModel.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"use strict"
const db = require("./connection");
module.exports = function (tableName, idField) {
return {
db,
insert: (event, context, callback) => {
console.log("Insert to:", tableName, event);
var data = { tableName, data: event, idField };
db.insert(data).then(response => {
console.log("Response:", response);
callback(null, response);
}).catch(error => {
console.log("Error in insert", error);
callback(error);
})
},
update: (event, context, callback) => {
console.log("Update:", tableName, event);
var data = event;
var idParam = {
columnName: idField,
value: data[idField]
}
var where = "";
if (data[idField]) {
var where = `${[idField]} = ${data[idField]}`;
delete data[idField];
}
var data = { tableName, data, where };
db.update(data).then(response => {
console.log("UPDATE Response:", response);
var data = { tableName, where: [idParam] };
db.get(data).then(response => {
console.log("Update Response:", response);
callback(null, response[0]);
}).catch(error => {
console.log("Error in Update", error);
callback(error);
})
}).catch(error => {
console.log("Error in update", error);
callback(error);
})
},
updateBulk: (event, context, callback) => {
console.log("Update:", tableName);
var { data, where } = event;
if (!data || !where) {
callback("Invalid data. data or where keys missing");
}
db.updateBulk({ tableName, data, where }).then(response => {
console.log("UPDATE bulk Response:", response);
var data = { tableName, where};
db.get(data).then(response => {
console.log("Update bulk Response:", response);
callback(null, response[0]);
}).catch(error => {
console.log("Error in update bulk", error);
callback(error);
})
}).catch(error => {
console.log("Error in update", error);
callback(error);
})
},
delete: (event, context, callback) => {
console.log("Delete :", tableName);
var where = Object.keys(event).map(key => {
return `${key} = '${event[key]}'`;
}).join(" and ");
var data = { tableName, where };
db.delete(data).then(response => {
console.log("Response:", response);
callback(null, response);
}).catch(error => {
console.log("Error in delete", error);
callback(error);
})
},
get: (event, context, callback) => {
console.log("Get :", tableName, event);
var params = event.query || {};
var where = Object.keys(params).map(key => {
return {
columnName: key,
value: params[key]
}
})
var data = { tableName, where };
db.get(data).then(response => {
console.log("Response:", response);
callback(null, response);
}).catch(error => {
console.log("Error in Get", error);
callback(error);
})
},
query: (event, context, callback) => {
console.log("Query :", tableName);
if (!event.tableName)
event.tableName = tableName;
db.query({query: event}).then(response => {
console.log("Response:", response);
callback(null, response);
}).catch(error => {
console.log("Error in query", error);
callback(error);
})
}
}
}