Skip to content

Commit 232bee6

Browse files
committed
Adds ability to call db.load() with multiple files (closes #329)
Files can be passed one by one as a separate argument or as Array. Mixed arguments are possible. The callback must be the last argument and is now optional.
1 parent c11314a commit 232bee6

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

lib/ORM.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var url = require("url");
55
var hat = require("hat");
66
var Query = require("sql-query");
77
var enforce = require("enforce");
8+
var _ = require("lodash");
89

910
var Model = require("./Model").Model;
1011
var DriverAliases = require("./Drivers/aliases");
@@ -254,12 +255,33 @@ ORM.prototype.close = function (cb) {
254255

255256
return this;
256257
};
257-
ORM.prototype.load = function (file, cb) {
258-
try {
259-
return require(Utilities.getRealPath(file))(this, cb);
260-
} catch (ex) {
261-
return cb(ex);
258+
ORM.prototype.load = function () {
259+
var files = _.flatten(Array.prototype.slice.apply(arguments));
260+
var cb = function () {};
261+
262+
if (typeof files[files.length - 1] == "function") {
263+
cb = files.pop();
262264
}
265+
266+
var loadNext = function () {
267+
if (files.length === 0) {
268+
return cb();
269+
}
270+
271+
var file = files.shift();
272+
273+
try {
274+
return require(Utilities.getRealPath(file, 4))(this, function (err) {
275+
if (err) return cb(err);
276+
277+
return loadNext();
278+
});
279+
} catch (ex) {
280+
return cb(ex);
281+
}
282+
}.bind(this);
283+
284+
return loadNext();
263285
};
264286
ORM.prototype.sync = function (cb) {
265287
var modelIds = Object.keys(this.models);

test/integration/db.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,56 @@ describe("db.load()", function () {
129129
});
130130
});
131131

132+
describe("db.load()", function () {
133+
var db = null;
134+
135+
before(function (done) {
136+
helper.connect(function (connection) {
137+
db = connection;
138+
139+
return done();
140+
});
141+
});
142+
143+
after(function () {
144+
return db.close();
145+
});
146+
147+
it("should be able to load more than one file", function (done) {
148+
db.load("../support/spec_load_second", "../support/spec_load_third", function () {
149+
db.models.should.have.property("person");
150+
db.models.should.have.property("pet");
151+
152+
return done();
153+
});
154+
});
155+
});
156+
157+
describe("db.load()", function () {
158+
var db = null;
159+
160+
before(function (done) {
161+
helper.connect(function (connection) {
162+
db = connection;
163+
164+
return done();
165+
});
166+
});
167+
168+
after(function () {
169+
return db.close();
170+
});
171+
172+
it("should be able to load more than one file passed as Array", function (done) {
173+
db.load([ "../support/spec_load_second", "../support/spec_load_third" ], function () {
174+
db.models.should.have.property("person");
175+
db.models.should.have.property("pet");
176+
177+
return done();
178+
});
179+
});
180+
});
181+
132182
describe("db.serial()", function () {
133183
var db = null;
134184

0 commit comments

Comments
 (0)