Skip to content

Commit

Permalink
feat(base_model): add findBy and findByOrFail clause alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz committed Mar 26, 2024
1 parent 196c97c commit 0515560
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
33 changes: 31 additions & 2 deletions src/orm/base_model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,21 @@ class BaseModelImpl implements LucidRow {
/**
* Find model instance using a key/value pair
*/
static async findBy(key: string, value: any, options?: ModelAdapterOptions) {
// @ts-expect-error - Return type should be inferred when used in a model
static findBy(clause: Record<string, unknown>, options?: ModelAdapterOptions)
// @ts-expect-error - Return type should be inferred when used in a model
static findBy(key: string, value: any, options?: ModelAdapterOptions)
static async findBy(
key: string | Record<string, unknown>,
value?: any | ModelAdapterOptions,
options?: ModelAdapterOptions
) {
if (typeof key === 'object') {
return this.query(value as ModelAdapterOptions)
.where(key)
.first()
}

if (value === undefined) {
throw new Exception('"findBy" expects a value. Received undefined')
}
Expand All @@ -714,10 +728,25 @@ class BaseModelImpl implements LucidRow {
/**
* Find model instance using a key/value pair
*/
static async findByOrFail(key: string, value: any, options?: ModelAdapterOptions) {
// @ts-expect-error - Return type should be inferred when used in a model
static findByOrFail(clause: Record<string, unknown>, options?: ModelAdapterOptions)
// @ts-expect-error - Return type should be inferred when used in a model
static findByOrFail(key: string, value: any, options?: ModelAdapterOptions)
static async findByOrFail(
key: string | Record<string, unknown>,
value?: any | ModelAdapterOptions,
options?: ModelAdapterOptions
) {
if (typeof key === 'object') {
return this.query(value as ModelAdapterOptions)
.where(key)
.firstOrFail()
}

if (value === undefined) {
throw new Exception('"findByOrFail" expects a value. Received undefined')
}

return this.query(options).where(key, value).firstOrFail()
}

Expand Down
20 changes: 19 additions & 1 deletion src/types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,15 @@ export interface LucidModel {
options?: ModelAdapterOptions
): Promise<InstanceType<T>>

/**
* Find one using a clause
*/
findBy<T extends LucidModel>(
this: T,
clause: Record<string, unknown>,
options?: ModelAdapterOptions
): Promise<null | InstanceType<T>>

/**
* Find one using a key-value pair
*/
Expand All @@ -985,6 +994,15 @@ export interface LucidModel {
options?: ModelAdapterOptions
): Promise<null | InstanceType<T>>

/**
* Find one using a clause or fail
*/
findByOrFail<T extends LucidModel>(
this: T,
clause: Record<string, unknown>,
options?: ModelAdapterOptions
): Promise<InstanceType<T>>

/**
* Find one using a key-value pair or fail
*/
Expand All @@ -996,7 +1014,7 @@ export interface LucidModel {
): Promise<InstanceType<T>>

/**
* Find multiple models instance using a key/value pair
* Find multiple models instance using a clause
*/
findManyBy<T extends LucidModel>(
clause: Record<string, unknown>,
Expand Down

0 comments on commit 0515560

Please sign in to comment.