From fa8a587215020ceb50c750f275e3605fe5c62416 Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Sat, 24 Mar 2012 17:50:52 +0400 Subject: [PATCH] Delayed database calls --- lib/abstract-class.js | 34 ++++++++++++++++++++++++++++++++++ lib/adapters/mysql.js | 2 +- package.json | 4 ++-- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/abstract-class.js b/lib/abstract-class.js index c8f13c37..fb0e5a7c 100644 --- a/lib/abstract-class.js +++ b/lib/abstract-class.js @@ -117,6 +117,8 @@ AbstractClass.prototype.whatTypeName = function (propName) { * @param callback(err, obj) */ AbstractClass.create = function (data, callback) { + if (stillConnecting(this.schema, this, arguments)) return; + var modelName = this.modelName; 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) { + if (stillConnecting(this.schema, this, arguments)) return; + var Model = this; if (!data.id) return this.create(data, callback); if (this.schema.adapter.updateOrCreate) { @@ -192,6 +204,8 @@ AbstractClass.upsert = AbstractClass.updateOrCreate = function upsert(data, call }; AbstractClass.exists = function exists(id, cb) { + if (stillConnecting(this.schema, this, arguments)) return; + if (id) { this.schema.adapter.exists(this.modelName, id, cb); } else { @@ -200,6 +214,8 @@ AbstractClass.exists = function exists(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) { var obj = null; if (data) { @@ -224,6 +240,8 @@ AbstractClass.find = function find(id, cb) { * @param cb (err, array of AbstractClass) */ AbstractClass.all = function all(params, cb) { + if (stillConnecting(this.schema, this, arguments)) return; + if (arguments.length === 1) { cb = params; params = null; @@ -253,6 +271,8 @@ AbstractClass.all = function all(params, cb) { }; AbstractClass.findOne = function findOne(params, cb) { + if (stillConnecting(this.schema, this, arguments)) return; + if (typeof params === 'function') { cb = params; params = {}; @@ -273,6 +293,8 @@ function substractDirtyAttributes(object, data) { } AbstractClass.destroyAll = function destroyAll(cb) { + if (stillConnecting(this.schema, this, arguments)) return; + this.schema.adapter.destroyAll(this.modelName, function (err) { clearCache(this); cb(err); @@ -280,6 +302,8 @@ AbstractClass.destroyAll = function destroyAll(cb) { }; AbstractClass.count = function (where, cb) { + if (stillConnecting(this.schema, this, arguments)) return; + if (typeof where === 'function') { cb = where; where = null; @@ -298,6 +322,8 @@ AbstractClass.toString = function () { * @param callback(err, obj) */ AbstractClass.prototype.save = function (options, callback) { + if (stillConnecting(this.constructor.schema, this, arguments)) return; + if (typeof options == 'function') { callback = options; options = {}; @@ -386,6 +412,8 @@ AbstractClass.prototype.toObject = function (onlySchema) { }; AbstractClass.prototype.destroy = function (cb) { + if (stillConnecting(this.constructor.schema, this, arguments)) return; + this.trigger('destroy', function (destroyed) { this._adapter().destroy(this.constructor.modelName, this.id, function (err) { removeFromCache(this.constructor, this.id); @@ -397,12 +425,16 @@ AbstractClass.prototype.destroy = function (cb) { }; AbstractClass.prototype.updateAttribute = function (name, value, cb) { + if (stillConnecting(this.constructor.schema, this, arguments)) return; + data = {}; data[name] = value; this.updateAttributes(data, cb); }; AbstractClass.prototype.updateAttributes = function updateAttributes(data, cb) { + if (stillConnecting(this.constructor.schema, this, arguments)) return; + var inst = this; var model = this.constructor.modelName; @@ -465,6 +497,8 @@ AbstractClass.prototype.propertyChanged = function (attr) { }; AbstractClass.prototype.reload = function (cb) { + if (stillConnecting(this.constructor.schema, this, arguments)) return; + var obj = getCached(this.constructor, this.id); if (obj) { obj.reset(); diff --git a/lib/adapters/mysql.js b/lib/adapters/mysql.js index c57b7084..ae0da123 100644 --- a/lib/adapters/mysql.js +++ b/lib/adapters/mysql.js @@ -26,7 +26,7 @@ exports.initialize = function initializeSchema(schema, callback) { var dbName = s.database; schema.client.query('CREATE DATABASE ' + dbName, function (error) { if (!error) { - callback(); + schema.client.query('USE ' + s.database, callback); } else { throw error; } diff --git a/package.json b/package.json index 74b11a39..a1aa4b49 100644 --- a/package.json +++ b/package.json @@ -1,16 +1,16 @@ { "name": "jugglingdb", "description": "ORM for every database: redis, mysql, neo4j, mongodb, postgres, sqlite", - "version": "0.1.4", + "version": "0.1.5", "author": "Anatoliy Chakkaev ", "contributors": [ { "name": "Anatoliy Chakkaev", "email": "rpm1602@gmail.com" }, { "name": "Julien Guimont", "email": "julien.guimont@gmail.com" }, { "name": "Henri Bergius", "email": "henri.bergius@iki.fi" }, { "name": "redvulps", "email": "fabopereira@gmail.com" }, + { "name": "Felipe Sateler", "email": "fsateler@gmail.com" }, { "name": "Amir M. Mahmoudi", "email": "a@geeknux.com" }, { "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" } ], "repository": {