Skip to content

Commit

Permalink
Fix Property.mapsTo.
Browse files Browse the repository at this point in the history
Closes #506
  • Loading branch information
dxg committed May 22, 2014
1 parent 7234015 commit 1cd44d8
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 36 deletions.
3 changes: 2 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### v2.1.14 - ***
### v2.1.14 - 22 May 2014
- Allow explicitly specifying `key: true` on properties rather than passing in an array of ids.
- Fix Property.mapsTo (#506)

### v2.1.13 - 21 May 2014
- Dont modify array passed to execQuery
Expand Down
6 changes: 3 additions & 3 deletions lib/AggregateFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ function AggregateFunctions(opts) {
throw new ORMError('PARAM_MISMATCH', "When using append you must at least define one property");
}
if (Array.isArray(arguments[0])) {
opts.properties = opts.properties.concat(arguments[0]);
opts.propertyList = opts.propertyList.concat(arguments[0]);
} else {
opts.properties = opts.properties.concat(Array.prototype.slice.apply(arguments));
opts.propertyList = opts.propertyList.concat(Array.prototype.slice.apply(arguments));
}
return this;
},
Expand Down Expand Up @@ -97,7 +97,7 @@ function AggregateFunctions(opts) {
throw new ORMError('PARAM_MISMATCH', "Missing aggregate functions");
}

var query = opts.driver.getQuery().select().from(opts.table).select(opts.properties);
var query = opts.driver.getQuery().select().from(opts.table).select(opts.propertyList);
var i, j;

for (i = 0; i < aggregates.length; i++) {
Expand Down
16 changes: 13 additions & 3 deletions lib/ChainFind.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _ = require("lodash");
var Utilities = require("./Utilities");
var ChainInstance = require("./ChainInstance");
var Promise = require("./Promise").Promise;

Expand Down Expand Up @@ -45,7 +46,7 @@ function ChainFind(Model, opts) {
} else {
omit = Array.prototype.slice.apply(arguments);
}
this.only(_.difference(Object.keys(Model.allProperties), omit));
this.only(_.difference(Object.keys(opts.properties), omit));
return this;
},
limit: function (limit) {
Expand Down Expand Up @@ -170,9 +171,18 @@ function ChainFind(Model, opts) {
return this;
},
all: function (cb) {
opts.driver.find(opts.only, opts.table, opts.conditions, {
var order, conditions;

conditions = Utilities.transformPropertyNames(
opts.conditions, opts.properties
);
order = Utilities.transformOrderPropertyNames(
opts.order, opts.properties
);

opts.driver.find(opts.only, opts.table, conditions, {
limit : opts.limit,
order : opts.order,
order : order,
merge : opts.merge,
offset : opts.offset,
exists : opts.exists
Expand Down
5 changes: 4 additions & 1 deletion lib/Drivers/DML/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ Driver.prototype.hasMany = function (Model, association) {

if (options.order) {
options.order[0] = options.order[0][1];
options.order = Utilities.standardizeOrder(options.order);
options.order = Utilities.transformOrderPropertyNames(
Utilities.standardizeOrder(options.order),
Model.allProperties
);
}

options.extra_props = association.props;
Expand Down
15 changes: 10 additions & 5 deletions lib/Instance.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var Property = require("./Property");
var Hook = require("./Hook");
var enforce = require("enforce");
var Utilities = require("./Utilities");
var Property = require("./Property");
var Hook = require("./Hook");
var enforce = require("enforce");

exports.Instance = Instance;

Expand Down Expand Up @@ -156,9 +157,9 @@ function Instance(Model, opts) {
}

if (opts.driver.propertyToValue) {
data[prop.mapsTo] = opts.driver.propertyToValue(opts.data[k], prop);
data[k] = opts.driver.propertyToValue(opts.data[k], prop);
} else {
data[prop.mapsTo] = opts.data[k];
data[k] = opts.data[k];
}
} else {
data[k] = opts.data[k];
Expand Down Expand Up @@ -191,6 +192,8 @@ function Instance(Model, opts) {
}, true);
}

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

opts.driver.insert(opts.table, data, opts.id, function (save_err, info) {
if (save_err) {
return saveError(cb, save_err);
Expand Down Expand Up @@ -248,6 +251,8 @@ function Instance(Model, opts) {
for (i = 0; i < opts.id.length; i++) {
conditions[opts.id[i]] = data[opts.id[i]];
}
changes = Utilities.transformPropertyNames(changes, Model.allProperties);

opts.driver.update(opts.table, changes, conditions, function (err) {
if (err) {
return saveError(cb, err);
Expand Down
37 changes: 15 additions & 22 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ function Model(opts) {
many_associations : many_associations,
extend_associations : extend_associations,
association_properties : association_properties,
setupAssociations : setupAssociations
setupAssociations : setupAssociations,
fieldToPropertyMap : fieldToPropertyMap
});
instance.on("ready", function (err) {
if (--pending > 0) {
Expand Down Expand Up @@ -143,18 +144,6 @@ function Model(opts) {
return instance;
};

var mapDatastoreFieldsToProperties = function (dataIn) {
var k, prop;
var dataOut = {};

for (k in dataIn) {
prop = fieldToPropertyMap[k];
if (!prop) dataOut[k] = dataIn[k];
else dataOut[prop.name] = dataIn[k];
}
return dataOut;
}

var model = function () {
var instance, i;

Expand Down Expand Up @@ -299,15 +288,15 @@ function Model(opts) {
return cb(new ORMError("Not found", 'NOT_FOUND', { model: opts.table }));
}

data = mapDatastoreFieldsToProperties(data[0]);
Utilities.renameDatastoreFieldsToPropertyNames(data[0], fieldToPropertyMap);

var uid = opts.driver.uid + "/" + opts.table + "/" + ids.join("/");

Singleton.get(uid, {
cache : (options.hasOwnProperty("cache") ? options.cache : opts.cache),
save_check : opts.settings.get("instance.cacheSaveCheck")
}, function (cb) {
return createInstance(data, {
return createInstance(data[0], {
uid : uid,
autoSave : options.autoSave,
autoFetch : (options.autoFetchLimit === 0 ? false : options.autoFetch),
Expand Down Expand Up @@ -399,12 +388,15 @@ function Model(opts) {
order : order,
merge : merge,
offset : options.offset,
properties : allProperties,
newInstance : function (data, cb) {
var uid = opts.driver.uid + "/" + opts.table + (merge ? "+" + merge.from.table : "");
for (var i = 0; i < opts.id.length; i++) {
uid += "/" + data[opts.id[i]];
}

Utilities.renameDatastoreFieldsToPropertyNames(data, fieldToPropertyMap);

Singleton.get(uid, {
cache : options.cache,
save_check : opts.settings.get("instance.cacheSaveCheck")
Expand Down Expand Up @@ -495,12 +487,12 @@ function Model(opts) {

model.aggregate = function () {
var conditions = {};
var properties = [];
var propertyList = [];

for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === "object") {
if (Array.isArray(arguments[i])) {
properties = arguments[i];
propertyList = arguments[i];
} else {
conditions = arguments[i];
}
Expand All @@ -512,11 +504,12 @@ function Model(opts) {
}

return new require("./AggregateFunctions")({
table : opts.table,
driver_name : opts.driver_name,
driver : opts.driver,
conditions : conditions,
properties : properties
table : opts.table,
driver_name : opts.driver_name,
driver : opts.driver,
conditions : conditions,
propertyList : propertyList,
properties : allProperties
});
};

Expand Down
53 changes: 53 additions & 0 deletions lib/Utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,56 @@ exports.getRealPath = function (path_str, stack_index) {

return path_str;
};

exports.transformPropertyNames = function (dataIn, properties) {
var k, prop;
var dataOut = {};

for (k in dataIn) {
prop = properties[k];
if (prop) {
dataOut[prop.mapsTo] = dataIn[k];
} else {
dataOut[k] = dataIn[k];
}
}
return dataOut;
};

exports.transformOrderPropertyNames = function (order, properties) {
if (!order) return order;

var i, item;
var newOrder = JSON.parse(JSON.stringify(order));

// Rename order properties according to mapsTo
for (var i = 0; i < newOrder.length; i++) {
item = newOrder[i];

// orderRaw
if (Array.isArray(item[1])) continue;

if (Array.isArray(item[0])) {
// order on a hasMany
// [ ['modelName', 'propName'], 'Z']
item[0][1] = properties[item[0][1]].mapsTo;
} else {
// normal order
item[0] = properties[item[0]].mapsTo;
}
}
return newOrder;
}

exports.renameDatastoreFieldsToPropertyNames = function (data, fieldToPropertyMap) {
var k, prop;

for (k in data) {
prop = fieldToPropertyMap[k];
if (prop && prop.name != k) {
data[prop.name] = data[k];
delete data[k];
}
}
return data;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"sqlite",
"mongodb"
],
"version" : "2.1.13",
"version" : "2.1.14",
"license" : "MIT",
"homepage" : "http://dresende.github.io/node-orm2",
"repository" : "http://github.com/dresende/node-orm2.git",
Expand Down
Loading

0 comments on commit 1cd44d8

Please sign in to comment.