Skip to content

Commit

Permalink
feat: add first and firstOrFail as static model methods
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Apr 6, 2020
1 parent b5302d5 commit 05f2422
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
16 changes: 16 additions & 0 deletions adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,22 @@ declare module '@ioc:Adonis/Lucid/Model' {
options?: ModelAdapterOptions,
): Promise<InstanceType<T>>

/**
* Same as `query().first()`
*/
first<T extends LucidModel> (
this: T,
options?: ModelAdapterOptions
): Promise<null | InstanceType<T>>

/**
* Same as `query().firstOrFail()`
*/
firstOrFail<T extends LucidModel> (
this: T,
options?: ModelAdapterOptions,
): Promise<null | InstanceType<T>>

/**
* Find many using an array of primary keys
*/
Expand Down
14 changes: 14 additions & 0 deletions src/Orm/BaseModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,20 @@ export class BaseModel implements LucidRow {
return this.query(options).where(this.primaryKey, value).firstOrFail()
}

/**
* Same as `query().first()`
*/
public static async first (options?: ModelAdapterOptions) {
return this.query(options).first()
}

/**
* Same as `query().firstOrFail()`
*/
public static async firstOrFail (options?: ModelAdapterOptions) {
return this.query(options).firstOrFail()
}

/**
* Find model instance using a key/value pair
*/
Expand Down

0 comments on commit 05f2422

Please sign in to comment.