-
Notifications
You must be signed in to change notification settings - Fork 0
/
protolus-data.js
349 lines (341 loc) · 13.1 KB
/
protolus-data.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
var ext = require('prime-ext');
var prime = ext(require('prime'));
var Class = require('Classy');
var type = require('prime/util/type');
var string = ext(require('prime/es5/string'));
var array = ext(require('prime/es5/array'));
var fn = require('prime/es5/function');
var regexp = require('prime/es5/regexp');
var Emitter = require('prime/util/emitter');
var fs = require('fs');
var Options = require('prime-ext/options');
var Registry = require('prime-ext/registry');
var Filters = require('prime-ext/filters');
var InternalWorker = require('prime-ext/internal-worker');
var ProtolusData = new Class({
data : {},
primaryKey : 'id',
datasource : null,
exists : false,
fields : [],
virtuals : {},
fieldOptions : {},
initialize : function(options){
if(!options.datasource) new Error('Datasource not specified for object!');
if(!options.name) new Error('Data name not specified for object!');
if(!ProtolusData.sources[options.datasource]) new Error('Datasource not found for object!');
this.options = options;
this.datasource = ProtolusData.Source.get(options.datasource);
//console.log('C', (this.datasource == undefined), options, prime.keys(ProtolusData.sources));
if(this.datasource){
switch(this.datasource.options.type){ //convert from switch to DS callout
case 'mongo':
//console.log('D');
if(this.primaryKey == 'id'){ //if we index by id, we'll assume mongo's _id will do just as well
array.erase(this.fields, 'id');
this.virtualAlias('id', '_id');
/*
this.virtualSetter('id', function(value){
console.log('id', value);
throw ('Mongo IDs cannot be altered!');
}); //*/
this.primaryKey = '_id';
this.setOption('type', '_id', 'mongoid');
}
break;
case 'mysql':
break;
}
}
},
get : function(key, typed){
if(this.virtuals[key] && this.virtuals[key].get){
return this.virtuals[key].get(key, typed);
}else{
if(this.data[key]){
if(typed && this.fieldOptions[key] && this.fieldOptions[key]['type']){
return this.datasource.getRepresentation(this.fieldOptions[key]['type'], this.data[key]);
}else{
return this.data[key];
}
}else{
}
}
},
getByType : function(key, value){
if(this.virtuals[key] && this.virtuals[key].get){
return this.virtuals[key].type(value);
}else if(this.data[key]){
if(this.fieldOptions[key] && this.fieldOptions[key]['type']) return this.datasource.getRepresentation(this.fieldOptions[key]['type'], value);
else return value;
}else return value;
},
set : function(key, value){
if(this.virtuals[key] && this.virtuals[key].set){
this.data[key] = this.virtuals[key].set(value);
}else{
this.data[key] = value;
}
},
setOption : function(option, key, value){
if(!this.fieldOptions[key]) this.fieldOptions[key] = {};
this.fieldOptions[key][option] = value;
},
virtualSetter : function(key, callback){
if(!this.virtuals[key]) this.virtuals[key] = {};
this.virtuals[key]['set'] = callback;
},
virtualGetter : function(key, callback){
if(!this.virtuals[key]) this.virtuals[key] = {};
this.virtuals[key]['get'] = callback;
},
virtualByType : function(key, callback){
if(!this.virtuals[key]) this.virtuals[key] = {};
this.virtuals[key]['type'] = callback;
},
virtualAlias : function(key, value){
if(!this.virtuals[key]) this.virtuals[key] = {};
this.virtuals[key]['alias'] = value;
this.virtualGetter(key, fn.bind(function(typed){
if(typed && this.fieldOptions[key] && this.fieldOptions[key]['type']) return this.datasource.getRepresentation(this.fieldOptions[key]['type'], value);
return this.get(value);
}, this));
this.virtualSetter(key, fn.bind(function(incoming){
return this.data[value] = incoming;
}, this));
this.virtualByType(key, fn.bind(function(incoming){
return this.getByType(value, incoming);
}, this));
},
load : function(id, callback, errorCallback){
this.set(this.primaryKey, id);
return this.datasource.load(this, fn.bind(function(data){
this.exists = true;
//this.data = data;
if(callback) callback(data);
}, this), errorCallback);
},
delete : function(callback, errorCallback){
return this.datasource.delete(this, callback, errorCallback);
},
save : function(callback, errorCallback){
//if(!this.db) console.log('could not find datasource:'+this.options.datasource, Data.sources);
if(this.permissions === true){//create new perms
if(this.exists){
if(this.progenitor){
this.permissions = this.progenitor.can('write', this);
}else{
console.log(new Error().stack);
throw('object edited by unknown progenitor');
}
}else{
if(this.progenitor){
this.permissions = this.progenitor.newPermissions();
}else{
console.log(new Error().stack);
throw('object created by unknown progenitor');
}
}
}
return this.datasource.save(this, fn.bind(function(data, info){
this.data = data;
if(callback) callback(data, info);
}, this), errorCallback);
},
});
ProtolusData.dummies = {};
ProtolusData.classes = {};
ProtolusData.dummy = function(type, classDefinition){
//if(!context) context = this;
if(!ProtolusData.dummies[type]) ProtolusData.dummies[type] = new classDefinition();
/*if(!ProtolusData.dummies[type]){
try{
context.eval('ProtolusData.dummies[type] = new cd();');
}catch(ex){
throw('Object type(\''+type+'\') not found');
}
}*/
return ProtolusData.dummies[type];
};
ProtolusData.register = function(type, classDefinition){
ProtolusData.classes[type] = classDefinition;
};
ProtolusData.require = function(type, makeGlobal){
var classDefinition = (ProtolusData.internalRequire || require)(type);
ProtolusData.dummies[type] = new classDefinition();
ProtolusData.register(type, classDefinition);
if(makeGlobal) GLOBAL[type] = classDefinition;
return classDefinition;
};
ProtolusData.parse = function(query, options){
if(!ProtolusData.parser) ProtolusData.parser = new ProtolusData.WhereParser();
return ProtolusData.parser.parse(query); //options?
};
ProtolusData.coreFields = ['modification_time', 'creation_time', 'modifier_id', 'creator_id', 'record_status'];
ProtolusData.sources = {};
ProtolusData.autoLink = false; // join logic
var queryTyper = function(query, dummy, datasource){
array.forEach(query, function(item, index){
if(item.type == 'expression' && dummy.virtuals[item.key]){
var alias = dummy.virtuals[item.key].alias
query[index].key = alias;
var a = query[index].value;
if(query[index].key == '_id'){
var ObjectID = require('mongodb').ObjectID;
query[index].value = ObjectID(query[index].value);
}
}
});
};
ProtolusData.search = function(objType, querystring, options, errorCallback){ //query is a query object or an object
if(type(options) == 'function') options = {onSuccess: options};
if(!options) options = {};
if(errorCallback && type(errorCallback) == 'function') options['onFailure'] = errorCallback;
var dummy = ProtolusData.dummy(objType);
var datasource = Datasource.get(dummy.options.datasource);
var query = ProtolusData.parse(querystring);
queryTyper(query, dummy, datasource);
return datasource.search(objType, query, options);
};
ProtolusData.query = function(objType, querystring, options, errorCallback){ //query is a query object or an object
if(type(options) == 'function') options = {onSuccess: options};
if(!options) options = {};
if(errorCallback && type(errorCallback) == 'function') options['onFailure'] = errorCallback;
if(!errorCallback) errorCallback = function(ob){ throw(ob); };
var dummy = ProtolusData.dummy(objType);
var datasource = ProtolusData.Source.get(dummy.options.datasource);
var query = ProtolusData.parse(querystring);
queryTyper(query, dummy, datasource);
if(!datasource) return errorCallback('There is no datasource by the name \''+dummy.options.datasource+'\'');
return datasource.query(objType, query, options);
};
ProtolusData.id = function(type){
if(!type) type = 'uuid';
switch(type){
case 'uuid' :
return System.uuid.v1();
break;
default:
}
};
ProtolusData.BitMask = require('bit-mask');
ProtolusData.Owner = new Class({ //an owner is an instance of Data
groups : [],
id : false,
can : function(action, object){
if(object.permissions){ //todo: these should cascade
var mask = new ProtolusData.OwnershipMask(object.permissions.mask);
if(this.id == object.permissions.owner){ //user
return mask.hasPermission('user', action);
}else if(this.groups.contains(object.permissions.group)){
return mask.hasPermission('group', action);
}else{
return mask.hasPermission('world', action);
}
} else return true; //no perms
},
newPermissions : function(){
var owner = this.get(this.primaryKey);
return {
'owner':owner,
'group':this.groups[0],
'mask':744
}
}
});
ProtolusData.OwnershipMask = ProtolusData.BitMask.OwnershipMask;
ProtolusData.new = function(type){
try{
eval('this.lastProtolusObject = new GLOBAL.'+type+'();');
var result = this.lastProtolusObject;
delete this.lastProtolusObject;
return result;
}catch(ex){
console.log(ex);
throw('Object creation('+type+') error!');
}
}
ProtolusData.loadSource = function(id){
ProtolusData.Source[id] = require('./sources/'+id.toLowerCase());
}
ProtolusData.WhereParser = require('where-parser');
ProtolusData.Source = new Class({
Implements : Options,
initialize : function(options){
this.setOptions(options);
if(
options.debug &&
!(typeOf(options.debug) == 'string' && options.debug.toLowerCase() !== 'false')
) this.debug = true;
ProtolusData.sources[options.name] = this;
},
search : function(theType, query, options, callback){
var successFunction = options.onSuccess;
options.onSuccess = function(data){
results = [];
data.each(function(row){
var dummy = Data.new(theType);
dummy.data = row;
results.push(dummy);
});
if(successFunction) successFunction(results);
};
var failureFunction = options.onFailure;
options.onFailure = function(error){
if(failureFunction) failureFunction(error);
};
this.query(theType, query, options, callback);
},
query : function(theType, query, options, callback){
var dummy = ProtolusData.dummy(theType);
theType = dummy.options.name;
if(type(options) == 'function'){
callback = options;
options = {};
}
if(!options) options = {};
var predicate = this.buildPredicate(query, options, dummy);
return this.performSearch(theType, predicate, options, (callback || options.onSuccess), (
options.onFailure || function(err){ console.log('['+'⚠ ERROR'+']:'+JSON.stringify(err)); }
));
},
handlePermissions: function(){
},
save: function(object, callback){
return false;
},
escape: function(value){
return value;
},
parseWhere: function(clause){
//block parse
//split off grouping/ordering
//parse disriminants
}
});
ProtolusData.Source.get = function(name){
return ProtolusData.sources[name];
};
ProtolusData.Stream = new Class({
Implements: Options,
initialize : function(options){
this.setOptions(options);
},
emit : function(options, callback){
},
on: function(type, callback){
return value;
}
});
ProtolusData.Container = function(object){
object.Extends = ProtolusData;
var container = new Class(object);
return container;
};
ProtolusData.Stream.get = function(name){
return ProtolusData.streams[name];
};
//ProtolusData.internalRequire = function(topLevelRequire){
// ProtolusData.TLRQ = topLevelRequire;
//};
module.exports = ProtolusData;