Comparing version 0.4.9 to 0.5.0
@@ -779,3 +779,3 @@ !function(){ | ||
} | ||
}.bind(this)); | ||
}.bind(this), transaction); | ||
} | ||
@@ -782,0 +782,0 @@ else { |
@@ -28,4 +28,5 @@ !function(){ | ||
Class.define(this, '_originalRecords' , Class([]).Writable()); | ||
Class.define(this, '_error' , Class(null).Writable()); | ||
Class.define(this, 'isMapping' , Class(!!options.isMapping)); | ||
Class.define(this, '_queries' , Class([])); | ||
@@ -84,44 +85,10 @@ this.Model = this._orm[this._database][this._definition.name]; | ||
, _save: {value: function(transaction, noReload, callback) { | ||
// wait until the relatingset is idle, then store all records and create relations | ||
if (this._error) callback(this._error); | ||
else { | ||
if (this._workers) { | ||
this.once('idle', function() { | ||
if (this._error) callback(this._error); | ||
else this._executeSave(transaction, noReload, callback); | ||
}.bind(this)); | ||
} | ||
else { | ||
this._executeSave(transaction, noReload, callback); | ||
} | ||
} | ||
}} | ||
, _executeSave: {value: function(transaction, noReload, callback) { | ||
if (debug) log.info('Relatingset for «'+this._definition.name+'» is storing its items ...'); | ||
// check for query items | ||
var queries = []; | ||
for (var i = 0; i < this.length; i++) { | ||
if (this[i].isQuery) { log.warn('query'); | ||
queries.push(proto.splice.call(this, i, 1)[0]); | ||
i--; | ||
} | ||
} | ||
// execute all queries | ||
async.each(queries, function(query, next){ | ||
this._addQueryItem(item, next); | ||
}.bind(this), function(err){ | ||
// get items stored as query | ||
this._addQueryItems(transaction, function(err) { | ||
if (err) callback(err); | ||
else { | ||
// save all unsaved items | ||
async.each(this, function(item, next){ | ||
if (item.isQuery) { | ||
this._addQueryItem(item, next); | ||
} | ||
else if (!item.isFromDatabase() || !item.isSaved()) { | ||
if (!item.isFromDatabase() || !item.isSaved()) { | ||
// we have to save this item before we can proceed | ||
@@ -162,3 +129,30 @@ // if we're a belongs to set we need to set our id on the item | ||
/* | ||
* execute queries, add the items | ||
*/ | ||
, _addQueryItems: function(transaction, callback) { | ||
async.each(this._queries, function(config, done) { | ||
config.query.find(function(err, set) { | ||
if (err) done(err); | ||
else if(!set || !set.length) done(); | ||
else { | ||
try { | ||
if (config.mode === 'splice') this.splice.apply(this, [config.position, 0].concat(proto.slice.call(set, 0))); | ||
else { | ||
set.forEach(function(item) { | ||
this[config.mode](item); | ||
}.bind(this)); | ||
} | ||
} catch (err) { | ||
if (err) return done(err); | ||
} | ||
done(); | ||
} | ||
}.bind(this), transaction); | ||
}.bind(this), callback); | ||
} | ||
, _getChangedRecords: {value: function(callback){ | ||
@@ -301,7 +295,4 @@ var removed = [] | ||
/* | ||
* the push() method accepts eiteher a quer or a saved or not saved | ||
* model of the target entity. if it gets a query it execute it immediatelly | ||
* in order to get the records which it shoudl link. it wil lpush all models | ||
* added or found via query to the _addedRecords array which will be stored / | ||
* linked when the save method gets calleds | ||
* the push() method accepts eiteher a quer yor a saved or not saved | ||
* model of the target entity. | ||
* | ||
@@ -311,24 +302,12 @@ * @param <Object> item: query or model | ||
*/ | ||
, push: { value: function push (item, callback) { | ||
, push: { value: function push (item) { | ||
if (type.object(item)) { | ||
// check if the item is a model or a query | ||
if (item.isQuery) { | ||
// execute the query, add it to | ||
this._addQueryItem(item, callback); | ||
} | ||
if (type.function(item.isQuery) && item.isQuery()) this._queries.push({mode:'push', query: item}); | ||
else { | ||
if (this._isCorrectType(item)) { | ||
this._protoPush(item); | ||
if (callback) callback(null, this.length); | ||
} | ||
else { | ||
this._error = new Error('Attempt to add models of type «'+item.getName()+'» to a relatingset of type «'+this._definition.name+'» via a query!'); | ||
if (callback) callback(this._error); | ||
} | ||
if (this._isCorrectType(item)) this._protoPush(item); | ||
else throw new Error('Cannot add models of type «'+item.getName()+'» to a relatingSet of type «'+this._definition.name+'»!'); | ||
} | ||
} | ||
else { | ||
this._error = new Error('Push was called with an invalid value: «'+type(item)+'»') | ||
if (callback) callback(this._error); | ||
} | ||
else throw new Error('Cannot add item of type «'+type(item)+'», expected model or query!'); | ||
@@ -342,41 +321,20 @@ return this.length; | ||
, splice: {value:function(index, howMany) { | ||
var newItems = proto.slice.call(arguments, 2) | ||
, removedItems = proto.splice.call(this, index, howMany) | ||
, callback = new Arguments(arguments).getFunction(); | ||
var removedItems = proto.splice.call(this, index, howMany) | ||
, item; | ||
// remove the callback if present | ||
if (callback) newItems.pop(); | ||
if (arguments.length > 2) { | ||
for (var i = 2, l = arguments.length; i< l; i++ ) { | ||
item = arguments[i]; | ||
if (newItems.length) { | ||
asyn.each(newItems, function(item, next){ | ||
if (type.object(item)) { | ||
if (item.isQuery) this._addQueryItem(item, next, index); | ||
// check if the item is a model or a query | ||
if (type.function(item.isQuery) && item.isQuery()) this._queries.push({mode:'splice', position: index, query: item}); | ||
else { | ||
if (this._isCorrectType(item)) { | ||
proto.splice.call(this, index, 0, item); | ||
next(); | ||
} | ||
else { | ||
this._error = new Error('Attempt to add models of type «'+item.getName()+'» to a relatingset of type «'+this._definition.name+'»!'); | ||
next(this._error); | ||
} | ||
if (this._isCorrectType(item)) proto.splice.call(this, index, 0, item); | ||
else throw new Error('Cannot add models of type «'+item.getName()+'» to a relatingSet of type «'+this._definition.name+'»!'); | ||
} | ||
} | ||
else { | ||
this._error = new Error('Push was called with an invalid value: «'+type(item)+'»') | ||
next(this._error); | ||
} | ||
}.bind(this) | ||
, function(err){ | ||
if (err) { | ||
this._error = err; | ||
callback(err, removedItems); | ||
} | ||
else callback(null, removedItems); | ||
}.bind(this)); | ||
else throw new Error('Cannot add item of type «'+type(item)+'», expected model or query!'); | ||
} | ||
} | ||
else { | ||
if (callback) callback(null, removedItems); | ||
} | ||
@@ -392,21 +350,9 @@ return removedItems; | ||
// check if the item is a model or a query | ||
if (item.isQuery) { | ||
// execute the query, add it to | ||
this._addQueryItem(item, callback, null, true); | ||
} | ||
if (type.function(item.isQuery) && item.isQuery()) this._queries.push({mode:'unshift', query: item}); | ||
else { | ||
if (this._isCorrectType(item)){ | ||
proto.unshift.call(this, item); | ||
if (callback) callback(null, this.length); | ||
} | ||
else { | ||
this._error = new Error('Attempt to add models of type «'+item.getName()+'» to a relatingset of type «'+this._definition.name+'»!'); | ||
if (callback) callback(this._error); | ||
} | ||
if (this._isCorrectType(item)) proto.splice.unshift(this, item); | ||
else throw new Error('Cannot add models of type «'+item.getName()+'» to a relatingSet of type «'+this._definition.name+'»!'); | ||
} | ||
} | ||
else { | ||
this._error = new Error('Unshift was called with an invalid value: «'+type(item)+'»') | ||
if (callback) callback(this._error); | ||
} | ||
else throw new Error('Cannot add item of type «'+type(item)+'», expected model or query!'); | ||
@@ -418,34 +364,2 @@ return this.length; | ||
, _addQueryItem: {value: function(item, callback, index, unshift){ | ||
this._workers++; | ||
item.find(function(err, records){ | ||
var err; | ||
this._workers--; | ||
if (err) { | ||
if (callback) callback(err); | ||
else this._error = err; | ||
} | ||
else { | ||
records.forEach(function(model){ | ||
if (this._isCorrectType(model)){ | ||
if (type.number(index)) proto.splice.call(this, index, 0, model); | ||
else if (unshift) proto.unshift.call(this, model); | ||
else this._protoPush(model); | ||
} | ||
else { | ||
err = this._error = new Error('Attempt to add models of type «'+model.getName()+'» to a relatingset of type «'+this._definition.name+'» via a query!'); | ||
} | ||
}.bind(this)); | ||
if (callback) callback(err, this.length); | ||
} | ||
}.bind(this)); | ||
}} | ||
// internal only method for adding records which were already mappend to the collection | ||
@@ -452,0 +366,0 @@ , addExisiting: { value: function(item){ |
{ | ||
"name" : "ee-orm" | ||
, "description" : "An easy to use ORM for node.js. Supports eager loading, complex queries, joins, transactions, complex database clusters & connection pooling." | ||
, "version" : "0.4.9" | ||
, "version" : "0.5.0" | ||
, "homepage" : "https://github.com/eventEmitter/ee-orm" | ||
@@ -6,0 +6,0 @@ , "author" : "Michael van der Weg <michael@eventemitter.com> (http://eventemitter.com/)" |
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
231758
4501