Skip to content

Commit b850f5e

Browse files
committed
Rename opts.id to opts.keys
1 parent d334464 commit b850f5e

File tree

8 files changed

+69
-110
lines changed

8 files changed

+69
-110
lines changed

lib/ChainFind.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function ChainFind(Model, opts) {
9090
return this;
9191
},
9292
remove: function (cb) {
93-
opts.driver.find([ opts.id ], opts.table, opts.conditions, {
93+
opts.driver.find([ opts.keys ], opts.table, opts.conditions, {
9494
limit : opts.limit,
9595
order : opts.order,
9696
merge : opts.merge,
@@ -107,16 +107,12 @@ function ChainFind(Model, opts) {
107107
var ids = [], conditions = {};
108108
var or;
109109

110-
if (!Array.isArray(opts.id)) {
111-
opts.id = [ opts.id ];
112-
}
113-
114110
conditions.or = [];
115111

116112
for (var i = 0; i < data.length; i++) {
117113
or = {};
118-
for (var j = 0; j < opts.id.length; j++) {
119-
or[opts.id[j]] = data[i][opts.id[j]];
114+
for (var j = 0; j < opts.keys.length; j++) {
115+
or[opts.keys[j]] = data[i][opts.keys[j]];
120116
}
121117
conditions.or.push(or);
122118
}
@@ -210,19 +206,19 @@ function ChainFind(Model, opts) {
210206
var idMap = {};
211207
var count = 0;
212208

213-
var ids = _.map(data, function (instance) {
214-
var id = instance[opts.id[0]];
209+
var keys = _.map(data, function (instance) {
210+
var key = instance[opts.keys[0]];
215211
// Create the association arrays
216212
for (var i = 0, association; association = opts.__eager[i]; i++) {
217213
instance[association.name] = [];
218214
}
219215

220-
idMap[id] = count++;
221-
return id;
216+
idMap[key] = count++;
217+
return key;
222218
});
223219

224220
_.map(opts.__eager, function (association) {
225-
opts.driver.eagerQuery(association, opts, ids, function (err, instances) {
221+
opts.driver.eagerQuery(association, opts, keys, function (err, instances) {
226222
for (var i = 0, instance; instance = instances[i]; i++) {
227223
// Perform a parent lookup with $p, and initialize it as an instance.
228224
data[idMap[instance.$p]][association.name].push(association.model(instance));

lib/Drivers/DML/_shared.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,22 @@ module.exports = {
99
var cb = arguments[2];
1010
}
1111
return this.execSimpleQuery(query, cb);
12+
},
13+
eagerQuery: function (association, opts, keys, cb) {
14+
var desiredKey = Object.keys(association.field);
15+
var assocKey = Object.keys(association.mergeAssocId);
16+
17+
var where = {};
18+
where[desiredKey] = keys;
19+
20+
var query = this.query.select()
21+
.from(association.model.table)
22+
.select(opts.only)
23+
.from(association.mergeTable, assocKey, opts.keys)
24+
.select(desiredKey).as("$p")
25+
.where(association.mergeTable, where)
26+
.build();
27+
28+
this.execSimpleQuery(query, cb);
1229
}
1330
};

lib/Drivers/DML/mysql.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,6 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) {
133133
this.execSimpleQuery(q, cb);
134134
};
135135

136-
Driver.prototype.eagerQuery = function (association, opts, ids, cb) {
137-
var desiredKey = Object.keys(association.field);
138-
var assocKey = Object.keys(association.mergeAssocId);
139-
140-
var where = {};
141-
where[desiredKey] = ids;
142-
143-
var query = this.query.select()
144-
.from(association.model.table)
145-
.select(opts.only)
146-
.from(association.mergeTable, assocKey, opts.id)
147-
.select(desiredKey).as("$p")
148-
.where(association.mergeTable, where)
149-
.build();
150-
151-
this.execSimpleQuery(query, cb);
152-
};
153-
154136
Driver.prototype.count = function (table, conditions, opts, cb) {
155137
var q = this.query.select()
156138
.from(table)

lib/Drivers/DML/postgres.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -175,24 +175,6 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) {
175175
this.execSimpleQuery(q, cb);
176176
};
177177

178-
Driver.prototype.eagerQuery = function (association, opts, ids, cb) {
179-
var desiredKey = Object.keys(association.field);
180-
var assocKey = Object.keys(association.mergeAssocId);
181-
182-
var where = {};
183-
where[desiredKey] = ids;
184-
185-
var query = this.query.select()
186-
.from(association.model.table)
187-
.select(opts.only)
188-
.from(association.mergeTable, assocKey, opts.id)
189-
.select(desiredKey).as("$p")
190-
.where(association.mergeTable, where)
191-
.build();
192-
193-
this.execSimpleQuery(query, cb);
194-
};
195-
196178
Driver.prototype.count = function (table, conditions, opts, cb) {
197179
var q = this.query.select().from(table).count(null, 'c');
198180

lib/Drivers/DML/sqlite.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,6 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) {
117117
this.db.all(q, cb);
118118
};
119119

120-
Driver.prototype.eagerQuery = function (association, opts, ids, cb) {
121-
var desiredKey = Object.keys(association.field);
122-
var assocKey = Object.keys(association.mergeAssocId);
123-
124-
var where = {};
125-
where[desiredKey] = ids;
126-
127-
var query = this.query.select()
128-
.from(association.model.table)
129-
.select(opts.only)
130-
.from(association.mergeTable, assocKey, opts.id)
131-
.select(desiredKey).as("$p")
132-
.where(association.mergeTable, where)
133-
.build();
134-
135-
this.execSimpleQuery(query, cb);
136-
};
137-
138120
Driver.prototype.count = function (table, conditions, opts, cb) {
139121
var q = this.query.select()
140122
.from(table)

lib/Instance.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function Instance(Model, opts) {
99
opts = opts || {};
1010
opts.data = opts.data || {};
1111
opts.extra = opts.extra || {};
12-
opts.id = opts.id || "id";
12+
opts.keys = opts.keys || "id";
1313
opts.changes = (opts.is_new ? Object.keys(opts.data) : []);
1414
opts.extrachanges = [];
1515
opts.associations = {};
@@ -194,14 +194,14 @@ function Instance(Model, opts) {
194194

195195
data = Utilities.transformPropertyNames(data, Model.allProperties);
196196

197-
opts.driver.insert(opts.table, data, opts.id, function (save_err, info) {
197+
opts.driver.insert(opts.table, data, opts.keys, function (save_err, info) {
198198
if (save_err) {
199199
return saveError(cb, save_err);
200200
}
201201

202202
opts.changes.length = 0;
203-
for (var i = 0; i < opts.id.length; i++) {
204-
opts.data[opts.id[i]] = info.hasOwnProperty(opts.id[i]) ? info[opts.id[i]] : data[opts.id[i]];
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]];
205205
}
206206
opts.is_new = false;
207207

@@ -248,8 +248,8 @@ function Instance(Model, opts) {
248248
for (var i = 0; i < opts.changes.length; i++) {
249249
changes[opts.changes[i]] = data[opts.changes[i]];
250250
}
251-
for (i = 0; i < opts.id.length; i++) {
252-
conditions[opts.id[i]] = data[opts.id[i]];
251+
for (i = 0; i < opts.keys.length; i++) {
252+
conditions[opts.keys[i]] = data[opts.keys[i]];
253253
}
254254
changes = Utilities.transformPropertyNames(changes, Model.allProperties);
255255

@@ -356,7 +356,7 @@ function Instance(Model, opts) {
356356

357357
for (i = 0; i < opts.extra_info.id.length; i++) {
358358
conditions[opts.extra_info.id_prop[i]] = opts.extra_info.id[i];
359-
conditions[opts.extra_info.assoc_prop[i]] = opts.data[opts.id[i]];
359+
conditions[opts.extra_info.assoc_prop[i]] = opts.data[opts.keys[i]];
360360
}
361361

362362
opts.driver.update(opts.extra_info.table, data, conditions, function (err) {
@@ -369,8 +369,8 @@ function Instance(Model, opts) {
369369
}
370370

371371
var conditions = {};
372-
for (var i = 0; i < opts.id.length; i++) {
373-
conditions[opts.id[i]] = opts.data[opts.id[i]];
372+
for (var i = 0; i < opts.keys.length; i++) {
373+
conditions[opts.keys[i]] = opts.data[opts.keys[i]];
374374
}
375375

376376
Hook.wait(instance, opts.hooks.beforeRemove, function (err) {
@@ -407,8 +407,8 @@ function Instance(Model, opts) {
407407
}
408408
}
409409

410-
for (var i = 0; i < opts.id.length; i++) {
411-
conditions[opts.id[i]] = opts.data[opts.id[i]];
410+
for (var i = 0; i < opts.keys.length; i++) {
411+
conditions[opts.keys[i]] = opts.data[opts.keys[i]];
412412
}
413413

414414
Hook.wait(instance, opts.hooks.beforeSave, function (err) {
@@ -642,8 +642,8 @@ function Instance(Model, opts) {
642642
enumerable: false
643643
});
644644

645-
for (i = 0; i < opts.id.length; i++) {
646-
if (!opts.data.hasOwnProperty(opts.id[i])) {
645+
for (i = 0; i < opts.keys.length; i++) {
646+
if (!opts.data.hasOwnProperty(opts.keys[i])) {
647647
opts.changes = Object.keys(opts.data);
648648
break;
649649
}

0 commit comments

Comments
 (0)