bookshelf-modelbase-plus
Advanced tools
Comparing version 2.10.2 to 2.10.3
@@ -171,3 +171,12 @@ const _ = require('lodash'); | ||
if (options.order_by || options.order_by === '') { | ||
q.orderBy(options.order_by || new this().idAttribute || 'id'); | ||
const order = _.castArray(options.order_by || new this().idAttribute || 'id') | ||
.forEach((column) => { | ||
if (column) { | ||
if (column[0] === '-') { | ||
q.query(qb => qb.orderBy(column.slice(1), 'desc')); | ||
} else { | ||
q.query(qb => qb.orderBy(column, 'asc')); | ||
} | ||
} | ||
}); | ||
} | ||
@@ -327,6 +336,8 @@ if (options.select) { | ||
} | ||
const debug = options.debug; | ||
let previousAttrs = null; | ||
let unchanged = false; | ||
this | ||
.query({where: findArgs}) | ||
.fetch({ require: true }) | ||
.fetch({ require: true, debug }) | ||
.then((model) => { | ||
@@ -347,2 +358,7 @@ if (_.isEmpty(data)) { | ||
const toSave = _.extend({}, existing, data); | ||
if (_.isEqual(existing, toSave)) { | ||
unchanged = true; | ||
return model; | ||
} | ||
// set updated_at column automatically | ||
@@ -356,9 +372,15 @@ if (model.hasTimestamps) { | ||
return model | ||
.save(toSave, { method: 'update', patch: true, require: true }); | ||
.save(toSave, { method: 'update', patch: true, require: true, debug }); | ||
}) | ||
.then((model) => { | ||
return this.query({ where: findArgs }).fetch({ softDelete: false }); | ||
if (unchanged) { | ||
return model; | ||
} | ||
const newWhere = _.mapValues(findArgs, (v, k) => _.has(data, k) ? data[k] : v); | ||
return this.query({ where: newWhere }).fetch({ softDelete: false, debug }); | ||
}) | ||
.then((model) => { | ||
model.saveAttributes(previousAttrs); | ||
if (!model.unchanged) { | ||
model.saveAttributes(previousAttrs); | ||
} | ||
resolve(model); | ||
@@ -365,0 +387,0 @@ }) |
{ | ||
"name": "bookshelf-modelbase-plus", | ||
"version": "2.10.2", | ||
"version": "2.10.3", | ||
"description": "Extended functionality for REST operations with validation, filtering, ordering, importing records.", | ||
@@ -5,0 +5,0 @@ "main": "./lib", |
@@ -134,2 +134,22 @@ /* global jest */ | ||
test('should be able to order by multiple', () => { | ||
return User | ||
.getList({ order_by: ['email', 'first_name'] }, User.columns) | ||
.then((models) => { | ||
expect(models.toJSON().map(m => m.email)).toEqual([ | ||
'email@user0.com', | ||
'email@user1.com', | ||
'email@user2.com', | ||
]); | ||
}) | ||
.then(() => User.getList({ order_by: ['-email', '-first_name'] })) | ||
.then((models) => { | ||
expect(models.toJSON().map(m => m.email)).toEqual([ | ||
'email@user2.com', | ||
'email@user1.com', | ||
'email@user0.com', | ||
]); | ||
}); | ||
}); | ||
it('should be able to skip pagination with !paginate', () => { | ||
@@ -336,2 +356,11 @@ return User | ||
it('should update an entry when in query', () => { | ||
return User | ||
.getList({ email: 'emailtest@user2.com', limit: 1 }, User.columns) | ||
.then(models => User.updateOne( | ||
{ email: 'emailtest-updated', findBy: { email: 'emailtest@user2.com' } }, | ||
User.columns | ||
)).then(updatedModel => expect(updatedModel.get('email')).toBe('emailtest-updated')); | ||
}); | ||
it('should destroy an entry by composite key', () => { | ||
@@ -338,0 +367,0 @@ return User |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
78861
1517