adonis-lucid-mongodb
Advanced tools
Comparing version 1.0.35 to 1.0.36
@@ -8,3 +8,3 @@ { | ||
}, | ||
"version": "1.0.35", | ||
"version": "1.0.36", | ||
"scripts": { | ||
@@ -11,0 +11,0 @@ "lint": "standard", |
@@ -33,2 +33,3 @@ # AdonisJS Lucid MongoDB | ||
const users = yield User.where({or: [{gender: 'female', age: {gte: 20}}, {gender: 'male', age: {gte: 22}}]}).fetch() | ||
const user = yield User.where('name').eq('peter').where('age').gt(18).lte(60).sort('-age').first() | ||
@@ -40,3 +41,2 @@ | ||
const users = yield User.where({or: [{gender: 'female', age: {gte: 20}}, {gender: 'male', age: {gte: 22}}]}).fetch() | ||
@@ -183,2 +183,15 @@ // to query geo near you need declare field type as geometry and add 2d or 2dsphere index in migration file | ||
### Field type | ||
> Type of `mongodb.ObjectID` | ||
The objectId fields will be converted to mongodb.ObjectID before save to db. | ||
```js | ||
class Article extends LucidMongo { | ||
static get objectIdFields() { return ['_id', 'categoryId'] } //default return ['_id'] | ||
} | ||
``` | ||
The where query conditions will be converted to objectId too | ||
```js | ||
const article = yield Article.find('58ccb403f895502b84582c63') | ||
const articles = yield Article.where({department_id: {in: ['58ccb403f895502b84582c63', '58ccb403f895502b84582c63']}}).fetch() | ||
``` | ||
> Type of `date` | ||
@@ -190,3 +203,3 @@ ```js | ||
``` | ||
The field declare as date will convert to moment js object after get from db | ||
The field declare as date will be converted to moment js object after get from db | ||
```js | ||
@@ -196,7 +209,11 @@ const staff = yield Staff.first() | ||
``` | ||
You can set attribute of model as moment js object | ||
You can set attribute of model as moment js object, field will be converted to date before save to db | ||
```js | ||
staff.dob = moment(request.input('dob')) | ||
``` | ||
The where query conditions will be converted to date too | ||
```js | ||
const user = yield User.where({created_at: {gte: '2017-01-01'}}).fetch() | ||
``` | ||
Date type is UTC timezone | ||
> Type of `geometry` | ||
@@ -203,0 +220,0 @@ ```js |
@@ -542,2 +542,13 @@ 'use strict' | ||
/** | ||
* ObjectId fields will auto convert to mongodb.ObjectID | ||
* | ||
* @return {Array} | ||
* | ||
* @public | ||
*/ | ||
static get objectIdFields () { | ||
return [this.primaryKey] | ||
} | ||
/** | ||
* created at field getter method, by default | ||
@@ -544,0 +555,0 @@ * it returns an instance of moment js. |
@@ -74,3 +74,3 @@ 'use strict' | ||
// transform field with type | ||
value = this.getValueWithType(property, value) | ||
value = this.setValueWithType(property, value) | ||
const setter = util.makeSetterName(property) | ||
@@ -77,0 +77,0 @@ if (typeof (this[setter]) === 'function') { |
@@ -126,3 +126,6 @@ 'use strict' | ||
Dates.formatDate = function (date) { | ||
return moment(date).isValid() ? moment(date).format(this.constructor.dateFormat) : date | ||
if (moment.isMoment(date)) { | ||
return moment.utc(date).format(this.constructor.dateFormat) | ||
} | ||
return moment.utc(date).isValid() ? moment.utc(date).format(this.constructor.dateFormat) : date | ||
} |
@@ -6,2 +6,3 @@ 'use strict' | ||
const GeoPoint = require('geopoint') | ||
const objectId = require('mongodb').ObjectID | ||
const _ = require('lodash') | ||
@@ -11,3 +12,3 @@ | ||
* | ||
* @method getValueWithType | ||
* @method setValueWithType | ||
* | ||
@@ -19,9 +20,11 @@ * @param {Object} values | ||
*/ | ||
FieldTypes.getValueWithType = function (key, value) { | ||
FieldTypes.setValueWithType = function (key, value) { | ||
if (_.includes(this.constructor.boolFields, key)) { | ||
return !!value | ||
} else if (_.includes(this.constructor.dateFields, key)) { | ||
return moment(value) | ||
return moment.utc(value) | ||
} else if (_.includes(this.constructor.geoFields, key)) { | ||
return new GeoPoint(value.lat, value.lng) | ||
} else if (_.includes(this.constructor.objectIdFields, key)) { | ||
return key instanceof objectId ? key : objectId(key) | ||
} | ||
@@ -50,2 +53,4 @@ return value | ||
} | ||
} else if (value instanceof objectId) { | ||
return String(value) | ||
} | ||
@@ -52,0 +57,0 @@ return value |
@@ -15,3 +15,4 @@ 'use strict' | ||
const CE = require('../../Exceptions') | ||
const ObjectID = require('mongodb').ObjectID | ||
const objectId = require('mongodb').ObjectID | ||
const moment = require('moment') | ||
const debug = require('debug')('mquery') | ||
@@ -488,2 +489,17 @@ const methods = exports = module.exports = {} | ||
/** | ||
* pluck one field from the db query and return | ||
* them as an array. | ||
* | ||
* @param {Object} target | ||
* | ||
* @return {Function} | ||
*/ | ||
methods.pluck = function (target) { | ||
return function * (field) { | ||
const values = yield this.select(field).fetch() | ||
return values.map(field).value() | ||
} | ||
} | ||
/** | ||
* pluck primary keys from the SQL query and return | ||
@@ -498,3 +514,3 @@ * them as an array. | ||
return function () { | ||
return target.modelQueryBuilder.select(target.HostModel.primaryKey).pluck(target.HostModel.primaryKey) | ||
return this.pluck(target.HostModel.primaryKey) | ||
} | ||
@@ -532,3 +548,3 @@ } | ||
yield target.connect() | ||
const firstRow = yield target.modelQueryBuilder.select(field).findOne() | ||
const firstRow = yield target.modelQueryBuilder.select(field).first() | ||
return firstRow ? firstRow[field] : null | ||
@@ -600,3 +616,3 @@ } | ||
return function (key, values) { | ||
target.modelQueryBuilder.where(key).in(values) | ||
target.modelQueryBuilder.where(key).in(_formatValue(target.HostModel, key, values)) | ||
return this | ||
@@ -639,3 +655,3 @@ } | ||
return function * (id) { | ||
return yield this.where('_id', ObjectID(id)).first() | ||
return yield this.where(target.HostModel.primaryKey, id).first() | ||
} | ||
@@ -754,2 +770,3 @@ } | ||
if (k !== 'maxDistance' && k !== 'minDistance') { | ||
c = _formatValue(target.HostModel, key, c) | ||
target.modelQueryBuilder.where(key)[k](c) | ||
@@ -762,3 +779,3 @@ } | ||
} else { | ||
target.modelQueryBuilder.where(key, conditions) | ||
target.modelQueryBuilder.where(key, _formatValue(conditions)) | ||
} | ||
@@ -773,2 +790,19 @@ }) | ||
function _formatValue (model, key, value) { | ||
if (_.isArray(value)) { | ||
return _.map(value, v => _formatValue(model, key, v)) | ||
} | ||
if (_.includes(model.boolFields, key)) { | ||
return !!value | ||
} else if (_.includes(model.dateFields, key) && _.isString(value)) { | ||
return value instanceof Date ? value : moment.utc(value).toDate() | ||
} else if ((key === model.createTimestamp || key === model.updateTimestamp || key === model.deleteTimestamp) && _.isString(value)) { | ||
return value instanceof Date ? value : moment.utc(value).toDate() | ||
} else if (_.includes(model.objectIdFields, key) && _.isString(value)) { | ||
return objectId(value) | ||
} | ||
return value | ||
} | ||
/** | ||
@@ -775,0 +809,0 @@ * Aggregate count |
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
8488
320
245011