Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add whereIn for SelectBuilder #97

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/modularBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,71 @@ export class SelectBuilder<GenericResultWrapper, GenericResult = DefaultReturnOb
)
}

whereIn<T extends string | Array<string>, P extends T extends Array<string> ? Primitive[][] : Primitive[]>(
fields: T,
values: P
): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync> {
let whereInCondition: string
let whereInParams: Primitive[]

const seperateWithComma = (prev: string, next: string) => prev + ', ' + next

// if we have no values, we no-op
if (values.length === 0) {
return new SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>(
{
...this._options,
},
this._fetchAll,
this._fetchOne
)
}

if (!Array.isArray(fields)) {
// at this point, we know that it's a string
whereInCondition = `(${fields}) IN (VALUES `

whereInCondition += values.map(() => '(?)').reduce(seperateWithComma)
whereInCondition += ')'
// if it's not an array, we can assume that values is whereInParams[]
whereInParams = values as Primitive[]
} else {
// NOTE(lduarte): we assume that this is const throughout the values list, if it's not, oh well garbage in, garbage out
const fieldLength = fields.length

whereInCondition = `(${fields.map((val) => val).reduce(seperateWithComma)}) IN (VALUES `

const valuesString = `(${[...new Array(fieldLength).keys()].map(() => '?').reduce(seperateWithComma)})`

whereInCondition += [...new Array(fieldLength).keys()].map(() => valuesString).reduce(seperateWithComma)
whereInCondition += ')'
// finally, flatten the list since the whereInParams are in a single list
whereInParams = values.flat()
}

let conditions: string | Array<string> = [whereInCondition]
let params: Primitive[] = whereInParams
if ((this._options.where as any)?.conditions) {
conditions = (this._options.where as any)?.conditions.concat(conditions)
}

if ((this._options.where as any)?.params) {
params = (this._options.where as any)?.params.concat(params)
}

return new SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>(
{
...this._options,
where: {
conditions: conditions,
params: params,
},
},
this._fetchAll,
this._fetchOne
)
}

join(join: SelectAll['join']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync> {
return this._parseArray('join', this._options.join, join)
}
Expand Down
69 changes: 69 additions & 0 deletions tests/unit/select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,4 +790,73 @@ describe('Select Builder', () => {
expect(result.fetchType).toEqual('ALL')
}
})

it('select whereIn single field', async () => {
const result = new QuerybuilderTest().select('testTable').whereIn('field', [1, 2, 3, 4]).getQueryAll()

expect(result.query).toEqual('SELECT * FROM testTable WHERE (field) IN (VALUES (?), (?), (?), (?))')
expect(result.arguments).toEqual([1, 2, 3, 4])
expect(result.fetchType).toEqual('ALL')
})

it('select whereIn multiple fields', async () => {
const result = new QuerybuilderTest()
.select('testTable')
.whereIn(
['field', 'test'],
[
['somebody', 1],
['once', 2],
['told', 3],
['me', 4],
]
)
.getQueryAll()

expect(result.query).toEqual('SELECT * FROM testTable WHERE (field, test) IN (VALUES (?, ?), (?, ?))')
expect(result.arguments).toEqual(['somebody', 1, 'once', 2, 'told', 3, 'me', 4])
expect(result.fetchType).toEqual('ALL')
})

it('select whereIn multiple fields with another where', async () => {
const result = new QuerybuilderTest()
.select('testTable')
.where('commited = ?', 1)
.whereIn(
['field', 'test'],
[
['somebody', 1],
['once', 2],
['told', 3],
['me', 4],
]
)
.getQueryAll()

expect(result.query).toEqual(
'SELECT * FROM testTable WHERE (commited = ?) AND ((field, test) IN (VALUES (?, ?), (?, ?)))'
)
expect(result.arguments).toEqual([1, 'somebody', 1, 'once', 2, 'told', 3, 'me', 4])
expect(result.fetchType).toEqual('ALL')

const result2 = new QuerybuilderTest()
.select('testTable')
.whereIn(
['field', 'test'],
[
['somebody', 1],
['once', 2],
['told', 3],
['me', 4],
]
)
.where('commited = ?', 1)
.getQueryAll()

expect(result2.query).toEqual(
'SELECT * FROM testTable WHERE ((field, test) IN (VALUES (?, ?), (?, ?))) AND (commited = ?)'
)
expect(result2.arguments).toEqual(['somebody', 1, 'once', 2, 'told', 3, 'me', 4, 1])
expect(result2.fetchType).toEqual('ALL')
})
})