Skip to content

Commit

Permalink
Fix Property.mapsTo for keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dxg committed Jun 2, 2014
1 parent b850f5e commit f0930ff
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 147 deletions.
14 changes: 8 additions & 6 deletions lib/Drivers/DML/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Driver.prototype.count = function (table, conditions, opts, cb) {
});
};

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

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

var ids = {};
var i, ids = {}, prop;

if (id_prop !== null && docs.length) {
for (var k in docs[0]) {
if (id_prop.indexOf(k) >= 0) {
ids[k] = docs[0][k];
if (keyProperties && docs.length) {
for (i = 0; i < keyProperties.length; i++) {
prop = keyProperties[i];

if (prop.mapsTo in docs[0]) {
ids[prop.name] = docs[0][prop.mapsTo];
}
}
convertFromDB(ids, this.config.timezone);
Expand Down
15 changes: 8 additions & 7 deletions lib/Drivers/DML/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ Driver.prototype.count = function (table, conditions, opts, cb) {
this.execSimpleQuery(q, cb);
};

Driver.prototype.insert = function (table, data, id_prop, cb) {
Driver.prototype.insert = function (table, data, keyProperties, cb) {
var q = this.query.insert()
.into(table)
.set(data)
Expand All @@ -169,14 +169,15 @@ Driver.prototype.insert = function (table, data, id_prop, cb) {
this.execSimpleQuery(q, function (err, info) {
if (err) return cb(err);

var ids = {};
var i, ids = {}, prop;

if (id_prop !== null) {
if (id_prop.length == 1 && info.hasOwnProperty("insertId") && info.insertId !== 0 ) {
ids[id_prop[0]] = info.insertId;
if (keyProperties) {
if (keyProperties.length == 1 && info.hasOwnProperty("insertId") && info.insertId !== 0 ) {
ids[keyProperties[0].name] = info.insertId;
} else {
for (var i = 0; i < id_prop.length; i++) {
ids[id_prop[i]] = data[id_prop[i]];
for(i = 0; i < keyProperties.length; i++) {
prop = keyProperties[i];
ids[prop.name] = data[prop.mapsTo];
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions lib/Drivers/DML/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,21 @@ Driver.prototype.count = function (table, conditions, opts, cb) {
this.execSimpleQuery(q, cb);
};

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

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

var ids = {};
var i, ids = {}, prop;

if (id_prop !== null) {
for (var i = 0; i < id_prop.length; i++) {
ids[id_prop[i]] = results[0][id_prop[i]] || null;
if (keyProperties) {
for (i = 0; i < keyProperties.length; i++) {
prop = keyProperties[i];

ids[prop.name] = results[0][prop.mapsTo] || null;
}
}

Expand Down
35 changes: 16 additions & 19 deletions lib/Drivers/DML/redshift.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function Driver(config, connection, opts) {

util.inherits(Driver, postgres.Driver);

Driver.prototype.insert = function (table, data, id_prop, cb) {
Driver.prototype.insert = function (table, data, keyProperties, cb) {
var q = this.query.insert()
.into(table)
.set(data)
Expand All @@ -19,28 +19,25 @@ Driver.prototype.insert = function (table, data, id_prop, cb) {
require("../../Debug").sql('postgres', q);
}
this.execQuery(q, function (err, result) {
if (err) {
return cb(err);
}
if (err) return cb(err);
if (!keyProperties) return cb(null);

if (id_prop === null) {
return cb(null);
}
var i, ids = {}, prop;

if (id_prop.length == 1) {
return this.execQuery("SELECT LASTVAL() AS id", function (err, results) {
return cb(null, {
id: !err && results[0].id || null
});
});
}
if (keyNames.length == 1) {
this.execQuery("SELECT LASTVAL() AS id", function (err, results) {
if (err) return cb(err);

var ids = {};
ids[keyProperties[0].name] = results[0].id || null;
return cb(null, ids);
});
} else {
for(i = 0; i < keyProperties.length; i++) {
prop = keyProperties[i];
ids[prop.name] = data[prop.mapsTo] || null;
}

for (var i = 0; i < id_prop.length; i++) {
ids[id_prop[i]] = data[id_prop[i]] || null;
return cb(null, ids);
}

return cb(null, ids);
}.bind(this));
};
33 changes: 22 additions & 11 deletions lib/Drivers/DML/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Driver.prototype.count = function (table, conditions, opts, cb) {
this.db.all(q, cb);
};

Driver.prototype.insert = function (table, data, id_prop, cb) {
Driver.prototype.insert = function (table, data, keyProperties, cb) {
var q = this.query.insert()
.into(table)
.set(data)
Expand All @@ -156,18 +156,29 @@ Driver.prototype.insert = function (table, data, id_prop, cb) {
if (this.opts.debug) {
require("../../Debug").sql('sqlite', q);
}


this.db.all(q, function (err, info) {
if (err) {
return cb(err);
}
this.db.get("SELECT last_insert_rowid() AS last_row_id", function (err, row) {
if (err) {
return cb(err);
}
return cb(null, {
id: row.last_row_id
if (err) return cb(err);
if (!keyProperties) return cb(null);

var i, ids = {}, prop;

if (keyProperties.length == 1 && keyProperties[0].type == 'serial') {
this.db.get("SELECT last_insert_rowid() AS last_row_id", function (err, row) {
if (err) return cb(err);

ids[keyProperties[0].name] = row.last_row_id;

return cb(null, ids);
});
});
} else {
for (i = 0; i < keyProperties.length; i++) {
prop = keyProperties[i];
ids[prop.name] = data[prop.mapsTo] || null;
}
return cb(null, ids);
}
}.bind(this));
};

Expand Down
45 changes: 34 additions & 11 deletions lib/Instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function Instance(Model, opts) {
opts.changes = (opts.is_new ? Object.keys(opts.data) : []);
opts.extrachanges = [];
opts.associations = {};
opts.originalKeyValues = {};

var instance_saving = false;
var events = {};
Expand All @@ -27,6 +28,14 @@ function Instance(Model, opts) {
cb.apply(instance, args);
});
};
var rememberKeys = function () {
var i, prop;

for(i = 0; i < opts.keyProperties.length; i++) {
prop = opts.keyProperties[i];
opts.originalKeyValues[prop.name] = opts.data[prop.name];
}
};
var handleValidations = function (cb) {
var pending = [], errors = [], required;

Expand Down Expand Up @@ -185,6 +194,8 @@ function Instance(Model, opts) {
return nextHook();
};
var saveNew = function (saveOptions, data, cb) {
var i, prop;

var finish = function (err) {
runAfterSaveActions(function () {
if (err) return cb(err);
Expand All @@ -194,16 +205,19 @@ function Instance(Model, opts) {

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

opts.driver.insert(opts.table, data, opts.keys, function (save_err, info) {
opts.driver.insert(opts.table, data, opts.keyProperties, function (save_err, info) {
if (save_err) {
return saveError(cb, save_err);
}

opts.changes.length = 0;
for (var i = 0; i < opts.keys.length; i++) {
opts.data[opts.keys[i]] = info.hasOwnProperty(opts.keys[i]) ? info[opts.keys[i]] : data[opts.keys[i]];

for (i = 0; i < opts.keyProperties.length; i++) {
prop = opts.keyProperties[i];
opts.data[prop.name] = info.hasOwnProperty(prop.name) ? info[prop.name] : data[prop.name];
}
opts.is_new = false;
rememberKeys();

if (saveOptions.saveAssociations === false) {
return finish();
Expand All @@ -213,7 +227,7 @@ function Instance(Model, opts) {
});
};
var savePersisted = function (saveOptions, data, cb) {
var changes = {}, conditions = {};
var changes = {}, conditions = {}, i, prop;

var next = function (saved) {
var finish = function () {
Expand Down Expand Up @@ -245,11 +259,12 @@ function Instance(Model, opts) {
if (opts.changes.length === 0) {
next(false);
} else {
for (var i = 0; i < opts.changes.length; i++) {
for (i = 0; i < opts.changes.length; i++) {
changes[opts.changes[i]] = data[opts.changes[i]];
}
for (i = 0; i < opts.keys.length; i++) {
conditions[opts.keys[i]] = data[opts.keys[i]];
for (i = 0; i < opts.keyProperties.length; i++) {
prop = opts.keyProperties[i];
conditions[prop.mapsTo] = opts.originalKeyValues[prop.name];
}
changes = Utilities.transformPropertyNames(changes, Model.allProperties);

Expand All @@ -258,6 +273,7 @@ function Instance(Model, opts) {
return saveError(cb, err);
}
opts.changes.length = 0;
rememberKeys();

next(true);
});
Expand Down Expand Up @@ -464,8 +480,12 @@ function Instance(Model, opts) {
return opts.data[key];
},
set: function (val) {
if (Model.allProperties[key].key === true && opts.data[key] != null) {
return;
if (prop.key === true) {
if (prop.type == 'serial' && opts.data[key] != null) {
return;
} else {
opts.originalKeyValues[prop.name] = opts.data[prop.name];
}
}

if (!setInstanceProperty(key, val)) {
Expand Down Expand Up @@ -642,12 +662,15 @@ function Instance(Model, opts) {
enumerable: false
});

for (i = 0; i < opts.keys.length; i++) {
if (!opts.data.hasOwnProperty(opts.keys[i])) {
for (i = 0; i < opts.keyProperties.length; i++) {
var prop = opts.keyProperties[i];

if (!(prop.name in opts.data)) {
opts.changes = Object.keys(opts.data);
break;
}
}
rememberKeys();

opts.setupAssociations(instance);

Expand Down
5 changes: 2 additions & 3 deletions lib/LazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function addLazyLoadProperty(name, Instance, Model, property) {
var conditions = {};
conditions[Model.id] = Instance[Model.id];

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

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

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

item[property] = null;
item[Model.id] = Instance[Model.id];

return item.save(cb);
});
Expand Down
Loading

0 comments on commit f0930ff

Please sign in to comment.