arpeggio
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -25,2 +25,3 @@ 'use strict'; | ||
var Boom = require('boom'); | ||
var _ = require('lodash'); | ||
@@ -33,2 +34,3 @@ var CollectionModel = exports.CollectionModel = function () { | ||
this.baseModel = baseModel; | ||
this.subModels = {}; | ||
this.type = new baseModel().type; | ||
@@ -66,11 +68,14 @@ this.adapter = adapter.getAdapter(); | ||
if (!result) { | ||
_context.next = 10; | ||
if (result) { | ||
_context.next = 7; | ||
break; | ||
} | ||
return _context.abrupt('return', false); | ||
case 7: | ||
this.cursor = result.cursor; | ||
if (!(result.results.length > 0)) { | ||
_context.next = 10; | ||
_context.next = 11; | ||
break; | ||
@@ -85,7 +90,9 @@ } | ||
_this.models.push(model); | ||
_this._idMap = _this.adapter.mapIds(_this.models); | ||
}); | ||
return _context.abrupt('return', this); | ||
case 10: | ||
return _context.abrupt('return', false); | ||
case 11: | ||
_context.next = 16; | ||
break; | ||
@@ -113,4 +120,17 @@ case 13: | ||
key: 'buildQuery', | ||
value: function buildQuery() { | ||
return this.adapter.buildQuery(this.type); | ||
} | ||
}, { | ||
key: 'fetch', | ||
value: function () { | ||
var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2() { | ||
var _this2 = this; | ||
var _len2, | ||
args, | ||
_key2, | ||
result, | ||
_args2 = arguments; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
@@ -120,5 +140,47 @@ while (1) { | ||
case 0: | ||
return _context2.abrupt('return', this.adapter.buildQuery(this.type)); | ||
_context2.prev = 0; | ||
case 1: | ||
for (_len2 = _args2.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = _args2[_key2]; | ||
} | ||
_context2.next = 4; | ||
return this.adapter.readMany(this.type, args); | ||
case 4: | ||
result = _context2.sent; | ||
if (result) { | ||
_context2.next = 7; | ||
break; | ||
} | ||
return _context2.abrupt('return', false); | ||
case 7: | ||
if (!(result.length > 0)) { | ||
_context2.next = 10; | ||
break; | ||
} | ||
result.forEach(function (modelData) { | ||
var model = new _this2.baseModel(); | ||
model.ingest(modelData.data); | ||
model.key = modelData.key; | ||
_this2.models.push(model); | ||
_this2._idMap = _this2.adapter.mapIds(_this2.models); | ||
}); | ||
return _context2.abrupt('return', this); | ||
case 10: | ||
_context2.next = 15; | ||
break; | ||
case 12: | ||
_context2.prev = 12; | ||
_context2.t0 = _context2['catch'](0); | ||
throw Boom.wrap(_context2.t0); | ||
case 15: | ||
case 'end': | ||
@@ -128,10 +190,10 @@ return _context2.stop(); | ||
} | ||
}, _callee2, this); | ||
}, _callee2, this, [[0, 12]]); | ||
})); | ||
function buildQuery() { | ||
function fetch(_x2) { | ||
return _ref2.apply(this, arguments); | ||
} | ||
return buildQuery; | ||
return fetch; | ||
}() | ||
@@ -204,2 +266,5 @@ }, { | ||
case 26: | ||
return _context3.abrupt('return', this); | ||
case 27: | ||
case 'end': | ||
@@ -222,4 +287,3 @@ return _context3.stop(); | ||
var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee4() { | ||
var _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, model; | ||
var keysToDelete; | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
@@ -229,70 +293,148 @@ while (1) { | ||
case 0: | ||
_iteratorNormalCompletion2 = true; | ||
_didIteratorError2 = false; | ||
_iteratorError2 = undefined; | ||
_context4.prev = 3; | ||
_iterator2 = this.models[Symbol.iterator](); | ||
keysToDelete = []; | ||
this.models.forEach(function (m) { | ||
keysToDelete.push(m.getKey()); | ||
}); | ||
debugger; | ||
_context4.next = 5; | ||
return this.adapter.destroyMany(this.type, keysToDelete); | ||
case 5: | ||
if (_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done) { | ||
_context4.next = 12; | ||
return _context4.abrupt('return', this); | ||
case 6: | ||
case 'end': | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4, this); | ||
})); | ||
function destroy() { | ||
return _ref4.apply(this, arguments); | ||
} | ||
return destroy; | ||
}() | ||
}, { | ||
key: 'checkUniqueness', | ||
value: function () { | ||
var _ref5 = _asyncToGenerator(regeneratorRuntime.mark(function _callee5(field, value) { | ||
return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
_context5.next = 2; | ||
return this.query(this.buildQuery().filter(field, value)); | ||
case 2: | ||
if (!(this.models.length > 0)) { | ||
_context5.next = 4; | ||
break; | ||
} | ||
model = _step2.value; | ||
_context4.next = 9; | ||
return model.destroy(); | ||
throw Boom.conflict('uniqueness conflict'); | ||
case 9: | ||
_iteratorNormalCompletion2 = true; | ||
_context4.next = 5; | ||
break; | ||
case 4: | ||
return _context5.abrupt('return', this); | ||
case 12: | ||
_context4.next = 18; | ||
break; | ||
case 5: | ||
case 'end': | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5, this); | ||
})); | ||
case 14: | ||
_context4.prev = 14; | ||
_context4.t0 = _context4['catch'](3); | ||
_didIteratorError2 = true; | ||
_iteratorError2 = _context4.t0; | ||
function checkUniqueness(_x3, _x4) { | ||
return _ref5.apply(this, arguments); | ||
} | ||
case 18: | ||
_context4.prev = 18; | ||
_context4.prev = 19; | ||
return checkUniqueness; | ||
}() | ||
}, { | ||
key: 'byId', | ||
value: function byId(id) { | ||
if (!this._idMap) { | ||
return false; | ||
} | ||
return this.adapter.byId(this._idMap, id); | ||
} | ||
}, { | ||
key: 'report', | ||
value: function report(field) { | ||
return this.models.map(function (m) { | ||
return m.field; | ||
}); | ||
} | ||
}, { | ||
key: 'load', | ||
value: function () { | ||
var _ref6 = _asyncToGenerator(regeneratorRuntime.mark(function _callee7(field) { | ||
var _this3 = this; | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
if (!this.submodels[field]) { | ||
_context7.next = 2; | ||
break; | ||
} | ||
case 21: | ||
_context4.prev = 21; | ||
return _context7.delegateYield(regeneratorRuntime.mark(function _callee6() { | ||
var subModels, key, idList; | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
subModels = new _this3.submodels[field]['model'](); | ||
key = _this3.submodels[field]['key']; | ||
idList = []; | ||
if (!_didIteratorError2) { | ||
_context4.next = 24; | ||
break; | ||
} | ||
throw _iteratorError2; | ||
_this3.models.forEach(function (m) { | ||
idList.push(m.report(key)); | ||
}); | ||
idList = _.compact(idList); | ||
case 24: | ||
return _context4.finish(21); | ||
if (!idList.length) { | ||
_context6.next = 8; | ||
break; | ||
} | ||
case 25: | ||
return _context4.finish(18); | ||
_context6.next = 8; | ||
return subModels.fetch(idList); | ||
case 26: | ||
case 8: | ||
_this3.models.forEach(function (m) { | ||
m.addSubmodel(field, subModels.byId(m.report(key))); | ||
}); | ||
case 9: | ||
case 'end': | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _callee6, _this3); | ||
})(), 't0', 2); | ||
case 2: | ||
return _context7.abrupt('return', this); | ||
case 3: | ||
case 'end': | ||
return _context4.stop(); | ||
return _context7.stop(); | ||
} | ||
} | ||
}, _callee4, this, [[3, 14, 18, 26], [19,, 21, 25]]); | ||
}, _callee7, this); | ||
})); | ||
function destroy() { | ||
return _ref4.apply(this, arguments); | ||
function load(_x5) { | ||
return _ref6.apply(this, arguments); | ||
} | ||
return destroy; | ||
return load; | ||
}() | ||
@@ -299,0 +441,0 @@ }, { |
@@ -37,2 +37,3 @@ 'use strict'; | ||
this.timestamps = false; | ||
this.subModels = {}; | ||
this.key = false; | ||
@@ -73,3 +74,10 @@ this.data = {}; | ||
if (result) { | ||
_context.next = 9; | ||
break; | ||
} | ||
throw Boom.notFound(); | ||
case 9: | ||
this.key = result.key; | ||
@@ -80,8 +88,12 @@ this.assignData(result.data); | ||
case 11: | ||
_context.prev = 11; | ||
case 13: | ||
_context.next = 18; | ||
break; | ||
case 15: | ||
_context.prev = 15; | ||
_context.t0 = _context['catch'](0); | ||
throw Boom.wrap(_context.t0); | ||
case 14: | ||
case 18: | ||
case 'end': | ||
@@ -91,3 +103,3 @@ return _context.stop(); | ||
} | ||
}, _callee, this, [[0, 11]]); | ||
}, _callee, this, [[0, 15]]); | ||
})); | ||
@@ -149,15 +161,22 @@ | ||
value: function assignData(inData) { | ||
var data = Object.assign({}, inData); | ||
this.parseSubmodels(); | ||
this.prevData = data; | ||
Object.assign(this.data, data); | ||
} | ||
}, { | ||
key: 'parseSubmodels', | ||
value: function parseSubmodels() { | ||
var _this = this; | ||
var data = Object.assign({}, inData); | ||
if (this.submodels) { | ||
this.submodels.forEach(function (s) { | ||
Object.keys(this.submodels).forEach(function (s) { | ||
var key = s[0]; | ||
var Model = s[1]; | ||
if (data[key]) { | ||
var keyData = data[key]; | ||
if (_this.data[key]) { | ||
var keyData = _this.data[key]; | ||
delete data[key]; | ||
delete _this.data[key]; | ||
if (_.isArray(keyData)) { | ||
@@ -185,4 +204,2 @@ (function () { | ||
} | ||
this.prevData = data; | ||
Object.assign(this.data, data); | ||
} | ||
@@ -354,2 +371,7 @@ }, { | ||
}, { | ||
key: 'setMeta', | ||
value: function setMeta(field, value) { | ||
this.meta[field] = value; | ||
} | ||
}, { | ||
key: 'ingest', | ||
@@ -364,5 +386,8 @@ value: function ingest(d) { | ||
value: function updateTimestamps() { | ||
this.data.updatedAt = new Date(); | ||
var timestamp = Date.now(); | ||
timestamp = timestamp / 1000; | ||
this.data.updatedAt = timestamp; | ||
if (this.isNew) { | ||
this.data.createdAt = new Date(); | ||
this.data.createdAt = timestamp; | ||
} | ||
@@ -407,2 +432,60 @@ } | ||
}, { | ||
key: 'report', | ||
value: function report(field) { | ||
return this.data[field]; | ||
} | ||
}, { | ||
key: 'get', | ||
value: function get(field) { | ||
return this.report(field); | ||
} | ||
}, { | ||
key: 'getKey', | ||
value: function getKey() { | ||
return this.adapter.getKey(this.key); | ||
} | ||
}, { | ||
key: 'addSubmodel', | ||
value: function addSubmodel(field, model) { | ||
if (this.submodels[field]) { | ||
this.children[field] = model; | ||
} | ||
} | ||
}, { | ||
key: 'load', | ||
value: function () { | ||
var _ref7 = _asyncToGenerator(regeneratorRuntime.mark(function _callee7(field, id) { | ||
var model, key; | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
if (!this.submodels[field]) { | ||
_context7.next = 6; | ||
break; | ||
} | ||
model = new this.submodels[field]['model'](); | ||
key = id || this.get(this.submodels[field]['key']); | ||
_context7.next = 5; | ||
return model.fetch(key); | ||
case 5: | ||
this.addSubmodel(field, model); | ||
case 6: | ||
case 'end': | ||
return _context7.stop(); | ||
} | ||
} | ||
}, _callee7, this); | ||
})); | ||
function load(_x5, _x6) { | ||
return _ref7.apply(this, arguments); | ||
} | ||
return load; | ||
}() | ||
}, { | ||
key: 'toJSON', | ||
@@ -412,4 +495,5 @@ value: function toJSON() { | ||
var _id = this.getKey(); | ||
var currentData = Object.assign({ | ||
_id: this.adapter.getKey(this.key) | ||
_id: _id | ||
}, this.data); | ||
@@ -437,4 +521,6 @@ var localData = {}; | ||
}); | ||
} else if (_.isObject(modelData)) { | ||
localData[key] = modelData.toJSON(); | ||
} else { | ||
localData[key] = modelData.toJSON(); | ||
localData[key] = null; | ||
} | ||
@@ -447,3 +533,6 @@ }; | ||
for (var _key4 in this.meta) { | ||
localData[_key4] = this.meta[_key4]; | ||
if (!localData._meta) { | ||
localData._meta = {}; | ||
} | ||
localData._meta[_key4] = this.meta[_key4]; | ||
} | ||
@@ -450,0 +539,0 @@ return localData; |
{ | ||
"name": "arpeggio", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "extremely opinionated ORM/ODM", | ||
@@ -5,0 +5,0 @@ "main": "lib/arpeggio.js", |
@@ -6,2 +6,3 @@ /** | ||
var Boom = require('boom') | ||
var _ = require('lodash') | ||
@@ -15,2 +16,3 @@ import * as adapter from '../adapter' | ||
this.baseModel = baseModel | ||
this.subModels = {} | ||
this.type = new baseModel().type | ||
@@ -24,16 +26,17 @@ this.adapter = adapter.getAdapter() | ||
if (result) { | ||
this.cursor = result.cursor | ||
if (result.results.length > 0) { | ||
result.results.forEach(modelData => { | ||
let model = new this.baseModel() | ||
if (!result) { | ||
return false | ||
} | ||
this.cursor = result.cursor | ||
if (result.results.length > 0) { | ||
result.results.forEach(modelData => { | ||
let model = new this.baseModel() | ||
model.ingest(modelData.data) | ||
model.key = modelData.key | ||
this.models.push(model) | ||
}) | ||
return this | ||
} | ||
model.ingest(modelData.data) | ||
model.key = modelData.key | ||
this.models.push(model) | ||
this._idMap = this.adapter.mapIds(this.models) | ||
}) | ||
return this | ||
} | ||
return false | ||
} | ||
@@ -45,6 +48,30 @@ catch (err) { | ||
async buildQuery() { | ||
buildQuery() { | ||
return this.adapter.buildQuery(this.type) | ||
} | ||
async fetch(...args) { | ||
try { | ||
let result = await this.adapter.readMany(this.type, args) | ||
if (!result) { | ||
return false | ||
} | ||
if (result.length > 0) { | ||
result.forEach(modelData => { | ||
let model = new this.baseModel() | ||
model.ingest(modelData.data) | ||
model.key = modelData.key | ||
this.models.push(model) | ||
this._idMap = this.adapter.mapIds(this.models) | ||
}) | ||
return this | ||
} | ||
} | ||
catch (err) { | ||
throw Boom.wrap(err) | ||
} | ||
} | ||
async save() { | ||
@@ -54,10 +81,57 @@ for (let model of this.models) { | ||
} | ||
return this | ||
} | ||
async destroy() { | ||
for (let model of this.models) { | ||
await model.destroy() | ||
let keysToDelete = [] | ||
this.models.forEach(m => { | ||
keysToDelete.push(m.getKey()) | ||
}) | ||
debugger | ||
await this.adapter.destroyMany(this.type, keysToDelete) | ||
return this | ||
} | ||
async checkUniqueness(field, value) { | ||
await this.query( | ||
this.buildQuery().filter(field, value) | ||
) | ||
if (this.models.length > 0) { | ||
throw Boom.conflict('uniqueness conflict') | ||
} | ||
return this | ||
} | ||
byId(id) { | ||
if (!this._idMap) { | ||
return false | ||
} | ||
return this.adapter.byId(this._idMap, id) | ||
} | ||
report(field) { | ||
return this.models.map(m => m.field) | ||
} | ||
async load(field) { | ||
if (this.submodels[field]) { | ||
let subModels = new this.submodels[field]['model']() | ||
let key = this.submodels[field]['key'] | ||
let idList = [] | ||
this.models.forEach(m => { | ||
idList.push(m.report(key)) | ||
}) | ||
idList = _.compact(idList) | ||
if (idList.length) { | ||
await subModels.fetch(idList) | ||
} | ||
this.models.forEach(m => { | ||
m.addSubmodel(field, subModels.byId(m.report(key))) | ||
}) | ||
} | ||
return this | ||
} | ||
toJSON() { | ||
@@ -64,0 +138,0 @@ let localData = [] |
@@ -19,2 +19,3 @@ /** | ||
this.timestamps = false | ||
this.subModels = {} | ||
this.key = false | ||
@@ -33,6 +34,11 @@ this.data = {} | ||
this.key = result.key | ||
this.assignData(result.data) | ||
this.isNew = false | ||
return this | ||
if (!result) { | ||
throw Boom.notFound() | ||
} | ||
else { | ||
this.key = result.key | ||
this.assignData(result.data) | ||
this.isNew = false | ||
return this | ||
} | ||
} | ||
@@ -56,11 +62,17 @@ catch (err) { | ||
this.parseSubmodels() | ||
this.prevData = data | ||
Object.assign(this.data, data) | ||
} | ||
parseSubmodels() { | ||
if (this.submodels) { | ||
this.submodels.forEach(s => { | ||
Object.keys(this.submodels).forEach(s => { | ||
let key = s[0] | ||
let Model = s[1] | ||
if (data[key]) { | ||
let keyData = data[key] | ||
if (this.data[key]) { | ||
let keyData = this.data[key] | ||
delete data[key] | ||
delete this.data[key] | ||
if (_.isArray(keyData)) { | ||
@@ -87,4 +99,2 @@ this.children[key] = [] | ||
} | ||
this.prevData = data | ||
Object.assign(this.data, data) | ||
} | ||
@@ -154,2 +164,6 @@ | ||
setMeta(field, value) { | ||
this.meta[field] = value | ||
} | ||
ingest(d) { | ||
@@ -162,5 +176,8 @@ this.isNew = false | ||
updateTimestamps() { | ||
this.data.updatedAt = new Date() | ||
let timestamp = Date.now() | ||
timestamp = timestamp / 1000 | ||
this.data.updatedAt = timestamp | ||
if (this.isNew) { | ||
this.data.createdAt = new Date() | ||
this.data.createdAt = timestamp | ||
} | ||
@@ -176,5 +193,34 @@ } | ||
report(field) { | ||
return this.data[field] | ||
} | ||
get(field) { | ||
return this.report(field) | ||
} | ||
getKey() { | ||
return this.adapter.getKey(this.key) | ||
} | ||
addSubmodel(field, model) { | ||
if (this.submodels[field]) { | ||
this.children[field] = model | ||
} | ||
} | ||
async load(field, id) { | ||
if (this.submodels[field]) { | ||
let model = new this.submodels[field]['model']() | ||
let key = id || this.get(this.submodels[field]['key']) | ||
await model.fetch(key) | ||
this.addSubmodel(field, model) | ||
} | ||
} | ||
toJSON() { | ||
let _id = this.getKey() | ||
let currentData = Object.assign({ | ||
_id: this.adapter.getKey(this.key) | ||
_id: _id | ||
}, this.data) | ||
@@ -202,8 +248,14 @@ let localData = {} | ||
} | ||
else { | ||
else if (_.isObject(modelData)) { | ||
localData[key] = modelData.toJSON() | ||
} | ||
else { | ||
localData[key] = null | ||
} | ||
} | ||
for (let key in this.meta) { | ||
localData[key] = this.meta[key] | ||
if (!localData._meta) { | ||
localData._meta = {} | ||
} | ||
localData._meta[key] = this.meta[key] | ||
} | ||
@@ -210,0 +262,0 @@ return localData |
40622
1212
2