Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge pull request #73 from vortec/master
Browse files Browse the repository at this point in the history
Add callback support for sync()
  • Loading branch information
dresende committed Oct 25, 2012
2 parents 74a6d41 + 18bef91 commit 7206c75
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
8 changes: 6 additions & 2 deletions lib/databases/mongodb.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ DBClient.prototype.getCollection = function (collection, cb) {
})(this)); })(this));
}; };


DBClient.prototype.createCollection = function (collection, fields, assocs) { DBClient.prototype.createCollection = function (collection, fields, assocs, callback) {
var _collection = collection.toLowerCase(collection); var _collection = collection.toLowerCase(collection);
this._client.createCollection(_collection, function () {}); this._client.createCollection(_collection, function () {
if (callback !== undefined) {
callback(true);
}
});


return; return;
}; };
Expand Down
11 changes: 5 additions & 6 deletions lib/databases/mysql.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function DBClient(client) {
DBClient.prototype.getClient = function () { DBClient.prototype.getClient = function () {
return this._client; return this._client;
}; };
DBClient.prototype.createCollection = function (collection, fields, assocs, opts) { DBClient.prototype.createCollection = function (collection, fields, assocs, opts, callback) {
var _table = collection.toLowerCase(); var _table = collection.toLowerCase();
var _query = "", _fields = [], _indexes = []; var _query = "", _fields = [], _indexes = [];
var k, assoc_fields; var k, assoc_fields;
Expand Down Expand Up @@ -125,11 +125,10 @@ DBClient.prototype.createCollection = function (collection, fields, assocs, opts
_query = _query.replace("%values", _fields.join(", ")); _query = _query.replace("%values", _fields.join(", "));


this._client.query(_query, function (err, info) { this._client.query(_query, function (err, info) {
/* if (callback !== undefined) {
console.log(err); var success = !err;
console.log(info); callback(success);
console.log("collection synced"); }
*/
}); });
}; };
DBClient.prototype.selectRecords = function (collection, config) { DBClient.prototype.selectRecords = function (collection, config) {
Expand Down
7 changes: 6 additions & 1 deletion lib/databases/postgresql.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ DBClient.prototype.getClient = function () {
return this._client; return this._client;
}; };


DBClient.prototype.createCollection = function (collection, fields, assocs, opts) { /* opts not used yet */ DBClient.prototype.createCollection = function (collection, fields, assocs, opts, callback) { /* opts not used yet */
var _table = collection.toLowerCase(); var _table = collection.toLowerCase();
var _query = "CREATE TABLE \"%table\" (%values)", _fields = [], _indexes = []; var _query = "CREATE TABLE \"%table\" (%values)", _fields = [], _indexes = [];
_query = _query.replace("%table", _table); _query = _query.replace("%table", _table);
Expand Down Expand Up @@ -129,6 +129,11 @@ DBClient.prototype.createCollection = function (collection, fields, assocs, opts
self._client.query("CREATE INDEX \"" + _table + "_" + _indexes[i] + "_idx\" " + "ON \"" + _table + "\"(" + _indexes[i] + ")", emptycb); self._client.query("CREATE INDEX \"" + _table + "_" + _indexes[i] + "_idx\" " + "ON \"" + _table + "\"(" + _indexes[i] + ")", emptycb);
} }
} }

if (callback !== undefined) {
var success = !err;
callback(success);
}
}); });
}; };
DBClient.prototype.selectRecords = function (collection, config) { DBClient.prototype.selectRecords = function (collection, config) {
Expand Down
4 changes: 2 additions & 2 deletions lib/orm.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ ORM.prototype.define = function (model, fields, colParams) {


return this; return this;
}; };
Model.sync = function (opts) { Model.sync = function (opts, callback) {
for (var i = 0; i < associations.length; i++) { for (var i = 0; i < associations.length; i++) {
if (!associations[i].hasOwnProperty("opts") || !associations[i].opts.hasOwnProperty("properties")) continue; if (!associations[i].hasOwnProperty("opts") || !associations[i].opts.hasOwnProperty("properties")) continue;


Expand All @@ -387,7 +387,7 @@ ORM.prototype.define = function (model, fields, colParams) {
} }
} }
} }
orm._db.createCollection(model, fields, associations, opts); orm._db.createCollection(model, fields, associations, opts, callback);
}; };
Model.clear = function (callback) { Model.clear = function (callback) {
orm._db.clearRecords(model, { orm._db.clearRecords(model, {
Expand Down

0 comments on commit 7206c75

Please sign in to comment.