adonis-lucid-mongodb
Advanced tools
Comparing version 0.11.5 to 1.0.0
@@ -8,3 +8,3 @@ { | ||
}, | ||
"version": "0.11.5", | ||
"version": "1.0.0", | ||
"scripts": { | ||
@@ -11,0 +11,0 @@ "lint": "standard", |
@@ -79,7 +79,64 @@ # AdonisJS Lucid MongoDB | ||
``` | ||
## Usage | ||
The usage of LucidMongo is similar to Lucid | ||
[Official Documentation](http://adonisjs.com/docs/2.0/installation) | ||
[Official Documentation of Lucid here](http://adonisjs.com/docs/2.0/installation) | ||
## <a name="contribution-guidelines"></a>Contribution Guidelines | ||
### Query | ||
This package support `where` method with some type of params | ||
```js | ||
const users = yield User.where('name', 'peter').fetch() | ||
const users = yield User.where({name: 'peter'}).fetch() | ||
const user = yield User.where('name').equal('peter').first() | ||
``` | ||
### Aggregation | ||
```js | ||
const max = yield User.query().max('age') | ||
const total = yield Employ.where(active, true).sum('salary', '$department_id') | ||
const avg = yield Employ.where(active, true).avg('salary', {department: '$department_id', role: '$role_id'}) | ||
``` | ||
### Addition relations | ||
1. `morphMany:` A model can belong to more than one other model, on a single association. For example, you might have a Picture model that belongs to either an Author model or a Reader model | ||
2. `embedsOne:` EmbedsOne is used to represent a model that embeds another model, for example, a Customer embeds one billingAddress. | ||
3. `embedsMany:` Use an embedsMany relation to indicate that a model can embed many instances of another model. For example, a Customer can have multiple email addresses and each email address is a complex object that contains label and address. | ||
4. `referMany:` Similar to populate of mongoosejs | ||
### Nested query | ||
```js | ||
const user = User.with('emails').find(1) | ||
const user = User.with('emails', 'phone').find(1) | ||
const user = User.with(['emails', 'phone']).find(1) | ||
const user = User.with({relation: 'email', 'scope': {verified: true}}).find(1) | ||
const user = User.with({relation: 'email', 'scope': query => { | ||
query.where(active, true).limit(1) | ||
}}).find(1) | ||
``` | ||
### Migration | ||
Current only support create, drop, rename collection and index | ||
```js | ||
up () { | ||
this.create('articles', (collection) => { | ||
collection.index('name_of_index', {title: 1}) | ||
}) | ||
this.rename('articles', 'posts') | ||
} | ||
``` | ||
### <a name="contribution-guidelines"></a>Contribution Guidelines | ||
In favor of active development we accept contributions for everyone. You can contribute by submitting a bug, creating pull requests or even improving documentation. |
@@ -181,3 +181,3 @@ 'use strict' | ||
if (connection && connectionPools[connection]) { | ||
connectionPools[connection].client.destroy() | ||
connectionPools[connection].close() | ||
delete connectionPools[connection] | ||
@@ -188,3 +188,3 @@ return | ||
_.each(connectionPools, (pool) => { | ||
pool.client.destroy() | ||
pool.close() | ||
}) | ||
@@ -395,2 +395,74 @@ connectionPools = {} | ||
Database.schema = { | ||
createCollection: function * (collectionName, callback) { | ||
const db = yield Database.connection('default') | ||
const collection = yield db.createCollection(collectionName) | ||
const schemaBuilder = new SchemaBuilder(collection) | ||
callback(schemaBuilder) | ||
return yield schemaBuilder.build() | ||
}, | ||
createCollectionIfNotExists: function * (collectionName, callback) { | ||
const db = yield Database.connection('default') | ||
const collection = yield db.createCollection(collectionName) | ||
const schemaBuilder = new SchemaBuilder(collection) | ||
callback(schemaBuilder) | ||
return yield schemaBuilder.build() | ||
}, | ||
dropCollection: function * (collectionName) { | ||
const db = yield Database.connection('default') | ||
return yield db.dropCollection(collectionName) | ||
}, | ||
dropIfExists: function * (collectionName) { | ||
const db = yield Database.connection('default') | ||
return yield db.dropCollection(collectionName) | ||
}, | ||
renameCollection: function * (collectionName, target) { | ||
const db = yield Database.connection('default') | ||
return yield db.collection(collectionName).rename(target) | ||
} | ||
} | ||
function SchemaBuilder (collection) { | ||
this.collection = collection | ||
this.createIndexes = [] | ||
this.dropIndexes = [] | ||
this.increments = function () {} | ||
this.timestamps = function () {} | ||
this.softDeletes = function () {} | ||
this.string = function () {} | ||
this.timestamp = function () {} | ||
this.boolean = function () {} | ||
this.integer = function () {} | ||
this.double = function () {} | ||
this.index = function (name, keys, options) { | ||
if (!name) { | ||
throw new CE.InvalidArgumentException(`param name is required to create index`) | ||
} | ||
if (!keys || !_.size(keys)) { | ||
throw new CE.InvalidArgumentException(`param keys is required to create index`) | ||
} | ||
options = options || {} | ||
options['name'] = name | ||
this.createIndexes.push({keys, options}) | ||
} | ||
this.dropIndex = function (name) { | ||
this.dropIndexes.push(name) | ||
} | ||
this.build = function * () { | ||
for (var i in this.createIndexes) { | ||
var createIndex = this.createIndexes[i] | ||
yield this.collection.createIndex(createIndex.keys, createIndex.options) | ||
} | ||
for (var j in this.dropIndexes) { | ||
var dropIndex = this.dropIndexes[j] | ||
yield this.collection.dropIndex(dropIndex.keys, dropIndex.options) | ||
} | ||
} | ||
} | ||
/** | ||
@@ -404,3 +476,3 @@ * these methods are not proxied and instead actual implementations | ||
*/ | ||
const customImplementations = ['_resolveConnectionKey', '_setConfigProvider', 'getConnectionPools', 'connection', 'close'] | ||
const customImplementations = ['_resolveConnectionKey', '_setConfigProvider', 'getConnectionPools', 'connection', 'close', 'schema'] | ||
@@ -407,0 +479,0 @@ mquery.prototype.forPage = Database.forPage |
@@ -23,3 +23,3 @@ 'use strict' | ||
this.binding = Ioc.use('Adonis/Src/Database') | ||
this.returningField = 'id' | ||
this.returningField = '_id' | ||
} | ||
@@ -26,0 +26,0 @@ |
@@ -750,1 +750,105 @@ 'use strict' | ||
} | ||
/** | ||
* Aggregate max | ||
* | ||
* @param {String} key | ||
* @param {Mixed} [value] | ||
* | ||
* @number | ||
*/ | ||
methods.max = function (target) { | ||
return function * (key, groupBy) { | ||
const $match = target.modelQueryBuilder._conditions | ||
const connection = yield target.connect() | ||
const $group = { | ||
_id: groupBy, | ||
max: {$max: '$' + key} | ||
} | ||
return new Promise((resolve, reject) => { | ||
connection.collection(target.HostModel.collection) | ||
.aggregate([{$match}, {$group}], (err, result) => { | ||
if (err) reject(err) | ||
resolve(groupBy ? result : result[0].max) | ||
}) | ||
}) | ||
} | ||
} | ||
/** | ||
* Aggregate min | ||
* | ||
* @param {String} key | ||
* @param {Mixed} [value] | ||
* | ||
* @number | ||
*/ | ||
methods.min = function (target) { | ||
return function * (key, groupBy) { | ||
const $match = target.modelQueryBuilder._conditions | ||
const connection = yield target.connect() | ||
const $group = { | ||
_id: groupBy, | ||
min: {$min: '$' + key} | ||
} | ||
return new Promise((resolve, reject) => { | ||
connection.collection(target.HostModel.collection) | ||
.aggregate([{$match}, {$group}], (err, result) => { | ||
if (err) reject(err) | ||
resolve(groupBy ? result : result[0].min) | ||
}) | ||
}) | ||
} | ||
} | ||
/** | ||
* Aggregate sum | ||
* | ||
* @param {String} key | ||
* @param {Mixed} [value] | ||
* | ||
* @number | ||
*/ | ||
methods.sum = function (target) { | ||
return function * (key, groupBy) { | ||
const $match = target.modelQueryBuilder._conditions | ||
const connection = yield target.connect() | ||
const $group = { | ||
_id: groupBy, | ||
sum: {$sum: '$' + key} | ||
} | ||
return new Promise((resolve, reject) => { | ||
connection.collection(target.HostModel.collection) | ||
.aggregate([{$match}, {$group}], (err, result) => { | ||
if (err) reject(err) | ||
resolve(groupBy ? result : result[0].sum) | ||
}) | ||
}) | ||
} | ||
} | ||
/** | ||
* Aggregate avg | ||
* | ||
* @param {String} key | ||
* @param {Mixed} [value] | ||
* | ||
* @number | ||
*/ | ||
methods.avg = function (target) { | ||
return function * (key, groupBy) { | ||
const $match = target.modelQueryBuilder._conditions | ||
const connection = yield target.connect() | ||
const $group = { | ||
_id: groupBy, | ||
avg: {$avg: '$' + key} | ||
} | ||
return new Promise((resolve, reject) => { | ||
connection.collection(target.HostModel.collection) | ||
.aggregate([{$match}, {$group}], (err, result) => { | ||
if (err) reject(err) | ||
resolve(groupBy ? result : result[0].avg) | ||
}) | ||
}) | ||
} | ||
} |
@@ -48,3 +48,2 @@ 'use strict' | ||
} | ||
const migrationActions = this._mapMigrationsToActions(migrations, 'up') | ||
@@ -59,3 +58,2 @@ /** | ||
} | ||
const nextBatch = yield this._getNextBatchNumber() | ||
@@ -62,0 +60,0 @@ yield this._makeLockCollection() |
@@ -12,2 +12,4 @@ 'use strict' | ||
const mquery = require('mquery') | ||
const Batch = exports = module.exports = {} | ||
@@ -23,4 +25,5 @@ | ||
Batch._getRecentBatchNumber = function * () { | ||
const result = yield this.database.from(this.migrationsCollection).max('batch as batch') | ||
const batchNumber = result[0].batch || 1 | ||
const db = yield this.database.connection('default') | ||
const result = yield mquery().collection(db.collection(this.migrationsCollection)).sort('-batch').findOne() | ||
const batchNumber = result ? (result.batch || 1) : 1 | ||
return Number(batchNumber) - 1 | ||
@@ -38,4 +41,5 @@ } | ||
Batch._getNextBatchNumber = function * () { | ||
const result = yield this.database.collection(this.migrationsCollection).max('batch as batch') | ||
const batchNumber = result[0].batch || 0 | ||
const db = yield this.database.connection('default') | ||
const result = yield mquery().collection(db.collection(this.migrationsCollection)).sort('-batch').findOne() | ||
const batchNumber = result ? result.batch : 0 | ||
return Number(batchNumber) + 1 | ||
@@ -55,3 +59,4 @@ } | ||
const migrations = {name: migration, batch: batchNumber, migration_time: new Date()} | ||
return yield this.database.collection(this.migrationsCollection).insert(migrations) | ||
const db = yield this.database.connection('default') | ||
return yield db.collection(this.migrationsCollection).insert(migrations) | ||
} | ||
@@ -68,3 +73,4 @@ | ||
Batch._revertProgress = function * (file, batchNumber) { | ||
return yield this.database.collection(this.migrationsCollection).where('name', file).delete() | ||
const db = yield this.database.connection('default') | ||
return yield mquery().collection(db.collection(this.migrationsCollection)).where('name', file).remove() | ||
} |
@@ -13,2 +13,3 @@ 'use strict' | ||
const _ = require('lodash') | ||
const mquery = require('mquery') | ||
const CE = require('../../Exceptions') | ||
@@ -27,3 +28,3 @@ const Lock = exports = module.exports = {} | ||
.createCollectionIfNotExists(this.lockCollection, function (collection) { | ||
collection.increments('id') | ||
collection.increments('_id') | ||
collection.boolean('is_locked') | ||
@@ -38,4 +39,5 @@ }) | ||
*/ | ||
Lock._addLock = function () { | ||
return this.database.insert({is_locked: 1}).into(this.lockCollection) | ||
Lock._addLock = function * () { | ||
const db = yield this.database.connection('default') | ||
return yield db.collection(this.lockCollection).insert({is_locked: 1}) | ||
} | ||
@@ -52,7 +54,8 @@ | ||
Lock._checkLock = function * () { | ||
const result = yield this.database | ||
.from(this.lockCollection) | ||
const db = yield this.database.connection('default') | ||
const result = yield mquery().collection(db.collection(this.lockCollection)) | ||
.where('is_locked', 1) | ||
.orderBy('id', 'desc') | ||
.sort('-_id') | ||
.limit(1) | ||
.find() | ||
@@ -74,3 +77,3 @@ if (_.size(result)) { | ||
Lock._deleteLock = function * () { | ||
return this.database.schema.dropCollection(this.lockCollection) | ||
return yield this.database.schema.dropCollection(this.lockCollection) | ||
} |
@@ -18,3 +18,3 @@ 'use strict' | ||
const logger = new CatLog('adonis:lucid') | ||
const mquery = require('mquery') | ||
const Migrate = exports = module.exports = {} | ||
@@ -58,4 +58,3 @@ | ||
*/ | ||
Migrate._callSchemaActions = function (defination, connection) { | ||
const builder = this.database.connection(connection).schema | ||
Migrate._callSchemaActions = function (definition, connection) { | ||
/** | ||
@@ -65,5 +64,5 @@ * Custom check to allow access the database provider and | ||
*/ | ||
if (defination.action === 'db') { | ||
if (definition.action === 'db') { | ||
return co.wrap(function * () { | ||
return yield defination.callback(this.database) | ||
return yield definition.callback(this.database) | ||
}.bind(this)) | ||
@@ -75,6 +74,7 @@ } | ||
* need to wrap schema method inside a function to keep | ||
* the API scollection. | ||
* the API collection. | ||
*/ | ||
return (function () { | ||
return builder[defination.action](defination.key, this._wrapSchemaCallback(defination.callback)) | ||
// const builder = yield this.database.connection(connection) | ||
return this.database.schema[definition.action](definition.key, this._wrapSchemaCallback(definition.callback)) | ||
}.bind(this)) | ||
@@ -148,6 +148,4 @@ } | ||
*/ | ||
Migrate._makeMigrationsCollection = function () { | ||
return this | ||
.database | ||
.schema | ||
Migrate._makeMigrationsCollection = function * () { | ||
return yield this.database.schema | ||
.createCollectionIfNotExists(this.migrationsCollection, function (collection) { | ||
@@ -215,3 +213,2 @@ collection.increments('id') | ||
const diff = direction === 'down' ? _.reverse(_.intersection(values, _.keys(files))) : _.difference(_.keys(files), values) | ||
return _.reduce(diff, (result, name) => { | ||
@@ -252,4 +249,5 @@ result[name] = files[name] | ||
*/ | ||
Migrate._getMigratedFiles = function () { | ||
return this.database.select('name as name').from(this.migrationsCollection).orderBy('name').pluck('name') | ||
Migrate._getMigratedFiles = function * () { | ||
const db = yield this.database.connection('default') | ||
return mquery().collection(db.collection(this.migrationsCollection)).find().select('name').sort('name') | ||
} | ||
@@ -266,9 +264,9 @@ | ||
*/ | ||
Migrate._getFilesTillBatch = function (batch) { | ||
return this.database | ||
Migrate._getFilesTillBatch = function * (batch) { | ||
const db = yield this.database.connection('default') | ||
const migrateFiles = yield mquery().collection(db.collection(this.migrationsCollection)).find() | ||
.select('name') | ||
.from(this.migrationsCollection) | ||
.where('batch', '>', batch) | ||
.orderBy('name') | ||
.pluck('name') | ||
.where('batch').gt(batch) | ||
.sort('name') | ||
return _.map(migrateFiles, 'name') | ||
} | ||
@@ -275,0 +273,0 @@ |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
225810
8160
1
142