Skip to content

Commit 87e0926

Browse files
committed
can save() before connected, and will connect
1 parent fe8b004 commit 87e0926

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

index.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,29 @@ var rowBase = function() {
4444
}
4545

4646
function insert(obj) {
47-
var keys = fieldsForObject(obj);
48-
var statementString = insertStatement(obj, keys);
47+
return obj._meta.db.whenConnected(function () {
48+
var keys = fieldsForObject(obj);
49+
var statementString = insertStatement(obj, keys);
4950

50-
var params = _.pick(obj, keys);
51+
var params = _.pick(obj, keys);
5152

52-
if (obj._meta.db.driver.outputIdKeys && !obj._meta.compoundKey) {
53-
params = _.extend(params, obj._meta.db.driver.outputIdKeys(obj._meta.idType));
54-
}
53+
if (obj._meta.db.driver.outputIdKeys && !obj._meta.compoundKey) {
54+
params = _.extend(params, obj._meta.db.driver.outputIdKeys(obj._meta.idType));
55+
}
5556

56-
return obj._meta.db.query(statementString, params, {
57-
insert: !obj._meta.compoundKey,
58-
statement: obj._meta.compoundKey,
59-
id: obj._meta.id
60-
}).then(function (insertedId) {
61-
obj.setSaved();
57+
return obj._meta.db.query(statementString, params, {
58+
insert: !obj._meta.compoundKey,
59+
statement: obj._meta.compoundKey,
60+
id: obj._meta.id
61+
}).then(function (insertedId) {
62+
obj.setSaved();
6263

63-
if (!obj._meta.compoundKey) {
64-
obj[obj._meta.id] = insertedId;
65-
}
64+
if (!obj._meta.compoundKey) {
65+
obj[obj._meta.id] = insertedId;
66+
}
6667

67-
return obj.setNotChanged();
68+
return obj.setNotChanged();
69+
});
6870
});
6971
}
7072

@@ -330,7 +332,7 @@ exports.db = function(config) {
330332
query: function(query, params, options) {
331333
var self = this;
332334

333-
function runQuery() {
335+
return this.whenConnected(function () {
334336
var command = options && options.insert
335337
? self.driver.insert(query, params, options)
336338
: self.driver.query(query, params, options)
@@ -342,12 +344,14 @@ exports.db = function(config) {
342344
self.logError(query, params, e);
343345
throw e;
344346
});
345-
}
347+
});
348+
},
346349

350+
whenConnected: function(fn) {
347351
if (this.runningBeginSession) {
348-
return runQuery();
352+
return fn();
349353
} else {
350-
return this.connect().then(runQuery);
354+
return this.connect().then(fn);
351355
}
352356
},
353357

test/describeDatabase.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,27 @@ module.exports = function(name, config, database) {
112112
});
113113
});
114114

115+
it("can insert without connecting", function() {
116+
var db = sworm.db(config);
117+
var person = db.model({
118+
table: 'people'
119+
});
120+
var p = person({
121+
name: "bob"
122+
});
123+
return p.save().then(function() {
124+
expect(p.id).to.exist;
125+
126+
return db.query("select * from people").then(function(people) {
127+
return expect(database.clean(people)).to.eql([{
128+
id: p.id,
129+
name: "bob",
130+
address_id: null
131+
}]);
132+
});
133+
});
134+
});
135+
115136
it("can insert emtpy rows", function() {
116137
var personWeirdId = db.model({
117138
table: "people_weird_id",

0 commit comments

Comments
 (0)