Skip to content

Commit

Permalink
Merge branch 'release/4.0.11'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 15, 2017
2 parents a97a5e1 + b3479e5 commit 55568c2
Show file tree
Hide file tree
Showing 14 changed files with 305 additions and 53 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
<a name="4.0.11"></a>
## [4.0.11](https://github.com/adonisjs/adonis-lucid/compare/v4.0.10...v4.0.11) (2017-08-15)


### Bug Fixes

* **database:** database.on bind events on knex and not query ([948d05b](https://github.com/adonisjs/adonis-lucid/commit/948d05b))
* **lucid:** call afterFind hook via await keyword ([c4de51d](https://github.com/adonisjs/adonis-lucid/commit/c4de51d))
* **package:** add missing dependencies ([5e7e304](https://github.com/adonisjs/adonis-lucid/commit/5e7e304))


### Features

* **commands:** add migration:status command ([a6bc882](https://github.com/adonisjs/adonis-lucid/commit/a6bc882))
* **lucid:** add support for transactions in save operations ([687de3c](https://github.com/adonisjs/adonis-lucid/commit/687de3c))



<a name="4.0.10"></a>
## [4.0.10](https://github.com/adonisjs/adonis-lucid/compare/v4.0.8...v4.0.10) (2017-08-05)

Expand Down
59 changes: 59 additions & 0 deletions commands/MigrationStatus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict'

/*
* adonis-lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const BaseMigration = require('./BaseMigration')

class MigrationStatus extends BaseMigration {
/**
* Command signature required by ace
*
* @method signature
*
* @return {String}
*/
static get signature () {
return 'migration:status'
}

/**
* Command description
*
* @method description
*
* @return {String}
*/
static get description () {
return 'Check migrations current status'
}

/**
* Method called when command is executed. This method
* will print a table with the migrations status.
*
* @method handle
*
* @return {void|Array}
*/
async handle () {
try {
const migrations = await this.migration.status(this._getSchemaFiles())
const head = ['File name', 'Migrated', 'Batch']
const body = migrations.map((migration) => {
return [migration.name, migration.migrated ? 'Yes' : 'No', migration.batch]
})
this.table(head, body)
} catch (error) {
console.log(error)
}
}
}

module.exports = MigrationStatus
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adonisjs/lucid",
"version": "4.0.10",
"version": "4.0.11",
"description": "SQL ORM built on top of Active Record pattern",
"main": "index.js",
"scripts": {
Expand All @@ -24,6 +24,7 @@
"homepage": "https://github.com/adonisjs/adonis-lucid#readme",
"dependencies": {
"@adonisjs/generic-exceptions": "^1.0.0",
"chance": "^1.0.10",
"debug": "^2.6.8",
"knex": "^0.13.0",
"lodash": "^4.17.4",
Expand All @@ -36,7 +37,6 @@
"@adonisjs/ace": "git+https://github.com/adonisjs/ace.git#dawn",
"@adonisjs/fold": "^4.0.2",
"@adonisjs/sink": "^1.0.11",
"chance": "^1.0.10",
"clear-require": "^2.0.0",
"coveralls": "^2.13.1",
"cz-conventional-changelog": "^2.0.0",
Expand Down
2 changes: 2 additions & 0 deletions providers/MigrationsProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class MigrationsProvider extends ServiceProvider {
this.app.bind('Adonis/Commands/Migration:Rollback', () => require('../commands/MigrationRollback'))
this.app.bind('Adonis/Commands/Migration:Refresh', () => require('../commands/MigrationRefresh'))
this.app.bind('Adonis/Commands/Migration:Reset', () => require('../commands/MigrationReset'))
this.app.bind('Adonis/Commands/Migration:Status', () => require('../commands/MigrationStatus'))
this.app.bind('Adonis/Commands/Seed', () => require('../commands/Seed'))
}

Expand Down Expand Up @@ -105,6 +106,7 @@ class MigrationsProvider extends ServiceProvider {
ace.addCommand('Adonis/Commands/Migration:Rollback')
ace.addCommand('Adonis/Commands/Migration:Refresh')
ace.addCommand('Adonis/Commands/Migration:Reset')
ace.addCommand('Adonis/Commands/Migration:Status')
ace.addCommand('Adonis/Commands/Seed')
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/Database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ class Database {
return new Proxy(this, proxyHandler)
}

/**
* Bind listeners for database events. Which are
* `query`, `query-error`, `query-response` and
* `sql`
*
* @method on
*
* @param {Strign} event
* @param {Function} callback
*
* @chainable
*/
on (event, callback) {
this.knex.on(event, callback)
return this
}

/**
* The schema builder instance to be used
* for creating database schema.
Expand Down
53 changes: 41 additions & 12 deletions src/Lucid/Model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,13 +401,14 @@ class Model extends BaseModel {
* @method create
*
* @param {Object} payload
* @param {Object} [trx]
*
* @return {Model} Model instance is returned
*/
static async create (payload) {
static async create (payload, trx) {
const modelInstance = new this()
modelInstance.fill(payload)
await modelInstance.save()
await modelInstance.save(trx)
return modelInstance
}

Expand All @@ -417,18 +418,19 @@ class Model extends BaseModel {
* @method createMany
*
* @param {Array} payloadArray
* @param {Object} [trx]
*
* @return {Array} Array of model instances is returned
*
* @throws {InvalidArgumentException} If payloadArray is not an array
*/
static async createMany (payloadArray) {
static async createMany (payloadArray, trx) {
if (payloadArray instanceof Array === false) {
throw GE
.InvalidArgumentException
.invalidParameter(`${this.name}.createMany expects an array of values`, payloadArray)
}
return Promise.all(payloadArray.map((payload) => this.create(payload)))
return Promise.all(payloadArray.map((payload) => this.create(payload, trx)))
}

/**
Expand Down Expand Up @@ -627,11 +629,13 @@ class Model extends BaseModel {
* @method _insert
* @async
*
* @param {Object} trx
*
* @return {Boolean}
*
* @private
*/
async _insert () {
async _insert (trx) {
/**
* Executing before hooks
*/
Expand All @@ -644,8 +648,20 @@ class Model extends BaseModel {
this._setUpdatedAt(this.$attributes)
this._formatDateFields(this.$attributes)

const result = await this.constructor
.query()
const query = this.constructor.query()

/**
* If trx is defined then use it for the save
* operation.
*/
if (trx) {
query.transacting(trx)
}

/**
* Execute query
*/
const result = await query
.returning(this.constructor.primaryKey)
.insert(this.$attributes)

Expand Down Expand Up @@ -678,21 +694,32 @@ class Model extends BaseModel {
* @method _update
* @async
*
* @param {Object} trx
*
* @return {Boolean}
*/
async _update () {
async _update (trx) {
/**
* Executing before hooks
*/
await this.constructor.$hooks.before.exec('update', this)
let affected = 0

const query = this.constructor.query()

/**
* If trx is defined then use it for the update
* operation.
*/
if (trx) {
query.transacting(trx)
}

if (this.isDirty) {
/**
* Set proper timestamps
*/
affected = await this.constructor
.query()
affected = await query
.where(this.constructor.primaryKey, this.primaryKeyValue)
.ignoreScopes()
.update(this.dirty)
Expand Down Expand Up @@ -796,10 +823,12 @@ class Model extends BaseModel {
* @method save
* @async
*
* @param {Object} trx Transaction object to be used
*
* @return {Boolean} Whether or not the model was persisted
*/
async save () {
return this.isNew ? this._insert() : this._update()
async save (trx) {
return this.isNew ? this._insert(trx) : this._update(trx)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Lucid/QueryBuilder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ class QueryBuilder {
await modelInstance.loadMany(this._eagerLoads)
}

this.Model.$hooks.after.exec('find', modelInstance)
await this.Model.$hooks.after.exec('find', modelInstance)
return modelInstance
}

Expand Down
13 changes: 8 additions & 5 deletions src/Lucid/Relations/BelongsTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,17 @@ class BelongsTo extends BaseRelation {
* @async
*
* @param {Object} relatedInstance
* @param {Object} [trx]
*
* @return {Promise}
*/
async associate (relatedInstance) {
async associate (relatedInstance, trx) {
if (relatedInstance.isNew) {
await relatedInstance.save()
await relatedInstance.save(trx)
}

this.parentInstance[this.primaryKey] = relatedInstance[this.foreignKey]
return this.parentInstance.save()
return this.parentInstance.save(trx)
}

/**
Expand All @@ -146,15 +147,17 @@ class BelongsTo extends BaseRelation {
* @method dissociate
* @async
*
* @param {Object} [trx]
*
* @return {Promise}
*/
async dissociate () {
async dissociate (trx) {
if (this.parentInstance.isNew) {
throw CE.ModelRelationException.unsavedModelInstance('Cannot dissociate relationship since model instance is not persisted')
}

this.parentInstance[this.primaryKey] = null
return this.parentInstance.save()
return this.parentInstance.save(trx)
}
}

Expand Down
Loading

0 comments on commit 55568c2

Please sign in to comment.