Skip to content

Commit

Permalink
add deep sort transformations
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Tiertant committed Dec 21, 2018
1 parent 98f0bcf commit 89bdd38
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 13 deletions.
27 changes: 23 additions & 4 deletions lib/offshore/core/transformations.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,27 @@ Transformation.prototype.serialize = function(attributes, behavior) {
// Transform sort column name
if (criteria.sort && _.isObject(criteria.sort)) {
_.keys(criteria.sort).forEach(function(order) {
if (self._transformations[order]) {
if (order.includes('.')) {
var parts = order.split('.');
var parentAttr = self.attributes[parts[0]];
if (!parentAttr || !parentAttr.references) {
return;
}
var transformed = parts.shift();
var current_collection = self.collections[parentAttr.references];
while (parts.length > 1) {
var part = parts.shift();
parentAttr = current_collection._transformer.attributes[part];
if (parentAttr && parentAttr.references) {
current_collection = self.collections[parentAttr.references];
}
transformed += '.' + part;
};
transformed += '.' + current_collection._transformer.serialize(parts[0]);
criteria.sort[transformed] = criteria.sort[order];
delete criteria.sort[order];

} else if (self._transformations[order]) {
criteria.sort[self._transformations[order]] = criteria.sort[order];
delete criteria.sort[order];
}
Expand Down Expand Up @@ -201,9 +221,8 @@ Transformation.prototype.serialize = function(attributes, behavior) {
if (type !== 'json' && _.isPlainObject(obj[property])) {

// check if parentAttr is a relation
if (parentAttr && (parentAttr.collection || parentAttr.model)) {
var collection = parentAttr.collection || parentAttr.model;
return obj[property] = self.collections[collection]._transformer.serialize(obj[property]);
if (parentAttr && parentAttr.references) {
return obj[property] = self.collections[parentAttr.references]._transformer.serialize(obj[property]);
}

// check if object key is in the transformations
Expand Down
72 changes: 63 additions & 9 deletions test/unit/core/core.transformations/transformations.serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Core Transformations', function() {
});

describe('with associations', function() {
var transformer;
var colls = {};

/**
* Build up real offshore schema for accurate testing
Expand All @@ -66,6 +66,10 @@ describe('Core Transformations', function() {
uuid: {
type: 'string',
primaryKey: true
},
foo: {
collection: 'foo',
via: 'customer'
}
}
}));
Expand All @@ -75,26 +79,76 @@ describe('Core Transformations', function() {
tableName: 'foo',
attributes: {
customer: {
model: 'customer'
model: 'customer',
columnName: 'customer_uuid'
},
bar: {
model: 'bar',
columnName: 'foobar_id'
}
}
}));

collections.push(Offshore.Collection.extend({
identity: 'bar',
tableName: 'bar',
attributes: {
id: {
type: 'integer',
primaryKey: true,
columnName: 'bar_id'
},
name: {
type: 'string',
columnName: 'bar_name'
}
}
}));

var schema = new Schema(collections);
transformer = new Transformer(schema.foo.attributes, schema.schema);
colls.foo = {
_transformer: new Transformer(schema.foo.attributes, colls)
};
colls.customer = {
_transformer: new Transformer(schema.customer.attributes, colls)
};
colls.bar = {
_transformer: new Transformer(schema.bar.attributes, colls)
};
});

it('should change customer key to customer_uuid', function() {
var values = transformer.serialize({ customer: 1 });
assert(values.customer);
assert(values.customer === 1);
var values = colls.foo._transformer.serialize({ customer: 1 });
assert(values.customer_uuid);
assert(values.customer_uuid === 1);
});

it('should work recursively', function() {
var values = transformer.serialize({ where: { user: { customer: 1 }}});
assert(values.where.user.customer);
assert(values.where.user.customer === 1);
var values = colls.foo._transformer.serialize({ where: { user: { customer: 1 }}});
assert(values.where.user.customer_uuid);
assert(values.where.user.customer_uuid === 1);
});

it('should work deeply', function() {
var values = colls.customer._transformer.serialize({
where: {
foo: {
and: [
{ bar: [1, 2] },
{ bar: { name: 'a' }}
]
}
},
sort: { 'foo.bar.name': -1 }
});
assert.deepEqual(values.where.foo.and, [
{ foobar_id: [1, 2] },
{ bar: { bar_name: 'a' } }
]);
assert(values.sort['foo.bar.bar_name']);
assert.equal(values.sort['foo.bar.bar_name'], -1);
});

});
});

Expand Down

0 comments on commit 89bdd38

Please sign in to comment.