Delayed database calls

This commit is contained in:
Anatoliy Chakkaev 2012-03-24 17:50:52 +04:00
parent 29afdff4dc
commit fa8a587215
3 changed files with 37 additions and 3 deletions

View File

@ -117,6 +117,8 @@ AbstractClass.prototype.whatTypeName = function (propName) {
* @param callback(err, obj) * @param callback(err, obj)
*/ */
AbstractClass.create = function (data, callback) { AbstractClass.create = function (data, callback) {
if (stillConnecting(this.schema, this, arguments)) return;
var modelName = this.modelName; var modelName = this.modelName;
if (typeof data === 'function') { if (typeof data === 'function') {
@ -167,7 +169,17 @@ AbstractClass.create = function (data, callback) {
} }
}; };
function stillConnecting(schema, obj, args) {
if (schema.connected) return false;
var method = args.callee;
schema.on('connected', function () {
method.apply(obj, [].slice.call(args));
});
};
AbstractClass.upsert = AbstractClass.updateOrCreate = function upsert(data, callback) { AbstractClass.upsert = AbstractClass.updateOrCreate = function upsert(data, callback) {
if (stillConnecting(this.schema, this, arguments)) return;
var Model = this; var Model = this;
if (!data.id) return this.create(data, callback); if (!data.id) return this.create(data, callback);
if (this.schema.adapter.updateOrCreate) { if (this.schema.adapter.updateOrCreate) {
@ -192,6 +204,8 @@ AbstractClass.upsert = AbstractClass.updateOrCreate = function upsert(data, call
}; };
AbstractClass.exists = function exists(id, cb) { AbstractClass.exists = function exists(id, cb) {
if (stillConnecting(this.schema, this, arguments)) return;
if (id) { if (id) {
this.schema.adapter.exists(this.modelName, id, cb); this.schema.adapter.exists(this.modelName, id, cb);
} else { } else {
@ -200,6 +214,8 @@ AbstractClass.exists = function exists(id, cb) {
}; };
AbstractClass.find = function find(id, cb) { AbstractClass.find = function find(id, cb) {
if (stillConnecting(this.schema, this, arguments)) return;
this.schema.adapter.find(this.modelName, id, function (err, data) { this.schema.adapter.find(this.modelName, id, function (err, data) {
var obj = null; var obj = null;
if (data) { if (data) {
@ -224,6 +240,8 @@ AbstractClass.find = function find(id, cb) {
* @param cb (err, array of AbstractClass) * @param cb (err, array of AbstractClass)
*/ */
AbstractClass.all = function all(params, cb) { AbstractClass.all = function all(params, cb) {
if (stillConnecting(this.schema, this, arguments)) return;
if (arguments.length === 1) { if (arguments.length === 1) {
cb = params; cb = params;
params = null; params = null;
@ -253,6 +271,8 @@ AbstractClass.all = function all(params, cb) {
}; };
AbstractClass.findOne = function findOne(params, cb) { AbstractClass.findOne = function findOne(params, cb) {
if (stillConnecting(this.schema, this, arguments)) return;
if (typeof params === 'function') { if (typeof params === 'function') {
cb = params; cb = params;
params = {}; params = {};
@ -273,6 +293,8 @@ function substractDirtyAttributes(object, data) {
} }
AbstractClass.destroyAll = function destroyAll(cb) { AbstractClass.destroyAll = function destroyAll(cb) {
if (stillConnecting(this.schema, this, arguments)) return;
this.schema.adapter.destroyAll(this.modelName, function (err) { this.schema.adapter.destroyAll(this.modelName, function (err) {
clearCache(this); clearCache(this);
cb(err); cb(err);
@ -280,6 +302,8 @@ AbstractClass.destroyAll = function destroyAll(cb) {
}; };
AbstractClass.count = function (where, cb) { AbstractClass.count = function (where, cb) {
if (stillConnecting(this.schema, this, arguments)) return;
if (typeof where === 'function') { if (typeof where === 'function') {
cb = where; cb = where;
where = null; where = null;
@ -298,6 +322,8 @@ AbstractClass.toString = function () {
* @param callback(err, obj) * @param callback(err, obj)
*/ */
AbstractClass.prototype.save = function (options, callback) { AbstractClass.prototype.save = function (options, callback) {
if (stillConnecting(this.constructor.schema, this, arguments)) return;
if (typeof options == 'function') { if (typeof options == 'function') {
callback = options; callback = options;
options = {}; options = {};
@ -386,6 +412,8 @@ AbstractClass.prototype.toObject = function (onlySchema) {
}; };
AbstractClass.prototype.destroy = function (cb) { AbstractClass.prototype.destroy = function (cb) {
if (stillConnecting(this.constructor.schema, this, arguments)) return;
this.trigger('destroy', function (destroyed) { this.trigger('destroy', function (destroyed) {
this._adapter().destroy(this.constructor.modelName, this.id, function (err) { this._adapter().destroy(this.constructor.modelName, this.id, function (err) {
removeFromCache(this.constructor, this.id); removeFromCache(this.constructor, this.id);
@ -397,12 +425,16 @@ AbstractClass.prototype.destroy = function (cb) {
}; };
AbstractClass.prototype.updateAttribute = function (name, value, cb) { AbstractClass.prototype.updateAttribute = function (name, value, cb) {
if (stillConnecting(this.constructor.schema, this, arguments)) return;
data = {}; data = {};
data[name] = value; data[name] = value;
this.updateAttributes(data, cb); this.updateAttributes(data, cb);
}; };
AbstractClass.prototype.updateAttributes = function updateAttributes(data, cb) { AbstractClass.prototype.updateAttributes = function updateAttributes(data, cb) {
if (stillConnecting(this.constructor.schema, this, arguments)) return;
var inst = this; var inst = this;
var model = this.constructor.modelName; var model = this.constructor.modelName;
@ -465,6 +497,8 @@ AbstractClass.prototype.propertyChanged = function (attr) {
}; };
AbstractClass.prototype.reload = function (cb) { AbstractClass.prototype.reload = function (cb) {
if (stillConnecting(this.constructor.schema, this, arguments)) return;
var obj = getCached(this.constructor, this.id); var obj = getCached(this.constructor, this.id);
if (obj) { if (obj) {
obj.reset(); obj.reset();

View File

@ -26,7 +26,7 @@ exports.initialize = function initializeSchema(schema, callback) {
var dbName = s.database; var dbName = s.database;
schema.client.query('CREATE DATABASE ' + dbName, function (error) { schema.client.query('CREATE DATABASE ' + dbName, function (error) {
if (!error) { if (!error) {
callback(); schema.client.query('USE ' + s.database, callback);
} else { } else {
throw error; throw error;
} }

View File

@ -1,16 +1,16 @@
{ {
"name": "jugglingdb", "name": "jugglingdb",
"description": "ORM for every database: redis, mysql, neo4j, mongodb, postgres, sqlite", "description": "ORM for every database: redis, mysql, neo4j, mongodb, postgres, sqlite",
"version": "0.1.4", "version": "0.1.5",
"author": "Anatoliy Chakkaev <rpm1602@gmail.com>", "author": "Anatoliy Chakkaev <rpm1602@gmail.com>",
"contributors": [ "contributors": [
{ "name": "Anatoliy Chakkaev", "email": "rpm1602@gmail.com" }, { "name": "Anatoliy Chakkaev", "email": "rpm1602@gmail.com" },
{ "name": "Julien Guimont", "email": "julien.guimont@gmail.com" }, { "name": "Julien Guimont", "email": "julien.guimont@gmail.com" },
{ "name": "Henri Bergius", "email": "henri.bergius@iki.fi" }, { "name": "Henri Bergius", "email": "henri.bergius@iki.fi" },
{ "name": "redvulps", "email": "fabopereira@gmail.com" }, { "name": "redvulps", "email": "fabopereira@gmail.com" },
{ "name": "Felipe Sateler", "email": "fsateler@gmail.com" },
{ "name": "Amir M. Mahmoudi", "email": "a@geeknux.com" }, { "name": "Amir M. Mahmoudi", "email": "a@geeknux.com" },
{ "name": "Justinas Stankevičius", "email": "justinas@justinas.me" }, { "name": "Justinas Stankevičius", "email": "justinas@justinas.me" },
{ "name": "Felipe Sateler", "email": "fsateler@gmail.com" },
{ "name": "Rick O'Toole", "email": "patrick.n.otoole@gmail.com" } { "name": "Rick O'Toole", "email": "patrick.n.otoole@gmail.com" }
], ],
"repository": { "repository": {