UNPKG

spincycle

Version:

A reactive message router and object manager that lets clients subscribe to object property changes on the server

465 lines (434 loc) 15 kB
// Generated by CoffeeScript 1.12.6 (function() { var DB, OMgr, ResolveModule, SuperModel, all, debug, defer, error, resolver, uuid, bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; defer = require('node-promise').defer; all = require('node-promise').allOrNone; uuid = require('node-uuid'); OMgr = require('./OStore'); DB = require('./DB'); error = require('./Error').error; ResolveModule = require('./ResolveModule'); resolver = new ResolveModule(); debug = process.env["DEBUG"]; SuperModel = (function() { SuperModel.resolver = resolver; SuperModel.prototype.resolver = resolver; SuperModel.oncreatelisteners = []; SuperModel.onCreate = function(cb) { return SuperModel.oncreatelisteners.push(cb); }; function SuperModel(record1) { var missing, q; this.record = record1 != null ? record1 : {}; this.insertObj = bind(this.insertObj, this); this.createObjectFromRecord = bind(this.createObjectFromRecord, this); this.resolveObj = bind(this.resolveObj, this); this.loadFromIds = bind(this.loadFromIds, this); this.unPrettify = bind(this.unPrettify, this); this.prettyPrint = bind(this.prettyPrint, this); this["delete"] = bind(this["delete"], this); this.serialize = bind(this.serialize, this); this.toClient = bind(this.toClient, this); this.getRecord = bind(this.getRecord, this); this.id = this.record.id || uuid.v4(); this.record = this.unPrettify(this.record); missing = true; this.constructor.model.forEach((function(_this) { return function(mp) { if (!_this.record[mp.name]) { if (mp.array === 'true') { _this.record[mp.name] = []; } else if (mp.hashtable === 'true') { _this.record[mp.name] = {}; } else { if (Array.isArray(mp["default"])) { _this.record[mp.name] = []; } else { _this.record[mp.name] = mp["default"] || ' '; } } } if (mp.name === 'createdAt' || mp.name === 'createdBy') { return missing = false; } }; })(this)); if (missing) { this.constructor.model.push({ name: 'createdAt', "public": true, value: 'createdAt' }); this.constructor.model.push({ name: 'modifiedAt', "public": true, value: 'modifiedAt' }); this.constructor.model.push({ name: 'createdBy', "public": true, value: 'createdBy' }); } this.type = this.constructor.type; q = defer(); OMgr.storeObject(this); if (this.record._rev) { this._rev = this.record._rev; } this.loadFromIds(this.constructor.model).then((function(_this) { return function() { if (!_this.createdAt || _this.createdAt === ' ') { _this.createdAt = Date.now(); } if (!_this.modifiedAt || _this.modifiedAt === ' ') { _this.modifiedAt = Date.now(); } if (!_this.createdBy || _this.createdBy === ' ') { _this.createdBy = 'SYSTEM'; } if (!_this.record.id) { SuperModel.oncreatelisteners.forEach(function(listener) { return listener(_this); }); } if (_this.postCreate) { if (debug) { console.log('calling PostCreate on ' + _this.type + ' with deferred ' + q); } return _this.postCreate(q); } else { return q.resolve(_this); } }; })(this), error); return q; } SuperModel.prototype.getRecord = function() { return this._getRecord(this, this.constructor.model, this.record); }; SuperModel.prototype._getRecord = function(me, model, record) { var rv; rv = {}; if (this._rev) { rv._rev = this._rev; } model.forEach((function(_this) { return function(v) { var ha, hk, hv, k, marr, varr; k = v.name; if ((v.value && v.value !== 0) && v.type) { if (me[k]) { if (v.storedirectly) { return rv[k] = me[k]._getRecord(me[k], me[k].constructor.model, me[k].record); } else { return rv[k] = me[k].id; } } else { return rv[k] = null; } } else if ((v.value && v.value !== 0) && !v.type) { rv[k] = me[v.value]; if (!rv[k] && rv[k] !== 0) { if (record && record[k]) { return rv[k] = record[k]; } else { return rv[k] = void 0; } } } else if (v.hashtable) { varr = []; ha = me[v.name] || []; for (hk in ha) { hv = ha[hk]; if (v.storedirectly) { varr.push(hv._getRecord(hv, hv.constructor.model, hv.record)); } else { varr.push(hv.id); } } return rv[k] = varr; } else if (v.array) { varr = []; marr = me[v.name] || []; marr.forEach(function(av) { if (av && av !== null && av !== 'null') { if (v.storedirectly) { return varr.push(av._getRecord(av, av.constructor.model, av.record)); } else { if (av.id) { return varr.push(av.id); } } } }); return rv[k] = varr; } else { if (debug) { return console.log('**************** AAAUAGHH!!! property ' + k + ' was not resolved in SuperModel::_getRecord'); } } }; })(this)); rv.id = this.id; rv.type = this.constructor.type; return rv; }; SuperModel.prototype.toClient = function() { var k, r, ra, rv, v; r = this.getRecord(); ra = this.constructor.model; rv = {}; for (k in r) { v = r[k]; ra.forEach((function(_this) { return function(el) { var res; if (el.name === k && k !== 'record' && el["public"]) { res = _this.prettyPrint(k, v); rv[k] = res; if (!rv[k] && el["default"]) { return rv[k] = el["default"]; } } }; })(this)); } rv.id = this.id; rv.type = this.constructor.type; return rv; }; SuperModel.prototype.serialize = function(updatedObj) { var q, record; q = defer(); if (debug) { console.log('SuperModel.serialize called'); } this._serializing = true; OMgr.storeObject(this); if (updatedObj) { delete updatedObj.record; } record = this.getRecord(); if (record.record) { delete record.record; } if (this._rev) { record._rev = this._rev; } DB.set(this.constructor.type, record, (function(_this) { return function(res) { if (debug) { console.log(' * serialized and persisted ' + _this.type + " id " + _this.id); } _this._serializing = false; if (res) { return q.resolve(_this); } else { return q.resolve(); } }; })(this)); return q; }; SuperModel.prototype["delete"] = function() { return DB.remove(this); }; SuperModel.prototype.prettyPrint = function(name, value) { var rv; rv = value; return rv; }; SuperModel.prototype.unPrettify = function(record) { return record; }; SuperModel.prototype.loadFromIds = function(model) { var alldone, allpromises, q; alldone = defer(); allpromises = []; if (!model || model.length === 0) { q = defer(); allpromises.push(q); q.resolve(); } else { model.forEach((function(_this) { return function(robj) { return (function(resolveobj) { var count, ids, r, temp, x, y; r = defer(); allpromises.push(r); if (resolveobj.value) { if (resolveobj.type) { if (resolveobj.storedirectly) { return _this.createObjectFromRecord(r, resolveobj, 0, _this.record[resolveobj.value]); } else { if (_this.record[resolveobj.value] && (_this.record[resolveobj.value] !== ' ')) { return _this.resolveObj(resolveobj, _this.record[resolveobj.value], r, 0); } else { _this[resolveobj.name] = null; return r.resolve(_this[resolveobj.name]); } } } else { if (_this.record[resolveobj.value]) { _this[resolveobj.name] = _this.record[resolveobj.value] || resolveobj["default"]; } return r.resolve(_this[resolveobj.name]); } } else { if (resolveobj.array === true) { if ((!_this[resolveobj.name]) || (Array.isArray(_this.record[resolveobj.name]) && _this.record[resolveobj.name].length > 0)) { _this[resolveobj.name] = []; } } if ((resolveobj.hashtable === true) && (!_this[resolveobj.name])) { _this[resolveobj.name] = {}; } ids = _this.record[resolveobj.ids]; if (!ids || typeof ids === 'undefined' || ids === 'undefined') { ids = []; return r.resolve(); } else { if (typeof ids === 'string') { ids = [ids]; } if (Array.isArray(ids)) { ids = ids.filter(function(ii) { return ii && ii !== null && ii !== "null" && ii !== "undefined"; }); } else { temp = {}; for (x in ids) { y = ids[x]; if (y && y !== null && y !== "null" && y !== "undefined") { temp[x] = y; } } ids = temp; } count = ids.length; if (!count && count !== 0) { return r.resolve(void 0); } else if (count === 0) { _this[resolveobj.name] = []; return r.resolve([]); } else { return ids.forEach(function(_id) { return (function(id) { --count; if (resolveobj.storedirectly) { return _this.createObjectFromRecord(r, resolveobj, count, id); } else { if ((!id || id === ' ') && _this[resolveobj.name]) { return r.resolve(_this[resolveobj.name]); } else { return _this.resolveObj(resolveobj, id, r, count); } } })(_id); }); } } } })(robj); }; })(this)); } all(allpromises, error).then((function(_this) { return function(results) { return alldone.resolve(results); }; })(this), error); return alldone; }; SuperModel.prototype.resolveObj = function(resolveobj, id, r, count) { if (typeof id !== 'string') { if (id.id) { id = id.id; } else { console.log('%%%%%%%%%%%%%%%%%%% ERROR: Supermodel.resolveObj given a non-string as id!! resolveObj was:'); console.dir(resolveobj); console.log('id was:'); console.dir(id); process.exit(-1); } } return OMgr.getObject(id, resolveobj.type).then((function(_this) { return function(oo) { if (oo) { _this.insertObj(resolveobj, oo); if (count === 0) { return r.resolve(oo); } } else { if (debug) { console.log('SuperModel did not find obj ' + resolveobj.name + ' [' + id + '] of type ' + resolveobj.type + ' in OStore. Getting from DB. typeof of id prop is ' + (typeof id)); } return DB.get(resolveobj.type, [id]).then(function(records) { var record; record = void 0; if (records && records[0]) { record = records[0]; } if (!record) { if (count === 0) { return r.resolve(_this[resolveobj.name]); } } else { if (!id || !resolveobj.type) { return r.resolve(null); } else { return _this.createObjectFromRecord(r, resolveobj, count, record); } } }, error); } }; })(this), error); }; SuperModel.prototype.createObjectFromRecord = function(r, resolveobj, count, record) { if (Array.isArray(record)) { throw new Error('got array instead of record in supermodel createObjectFromRecord !!!'); } if (record && record.type) { return SuperModel.resolver.createObjectFrom(record).then((function(_this) { return function(obj) { if (!obj) { if (count === 0) { return r.resolve(null); } } else { _this.insertObj(resolveobj, obj); if (count === 0) { return r.resolve(obj); } } }; })(this), error); } else { return r.resolve(); } }; SuperModel.prototype.insertObj = function(ro, o) { var property; OMgr.storeObject(o); if (ro.array === true) { this[ro.name].push(o); } else if (ro.hashtable === true) { if (ro.keyproperty) { property = ro.keyproperty; } else { property = 'name'; } this[ro.name][o[property]] = o; } else { this[ro.name] = o; } return OMgr.storeObject(o); }; return SuperModel; })(); module.exports = SuperModel; }).call(this); //# sourceMappingURL=SuperModel.js.map