Skip to content

Commit 5005da4

Browse files
committed
Refactoring to remove keys field
Appears to pass tests, removes all instances of the `keys` field on models and instances, and replaces it with the ability to pass an array to `id` to represent multiple primary keys.
1 parent 6a74796 commit 5005da4

File tree

13 files changed

+151
-168
lines changed

13 files changed

+151
-168
lines changed

lib/Associations/Extend.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ exports.prepare = function (db, Model, associations, association_properties, mod
2121
};
2222

2323
association.model = db.define(Model.table + "_" + name, properties, {
24-
id : null,
25-
keys : Object.keys(association.field),
24+
id : Object.keys(association.field),
2625
extension : true
2726
});
2827
association.model.hasOne(Model.table, Model, { extension: true, field: association.field });
@@ -64,7 +63,7 @@ function extendInstance(Model, Instance, Driver, association, opts) {
6463
if (!Instance[Model.id]) {
6564
cb(ErrorCodes.generateError(ErrorCodes.NOT_DEFINED, "Instance not saved, cannot get extension"));
6665
} else {
67-
association.model.get(util.values(Instance, Model.keys), function (err, extension) {
66+
association.model.get(util.values(Instance, Model.id), function (err, extension) {
6867
return cb(err, !err && extension ? true : false);
6968
});
7069
}
@@ -77,7 +76,7 @@ function extendInstance(Model, Instance, Driver, association, opts) {
7776
if (!Instance[Model.id]) {
7877
cb(ErrorCodes.generateError(ErrorCodes.NOT_DEFINED, "Instance not saved, cannot get extension"));
7978
} else {
80-
association.model.get(util.values(Instance, Model.keys), cb);
79+
association.model.get(util.values(Instance, Model.id), cb);
8180
}
8281
return this;
8382
},
@@ -96,8 +95,8 @@ function extendInstance(Model, Instance, Driver, association, opts) {
9695
}
9796

9897
var fields = Object.keys(association.field);
99-
for (i = 0; i < Model.keys.length; i++) {
100-
Extension[fields[i]] = Instance[Model.keys[i]];
98+
for (i = 0; i < Model.id.length; i++) {
99+
Extension[fields[i]] = Instance[Model.id[i]];
101100
}
102101

103102
Extension.save(cb);
@@ -114,8 +113,8 @@ function extendInstance(Model, Instance, Driver, association, opts) {
114113
} else {
115114
var conditions = {};
116115
var fields = Object.keys(association.field);
117-
for (i = 0; i < Model.keys.length; i++) {
118-
conditions[fields[i]] = Instance[Model.keys[i]];
116+
for (i = 0; i < Model.id.length; i++) {
117+
conditions[fields[i]] = Instance[Model.id[i]];
119118
}
120119

121120
association.model.find(conditions, function (err, extensions) {

lib/Associations/Many.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var _ = require("lodash");
66
var util = require("../Utilities");
77

88
exports.prepare = function (Model, associations) {
9-
if (Model.keys.length > 1) {
9+
if (Model.id.length > 1) {
1010
Model.hasMany = function () {
1111
throw ErrorCodes.generateError(ErrorCodes.NO_SUPPORT, "Model.hasMany() does not support multiple keys models");
1212
};
@@ -121,13 +121,13 @@ function extendInstance(Model, Instance, Driver, association, opts) {
121121

122122
options.__merge = {
123123
from: { table: association.mergeTable, field: Object.keys(association.mergeAssocId) },
124-
to: { table: association.model.table, field: association.model.keys },
124+
to: { table: association.model.table, field: association.model.id },
125125
where: [ association.mergeTable, {} ]
126126
};
127127
options.extra = association.props;
128128
options.extra_info = {
129129
table: association.mergeTable,
130-
id: util.values(Instance, Model.keys),
130+
id: util.values(Instance, Model.id),
131131
id_prop: Object.keys(association.mergeId),
132132
assoc_prop: Object.keys(association.mergeAssocId)
133133
};
@@ -196,13 +196,13 @@ function extendInstance(Model, Instance, Driver, association, opts) {
196196

197197
options.__merge = {
198198
from : { table: association.mergeTable, field: Object.keys(association.mergeAssocId) },
199-
to : { table: association.model.table, field: association.model.keys },
199+
to : { table: association.model.table, field: association.model.id },
200200
where : [ association.mergeTable, {} ]
201201
};
202202
options.extra = association.props;
203203
options.extra_info = {
204204
table: association.mergeTable,
205-
id: util.values(Instance, Model.keys),
205+
id: util.values(Instance, Model.id),
206206
id_prop: Object.keys(association.mergeId),
207207
assoc_prop: Object.keys(association.mergeAssocId)
208208
};

lib/Associations/One.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ exports.prepare = function (Model, associations, association_properties, model_f
8585
}
8686

8787
options.__merge = {
88-
from : { table: association.model.table, field: association.model.keys },
88+
from : { table: association.model.table, field: association.model.id },
8989
to : { table: Model.table, field: Object.keys(association.field) },
9090
where : [ association.model.table, conditions ],
9191
table : Model.table
@@ -155,14 +155,14 @@ function extendInstance(Model, Instance, Driver, association, opts) {
155155
}
156156

157157
if (association.reversed) {
158-
if (util.hasValues(Instance, Model.keys)) {
158+
if (util.hasValues(Instance, Model.id)) {
159159
association.model.find(util.getConditions(Model, Object.keys(association.field), Instance), opts, cb);
160160
} else {
161161
cb(null);
162162
}
163163
} else {
164164
if (Instance.isShell()) {
165-
Model.get(util.values(Instance, Model.keys), function (err, instance) {
165+
Model.get(util.values(Instance, Model.id), function (err, instance) {
166166
if (err || !util.hasValues(instance, Object.keys(association.field))) {
167167
return cb(null);
168168
}

lib/ChainFind.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ function addChainMethod(chain, association, opts) {
159159
var conditions = {};
160160

161161
var assocIds = Object.keys(association.mergeAssocId);
162-
var ids = association.model.keys;
162+
var ids = association.model.id;
163163
function mergeConditions(source) {
164164
for (i = 0; i < assocIds.length; i++) {
165165
if (typeof conditions[assocIds[i]] == 'undefined')
@@ -181,7 +181,7 @@ function addChainMethod(chain, association, opts) {
181181

182182
opts.exists.push({
183183
table : association.mergeTable,
184-
link : [ Object.keys(association.mergeId), association.model.keys ],
184+
link : [ Object.keys(association.mergeId), association.model.id ],
185185
conditions : conditions
186186
});
187187

lib/Drivers/DDL/mysql.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ exports.sync = function (driver, opts, cb) {
3636
var queries = [];
3737
var definitions = [];
3838
var k, i, pending;
39-
var primary_keys = opts.keys.map(function (k) { return driver.query.escapeId(k); });
39+
var primary_keys = opts.id.map(function (k) { return driver.query.escapeId(k); });
4040
var keys = [];
4141

42-
for (i = 0; i < opts.keys.length; i++) {
43-
if (opts.properties.hasOwnProperty(opts.keys[i])) continue;
42+
for (i = 0; i < opts.id.length; i++) {
43+
if (opts.properties.hasOwnProperty(opts.id[i])) continue;
4444

45-
keys.push(driver.query.escapeId(opts.keys[i]));
45+
keys.push(driver.query.escapeId(opts.id[i]));
4646
}
4747

4848
for (i = 0; i < keys.length; i++) {
49-
definitions.push(keys[i] + " INT(11) UNSIGNED NOT NULL");
49+
definitions.push(keys[i] + " INT(10) UNSIGNED NOT NULL");
5050
}
51-
if (opts.keys.length == 1 && !opts.extension) {
51+
if (opts.id.length == 1 && !opts.extension) {
5252
definitions[definitions.length - 1] += " AUTO_INCREMENT";
5353
}
5454

lib/Drivers/DDL/postgres.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ exports.sync = function (driver, opts, cb) {
2525
var typequeries = [];
2626
var definitions = [];
2727
var k, i, pending;
28-
var primary_keys = opts.keys.map(function (k) { return driver.query.escapeId(k); });
28+
var primary_keys = opts.id.map(function (k) { return driver.query.escapeId(k); });
2929
var keys = [];
30+
for (i = 0; i < opts.id.length; i++) {
31+
if (opts.properties.hasOwnProperty(opts.id[i])) continue;
3032

31-
for (i = 0; i < opts.keys.length; i++) {
32-
if (opts.properties.hasOwnProperty(opts.keys[i])) continue;
33-
34-
keys.push(driver.query.escapeId(opts.keys[i]));
33+
keys.push(driver.query.escapeId(opts.id[i]));
3534
}
3635

3736
for (i = 0; i < keys.length; i++) {
3837
definitions.push(keys[i] + " INTEGER NOT NULL");
3938
}
40-
if (opts.keys.length == 1 && !opts.extension) {
39+
40+
if (opts.id.length == 1 && !opts.extension) {
4141
definitions[definitions.length - 1] = keys[0] + " SERIAL";
4242
}
4343

lib/Drivers/DDL/sqlite.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,20 @@ exports.sync = function (driver, opts, cb) {
2323
var queries = [];
2424
var definitions = [];
2525
var k, i, pending;
26-
var primary_keys = opts.keys.map(function (k) { return driver.query.escapeId(k); });
26+
var primary_keys = opts.id.map(function (k) { return driver.query.escapeId(k); });
2727
var keys = [];
2828

29-
for (i = 0; i < opts.keys.length; i++) {
30-
if (opts.properties.hasOwnProperty(opts.keys[i])) continue;
29+
for (i = 0; i < opts.id.length; i++) {
30+
if (opts.properties.hasOwnProperty(opts.id[i])) continue;
3131

32-
keys.push(driver.query.escapeId(opts.keys[i]));
32+
keys.push(driver.query.escapeId(opts.id[i]));
3333
}
3434

3535
for (i = 0; i < keys.length; i++) {
3636
definitions.push(keys[i] + " INTEGER UNSIGNED NOT NULL");
3737
}
38-
if (opts.keys.length == 1 && !opts.extension) {
38+
39+
if (opts.id.length == 1 && !opts.extension) {
3940
definitions[definitions.length - 1] = keys[0] + " INTEGER PRIMARY KEY AUTOINCREMENT";
4041
}
4142

lib/Instance.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,14 @@ function Instance(Model, opts) {
167167
var saveNew = function (cb, saveOptions, data) {
168168
var next = afterSave.bind(this, cb, true);
169169

170-
opts.driver.insert(opts.table, data, opts.keys, function (save_err, info) {
170+
opts.driver.insert(opts.table, data, opts.id, function (save_err, info) {
171171
if (save_err) {
172172
return saveError(cb, save_err);
173173
}
174174

175175
opts.changes.length = 0;
176-
for (var i = 0; i < opts.keys.length; i++) {
177-
opts.data[opts.keys[i]] = info.hasOwnProperty(opts.keys[i]) ? info[opts.keys[i]] : data[opts.keys[i]];
176+
for (var i = 0; i < opts.id.length; i++) {
177+
opts.data[opts.id[i]] = info.hasOwnProperty(opts.id[i]) ? info[opts.id[i]] : data[opts.id[i]];
178178
}
179179
opts.is_new = false;
180180

@@ -192,8 +192,8 @@ function Instance(Model, opts) {
192192
for (var i = 0; i < opts.changes.length; i++) {
193193
changes[opts.changes[i]] = data[opts.changes[i]];
194194
}
195-
for (i = 0; i < opts.keys.length; i++) {
196-
conditions[opts.keys[i]] = data[opts.keys[i]];
195+
for (i = 0; i < opts.id.length; i++) {
196+
conditions[opts.id[i]] = data[opts.id[i]];
197197
}
198198

199199
opts.driver.update(opts.table, changes, conditions, function (save_err) {
@@ -298,7 +298,7 @@ function Instance(Model, opts) {
298298

299299
for (i = 0; i < opts.extra_info.id; i++) {
300300
conditions[opts.extra_info.id_prop[i]] = opts.extra_info.id[i];
301-
conditions[opts.extra_info.assoc_prop[i]] = opts.data[opts.keys[i]];
301+
conditions[opts.extra_info.assoc_prop[i]] = opts.data[opts.id[i]];
302302
}
303303

304304
opts.driver.update(opts.extra_info.table, data, conditions, function (err) {
@@ -311,8 +311,8 @@ function Instance(Model, opts) {
311311
}
312312

313313
var conditions = {};
314-
for (var i = 0; i < opts.keys.length; i++) {
315-
conditions[opts.keys[i]] = opts.data[opts.keys[i]];
314+
for (var i = 0; i < opts.id.length; i++) {
315+
conditions[opts.id[i]] = opts.data[opts.id[i]];
316316
}
317317

318318
Hook.wait(instance, opts.hooks.beforeRemove, function (err) {
@@ -350,8 +350,8 @@ function Instance(Model, opts) {
350350
}
351351
}
352352

353-
for (var i = 0; i < opts.keys.length; i++) {
354-
conditions[opts.keys[i]] = opts.data[opts.keys[i]];
353+
for (var i = 0; i < opts.id.length; i++) {
354+
conditions[opts.id[i]] = opts.data[opts.id[i]];
355355
}
356356

357357
Hook.wait(instance, opts.hooks.beforeSave, function (err) {
@@ -378,7 +378,7 @@ function Instance(Model, opts) {
378378
return opts.data[key];
379379
},
380380
set: function (val) {
381-
if (opts.keys.indexOf(key) >= 0 && opts.data.hasOwnProperty(key)) {
381+
if (opts.id.indexOf(key) >= 0 && opts.data.hasOwnProperty(key)) {
382382
throw new Error("Cannot change ID");
383383
}
384384

@@ -415,21 +415,21 @@ function Instance(Model, opts) {
415415
});
416416
};
417417

418-
for (var i = 0; i < opts.keys.length; i++) {
419-
if (!opts.data.hasOwnProperty(opts.keys[i])) {
420-
addInstanceProperty(opts.keys[i]);
418+
for (var i = 0; i < opts.id.length; i++) {
419+
if (!opts.data.hasOwnProperty(opts.id[i])) {
420+
addInstanceProperty(opts.id[i]);
421421
}
422422
}
423423

424424
for (var k in Model.properties) {
425-
if (Model.properties.hasOwnProperty(k) && !opts.data.hasOwnProperty(k) && opts.keys.indexOf(k) == -1) {
425+
if (Model.properties.hasOwnProperty(k) && !opts.data.hasOwnProperty(k) && opts.id.indexOf(k) == -1) {
426426
opts.data[k] = null;
427427
}
428428
}
429429

430430
for (k in opts.data) {
431431
if (!opts.data.hasOwnProperty(k)) continue;
432-
if (!Model.properties.hasOwnProperty(k) && opts.keys.indexOf(k) == -1 && opts.association_properties.indexOf(k) == -1) {
432+
if (!Model.properties.hasOwnProperty(k) && opts.id.indexOf(k) == -1 && opts.association_properties.indexOf(k) == -1) {
433433
if (!opts.extra.hasOwnProperty(k)) continue;
434434

435435
if (opts.driver.valueToProperty) {
@@ -548,8 +548,8 @@ function Instance(Model, opts) {
548548
enumerable: false
549549
});
550550

551-
for (var i = 0; i < opts.keys.length; i++) {
552-
if (!opts.data.hasOwnProperty(opts.keys[i])) {
551+
for (var i = 0; i < opts.id.length; i++) {
552+
if (!opts.data.hasOwnProperty(opts.id[i])) {
553553
opts.changes = Object.keys(opts.data);
554554
break;
555555
}
@@ -572,7 +572,7 @@ function Instance(Model, opts) {
572572
} else {
573573
var instanceInit = {};
574574
var usedChance = false;
575-
for (k in opts.one_associations[i].keys) {
575+
for (k in opts.one_associations[i].id) {
576576
if (!data.hasOwnProperty(k) && !usedChance) {
577577
instanceInit[k] = opts.data[asc.name];
578578
usedChance = true;

0 commit comments

Comments
 (0)