Skip to content

Commit c68e2ca

Browse files
authored
Merge pull request #851 from dresende/async_driver_functions
Add async versions of driver functions
2 parents bdb621f + 8f99b19 commit c68e2ca

File tree

13 files changed

+991
-612
lines changed

13 files changed

+991
-612
lines changed

Changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### v5.0.9
2+
- Add async versions of driver functions ([851](../../pull/851)
3+
14
### v5.0.8
25
- Improve Typescript typings - add offset prop to find options ([850](../../pull/850)
36

lib/Drivers/DML/_utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var Promise = require("bluebird");
2+
3+
function promisifyFunctions (target, functions) {
4+
functions.forEach(function (fnName) {
5+
target[fnName + 'Async'] = Promise.promisify(target[fnName]);
6+
});
7+
};
8+
9+
module.exports = {
10+
promisifyFunctions: promisifyFunctions,
11+
};

lib/Drivers/DML/mongodb.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Utilities = require("../../Utilities");
22
var mongodb = require("mongodb");
3+
var Promise = require("bluebird");
34
var util = require("util");
45
var _ = require('lodash');
56

@@ -440,6 +441,10 @@ function convertToDBVal(key, value, timezone) {
440441
return value;
441442
}
442443

444+
['ping', 'find', 'count', 'insert', 'update', 'remove', 'clear',].forEach(function (fnName) {
445+
Driver.prototype[fnName + 'Async'] = Promise.promisify(Driver.prototype[fnName]);
446+
});
447+
443448
Object.defineProperty(Driver.prototype, "isSql", {
444449
value: false
445450
});

lib/Drivers/DML/mysql.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var _ = require("lodash");
22
var mysql = require("mysql");
33
var Query = require("sql-query").Query;
44
var shared = require("./_shared");
5+
var utils = require("./_utils");
56
var DDL = require("../DDL/SQL");
67

78
exports.Driver = Driver;
@@ -290,6 +291,8 @@ Driver.prototype.propertyToValue = function (value, property) {
290291
return value;
291292
};
292293

294+
utils.promisifyFunctions(Driver.prototype, ['ping', 'execSimpleQuery', 'find', 'count', 'insert', 'update', 'remove', 'clear']);
295+
293296
Object.defineProperty(Driver.prototype, "isSql", {
294297
value: true
295298
});

lib/Drivers/DML/postgres.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
var _ = require("lodash");
2-
var pg = require("pg");
3-
var Query = require("sql-query").Query;
4-
var shared = require("./_shared");
5-
var DDL = require("../DDL/SQL");
1+
var _ = require("lodash");
2+
var Promise = require("bluebird");
3+
var pg = require("pg");
4+
var Query = require("sql-query").Query;
5+
var shared = require("./_shared");
6+
var utils = require("./_utils");
7+
var DDL = require("../DDL/SQL");
68

79
exports.Driver = Driver;
810

@@ -95,6 +97,8 @@ function Driver(config, connection, opts) {
9597

9698
_.extend(this.constructor.prototype, functions);
9799

100+
this.constructor.prototype.execSimpleQueryAsync = Promise.promisify(this.constructor.prototype.execSimpleQuery);
101+
98102
this.aggregate_functions = [
99103
"ABS", "CEIL", "FLOOR", "ROUND",
100104
"AVG", "MIN", "MAX",
@@ -342,6 +346,8 @@ Driver.prototype.propertyToValue = function (value, property) {
342346
return value;
343347
};
344348

349+
utils.promisifyFunctions(Driver.prototype, ['ping', 'find', 'count', 'insert', 'update', 'remove', 'clear']);
350+
345351
Object.defineProperty(Driver.prototype, "isSql", {
346352
value: true
347353
});

lib/Drivers/DML/redshift.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var Promise = require("bluebird");
12
var util = require("util");
23
var postgres = require("./postgres");
34

@@ -42,3 +43,4 @@ Driver.prototype.insert = function (table, data, keyProperties, cb) {
4243
}
4344
}.bind(this));
4445
};
46+
Driver.prototype.insertAsync = Promise.promisify(Driver.prototype.insert);

lib/Drivers/DML/sqlite.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ var util = require("util");
33
var sqlite3 = require("sqlite3");
44
var Query = require("sql-query").Query;
55
var shared = require("./_shared");
6+
var utils = require("./_utils");
67
var DDL = require("../DDL/SQL");
7-
var Promise = require("bluebird");
88

99
exports.Driver = Driver;
1010

@@ -212,12 +212,21 @@ Driver.prototype.remove = function (table, conditions, cb) {
212212

213213
Driver.prototype.clear = function (table, cb) {
214214
var debug = this.opts.debug;
215+
var self = this;
215216

216217
this.execQuery("DELETE FROM ??", [table], function (err) {
217218
if (err) return cb(err);
218219

219-
this.execQuery("DELETE FROM ?? WHERE NAME = ?", ['sqlite_sequence', table], cb);
220-
}.bind(this));
220+
self.execQuery("SELECT count(*) FROM ?? WHERE type=? AND name=?;", ['sqlite_master', 'table', 'sqlite_sequence'], function (err, data) {
221+
if (err) return cb(err);
222+
223+
if (data[0] && data[0]['count(*)'] === 1) {
224+
self.execQuery("DELETE FROM ?? WHERE NAME = ?", ['sqlite_sequence', table], cb);
225+
} else {
226+
cb();
227+
}
228+
});
229+
});
221230
};
222231

223232
Driver.prototype.valueToProperty = function (value, property) {
@@ -355,6 +364,8 @@ Driver.prototype.propertyToValue = function (value, property) {
355364
return value;
356365
};
357366

367+
utils.promisifyFunctions(Driver.prototype, ['ping', 'execSimpleQuery', 'find', 'count', 'insert', 'update', 'remove', 'clear']);
368+
358369
Object.defineProperty(Driver.prototype, "isSql", {
359370
value: true
360371
});

0 commit comments

Comments
 (0)