/
index.js
357 lines (323 loc) · 9.41 KB
/
index.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
var resource = require('resource'),
persistence = resource.define('persistence');
persistence.schema.description = "enables persistence for resources";
persistence.method('enable', enable);
//
// Enables a resource to persistence by
// creating a JugglingDB model to back the resource,
// allowing the resource to be instantiable and backed by a datasource
//
function enable (r, options) {
if(typeof options === "string") {
options = {
type: options
};
}
//
// Require JugglingDB.Schema
//
var Schema = require('./vendor/jugglingdb').Schema,
uuid = require('node-uuid'),
path = require('path');
//
// Map uuid library to persistence resource
//
persistence.uuid = uuid;
//
// Create new JugglingDB schema, based on incoming datasource type
//
var _type = mappings[options.type] || options.type || 'fs';
resource.use(options.type)
resource[options.type].start(function(){
var schema = new Schema(_type, {
database: options.name || "big",
host: options.host,
port: options.port,
path: options.path || path.join(resource.helper.appDir, 'db'),
username: options.username,
password: options.password,
options: options.options,
https: true // TODO: check that HTTPS actually does something
});
//
// Create empty schema object for mapping between resource and JugglingDB
//
var _schema = {};
//
// For every property in the resource schema, map the property to JugglingDB
//
Object.keys(r.schema.properties).forEach(function(p){
var prop = r.schema.properties[p];
_schema[p] = { type: jugglingType(prop) };
});
function jugglingType(prop) {
var typeMap = {
'string': String,
'number': Number,
'integer': Number,
'array': JSON,
'boolean': Boolean,
'object': JSON,
'null': null,
'any': String
};
return typeMap[prop.type] || String;
}
//
// Create a new JugglingDB schema based on temp schema
//
var Model = schema.define(r.name, _schema);
//
// Attach the CRUD methods to the resource
//
//
// CREATE method
//
function create (data, callback) {
//
// If no id is specified, create one using node-uuid
//
if(typeof data.id === 'undefined' || data.id.length === 0) {
data.id = uuid();
}
//
// JugglingDB's "create" method can act like a "create or update"
// depending on the adapter, even though JugglingDB has a separate code
// path for "createOrUpdate" (for example, the cradle adapter has this
// behavior). So, we use our internal "get" function to ensure that it does
// not already exist.
//
// TODO: It is technically possible to have a uuid collision here,
// though very unlikely.
//
get(data.id, function (err, result) {
if (err) {
//
// "not found" errors are good in this case, so call create. Note that
// this error message comes from the get function and not from
// JugglingDB.
//
if (err.message.match(/not found/)) {
return Model.create(data, callback);
}
//
// Other errors should be sent in the callback
//
return callback(err);
}
return callback(new Error(data.id + ' already exists'));
});
}
r.method('create', create, {
"description": "create a new " + r.name,
"properties": {
"options": {
"type": "object",
"properties": r.schema.properties
},
"callback": {
"type": "function"
}
}
});
//
// Get method
//
function get (id, callback){
// TODO: .all is now broken in fs adapter
// NOTE: JugglingDB.find is really resource.get
// NOTE: resource.get is JugglingDB.all with a filter
Model.find(id, function(err, result){
if(result === null) {
return callback(new Error(id + ' not found'));
}
// TODO: check if any of the fields are keys, if so, fetch them
callback(err, result);
});
}
r.method('get', get, {
"description": "get " + r.name + " by id",
"properties": {
"id": {
"type": "any",
"description": "the id of the object",
"required": true
},
"callback": {
"type": "function"
}
}
});
//
// Find method
//
function find (query, callback) {
//
// Remove any empty values from the query
//
for(var k in query) {
if(query[k].length === 0) {
delete query[k];
}
}
Model.all({ where: query }, function(err, results){
if (!Array.isArray(results)) {
results = [results];
}
callback(err, results);
});
}
var querySchema = {
properties: {}
}
Object.keys(r.schema.properties).forEach(function(prop){
if(typeof r.schema.properties[prop] === 'object') {
querySchema.properties[prop] = {};
for (var p in r.schema.properties[prop]) {
querySchema.properties[prop][p] = r.schema.properties[prop][p];
}
} else {
querySchema.properties[prop] = r.schema.properties[prop] || {};
}
querySchema.properties[prop].default = "";
querySchema.properties[prop].required = false;
//
// TODO: remove the following two lines and make enum search work correctly
//
querySchema.properties[prop].type = "any";
delete querySchema.properties[prop].enum;
delete querySchema.properties[prop].format;
});
r.method('find', find, {
"description": "search for instances of " + r.name,
"properties": {
"options": {
"type": "object",
"properties": querySchema.properties
},
"callback": {
"type": "function"
}
}
});
//
// All method
//
function all (callback) {
Model.all({}, callback);
}
r.method('all', all, {
"description": "gets all instances of " + r.name,
"properties": {
"callback": {
"type": "function"
}
}
});
//
// Update method
//
function update (options, callback){
//
// JugglingDB does not have a strict update and instead has
// updateOrCreate, so do a get first and act accordingly
//
get(options.id, function (err, result) {
if (err) {
//
// Unlike the case with strict create, "not found" errors mean we are
// unable to do an update
//
return callback(err);
}
Model.updateOrCreate(options, function(err, updated){
if(err) {
return callback(err);
}
get(options.id, callback);
});
});
}
r.method('update', update, {
"description": "updates a " + r.name + " by id",
"properties": {
"options": {
"type": "object",
"properties": r.schema.properties
},
"callback": {
"type": "function"
}
}
});
//
// Update or create
//
function updateOrCreate (options, callback) {
if(typeof options.id === 'undefined' || options.id.length === 0) {
options.id = uuid();
}
get(options.id, function(err, record){
if (err) {
Model.create(options, callback);
} else {
for (var p in options) {
record[p] = options[p];
}
record.save(callback);
}
});
}
r.method('updateOrCreate', updateOrCreate, {
"description": "updates a " + r.name + " by id, and creates if necessary",
"properties": {
"options": {
"type": "object",
"properties": r.schema.properties
},
"callback": {
"type": "function"
}
}
});
//
// Destroy method
//
function destroy (id, callback){
Model.find(id, function(err, result){
if (err) {
return callback(err);
}
result.destroy(function(){
callback(null, null);
});
});
}
r.method('destroy', destroy, {
"description": "destroys a " + r.name + " by id",
"properties": {
"id": {
"type": "string",
"description": "the id of the object",
"required": true
},
"callback": {
"type": "function"
}
}
});
// assign model to resource
r.model = Model; });
}
//
// Provider API mapping for JugglingDB to datasource API for convenience
//
var mappings = {
"couch": "cradle",
"couchdb": "cradle"
};
persistence.dependencies = {
"node-uuid": "*",
"async": "*"
};
exports.persistence = persistence;