Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dec0a6b

Browse files
author
tsv2013
committed
Survey SQL storage refactoring
1 parent ccf5d5c commit dec0a6b

File tree

5 files changed

+159
-144
lines changed

5 files changed

+159
-144
lines changed

‎express-app/db-adapters/in-memory.js‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var demoData = require("./demo-surveys");
22

33
var currentId = demoData.surveys.length + 1;
44

5-
function InMemoryDBAdapter(session) {
5+
function InMemoryStorage(session) {
66
function getTable(tableName) {
77
var table = session[tableName];
88
if (!table) {
@@ -13,13 +13,8 @@ function InMemoryDBAdapter(session) {
1313
}
1414

1515
function getObjectsFromStorage(tableName, callback) {
16-
// var objects = {};
1716
var table = getTable(tableName);
1817
callback(table);
19-
// table.forEach(function(item) {
20-
// objects[item.name] = item;
21-
// });
22-
// callback(objects);
2318
}
2419

2520
function findById(objects, id) {
@@ -123,4 +118,4 @@ function InMemoryDBAdapter(session) {
123118
};
124119
}
125120

126-
module.exports = InMemoryDBAdapter;
121+
module.exports = InMemoryStorage;

‎express-app/db-adapters/postgres.js‎

Lines changed: 11 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const fs = require("fs");
2+
const SQLCRUDAdapter = require("./sql-crud-adapter");
3+
const SurveyStorage = require("./survey-storage");
24

35
const readFileSync = filename => fs.readFileSync(filename).toString("utf8");
46

@@ -12,127 +14,19 @@ const dbConfig = {
1214
: null
1315
};
1416

15-
var currentId = 1;
16-
1717
const Pool = require('pg').Pool
1818
const pool = new Pool(dbConfig);
1919

20-
function PostgresDBAdapter(session) {
21-
22-
function getObjectsFromStorage(tableName, filter, callback) {
23-
filter = filter || [];
24-
let where = "";
25-
if(filter.length > 0) {
26-
where += " WHERE " + filter.map(fi => "" + fi.name + fi.op + fi.value).join(" AND ");
20+
function PostgresStorage(session) {
21+
function queryExecutorFunction() {
22+
if(!!process.env.DATABASE_LOG) {
23+
console.log(arguments[0]);
24+
console.log(arguments[1]);
2725
}
28-
29-
const command = "SELECT * FROM " + tableName + where;
30-
// console.log(command);
31-
pool.query(command, (error, results) => {
32-
if (error) {
33-
throw error;
34-
}
35-
callback(results.rows);
36-
});
37-
}
38-
39-
function deleteObjectFromStorage(tableName, idValue, callback) {
40-
pool.query("DELETE FROM " + tableName + " WHERE id='" + idValue + "'", (error, results) => {
41-
if (error) {
42-
throw error;
43-
}
44-
callback(results);
45-
});
46-
}
47-
48-
function createObjectInStorage(tableName, object, callback) {
49-
const valueNames = [];
50-
const valueIndexes = [];
51-
const values = [];
52-
Object.keys(object).forEach((key, index) => {
53-
if(object[key] !== undefined) {
54-
valueNames.push(key);
55-
valueIndexes.push("$" + (index + 1));
56-
values.push(object[key]);
57-
}
58-
});
59-
const command = "INSERT INTO " + tableName + " (" + valueNames.join(", ") + ") VALUES (" + valueIndexes.join(", ") + ") RETURNING id";
60-
// console.log(command);
61-
// console.log(values);
62-
pool.query(command, values, (error, results) => {
63-
if (error) {
64-
throw error;
65-
}
66-
console.log(JSON.stringify(results));
67-
callback(results.rows[0].id);
68-
});
26+
return pool.query.apply(pool, arguments);
6927
}
70-
71-
function updateObjectInStorage(tableName, object, callback) {
72-
const valueNames = [];
73-
const values = [];
74-
Object.keys(object).forEach((key, index) => {
75-
if(object[key] !== undefined) {
76-
valueNames.push(key + " = $" + (index + 1));
77-
values.push(object[key]);
78-
}
79-
});
80-
const command = "UPDATE " + tableName + " SET " + valueNames.join(", ") + " WHERE id = '" + object.id + "'";
81-
// console.log(command);
82-
// console.log(values);
83-
pool.query(command, values, (error, results) => {
84-
if (error) {
85-
throw error;
86-
}
87-
callback(object);
88-
});
89-
}
90-
91-
function addSurvey(name, callback) {
92-
var newObj = {
93-
name: name || ("New Survey" + " " + currentId++),
94-
json: "{}"
95-
};
96-
createObjectInStorage("surveys", newObj, id => {
97-
newObj.id = id;
98-
callback(newObj);
99-
});
100-
}
101-
102-
function postResults(postId, json, callback) {
103-
var newObj = {
104-
postid: postId,
105-
json: json
106-
};
107-
console.log(JSON.stringify(newObj));
108-
createObjectInStorage("results", newObj, id => {
109-
newObj.id = id;
110-
callback(newObj);
111-
});
112-
}
113-
114-
return {
115-
addSurvey: addSurvey,
116-
getSurvey: function (surveyId, callback) {
117-
getObjectsFromStorage("surveys", [{ name: "id", op: "=", value: "'" + surveyId + "'" }], function (results) { callback(results[0]); });
118-
},
119-
storeSurvey: function (id, name, json, callback) {
120-
updateObjectInStorage("surveys", { id: id, json: json }, function (results) { callback(results); });
121-
},
122-
getSurveys: function (callback) {
123-
getObjectsFromStorage("surveys", [], function (results) { callback(results); });
124-
},
125-
deleteSurvey: function (surveyId, callback) {
126-
deleteObjectFromStorage("surveys", surveyId, function (results) { callback(results); });
127-
},
128-
postResults: postResults,
129-
getResults: function (postId, callback) {
130-
getObjectsFromStorage("results", [{ name: "postid", op: "=", value: "'" + postId + "'" }], function (results) { callback({ id: postId, data: results.map(r => r.json)}); });
131-
},
132-
changeName: function (id, name, callback) {
133-
updateObjectInStorage("surveys", { id: id, name: name }, function (results) { callback(results); });
134-
}
135-
};
28+
const dbQueryAdapter = new SQLCRUDAdapter(queryExecutorFunction);
29+
return new SurveyStorage(dbQueryAdapter);
13630
}
13731

138-
module.exports = PostgresDBAdapter;
32+
module.exports = PostgresStorage;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
function SQLCRUDAdapter(queryExecutorFunction) {
2+
function getObjects(tableName, filter, callback) {
3+
filter = filter || [];
4+
let where = "";
5+
if(filter.length > 0) {
6+
where += " WHERE " + filter.map(fi => "" + fi.name + fi.op + fi.value).join(" AND ");
7+
}
8+
9+
const command = "SELECT * FROM " + tableName + where;
10+
queryExecutorFunction(command, (error, results) => {
11+
if (error) {
12+
throw error;
13+
}
14+
callback(results.rows);
15+
});
16+
}
17+
18+
function deleteObject(tableName, idValue, callback) {
19+
const command = "DELETE FROM " + tableName + " WHERE id='" + idValue + "'";
20+
queryExecutorFunction(command, (error, results) => {
21+
if (error) {
22+
throw error;
23+
}
24+
callback(results);
25+
});
26+
}
27+
28+
function createObject(tableName, object, callback) {
29+
const valueNames = [];
30+
const valueIndexes = [];
31+
const values = [];
32+
Object.keys(object).forEach((key, index) => {
33+
if(object[key] !== undefined) {
34+
valueNames.push(key);
35+
valueIndexes.push("$" + (index + 1));
36+
values.push(object[key]);
37+
}
38+
});
39+
const command = "INSERT INTO " + tableName + " (" + valueNames.join(", ") + ") VALUES (" + valueIndexes.join(", ") + ") RETURNING id";
40+
queryExecutorFunction(command, values, (error, results) => {
41+
if (error) {
42+
throw error;
43+
}
44+
// console.log(JSON.stringify(results));
45+
callback(results.rows[0].id);
46+
});
47+
}
48+
49+
function updateObject(tableName, object, callback) {
50+
const valueNames = [];
51+
const values = [];
52+
Object.keys(object).forEach((key, index) => {
53+
if(object[key] !== undefined) {
54+
valueNames.push(key + " = $" + (index + 1));
55+
values.push(object[key]);
56+
}
57+
});
58+
const command = "UPDATE " + tableName + " SET " + valueNames.join(", ") + " WHERE id = '" + object.id + "'";
59+
queryExecutorFunction(command, values, (error, results) => {
60+
if (error) {
61+
throw error;
62+
}
63+
callback(object);
64+
});
65+
}
66+
67+
return {
68+
create: createObject,
69+
retrieve: getObjects,
70+
update: updateObject,
71+
delete: deleteObject
72+
}
73+
}
74+
75+
module.exports = SQLCRUDAdapter;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function SurveyStorage(dbQueryAdapter) {
2+
let currentId = 1;
3+
4+
function addSurvey(name, callback) {
5+
var newObj = {
6+
name: name || ("New Survey" + " " + currentId++),
7+
json: "{}"
8+
};
9+
dbQueryAdapter.create("surveys", newObj, id => {
10+
newObj.id = id;
11+
callback(newObj);
12+
});
13+
}
14+
15+
function postResults(postId, json, callback) {
16+
var newObj = {
17+
postid: postId,
18+
json: json
19+
};
20+
console.log(JSON.stringify(newObj));
21+
dbQueryAdapter.create("results", newObj, id => {
22+
newObj.id = id;
23+
callback(newObj);
24+
});
25+
}
26+
27+
return {
28+
addSurvey: addSurvey,
29+
getSurvey: function (surveyId, callback) {
30+
dbQueryAdapter.retrieve("surveys", [{ name: "id", op: "=", value: "'" + surveyId + "'" }], function (results) { callback(results[0]); });
31+
},
32+
storeSurvey: function (id, name, json, callback) {
33+
dbQueryAdapter.update("surveys", { id: id, json: json }, function (results) { callback(results); });
34+
},
35+
getSurveys: function (callback) {
36+
dbQueryAdapter.retrieve("surveys", [], function (results) { callback(results); });
37+
},
38+
deleteSurvey: function (surveyId, callback) {
39+
dbQueryAdapter.delete("surveys", surveyId, function (results) { callback(results); });
40+
},
41+
postResults: postResults,
42+
getResults: function (postId, callback) {
43+
dbQueryAdapter.retrieve("results", [{ name: "postid", op: "=", value: "'" + postId + "'" }], function (results) { callback({ id: postId, data: results.map(r => r.json)}); });
44+
},
45+
changeName: function (id, name, callback) {
46+
dbQueryAdapter.update("surveys", { id: id, name: name }, function (results) { callback(results); });
47+
}
48+
};
49+
}
50+
51+
module.exports = SurveyStorage;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /