Skip to content

Commit

Permalink
feat(lucid): add static methods
Browse files Browse the repository at this point in the history
static methods for commonly required operations has been added
  • Loading branch information
thetutlage committed Jun 18, 2017
1 parent cff2f7e commit ed37c09
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 10 deletions.
72 changes: 62 additions & 10 deletions src/Lucid/Model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ class Model {
*
* @return {Model|Null}
*/
static async find (value) {
static find (value) {
return this.findBy(this.primaryKey, value)
}

Expand All @@ -742,17 +742,69 @@ class Model {
*
* @return {Model|Null}
*/
static async findBy (key, value) {
const result = await this.query().where(key, value).first()
static findBy (key, value) {
return this.query().where(key, value).first()
}

if (!result) {
return null
}
/**
* Returns the first row. This method will add orderBy asc
* clause
*
* @method first
*
* @return {Model|Null}
*/
static first () {
return this.query().orderBy(this.primaryKey, 'asc').first()
}

const modelInstance = new this()
modelInstance.newUp(result)
this.$hooks.after.exec('find', modelInstance)
return modelInstance
/**
* Returns last row. This method will add orderBy desc
* clause.
*
* @method last
*
* @return {Model|Null}
*/
static last () {
return this.query().orderBy(this.primaryKey, 'desc').first()
}

/**
* Fetch everything from the database
*
* @method all
*
* @return {Collection}
*/
static all () {
return this.query().fetch()
}

/**
* Select x number of rows
*
* @method pick
*
* @param {Number} [limit = 1]
*
* @return {Collection}
*/
static pick (limit = 1) {
return this.query().orderBy(this.primaryKey, 'asc').limit(limit).fetch()
}

/**
* Select x number of rows in inverse
*
* @method pickInverse
*
* @param {Number} [limit = 1]
*
* @return {Collection}
*/
static pickInverse (limit = 1) {
return this.query().orderBy(this.primaryKey, 'desc').limit(limit).fetch()
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/Lucid/QueryBuilder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,25 @@ class QueryBuilder {
this.applyScopes()
return this.query.update(this.model.formatDates(values))
}

/**
* Returns the first row from the database.
*
* @method first
*
* @return {Model|Null}
*/
async first () {
const result = await this.query.first()
if (!result) {
return null
}

const modelInstance = new this.model()
modelInstance.newUp(result)
this.model.$hooks.after.exec('find', modelInstance)
return modelInstance
}
}

module.exports = QueryBuilder
4 changes: 4 additions & 0 deletions src/Lucid/Serializers/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class Collection {
return this.rows[0]
}

size () {
return this.isOne ? 1 : this.rows.length
}

toJSON () {
return this.isOne ? this.rows.toObject() : this.rows.map((row) => row.toObject())
}
Expand Down
37 changes: 37 additions & 0 deletions test/unit/lucid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,4 +689,41 @@ test.group('Model', (group) => {
const user = await User.findBy('username', 'virk')
assert.deepEqual(hookInstance, user)
})

test('return everything from the database', async (assert) => {
class User extends Model {
}

User._bootIfNotBooted()

await ioc.use('Adonis/Src/Database').table('users').insert({ username: 'virk' })
const users = await User.all()
assert.instanceOf(users, CollectionSerializer)
})

test('pick x number of rows from database', async (assert) => {
class User extends Model {
}

User._bootIfNotBooted()

await ioc.use('Adonis/Src/Database').table('users').insert([{ username: 'virk' }, { username: 'nikk' }])
const users = await User.pick(1)
assert.instanceOf(users, CollectionSerializer)
assert.equal(users.size(), 1)
assert.equal(users.first().$attributes.username, 'virk')
})

test('pick inverse x number of rows from database', async (assert) => {
class User extends Model {
}

User._bootIfNotBooted()

await ioc.use('Adonis/Src/Database').table('users').insert([{ username: 'virk' }, { username: 'nikk' }])
const users = await User.pickInverse(1)
assert.instanceOf(users, CollectionSerializer)
assert.equal(users.size(), 1)
assert.equal(users.first().$attributes.username, 'nikk')
})
})

0 comments on commit ed37c09

Please sign in to comment.