Permalink
Browse files

add callback support for sync()

  • Loading branch information...
1 parent a29c998 commit 18bef91ba2433b06c9e0cb9a4aa0bed325a428bb @vortec vortec committed Sep 9, 2012
Showing with 19 additions and 11 deletions.
  1. +6 −2 lib/databases/mongodb.js
  2. +5 −6 lib/databases/mysql.js
  3. +6 −1 lib/databases/postgresql.js
  4. +2 −2 lib/orm.js
@@ -26,9 +26,13 @@ DBClient.prototype.getCollection = function (collection, cb) {
})(this));
};
-DBClient.prototype.createCollection = function (collection, fields, assocs) {
+DBClient.prototype.createCollection = function (collection, fields, assocs, callback) {
var _collection = collection.toLowerCase(collection);
- this._client.createCollection(_collection, function () {});
+ this._client.createCollection(_collection, function () {
+ if (callback !== undefined) {
+ callback(true);
+ }
+ });
return;
};
@@ -6,7 +6,7 @@ function DBClient(client) {
DBClient.prototype.getClient = function () {
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 _query = "", _fields = [], _indexes = [];
var k, assoc_fields;
@@ -125,11 +125,10 @@ DBClient.prototype.createCollection = function (collection, fields, assocs, opts
_query = _query.replace("%values", _fields.join(", "));
this._client.query(_query, function (err, info) {
- /*
- console.log(err);
- console.log(info);
- console.log("collection synced");
- */
+ if (callback !== undefined) {
+ var success = !err;
+ callback(success);
+ }
});
};
DBClient.prototype.selectRecords = function (collection, config) {
@@ -30,7 +30,7 @@ DBClient.prototype.getClient = function () {
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 _query = "CREATE TABLE \"%table\" (%values)", _fields = [], _indexes = [];
_query = _query.replace("%table", _table);
@@ -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);
}
}
+
+ if (callback !== undefined) {
+ var success = !err;
+ callback(success);
+ }
});
};
DBClient.prototype.selectRecords = function (collection, config) {
View
@@ -365,7 +365,7 @@ ORM.prototype.define = function (model, fields, colParams) {
return this;
};
- Model.sync = function (opts) {
+ Model.sync = function (opts, callback) {
for (var i = 0; i < associations.length; i++) {
if (!associations[i].hasOwnProperty("opts") || !associations[i].opts.hasOwnProperty("properties")) continue;
@@ -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) {
orm._db.clearRecords(model, {

0 comments on commit 18bef91

Please sign in to comment.