-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathindex.js
336 lines (291 loc) · 7.93 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
"use strict";
var Promise = require('bluebird'),
Property = require('./property'),
RID = require('../../recordid'),
utils = require('../../utils'),
errors = require('../../errors');
/**
* The class constructor.
* @param {Object} config The configuration for the class
*/
function Class(config) {
config = config || {};
if (!(this instanceof Class)) {
return new Class(config);
}
this.augment('property', Property);
this.augment('custom', require('./custom'));
this.configure(config);
}
Class.prototype.augment = utils.augment;
module.exports = exports = Class;
/**
* Configure the class instance.
* @param {Object} config The configuration object.
*/
Class.prototype.configure = function (config) {
this.db = config.db;
this.name = config.name || '';
this.shortName = config.shortName || null;
this.defaultClusterId = config.defaultClusterId || null;
this.clusterIds = config.clusterIds || [];
this.properties = (config.properties || []).map(function (item) {
item.class = this;
return new Property(item);
}, this);
this.superClass = config.superClass || null;
this.superClasses= config.superClasses || null;
this.originalName = this.name;
if (config.custom && config.custom.fields) {
this.custom.fields = config.custom.fields;
}
else if (config.customFields) {
this.custom.fields = config.customFields;
}
};
/**
* Return a list of records in the class.
*
* @param {Integer|Object} limit The maximum number of records to return, or a configuration object.
* @param {Integer} offset The offset to start returning records from.
* @promise {Object[]} An array of records in the class.
*/
Class.prototype.list = function (limit, offset) {
var query = 'SELECT * FROM ' + this.name,
config = {};
if (limit && typeof limit === 'object') {
config = limit;
limit = config.limit;
offset = config.offset;
}
limit = +limit || 20;
offset = +offset || 0;
if (limit !== Infinity) {
query += ' LIMIT ' + limit + ' OFFSET ' + offset;
}
else {
query += ' OFFSET ' + offset;
}
return this.db.query(query, config);
};
/**
* Find a list of records in the class.
*
* @param {Object} attributes The attributes to search with.
* @param {Integer} limit The maximum number of records to return
* @param {Integer} offset The offset to start returning records from.
* @promise {Object[]} An array of records in the class.
*/
Class.prototype.find = function (attributes, limit, offset) {
var query = 'SELECT * FROM ' + this.name,
keys = Object.keys(attributes),
total = keys.length,
conditions = [],
params = {},
key, sanitizedKey, value, i;
for (i = 0; i < total; i++) {
key = keys[i];
value = attributes[key];
sanitizedKey = key.replace(/\./g, '_');
params[sanitizedKey] = value;
conditions.push(key + ' = :' + sanitizedKey);
}
if (conditions.length) {
query += ' WHERE ' + conditions.join(' AND ');
}
limit = +limit || 20;
offset = +offset || 0;
if (limit !== Infinity) {
query += ' LIMIT ' + limit + ' OFFSET ' + offset;
}
else {
query += ' OFFSET ' + offset;
}
return this.db.query(query, {
params: params
});
};
/**
* Create a record for this class.
*
* @param {Object} record The record to create.
* @promise {Object} The created record.
*/
Class.prototype.create = function (record) {
if (Array.isArray(record)) {
return Promise.map(record, this.create.bind(this));
}
record['@class'] = this.name;
return this.db.record.create(record)
.then(function (record) {
// delete record['@class'];
return record;
});
};
/**
* Reload the class instance.
*
* @promise {Class} The class instance.
*/
Class.prototype.reload = function () {
return this.db.class.get(this.originalName, true)
.bind(this)
.then(function (item) {
this.configure(item);
return this;
});
};
/**
* Static methods.
* These methods are invoked with the database instance as `this`, not `Class`!
*/
/**
* The cached class items.
* @type {Object|false}
*/
exports.cached = false;
/**
* Retreive a list of classes from the database.
*
* @param {Boolean} refresh Whether to refresh the list or not.
* @promise {Object[]} An array of class objects.
*/
exports.list = function (refresh) {
if (!refresh && this.class.cached) {
return Promise.resolve(this.class.cached.items);
}
return this.send('record-load', {
cluster: 0,
position: 1
})
.bind(this)
.then(function (response) {
var record = response.records[0];
if (!record || !record.classes) {
return [];
}
else {
return record.classes;
}
})
.then(this.class.cacheData)
.then(function () {
return this.class.cached.items;
});
};
/**
* Create a new class.
*
* @param {String} name The name of the class to create.
* @param {String} parentName The name of the parent to extend, if any.
* @param {String|Integer} cluster The cluster name or id.
* @param {Boolean} isAbstract The flag for the abstract class
* @promise {Object} The created class object
*/
exports.create = function (name, parentName, cluster, isAbstract) {
var query = 'CREATE CLASS ' + name;
if (parentName) {
query += ' EXTENDS ' + parentName;
}
if (cluster) {
query += ' CLUSTER ' + cluster;
}
if (isAbstract) {
query += ' ABSTRACT';
}
return this.query(query)
.bind(this)
.then(function () {
return this.reload();
})
.then(function () {
return this.class.list(true);
})
.then(function (classes) {
return this.class.get(name);
});
};
/**
* Update the given class.
*
* @param {Object} class The class settings.
* @param {Boolean} reload Whether to reload the class, default to true.
* @promise {Object} The updated class.
*/
exports.update = function (cls, reload) {
var promises = [],
prefix = 'ALTER CLASS ' + cls.name + ' ';
if (reload == null) {
reload = true;
}
if (cls.superClass !== undefined) {
promises.push(this.exec(prefix + 'SUPERCLASS ' + cls.superClass));
}
return Promise.all(promises)
.bind(this)
.then(function () {
return this.class.get(cls.name, reload);
});
};
/**
* Delete a class.
*
* @param {String} name The name of the class to delete.
* @promise {Db} The database instance.
*/
exports.drop = function (name) {
return this.exec('DROP CLASS ' + name)
.bind(this)
.then(function () {
return this.class.list(true);
})
.then(function (classes) {
return this;
});
};
/**
* Get a class by name.
*
* @param {Integer|String} name The name of the class.
* @param {Boolean} refresh Whether to refresh the data, defaults to false.
* @promise {Object} The class object if it exists.
*/
exports.get = function (name, refresh) {
var className = name.toLocaleUpperCase();
if (!refresh && this.class.cached && this.class.cached.names[className]) {
return Promise.resolve(this.class.cached.names[className]);
}
else if (!this.class.cached || refresh) {
return this.class.list(refresh)
.bind(this)
.then(function () {
return this.class.cached.names[className] || Promise.reject(new errors.Request('No such class: ' + name));
});
}
else {
return Promise.reject(new errors.Request('No such class: ' + name));
}
};
/**
* Cache the given class data for fast lookup later.
*
* @param {Object[]} classes The class objects to cache.
* @return {Db} The db instance.
*/
exports.cacheData = function (classes) {
var total = classes.length,
item, i;
classes = classes.map(function (item) {
item.db = this;
return new Class(item);
}, this);
this.class.cached = {
names: {},
items: classes
};
for (i = 0; i < total; i++) {
item = classes[i];
this.class.cached.names[item.name.toLocaleUpperCase()] = item;
}
return this;
};