Comparing version 0.2.6 to 0.2.7
@@ -36,4 +36,5 @@ !function(){ | ||
this._setProperty('_relatingSets', options.relatingSets); | ||
this._setProperty('_getDatabase', options.getDatabase); | ||
this._setProperty('_fromDb', options.isFromDB || false); | ||
this._setProperty('_fromDb', options.isFromDB || false, true); | ||
@@ -65,32 +66,4 @@ //this._initializeRelatingSets(true); | ||
/* | ||
, _initializeRelatingSets: function(partial) { | ||
this._mappings.forEach(function(definition){ | ||
if (!this[definition.mapping.name] && (!partial || !this._relatingSets || this._relatingSets[definition.mapping.name])) { | ||
this[definition.mapping.name] = new RelatingSet({ | ||
orm: this._orm | ||
, definition: definition.mapping | ||
, column: definition.column | ||
, related: this | ||
, database: this._defintion.getDatabaseName() | ||
}); | ||
} | ||
}.bind(this)); | ||
this._belongsTo.forEach(function(definition){ | ||
if (!this[definition.belongs.name] && (!partial || !this._relatingSets || this._relatingSets[definition.belongs.name])) { | ||
this[definition.belongs.name] = new RelatingSet({ | ||
orm: this._orm | ||
, definition: definition.belongs | ||
, column: definition.column | ||
, related: this | ||
, database: this._defintion.getDatabaseName() | ||
}); | ||
} | ||
}.bind(this)); | ||
} | ||
*/ | ||
, _setValues: function(values){ | ||
@@ -100,3 +73,3 @@ Object.keys(values).forEach(function(property){ | ||
if (type.object(item) && item.isModel && item.isModel()) { | ||
if (type.object(item) && !type.null(item) && ((item.isModel && item.isModel()) || item.isQuery)) { | ||
// we got a model | ||
@@ -117,2 +90,21 @@ var name = item.getEntityName(); | ||
} | ||
else if (type.array(item)) { | ||
item.forEach(function(obj){ | ||
if (type.object(obj) && !type.null(obj) && ((obj.isModel && obj.isModel()) || obj.isQuery)) { | ||
var name = obj.getEntityName(); | ||
if (!type.undefined(this[name])){ | ||
if (type.boolean(this[name].isMapping)){ | ||
// relating set | ||
this[name].push(obj); | ||
} | ||
// must be a reference ... | ||
this[name] = obj; | ||
} | ||
else throw new Error('Cannot add «'+name+'» model to «'+this.getEntityName()+'» model! there is no relation between the thwo models!'); | ||
} | ||
else throw new Error('Expected a Query or a Model for the key «'+property+'»!'); | ||
}.bind(this)); | ||
} | ||
else if (property === '____id____') this._mappingIds.push(item); | ||
@@ -124,3 +116,2 @@ else this._values[property] = item; | ||
, _setProperty: function(name, value, writable){ | ||
@@ -149,2 +140,4 @@ Object.defineProperty(this, name, {value: value, writable: writable}); | ||
, reload: function(callback, transaction){ | ||
if (!this.isFromDatabase()) return callback(new Error('Cannot reload record «'+this.getEntityName()+'» without saving it first!')); | ||
var query = { | ||
@@ -164,3 +157,3 @@ select : ['*'] | ||
(transaction || this._orm.getDatabase()).executeQuery(query, function(err, data){ | ||
(transaction || this._getDatabase()).executeQuery(query, function(err, data){ | ||
if (err) callback(err); | ||
@@ -172,3 +165,6 @@ else { | ||
this._fromDb = true; | ||
callback(null, this); | ||
this._reloadRelated(function(err){ | ||
callback(err, this); | ||
}.bind(this), transaction); | ||
} | ||
@@ -182,3 +178,18 @@ } | ||
, _reloadRelated: function(callback, transaction) { | ||
async.each(Object.keys(this._mappings), function(keyName, next){ | ||
this._mappings[keyName].reload(next, transaction); | ||
}.bind(this), callback); | ||
/* | ||
Object.keys(this._belongsTo).forEach(function(keyName){ | ||
}.bind(this)); | ||
Object.keys(this._references).forEach(function(keyName){ | ||
}.bind(this));*/ | ||
} | ||
, _getChangedValues: function() { | ||
@@ -198,3 +209,2 @@ var data = {}; | ||
, delete: function(callback) { | ||
@@ -293,3 +303,9 @@ var callback = arg(arguments, 'function', function(){}) | ||
if (err) callback(err); | ||
else callback(null, this); | ||
else { | ||
this._fromDb = true; | ||
this.reload(function(err){ | ||
if (err) callback(err); | ||
else callback(null, this); | ||
}.bind(this)); | ||
} | ||
}.bind(this)); | ||
@@ -321,3 +337,2 @@ } | ||
if (this._fromDb){ | ||
@@ -350,9 +365,3 @@ if (this._changedValues.length) { | ||
this._saveChildren(transaction, noReload, function(err){ | ||
if (err) callback(err); | ||
else { | ||
if (!noReload) this.reload(callback, transaction); | ||
else callback(null, this); | ||
} | ||
}.bind(this)); | ||
this._saveChildren(transaction, noReload, callback); | ||
} | ||
@@ -359,0 +368,0 @@ else throw new Error('not implemented!'); |
@@ -26,2 +26,7 @@ !function(){ | ||
, getEntityName: function() { | ||
return this.tableName; | ||
} | ||
, init: function(options) { | ||
@@ -28,0 +33,0 @@ // remove deprecated parent property (ee-class implementation) |
@@ -113,30 +113,13 @@ !function(){ | ||
else { | ||
// scan for changes | ||
var removed = [] | ||
, added = [] | ||
, originalMap = this._createMap(this._originalRecords) | ||
, currentMap = this._createMap(this); | ||
// adde items | ||
Object.keys(currentMap).forEach(function(newItemKey){ | ||
if (!originalMap[newItemKey]) { | ||
// new item | ||
added.push(currentMap[newItemKey]); | ||
this._getChangedRecords(function(err, added, removed){ | ||
if (err) callback(err); | ||
else { | ||
// create / remove relational records | ||
async.wait(function(done){ | ||
this._deleteRelationRecords(removed, transaction, noReload, done); | ||
}.bind(this), function(done){ | ||
this._createRelationRecords(added, transaction, noReload, done); | ||
}.bind(this), callback); | ||
} | ||
}.bind(this)); | ||
// removed items | ||
Object.keys(originalMap).forEach(function(oldItemKey){ | ||
if (!currentMap[oldItemKey]) { | ||
// new item | ||
removed.Push(originalMap[oldItemKey]); | ||
} | ||
}.bind(this)); | ||
// create / remove relational records | ||
async.wait(function(done){ | ||
this._deleteRelationRecords(removed, transaction, noReload, done); | ||
}.bind(this), function(done){ | ||
this._createRelationRecords(added, transaction, noReload, done); | ||
}.bind(this), callback); | ||
} | ||
@@ -150,6 +133,30 @@ }.bind(this)); | ||
, _createRelationRecords: { value: function(addedRecords, transaction, noReload, callback) { | ||
//return log(addedRecords.length, this._relatesTo, this._column, this._definition); | ||
, _getChangedRecords: {value: function(callback){ | ||
var removed = [] | ||
, added = [] | ||
, originalMap = this._createMap(this._originalRecords) | ||
, currentMap = this._createMap(this); | ||
// adde items | ||
Object.keys(currentMap).forEach(function(newItemKey){ | ||
if (!originalMap[newItemKey]) { | ||
// new item | ||
added.push(currentMap[newItemKey]); | ||
} | ||
}.bind(this)); | ||
// removed items | ||
Object.keys(originalMap).forEach(function(oldItemKey){ | ||
if (!currentMap[oldItemKey]) { | ||
// new item | ||
removed.Push(originalMap[oldItemKey]); | ||
} | ||
}.bind(this)); | ||
callback(null, added, removed); | ||
}} | ||
, _createRelationRecords: { value: function(addedRecords, transaction, noReload, callback) { | ||
async.each(addedRecords, function(record, next){ | ||
@@ -160,5 +167,5 @@ if (this.isMapping) { | ||
values[this._definition.via.fk] = this._relatesTo[this._column.name]; | ||
values[this._definition.via.otherFk] = model[this._definition.column.name]; | ||
values[this._definition.via.otherFk] = record[this._definition.column.name]; | ||
new this._orm[this._database][this._definition.via.model.name](values).save(transaction, next); | ||
new this._orm[this._definition.model.getDatabaseName()][this._definition.via.model.name](values).save(transaction, next); | ||
} | ||
@@ -171,5 +178,2 @@ else next(); | ||
, _deleteRelationRecords: {value: function(removedRecords, transaction, noReload, callback) { | ||
//return log(removedRecords.length, this._relatesTo, this._column, this._definition ); | ||
async.each(removedRecords, function(record, next){ | ||
@@ -180,3 +184,3 @@ if (this.isMapping) { | ||
values[this._definition.via.fk] = this._relatesTo[this._column.name]; | ||
values[this._definition.via.otherFk] = model[this._definition.column.name]; | ||
values[this._definition.via.otherFk] = record[this._definition.column.name]; | ||
@@ -211,14 +215,45 @@ transaction[this._definition.via.model.name](values).delete(next) | ||
/* | ||
// reload all records | ||
, reload: {value: function(callback) { | ||
// get all records from the database. discards all data that was | ||
// modified but not stored before | ||
, reload: {value: function(callback, transaction) { | ||
// check if there are unsaved values on the relation, then reload all of them | ||
var doRelaod = function(){ | ||
this._reload(callback, (transaction || this._relatesTo._getDatabase())); | ||
}.bind(this); | ||
//this._errors = []; | ||
// wait until we become idle | ||
if (this._workers) this.on('idle', doRelaod); | ||
else doRelaod(); | ||
}} | ||
*/ | ||
, _reload: {value: function(callback, transaction) { | ||
// check if there are unsaved values on the relation, then reload all of them | ||
this._getChangedRecords(function(err, added, removed){ | ||
if (err) callback(err); | ||
else { | ||
log.error('reloading for relating sets needs to be implemented.'); | ||
return callback(); | ||
if (added.length || removed.length) callback(new Error('Cannot reload relation «'+this._definition.name+'» on model «'+this._relatesTo.getEntityName()+'», there are unsaved changes!')); | ||
else { | ||
if (this.isMapping) { | ||
// create a map of existing ids | ||
var query = { | ||
}; | ||
} | ||
else { | ||
} | ||
} | ||
} | ||
}.bind(this)); | ||
}} | ||
/* | ||
@@ -225,0 +260,0 @@ * the push() method accepts eiteher a quer or a saved or not saved |
@@ -39,2 +39,12 @@ !function(){ | ||
, notIn: function(values) { | ||
return function(){ | ||
return { | ||
fn: 'notIn' | ||
, values: values | ||
}; | ||
}; | ||
} | ||
, notNull: function() { | ||
@@ -41,0 +51,0 @@ return function(){ |
{ | ||
"name" : "ee-orm" | ||
, "description" : "An easy to use ORM for node.js. Supports advanced eager loading, complex queries, joins, transactions, complex database clusters & connection pooling." | ||
, "version" : "0.2.6" | ||
, "version" : "0.2.7" | ||
, "homepage" : "https://github.com/eventEmitter/ee-orm" | ||
@@ -6,0 +6,0 @@ , "author" : "Michael van der Weg <michael@eventemitter.com> (http://eventemitter.com/)" |
11
test.js
@@ -33,4 +33,11 @@ | ||
}) | ||
}).save(function(err, model){ | ||
log(err, model); | ||
, language: [ | ||
orm.eventbooster.language({code: 'fr'}) | ||
, orm.eventbooster.language({code: 'de'}) | ||
] | ||
}).save(function(err, resource){ log(err); | ||
resource.language.push(orm.eventbooster.language({code:'it'})); | ||
resource.save(function(err){ | ||
log(err, resource); | ||
}); | ||
}); | ||
@@ -37,0 +44,0 @@ return; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
105071
2469