Skip to content

Commit f0930ff

Browse files
committed
Fix Property.mapsTo for keys
1 parent b850f5e commit f0930ff

File tree

11 files changed

+307
-147
lines changed

11 files changed

+307
-147
lines changed

lib/Drivers/DML/mongodb.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ Driver.prototype.count = function (table, conditions, opts, cb) {
185185
});
186186
};
187187

188-
Driver.prototype.insert = function (table, data, id_prop, cb) {
188+
Driver.prototype.insert = function (table, data, keyProperties, cb) {
189189
convertToDB(data, this.config.timezone);
190190

191191
return this.db.collection(table).insert(
@@ -196,12 +196,14 @@ Driver.prototype.insert = function (table, data, id_prop, cb) {
196196
function (err, docs) {
197197
if (err) return cb(err);
198198

199-
var ids = {};
199+
var i, ids = {}, prop;
200200

201-
if (id_prop !== null && docs.length) {
202-
for (var k in docs[0]) {
203-
if (id_prop.indexOf(k) >= 0) {
204-
ids[k] = docs[0][k];
201+
if (keyProperties && docs.length) {
202+
for (i = 0; i < keyProperties.length; i++) {
203+
prop = keyProperties[i];
204+
205+
if (prop.mapsTo in docs[0]) {
206+
ids[prop.name] = docs[0][prop.mapsTo];
205207
}
206208
}
207209
convertFromDB(ids, this.config.timezone);

lib/Drivers/DML/mysql.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ Driver.prototype.count = function (table, conditions, opts, cb) {
160160
this.execSimpleQuery(q, cb);
161161
};
162162

163-
Driver.prototype.insert = function (table, data, id_prop, cb) {
163+
Driver.prototype.insert = function (table, data, keyProperties, cb) {
164164
var q = this.query.insert()
165165
.into(table)
166166
.set(data)
@@ -169,14 +169,15 @@ Driver.prototype.insert = function (table, data, id_prop, cb) {
169169
this.execSimpleQuery(q, function (err, info) {
170170
if (err) return cb(err);
171171

172-
var ids = {};
172+
var i, ids = {}, prop;
173173

174-
if (id_prop !== null) {
175-
if (id_prop.length == 1 && info.hasOwnProperty("insertId") && info.insertId !== 0 ) {
176-
ids[id_prop[0]] = info.insertId;
174+
if (keyProperties) {
175+
if (keyProperties.length == 1 && info.hasOwnProperty("insertId") && info.insertId !== 0 ) {
176+
ids[keyProperties[0].name] = info.insertId;
177177
} else {
178-
for (var i = 0; i < id_prop.length; i++) {
179-
ids[id_prop[i]] = data[id_prop[i]];
178+
for(i = 0; i < keyProperties.length; i++) {
179+
prop = keyProperties[i];
180+
ids[prop.name] = data[prop.mapsTo];
180181
}
181182
}
182183
}

lib/Drivers/DML/postgres.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,21 @@ Driver.prototype.count = function (table, conditions, opts, cb) {
200200
this.execSimpleQuery(q, cb);
201201
};
202202

203-
Driver.prototype.insert = function (table, data, id_prop, cb) {
203+
Driver.prototype.insert = function (table, data, keyProperties, cb) {
204204
var q = this.query.insert().into(table).set(data).build();
205205

206206
this.execSimpleQuery(q + " RETURNING *", function (err, results) {
207207
if (err) {
208208
return cb(err);
209209
}
210210

211-
var ids = {};
211+
var i, ids = {}, prop;
212212

213-
if (id_prop !== null) {
214-
for (var i = 0; i < id_prop.length; i++) {
215-
ids[id_prop[i]] = results[0][id_prop[i]] || null;
213+
if (keyProperties) {
214+
for (i = 0; i < keyProperties.length; i++) {
215+
prop = keyProperties[i];
216+
217+
ids[prop.name] = results[0][prop.mapsTo] || null;
216218
}
217219
}
218220

lib/Drivers/DML/redshift.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function Driver(config, connection, opts) {
99

1010
util.inherits(Driver, postgres.Driver);
1111

12-
Driver.prototype.insert = function (table, data, id_prop, cb) {
12+
Driver.prototype.insert = function (table, data, keyProperties, cb) {
1313
var q = this.query.insert()
1414
.into(table)
1515
.set(data)
@@ -19,28 +19,25 @@ Driver.prototype.insert = function (table, data, id_prop, cb) {
1919
require("../../Debug").sql('postgres', q);
2020
}
2121
this.execQuery(q, function (err, result) {
22-
if (err) {
23-
return cb(err);
24-
}
22+
if (err) return cb(err);
23+
if (!keyProperties) return cb(null);
2524

26-
if (id_prop === null) {
27-
return cb(null);
28-
}
25+
var i, ids = {}, prop;
2926

30-
if (id_prop.length == 1) {
31-
return this.execQuery("SELECT LASTVAL() AS id", function (err, results) {
32-
return cb(null, {
33-
id: !err && results[0].id || null
34-
});
35-
});
36-
}
27+
if (keyNames.length == 1) {
28+
this.execQuery("SELECT LASTVAL() AS id", function (err, results) {
29+
if (err) return cb(err);
3730

38-
var ids = {};
31+
ids[keyProperties[0].name] = results[0].id || null;
32+
return cb(null, ids);
33+
});
34+
} else {
35+
for(i = 0; i < keyProperties.length; i++) {
36+
prop = keyProperties[i];
37+
ids[prop.name] = data[prop.mapsTo] || null;
38+
}
3939

40-
for (var i = 0; i < id_prop.length; i++) {
41-
ids[id_prop[i]] = data[id_prop[i]] || null;
40+
return cb(null, ids);
4241
}
43-
44-
return cb(null, ids);
4542
}.bind(this));
4643
};

lib/Drivers/DML/sqlite.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Driver.prototype.count = function (table, conditions, opts, cb) {
147147
this.db.all(q, cb);
148148
};
149149

150-
Driver.prototype.insert = function (table, data, id_prop, cb) {
150+
Driver.prototype.insert = function (table, data, keyProperties, cb) {
151151
var q = this.query.insert()
152152
.into(table)
153153
.set(data)
@@ -156,18 +156,29 @@ Driver.prototype.insert = function (table, data, id_prop, cb) {
156156
if (this.opts.debug) {
157157
require("../../Debug").sql('sqlite', q);
158158
}
159+
160+
159161
this.db.all(q, function (err, info) {
160-
if (err) {
161-
return cb(err);
162-
}
163-
this.db.get("SELECT last_insert_rowid() AS last_row_id", function (err, row) {
164-
if (err) {
165-
return cb(err);
166-
}
167-
return cb(null, {
168-
id: row.last_row_id
162+
if (err) return cb(err);
163+
if (!keyProperties) return cb(null);
164+
165+
var i, ids = {}, prop;
166+
167+
if (keyProperties.length == 1 && keyProperties[0].type == 'serial') {
168+
this.db.get("SELECT last_insert_rowid() AS last_row_id", function (err, row) {
169+
if (err) return cb(err);
170+
171+
ids[keyProperties[0].name] = row.last_row_id;
172+
173+
return cb(null, ids);
169174
});
170-
});
175+
} else {
176+
for (i = 0; i < keyProperties.length; i++) {
177+
prop = keyProperties[i];
178+
ids[prop.name] = data[prop.mapsTo] || null;
179+
}
180+
return cb(null, ids);
181+
}
171182
}.bind(this));
172183
};
173184

lib/Instance.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function Instance(Model, opts) {
1313
opts.changes = (opts.is_new ? Object.keys(opts.data) : []);
1414
opts.extrachanges = [];
1515
opts.associations = {};
16+
opts.originalKeyValues = {};
1617

1718
var instance_saving = false;
1819
var events = {};
@@ -27,6 +28,14 @@ function Instance(Model, opts) {
2728
cb.apply(instance, args);
2829
});
2930
};
31+
var rememberKeys = function () {
32+
var i, prop;
33+
34+
for(i = 0; i < opts.keyProperties.length; i++) {
35+
prop = opts.keyProperties[i];
36+
opts.originalKeyValues[prop.name] = opts.data[prop.name];
37+
}
38+
};
3039
var handleValidations = function (cb) {
3140
var pending = [], errors = [], required;
3241

@@ -185,6 +194,8 @@ function Instance(Model, opts) {
185194
return nextHook();
186195
};
187196
var saveNew = function (saveOptions, data, cb) {
197+
var i, prop;
198+
188199
var finish = function (err) {
189200
runAfterSaveActions(function () {
190201
if (err) return cb(err);
@@ -194,16 +205,19 @@ function Instance(Model, opts) {
194205

195206
data = Utilities.transformPropertyNames(data, Model.allProperties);
196207

197-
opts.driver.insert(opts.table, data, opts.keys, function (save_err, info) {
208+
opts.driver.insert(opts.table, data, opts.keyProperties, function (save_err, info) {
198209
if (save_err) {
199210
return saveError(cb, save_err);
200211
}
201212

202213
opts.changes.length = 0;
203-
for (var i = 0; i < opts.keys.length; i++) {
204-
opts.data[opts.keys[i]] = info.hasOwnProperty(opts.keys[i]) ? info[opts.keys[i]] : data[opts.keys[i]];
214+
215+
for (i = 0; i < opts.keyProperties.length; i++) {
216+
prop = opts.keyProperties[i];
217+
opts.data[prop.name] = info.hasOwnProperty(prop.name) ? info[prop.name] : data[prop.name];
205218
}
206219
opts.is_new = false;
220+
rememberKeys();
207221

208222
if (saveOptions.saveAssociations === false) {
209223
return finish();
@@ -213,7 +227,7 @@ function Instance(Model, opts) {
213227
});
214228
};
215229
var savePersisted = function (saveOptions, data, cb) {
216-
var changes = {}, conditions = {};
230+
var changes = {}, conditions = {}, i, prop;
217231

218232
var next = function (saved) {
219233
var finish = function () {
@@ -245,11 +259,12 @@ function Instance(Model, opts) {
245259
if (opts.changes.length === 0) {
246260
next(false);
247261
} else {
248-
for (var i = 0; i < opts.changes.length; i++) {
262+
for (i = 0; i < opts.changes.length; i++) {
249263
changes[opts.changes[i]] = data[opts.changes[i]];
250264
}
251-
for (i = 0; i < opts.keys.length; i++) {
252-
conditions[opts.keys[i]] = data[opts.keys[i]];
265+
for (i = 0; i < opts.keyProperties.length; i++) {
266+
prop = opts.keyProperties[i];
267+
conditions[prop.mapsTo] = opts.originalKeyValues[prop.name];
253268
}
254269
changes = Utilities.transformPropertyNames(changes, Model.allProperties);
255270

@@ -258,6 +273,7 @@ function Instance(Model, opts) {
258273
return saveError(cb, err);
259274
}
260275
opts.changes.length = 0;
276+
rememberKeys();
261277

262278
next(true);
263279
});
@@ -464,8 +480,12 @@ function Instance(Model, opts) {
464480
return opts.data[key];
465481
},
466482
set: function (val) {
467-
if (Model.allProperties[key].key === true && opts.data[key] != null) {
468-
return;
483+
if (prop.key === true) {
484+
if (prop.type == 'serial' && opts.data[key] != null) {
485+
return;
486+
} else {
487+
opts.originalKeyValues[prop.name] = opts.data[prop.name];
488+
}
469489
}
470490

471491
if (!setInstanceProperty(key, val)) {
@@ -642,12 +662,15 @@ function Instance(Model, opts) {
642662
enumerable: false
643663
});
644664

645-
for (i = 0; i < opts.keys.length; i++) {
646-
if (!opts.data.hasOwnProperty(opts.keys[i])) {
665+
for (i = 0; i < opts.keyProperties.length; i++) {
666+
var prop = opts.keyProperties[i];
667+
668+
if (!(prop.name in opts.data)) {
647669
opts.changes = Object.keys(opts.data);
648670
break;
649671
}
650672
}
673+
rememberKeys();
651674

652675
opts.setupAssociations(instance);
653676

lib/LazyLoad.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function addLazyLoadProperty(name, Instance, Model, property) {
1414
var conditions = {};
1515
conditions[Model.id] = Instance[Model.id];
1616

17-
Model.find(conditions, { cache: false }).only(property).first(function (err, item) {
17+
Model.find(conditions, { cache: false }).only(Model.id.concat(property)).first(function (err, item) {
1818
return cb(err, item ? item[property] : null);
1919
});
2020

@@ -27,7 +27,7 @@ function addLazyLoadProperty(name, Instance, Model, property) {
2727
var conditions = {};
2828
conditions[Model.id] = Instance[Model.id];
2929

30-
Model.find(conditions, { cache: false }).only(property).first(function (err, item) {
30+
Model.find(conditions, { cache: false }).only(Model.id.concat(property)).first(function (err, item) {
3131
if (err) {
3232
return cb(err);
3333
}
@@ -36,7 +36,6 @@ function addLazyLoadProperty(name, Instance, Model, property) {
3636
}
3737

3838
item[property] = null;
39-
item[Model.id] = Instance[Model.id];
4039

4140
return item.save(cb);
4241
});

0 commit comments

Comments
 (0)