Skip to content

Commit 1c95bf1

Browse files
committed
Fix bug on select when calling with empty where
1 parent d00772d commit 1c95bf1

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

src/Builder.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ export class QueryBuilder<GenericResultWrapper> {
6161
return this.execute(q)
6262
},
6363
this._select({ ...params, limit: 1 }),
64-
typeof params.where === 'object' && !Array.isArray(params.where) && params.where.params
65-
? params.where.params
64+
typeof params.where === 'object' && !Array.isArray(params.where) && params.where?.params
65+
? params.where?.params
6666
: undefined,
6767
FetchTypes.ONE
6868
)
@@ -74,8 +74,8 @@ export class QueryBuilder<GenericResultWrapper> {
7474
return this.execute(q)
7575
},
7676
this._select(params),
77-
typeof params.where === 'object' && !Array.isArray(params.where) && params.where.params
78-
? params.where.params
77+
typeof params.where === 'object' && !Array.isArray(params.where) && params.where?.params
78+
? params.where?.params
7979
: undefined,
8080
FetchTypes.ALL
8181
)
@@ -99,10 +99,10 @@ export class QueryBuilder<GenericResultWrapper> {
9999
if (
100100
typeof params.onConflict?.where === 'object' &&
101101
!Array.isArray(params.onConflict?.where) &&
102-
params.onConflict?.where.params
102+
params.onConflict?.where?.params
103103
) {
104104
// 1 - on conflict where parameters
105-
args = args.concat(params.onConflict.where.params)
105+
args = args.concat(params.onConflict.where?.params)
106106
}
107107

108108
if (params.onConflict.data) {
@@ -135,8 +135,8 @@ export class QueryBuilder<GenericResultWrapper> {
135135
update<GenericResult = DefaultObject>(params: Update): Query<ArrayResult<GenericResultWrapper, GenericResult>> {
136136
let args = this._parse_arguments(params.data)
137137

138-
if (typeof params.where === 'object' && !Array.isArray(params.where) && params.where.params) {
139-
args = (params.where.params as Array<any>).concat(args)
138+
if (typeof params.where === 'object' && !Array.isArray(params.where) && params.where?.params) {
139+
args = (params.where?.params as Array<any>).concat(args)
140140
}
141141

142142
return new Query(
@@ -155,8 +155,8 @@ export class QueryBuilder<GenericResultWrapper> {
155155
return this.execute(q)
156156
},
157157
this._delete(params),
158-
typeof params.where === 'object' && !Array.isArray(params.where) && params.where.params
159-
? params.where.params
158+
typeof params.where === 'object' && !Array.isArray(params.where) && params.where?.params
159+
? params.where?.params
160160
: undefined,
161161
FetchTypes.ALL
162162
)
@@ -209,7 +209,7 @@ export class QueryBuilder<GenericResultWrapper> {
209209
if (
210210
typeof params.onConflict?.where === 'object' &&
211211
!Array.isArray(params.onConflict?.where) &&
212-
params.onConflict?.where.params
212+
params.onConflict?.where?.params
213213
) {
214214
index += (params.onConflict.where?.params).length
215215
}
@@ -246,8 +246,8 @@ export class QueryBuilder<GenericResultWrapper> {
246246

247247
_update(params: Update): string {
248248
const whereParamsLength: number =
249-
typeof params.where === 'object' && !Array.isArray(params.where) && params.where.params
250-
? Object.keys(params.where.params as Array<any>).length
249+
typeof params.where === 'object' && !Array.isArray(params.where) && params.where?.params
250+
? Object.keys(params.where?.params as Array<any>).length
251251
: 0
252252

253253
const set: Array<string> = []
@@ -310,7 +310,11 @@ export class QueryBuilder<GenericResultWrapper> {
310310

311311
if (typeof conditions === 'string') return ` WHERE ${conditions.toString()}`
312312

313-
return ` WHERE ${(conditions as Array<string>).join(' AND ')}`
313+
if ((conditions as Array<string>).length > 0) {
314+
return ` WHERE ${(conditions as Array<string>).join(' AND ')}`
315+
}
316+
317+
return ''
314318
}
315319

316320
_join(value?: Join | Array<Join>): string {

tests/builder/select.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,34 @@ describe('Select Builder', () => {
5656
expect(result.fetchType).toEqual('ONE')
5757
})
5858

59+
test('select with empty where', async () => {
60+
const result = new QuerybuilderTest().fetchOne({
61+
tableName: 'testTable',
62+
fields: '*',
63+
// @ts-ignore
64+
where: null,
65+
})
66+
67+
expect(trimQuery(result.query)).toEqual('SELECT * FROM testTable LIMIT 1')
68+
expect(result.arguments).toEqual(undefined)
69+
expect(result.fetchType).toEqual('ONE')
70+
})
71+
72+
test('select with empty where 2', async () => {
73+
const result = new QuerybuilderTest().fetchOne({
74+
tableName: 'testTable',
75+
fields: '*',
76+
where: {
77+
conditions: [],
78+
params: [],
79+
},
80+
})
81+
82+
expect(trimQuery(result.query)).toEqual('SELECT * FROM testTable LIMIT 1')
83+
expect(result.arguments).toEqual([])
84+
expect(result.fetchType).toEqual('ONE')
85+
})
86+
5987
test('select with simplified where list', async () => {
6088
const result = new QuerybuilderTest().fetchOne({
6189
tableName: 'testTable',

0 commit comments

Comments
 (0)