Skip to content

Commit 9e680a2

Browse files
committed
Use new integrated ddl-sql-sync.
This should fix dresende#416 as well as encourage code reuse
1 parent 9c30b76 commit 9e680a2

File tree

6 files changed

+31
-58
lines changed

6 files changed

+31
-58
lines changed

lib/Drivers/DDL/SQL.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
var _ = require("lodash");
22
var Sync = require("sql-ddl-sync").Sync;
33

4-
exports.sync = function (dialect, driver, opts, cb) {
4+
exports.sync = function (opts, cb) {
55
var sync = new Sync({
6-
dialect : dialect,
7-
db : driver.db,
6+
driver : this,
87
debug : false//function (text) { console.log(text); }
98
});
109

@@ -14,9 +13,9 @@ exports.sync = function (dialect, driver, opts, cb) {
1413
};
1514
var props = {};
1615

17-
if (driver.customTypes) {
18-
for (var k in driver.customTypes) {
19-
sync.defineType(k, driver.customTypes[k]);
16+
if (this.customTypes) {
17+
for (var k in this.customTypes) {
18+
sync.defineType(k, this.customTypes[k]);
2019
}
2120
}
2221
for (var k in opts.allProperties) {
@@ -41,24 +40,28 @@ exports.sync = function (dialect, driver, opts, cb) {
4140
}
4241

4342
sync.sync(cb);
43+
44+
return this;
4445
};
4546

46-
exports.drop = function (dialect, driver, opts, cb) {
47+
exports.drop = function (opts, cb) {
4748
var i, queries = [], pending;
4849

49-
queries.push("DROP TABLE IF EXISTS " + driver.query.escapeId(opts.table));
50+
queries.push("DROP TABLE IF EXISTS " + this.query.escapeId(opts.table));
5051

5152
for (i = 0; i < opts.many_associations.length; i++) {
52-
queries.push("DROP TABLE IF EXISTS " + driver.query.escapeId(opts.many_associations[i].mergeTable));
53+
queries.push("DROP TABLE IF EXISTS " + this.query.escapeId(opts.many_associations[i].mergeTable));
5354
}
5455

5556
pending = queries.length;
5657

5758
for (i = 0; i < queries.length; i++) {
58-
driver.execQuery(queries[i], function (err) {
59+
this.execQuery(queries[i], function (err) {
5960
if (--pending === 0) {
6061
return cb(err);
6162
}
6263
});
6364
}
65+
66+
return this;
6467
};

lib/Drivers/helpers.js renamed to lib/Drivers/DML/_shared.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
module.exports.sql = {
2+
module.exports = {
33
execQuery: function () {
44
if (arguments.length == 2) {
55
var query = arguments[0];
@@ -10,4 +10,4 @@ module.exports.sql = {
1010
}
1111
return this.execSimpleQuery(query, cb);
1212
}
13-
}
13+
};

lib/Drivers/DML/mysql.js

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
var _ = require("lodash");
22
var mysql = require("mysql");
33
var Query = require("sql-query").Query;
4-
var helpers = require("../helpers");
4+
var shared = require("./_shared");
5+
var DDL = require("../DDL/SQL");
56

67
exports.Driver = Driver;
78

89
function Driver(config, connection, opts) {
10+
this.dialect = 'mysql';
911
this.config = config || {};
1012
this.opts = opts || {};
1113
this.customTypes = {};
1214

1315
if (!this.config.timezone) {
1416
this.config.timezone = "local";
1517
}
16-
this.query = new Query({ dialect: "mysql", timezone: this.config.timezone });
18+
this.query = new Query({ dialect: this.dialect, timezone: this.config.timezone });
1719

1820
this.reconnect(null, connection);
1921

@@ -26,19 +28,7 @@ function Driver(config, connection, opts) {
2628
"DISTINCT"];
2729
}
2830

29-
_.extend(Driver.prototype, helpers.sql);
30-
31-
Driver.prototype.sync = function (opts, cb) {
32-
require("../DDL/SQL").sync("mysql", this, opts, cb);
33-
34-
return this;
35-
};
36-
37-
Driver.prototype.drop = function (opts, cb) {
38-
require("../DDL/SQL").drop("mysql", this, opts, cb);
39-
40-
return this;
41-
};
31+
_.extend(Driver.prototype, shared, DDL);
4232

4333
Driver.prototype.ping = function (cb) {
4434
this.db.ping(cb);

lib/Drivers/DML/postgres.js

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var _ = require("lodash");
22
var pg = require("pg");
33
var Query = require("sql-query").Query;
4-
var helpers = require("../helpers");
4+
var shared = require("./_shared");
5+
var DDL = require("../DDL/SQL");
56

67
exports.Driver = Driver;
78

@@ -71,14 +72,15 @@ var switchableFunctions = {
7172
function Driver(config, connection, opts) {
7273
var functions = switchableFunctions.client;
7374

75+
this.dialect = 'postgresql';
7476
this.config = config || {};
7577
this.opts = opts || {};
7678

7779
if (!this.config.timezone) {
7880
this.config.timezone = "local";
7981
}
8082

81-
this.query = new Query({ dialect: "postgresql", timezone: this.config.timezone });
83+
this.query = new Query({ dialect: this.dialect, timezone: this.config.timezone });
8284
this.customTypes = {};
8385

8486
if (connection) {
@@ -115,19 +117,7 @@ function Driver(config, connection, opts) {
115117
];
116118
}
117119

118-
_.extend(Driver.prototype, helpers.sql);
119-
120-
Driver.prototype.sync = function (opts, cb) {
121-
require("../DDL/SQL").sync("postgresql", this, opts, cb);
122-
123-
return this;
124-
};
125-
126-
Driver.prototype.drop = function (opts, cb) {
127-
require("../DDL/SQL").drop("postgresql", this, opts, cb);
128-
129-
return this;
130-
};
120+
_.extend(Driver.prototype, shared, DDL);
131121

132122
Driver.prototype.ping = function (cb) {
133123
this.execSimpleQuery("SELECT * FROM pg_stat_activity LIMIT 1", function () {

lib/Drivers/DML/sqlite.js

+5-15
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ var _ = require("lodash");
22
var util = require("util");
33
var sqlite3 = require("sqlite3");
44
var Query = require("sql-query").Query;
5-
var helpers = require("../helpers");
5+
var shared = require("./_shared");
6+
var DDL = require("../DDL/SQL");
67

78
exports.Driver = Driver;
89

910
function Driver(config, connection, opts) {
11+
this.dialect = 'sqlite';
1012
this.config = config || {};
1113
this.opts = opts || {};
1214

1315
if (!this.config.timezone) {
1416
this.config.timezone = "local";
1517
}
1618

17-
this.query = new Query({ dialect: "sqlite", timezone: this.config.timezone });
19+
this.query = new Query({ dialect: this.dialect, timezone: this.config.timezone });
1820
this.customTypes = {};
1921

2022
if (connection) {
@@ -37,19 +39,7 @@ function Driver(config, connection, opts) {
3739
"DISTINCT" ];
3840
}
3941

40-
_.extend(Driver.prototype, helpers.sql);
41-
42-
Driver.prototype.sync = function (opts, cb) {
43-
require("../DDL/SQL").sync("sqlite", this, opts, cb);
44-
45-
return this;
46-
};
47-
48-
Driver.prototype.drop = function (opts, cb) {
49-
require("../DDL/SQL").drop("sqlite", this, opts, cb);
50-
51-
return this;
52-
};
42+
_.extend(Driver.prototype, shared, DDL);
5343

5444
Driver.prototype.ping = function (cb) {
5545
process.nextTick(cb);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"sqlite",
1313
"mongodb"
1414
],
15-
"version" : "2.1.3",
15+
"version" : "2.1.4",
1616
"license" : "MIT",
1717
"homepage" : "http://dresende.github.io/node-orm2",
1818
"repository" : "http://github.com/dresende/node-orm2.git",

0 commit comments

Comments
 (0)