Skip to content

Commit

Permalink
feat(base_model): add findManyBy method
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz committed Mar 8, 2024
1 parent 67ba462 commit b14a0e7
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/orm/base_model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,29 @@ class BaseModelImpl implements LucidRow {
return this.query(options).where(key, value).firstOrFail()
}

/**
* Find multiple models instance using a key/value pair
*/
// @ts-expect-error - Return type should be inferred when used in a model
static findManyBy(clause: Record<string, unknown>, options?: ModelAdapterOptions)
// @ts-expect-error - Return type should be inferred when used in a model
static findManyBy(key: string, value: any[], options?: ModelAdapterOptions)
static findManyBy(
key: string | Record<string, unknown>,
value?: any[] | ModelAdapterOptions,
options?: ModelAdapterOptions
) {
if (typeof key === 'object') {
return this.query(value as ModelAdapterOptions).where(key)
}

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

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

/**
* Same as `query().first()`
*/
Expand Down
18 changes: 18 additions & 0 deletions src/types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,24 @@ export interface LucidModel {
options?: ModelAdapterOptions
): Promise<InstanceType<T>>

/**
* Find multiple models instance using a key/value pair
*
*/
findManyBy<T extends LucidModel>(
clause: Record<string, unknown>,
options?: ModelAdapterOptions
): Promise<InstanceType<T>[]>

/**
* Find multiple models instance using a key/value pair
*/
findManyBy<T extends LucidModel>(
key: string,
value: any[],
options?: ModelAdapterOptions
): Promise<InstanceType<T>[]>

/**
* Same as `query().first()`
*/
Expand Down
60 changes: 60 additions & 0 deletions test/orm/base_model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3780,6 +3780,66 @@ test.group('Base Model | fetch', (group) => {
assert.equal(users[1].$primaryKeyValue, 1)
})

test('find many using a clause', async ({ fs, assert }) => {
const app = new AppFactory().create(fs.baseUrl, () => {})
await app.init()
const db = getDb()
const adapter = ormAdapter(db)

const BaseModel = getBaseModel(adapter)

class User extends BaseModel {
@column({ isPrimary: true })
declare id: number

@column()
declare username: string

@column()
declare email: string
}

await db
.insertQuery()
.table('users')
.multiInsert([{ username: 'virk' }, { username: 'nikk' }])

const users = await User.findManyBy({ points: 0 })
assert.lengthOf(users, 2)
assert.equal(users[0].$primaryKeyValue, 1)
assert.equal(users[1].$primaryKeyValue, 2)
})

test('find many using a key/value pair', async ({ fs, assert }) => {
const app = new AppFactory().create(fs.baseUrl, () => {})
await app.init()
const db = getDb()
const adapter = ormAdapter(db)

const BaseModel = getBaseModel(adapter)

class User extends BaseModel {
@column({ isPrimary: true })
declare id: number

@column()
declare username: string

@column()
declare email: string
}

await db
.insertQuery()
.table('users')
.multiInsert([{ username: 'virk' }, { username: 'nikk' }])

const users = await User.findManyBy('points', 0)

Check failure on line 3837 in test/orm/base_model.spec.ts

View workflow job for this annotation

GitHub Actions / typecheck

No overload matches this call.

Check failure on line 3837 in test/orm/base_model.spec.ts

View workflow job for this annotation

GitHub Actions / typecheck

No overload matches this call.
assert.lengthOf(users, 2)
assert.equal(users[0].$primaryKeyValue, 1)
assert.equal(users[1].$primaryKeyValue, 2)
})

test('return the existing row when search criteria matches', async ({ fs, assert }) => {
const app = new AppFactory().create(fs.baseUrl, () => {})
await app.init()
Expand Down

0 comments on commit b14a0e7

Please sign in to comment.