-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathinmemorydbadapter.js
126 lines (113 loc) · 3.02 KB
/
inmemorydbadapter.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
119
120
121
122
123
124
125
126
var demoData = require("./demo-surveys");
var currentId = demoData.surveys.length + 1;
function InMemoryDBAdapter(session) {
function getTable(tableName) {
var table = session[tableName];
if (!table) {
table = [];
session[tableName] = table;
}
return table;
}
function getObjectsFromStorage(tableName, callback) {
// var objects = {};
var table = getTable(tableName);
callback(table);
// table.forEach(function(item) {
// objects[item.name] = item;
// });
// callback(objects);
}
function findById(objects, id) {
return objects.filter(function (o) { return o.id === id; })[0];
}
function addSurvey(name, callback) {
var table = getTable("surveys");
var newObj = {
id: '' + currentId++,
name: name || demoData.defaultName + " " + currentId,
json: "{}"
};
table.push(newObj);
callback(newObj);
}
function postResults(postId, json, callback) {
var table = getTable("results");
var results = findById(table, postId);
if (!results) {
results = {
id: postId,
data: []
}
table.push(results);
}
results.data.push(json);
callback({});
}
function getResults(postId, callback) {
var table = getTable("results");
var results = findById(table, postId);
callback(results);
}
function deleteSurvey(surveyId, callback) {
var table = getTable("surveys");
var survey = findById(table, surveyId);
table.splice(table.indexOf(survey), 1);
callback(survey);
}
function storeSurvey(id, name, json, callback) {
var table = getTable("surveys");
var survey = findById(table, id);
if (!!survey) {
survey.json = json;
} else {
survey = {
id: id,
name: name || id,
json: json
};
table.push(survey);
}
callback && callback(survey);
}
function changeName(id, name, callback) {
var table = getTable("surveys");
var survey = findById(table, id);
if (!!survey) {
survey.name = name;
}
callback && callback(survey);
}
function getSurveys(callback) {
getObjectsFromStorage("surveys", function (objects) {
if (objects.length > 0) {
callback(objects);
} else {
var surveys = getTable("surveys");
demoData.surveys.forEach(function (survey) {
surveys.push(JSON.parse(JSON.stringify(survey)));
})
var results = getTable("results");
demoData.results.forEach(function (result) {
results.push(JSON.parse(JSON.stringify(result)));
})
getObjectsFromStorage("surveys", callback);
}
});
}
return {
addSurvey: addSurvey,
getSurvey: function (surveyId, callback) {
getSurveys(function (surveys) {
callback(findById(surveys, surveyId));
});
},
storeSurvey: storeSurvey,
getSurveys: getSurveys,
deleteSurvey: deleteSurvey,
postResults: postResults,
getResults: getResults,
changeName: changeName
};
}
module.exports = InMemoryDBAdapter;