Comparing version 0.7.3 to 0.8.0
@@ -11,2 +11,3 @@ !function(){ | ||
, clone = require('./clone') | ||
, Promise = Promise || require('es6-promise').Promise | ||
, ORM; | ||
@@ -346,7 +347,24 @@ | ||
var args = new Arguments(arguments) | ||
, callback = args.getFunction(function(){}) | ||
, callback = args.getFunction() | ||
, transaction = args.getObject(); | ||
if (typeof callback !== 'function') { | ||
return new Promise(function(resolve, reject) { | ||
this._executeDelete(function(err, data) { | ||
if (err) reject(err); | ||
else resolve(data); | ||
}.bind(this), transaction, arguments); | ||
}.bind(this)); | ||
} | ||
else { | ||
return this._executeDelete(callback || function(){}, transaction, arguments); | ||
} | ||
} | ||
, _executeDelete: function(callback, transaction, args) { | ||
// we need to get a lock, else the orm will fail | ||
if (!this._getLock('delete', arguments)) return; | ||
if (!this._getLock('delete', args)) return; | ||
@@ -491,8 +509,30 @@ if (transaction || this._getDatabase().isTransaction()) { | ||
var args = new Arguments(arguments) | ||
, callback = args.getFunction(function(){}) | ||
, callback = args.getFunction() | ||
, noReload = args.getBoolean(false) | ||
, transaction = args.getObject(); | ||
if (typeof callback !== 'function') { | ||
// return promise | ||
return new Promise(function(resolve, reject) { | ||
this._executeSave(function(err, data) { | ||
if (err) reject(err); | ||
else resolve(data); | ||
}.bind(this), noReload, transaction, arguments); | ||
}.bind(this)); | ||
} | ||
else { | ||
return this._executeSave(callback || function(){}, noReload, transaction, arguments); | ||
} | ||
} | ||
, _executeSave: function(callback, noReload, transaction, args) { | ||
// we need to get a lock, else the orm will fail | ||
if (!this._getLock('save', arguments)) return; | ||
if (!this._getLock('save', args)) return; | ||
@@ -499,0 +539,0 @@ // either we get a transaction from the outside or |
@@ -14,2 +14,3 @@ !function(){ | ||
, QueryCompiler = require('./QueryCompiler') | ||
, Promise = Promise || require('es6-promise').Promise | ||
, ORM; | ||
@@ -660,5 +661,6 @@ | ||
, find: function(callback, transaction) { | ||
if (!callback) throw new Error('.find() was called without callback!'); | ||
/* | ||
* prepare the queries | ||
*/ | ||
, prepare: function() { | ||
new QueryCompiler({ | ||
@@ -668,22 +670,57 @@ orm : this._orm | ||
, resource : this._rootResource | ||
, transaction : transaction | ||
}).find(callback); | ||
}).prepare(); | ||
return this; | ||
} | ||
, findOne: function(callback, transaction) { | ||
if (!callback) throw new Error('.findOne() was called without callback!'); | ||
new QueryCompiler({ | ||
orm : this._orm | ||
, getDatabase : this._getDatabase | ||
, resource : this._rootResource | ||
, transaction : transaction | ||
}).findOne(callback); | ||
/* | ||
* execute the query, either using a classic callback or using a promise | ||
* | ||
* @param <Function> optional callback | ||
* @param <Object> optional transaction | ||
* @param <String> action to execute | ||
*/ | ||
, _execute: function(callback, transaction, action, arg) { | ||
if (typeof callback !== 'function') { | ||
// callback is transaction | ||
if (callback) transaction = callback; | ||
// return promise | ||
return new Promise(function(resolve, reject) { | ||
new QueryCompiler({ | ||
orm : this._orm | ||
, getDatabase : this._getDatabase | ||
, resource : this._rootResource | ||
, transaction : transaction | ||
})[action](function(err, data) { | ||
if (err) reject(err); | ||
else resolve(data); | ||
}.bind(this), arg); | ||
}.bind(this)); | ||
} | ||
else { | ||
new QueryCompiler({ | ||
orm : this._orm | ||
, getDatabase : this._getDatabase | ||
, resource : this._rootResource | ||
, transaction : transaction | ||
})[action](callback, arg); | ||
} | ||
} | ||
, find: function(callback, transaction) { | ||
return this._execute(callback, transaction, 'find'); | ||
} | ||
, findOne: function(callback, transaction) { | ||
return this._execute(callback, transaction, 'findOne'); | ||
} | ||
, count: function(callback, transaction, column) { | ||
if (!callback) throw new Error('.count() was called without callback!'); | ||
if (type.string(transaction)) { | ||
@@ -693,31 +730,13 @@ column = transaction; | ||
} | ||
else if (type.string(callback)) { | ||
column = callback; | ||
callback = null; | ||
} | ||
new QueryCompiler({ | ||
orm : this._orm | ||
, getDatabase : this._getDatabase | ||
, resource : this._rootResource | ||
, transaction : transaction | ||
}).count(callback, column); | ||
return this._execute(callback, transaction, 'count', column); | ||
} | ||
/* | ||
* prepare the queries | ||
*/ | ||
, prepare: function() { | ||
new QueryCompiler({ | ||
orm : this._orm | ||
, getDatabase : this._getDatabase | ||
, resource : this._rootResource | ||
}).prepare(); | ||
return this; | ||
} | ||
, delete: function(callback, transaction) { | ||
new QueryCompiler({ | ||
orm : this._orm | ||
, getDatabase : this._getDatabase | ||
, resource : this._rootResource | ||
, transaction : transaction | ||
}).delete(callback); | ||
return this._execute(callback, transaction, 'delete'); | ||
} | ||
@@ -728,10 +747,6 @@ | ||
this._rootResource.query.values = values; | ||
new QueryCompiler({ | ||
orm : this._orm | ||
, getDatabase : this._getDatabase | ||
, resource : this._rootResource | ||
, transaction : transaction | ||
}).update(callback); | ||
return this._execute(callback, transaction, 'update'); | ||
} | ||
}); | ||
}(); |
{ | ||
"name" : "ee-orm" | ||
, "description" : "ORM for node.js supporting postgres or mysql. Supports eager loading, complex queries, joins, transactions, complex database clusters & connection pooling." | ||
, "version" : "0.7.3" | ||
, "version" : "0.8.0" | ||
, "homepage" : "https://github.com/eventEmitter/ee-orm" | ||
@@ -29,2 +29,3 @@ , "author" : "Michael van der Weg <michael@eventemitter.com> (http://eventemitter.com/)" | ||
, "ee-postgres-connection" : "^0.4.0" | ||
, "es6-promise" : "2.0.0" | ||
} | ||
@@ -31,0 +32,0 @@ , "devDependencies": { |
24
test.js
var Class = require('ee-class') | ||
, log = require('ee-log') | ||
, assert = require('assert') | ||
, async = require('ee-async') | ||
@@ -12,3 +13,3 @@ , ORM = require('./') | ||
orm.on('load', function(err) { | ||
var db = orm.eventbooster | ||
var db = orm.ee_orm_test | ||
, start | ||
@@ -27,13 +28,14 @@ , count = 0 | ||
db.condition(['*']) | ||
.fetchConditionType(['*']) | ||
.getCondition_tenant(['*']) | ||
.getArticle_conditionTenant(['*']) | ||
.getArticleInstanceCart_articleConditionTenant(['*']) | ||
.fetchConditionStatus(['*']) | ||
.getArticleInstance_cart(['*']) | ||
.getCart({ | ||
id: 356 | ||
}).find(log); | ||
db.event(['*']).findOne().then(function(event){ log(event); | ||
return event.delete(); | ||
}).then(function(event) { log(event); | ||
return db.event(['*']).findOne(); | ||
}).then(function(evt) { | ||
log(evt); | ||
}).catch(function(err){ | ||
log(err); | ||
}); | ||
}); |
@@ -748,5 +748,66 @@ | ||
}); | ||
describe('[Promises]', function() { | ||
it ('should work on queries', function(done) { | ||
db.event(['*']).joinVenue(true).joinImages().count().then(function(data){ | ||
assert.equal(data, 1); | ||
return db.event(['id']).group('id').find(); | ||
}).then(function(data) { | ||
assert.equal(JSON.stringify(data), '[{"id":4},{"id":3},{"id":2}]'); | ||
done(); | ||
}).catch(function(err){ | ||
done(err); | ||
}); | ||
}); | ||
it ('should work when saving models', function(done) { | ||
db.event(['*'], {id: 3}).findOne().then(function(event){ | ||
assert.equal(JSON.stringify(event), '{"id":3,"id_venue":1,"title":"Mapping Test","startdate":"1970-01-01T00:00:00.000Z","enddate":null,"canceled":true,"created":null,"updated":null,"deleted":null}'); | ||
event.title = 'a changed one!'; | ||
return event.save(); | ||
}).then(function(event) { | ||
assert.equal(JSON.stringify(event), '{"id":3,"id_venue":1,"title":"a changed one!","startdate":"1970-01-01T00:00:00.000Z","enddate":null,"canceled":true,"created":null,"updated":null,"deleted":null}'); | ||
return db.event(['*'], {id: 3}).findOne(); | ||
}).then(function(evt) { | ||
assert.equal(JSON.stringify(evt), '{"id":3,"id_venue":1,"title":"a changed one!","startdate":"1970-01-01T00:00:00.000Z","enddate":null,"canceled":true,"created":null,"updated":null,"deleted":null}'); | ||
done(); | ||
}).catch(function(err){ | ||
done(err); | ||
}); | ||
}); | ||
it ('should work when deleting models', function(done) { | ||
db.event(['*'], {id: 3}).findOne().then(function(event){ | ||
assert.equal(JSON.stringify(event), '{"id":3,"id_venue":1,"title":"a changed one!","startdate":"1970-01-01T00:00:00.000Z","enddate":null,"canceled":true,"created":null,"updated":null,"deleted":null}'); | ||
return event.delete(); | ||
}).then(function(event) { | ||
assert.equal(JSON.stringify(event), '{"id":3,"id_venue":1,"title":"a changed one!","startdate":"1970-01-01T00:00:00.000Z","enddate":null,"canceled":true,"created":null,"updated":null,"deleted":null}'); | ||
return db.event(['*'], {id: 3}).findOne(); | ||
}).then(function(evt) { | ||
assert.equal(evt, undefined); | ||
done(); | ||
}).catch(function(err){ | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
describe('[Connection Pooling]', function(){ | ||
@@ -753,0 +814,0 @@ it('should be able to insert 1000 items at once', function(done){ |
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
264973
5085
11
+ Addedes6-promise@2.0.0
+ Addedes6-promise@2.0.0(transitive)