Skip to content

Commit cd270f2

Browse files
committed
refactor
1 parent 01de42e commit cd270f2

File tree

7 files changed

+174
-288
lines changed

7 files changed

+174
-288
lines changed

packages/runtime/src/client/crud/dialects/base-dialect.ts

Lines changed: 140 additions & 234 deletions
Large diffs are not rendered by default.

packages/runtime/src/client/crud/dialects/postgresql.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export class PostgresCrudDialect<Schema extends SchemaDef> extends BaseCrudDiale
153153

154154
if (this.canJoinWithoutNestedSelect(relationModelDef, payload)) {
155155
// build join directly
156-
tbl = this.buildModelSelect(eb, relationModel, relationSelectName, payload, false);
156+
tbl = this.buildModelSelect(relationModel, relationSelectName, payload, false);
157157

158158
// parent join filter
159159
tbl = this.buildRelationJoinFilter(
@@ -167,13 +167,7 @@ export class PostgresCrudDialect<Schema extends SchemaDef> extends BaseCrudDiale
167167
} else {
168168
// join with a nested query
169169
tbl = eb.selectFrom(() => {
170-
let subQuery = this.buildModelSelect(
171-
eb,
172-
relationModel,
173-
`${relationSelectName}$t`,
174-
payload,
175-
true,
176-
);
170+
let subQuery = this.buildModelSelect(relationModel, `${relationSelectName}$t`, payload, true);
177171

178172
// parent join filter
179173
subQuery = this.buildRelationJoinFilter(
@@ -237,7 +231,7 @@ export class PostgresCrudDialect<Schema extends SchemaDef> extends BaseCrudDiale
237231
} else {
238232
const joinPairs = buildJoinPairs(this.schema, model, parentAlias, relationField, relationModelAlias);
239233
query = query.where((eb) =>
240-
this.and(eb, ...joinPairs.map(([left, right]) => eb(sql.ref(left), '=', sql.ref(right)))),
234+
this.and(...joinPairs.map(([left, right]) => eb(sql.ref(left), '=', sql.ref(right)))),
241235
);
242236
}
243237
return query;
@@ -303,10 +297,7 @@ export class PostgresCrudDialect<Schema extends SchemaDef> extends BaseCrudDiale
303297
...Object.entries(relationModelDef.fields)
304298
.filter(([, value]) => !value.relation)
305299
.filter(([name]) => !(typeof payload === 'object' && (payload.omit as any)?.[name] === true))
306-
.map(([field]) => [
307-
sql.lit(field),
308-
this.fieldRef(relationModel, field, eb, relationModelAlias, false),
309-
])
300+
.map(([field]) => [sql.lit(field), this.fieldRef(relationModel, field, relationModelAlias, false)])
310301
.flatMap((v) => v),
311302
);
312303
} else if (payload.select) {
@@ -329,7 +320,7 @@ export class PostgresCrudDialect<Schema extends SchemaDef> extends BaseCrudDiale
329320
? // reference the synthesized JSON field
330321
eb.ref(`${parentResultName}$${field}.$data`)
331322
: // reference a plain field
332-
this.fieldRef(relationModel, field, eb, relationModelAlias, false);
323+
this.fieldRef(relationModel, field, relationModelAlias, false);
333324
return [sql.lit(field), fieldValue];
334325
}
335326
})
@@ -396,8 +387,8 @@ export class PostgresCrudDialect<Schema extends SchemaDef> extends BaseCrudDiale
396387
return query;
397388
}
398389

399-
override buildJsonObject(eb: ExpressionBuilder<any, any>, value: Record<string, Expression<unknown>>) {
400-
return eb.fn(
390+
override buildJsonObject(value: Record<string, Expression<unknown>>) {
391+
return this.eb.fn(
401392
'jsonb_build_object',
402393
Object.entries(value).flatMap(([key, value]) => [sql.lit(key), value]),
403394
);
@@ -415,11 +406,8 @@ export class PostgresCrudDialect<Schema extends SchemaDef> extends BaseCrudDiale
415406
return true;
416407
}
417408

418-
override buildArrayLength(
419-
eb: ExpressionBuilder<any, any>,
420-
array: Expression<unknown>,
421-
): ExpressionWrapper<any, any, number> {
422-
return eb.fn('array_length', [array]);
409+
override buildArrayLength(array: Expression<unknown>): ExpressionWrapper<any, any, number> {
410+
return this.eb.fn('array_length', [array]);
423411
}
424412

425413
override buildArrayLiteralSQL(values: unknown[]): string {

packages/runtime/src/client/crud/dialects/sqlite.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export class SqliteCrudDialect<Schema extends SchemaDef> extends BaseCrudDialect
155155

156156
if (this.canJoinWithoutNestedSelect(relationModelDef, payload)) {
157157
// join without needing a nested select on relation model
158-
tbl = this.buildModelSelect(eb, relationModel, subQueryName, payload, false);
158+
tbl = this.buildModelSelect(relationModel, subQueryName, payload, false);
159159

160160
// add parent join filter
161161
tbl = this.buildRelationJoinFilter(tbl, model, relationField, subQueryName, parentAlias);
@@ -166,7 +166,7 @@ export class SqliteCrudDialect<Schema extends SchemaDef> extends BaseCrudDialect
166166
const selectModelAlias = `${parentAlias}$${relationField}$sub`;
167167

168168
// select all fields
169-
let selectModelQuery = this.buildModelSelect(eb, relationModel, selectModelAlias, payload, true);
169+
let selectModelQuery = this.buildModelSelect(relationModel, selectModelAlias, payload, true);
170170

171171
// add parent join filter
172172
selectModelQuery = this.buildRelationJoinFilter(
@@ -203,10 +203,7 @@ export class SqliteCrudDialect<Schema extends SchemaDef> extends BaseCrudDialect
203203
...Object.entries(relationModelDef.fields)
204204
.filter(([, value]) => !value.relation)
205205
.filter(([name]) => !(typeof payload === 'object' && (payload.omit as any)?.[name] === true))
206-
.map(([field]) => [
207-
sql.lit(field),
208-
this.fieldRef(relationModel, field, eb, subQueryName, false),
209-
])
206+
.map(([field]) => [sql.lit(field), this.fieldRef(relationModel, field, subQueryName, false)])
210207
.flatMap((v) => v),
211208
);
212209
} else if (payload.select) {
@@ -237,7 +234,7 @@ export class SqliteCrudDialect<Schema extends SchemaDef> extends BaseCrudDialect
237234
} else {
238235
return [
239236
sql.lit(field),
240-
this.fieldRef(relationModel, field, eb, subQueryName, false) as ArgsType,
237+
this.fieldRef(relationModel, field, subQueryName, false) as ArgsType,
241238
];
242239
}
243240
}
@@ -345,8 +342,8 @@ export class SqliteCrudDialect<Schema extends SchemaDef> extends BaseCrudDialect
345342
return query;
346343
}
347344

348-
override buildJsonObject(eb: ExpressionBuilder<any, any>, value: Record<string, Expression<unknown>>) {
349-
return eb.fn(
345+
override buildJsonObject(value: Record<string, Expression<unknown>>) {
346+
return this.eb.fn(
350347
'json_object',
351348
Object.entries(value).flatMap(([key, value]) => [sql.lit(key), value]),
352349
);
@@ -364,11 +361,8 @@ export class SqliteCrudDialect<Schema extends SchemaDef> extends BaseCrudDialect
364361
return false;
365362
}
366363

367-
override buildArrayLength(
368-
eb: ExpressionBuilder<any, any>,
369-
array: Expression<unknown>,
370-
): ExpressionWrapper<any, any, number> {
371-
return eb.fn('json_array_length', [array]);
364+
override buildArrayLength(array: Expression<unknown>): ExpressionWrapper<any, any, number> {
365+
return this.eb.fn('json_array_length', [array]);
372366
}
373367

374368
override buildArrayLiteralSQL(_values: unknown[]): string {

packages/runtime/src/client/crud/operations/aggregate.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { ExpressionBuilder } from 'kysely';
21
import { sql } from 'kysely';
32
import { match } from 'ts-pattern';
43
import type { SchemaDef } from '../../../schema';
@@ -18,8 +17,8 @@ export class AggregateOperationHandler<Schema extends SchemaDef> extends BaseOpe
1817

1918
// table and where
2019
let subQuery = this.dialect
21-
.buildSelectModel(eb as ExpressionBuilder<any, any>, this.model, this.model)
22-
.where((eb1) => this.dialect.buildFilter(eb1, this.model, this.model, parsedArgs?.where));
20+
.buildSelectModel(this.model, this.model)
21+
.where(() => this.dialect.buildFilter(this.model, this.model, parsedArgs?.where));
2322

2423
// select fields: collect fields from aggregation body
2524
const selectedFields: string[] = [];

packages/runtime/src/client/crud/operations/base.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
143143
args: FindArgs<Schema, GetModels<Schema>, true> | undefined,
144144
): Promise<any[]> {
145145
// table
146-
let query = this.dialect.buildSelectModel(expressionBuilder(), model, model);
146+
let query = this.dialect.buildSelectModel(model, model);
147147

148148
if (args) {
149149
query = this.dialect.buildFilterSortTake(model, args, query, model);
@@ -1043,7 +1043,7 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
10431043
const idFields = requireIdFields(this.schema, model);
10441044
const query = kysely
10451045
.updateTable(model)
1046-
.where((eb) => this.dialect.buildFilter(eb, model, model, combinedWhere))
1046+
.where(() => this.dialect.buildFilter(model, model, combinedWhere))
10471047
.set(updateFields)
10481048
.returning(idFields as any)
10491049
.modifyEnd(
@@ -1155,7 +1155,7 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
11551155
const key = Object.keys(payload)[0];
11561156
const value = this.dialect.transformPrimitive(payload[key!], fieldDef.type as BuiltinType, false);
11571157
const eb = expressionBuilder<any, any>();
1158-
const fieldRef = this.dialect.fieldRef(model, field, eb);
1158+
const fieldRef = this.dialect.fieldRef(model, field);
11591159

11601160
return match(key)
11611161
.with('set', () => value)
@@ -1178,7 +1178,7 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
11781178
const key = Object.keys(payload)[0];
11791179
const value = this.dialect.transformPrimitive(payload[key!], fieldDef.type as BuiltinType, true);
11801180
const eb = expressionBuilder<any, any>();
1181-
const fieldRef = this.dialect.fieldRef(model, field, eb);
1181+
const fieldRef = this.dialect.fieldRef(model, field);
11821182

11831183
return match(key)
11841184
.with('set', () => value)
@@ -1273,7 +1273,7 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
12731273
if (!shouldFallbackToIdFilter) {
12741274
// simple filter
12751275
query = query
1276-
.where((eb) => this.dialect.buildFilter(eb, model, model, where))
1276+
.where(() => this.dialect.buildFilter(model, model, where))
12771277
.$if(limit !== undefined, (qb) => qb.limit(limit!));
12781278
} else {
12791279
query = query.where((eb) =>
@@ -1284,8 +1284,8 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
12841284
),
12851285
'in',
12861286
this.dialect
1287-
.buildSelectModel(eb, filterModel, filterModel)
1288-
.where(this.dialect.buildFilter(eb, filterModel, filterModel, where))
1287+
.buildSelectModel(filterModel, filterModel)
1288+
.where(this.dialect.buildFilter(filterModel, filterModel, where))
12891289
.select(this.buildIdFieldRefs(kysely, filterModel))
12901290
.$if(limit !== undefined, (qb) => qb.limit(limit!)),
12911291
),
@@ -1968,7 +1968,7 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
19681968
}
19691969

19701970
if (!needIdFilter) {
1971-
query = query.where((eb) => this.dialect.buildFilter(eb, model, model, where));
1971+
query = query.where(() => this.dialect.buildFilter(model, model, where));
19721972
} else {
19731973
query = query.where((eb) =>
19741974
eb(
@@ -1978,8 +1978,8 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
19781978
),
19791979
'in',
19801980
this.dialect
1981-
.buildSelectModel(eb, filterModel, filterModel)
1982-
.where((eb) => this.dialect.buildFilter(eb, filterModel, filterModel, where))
1981+
.buildSelectModel(filterModel, filterModel)
1982+
.where(() => this.dialect.buildFilter(filterModel, filterModel, where))
19831983
.select(this.buildIdFieldRefs(kysely, filterModel))
19841984
.$if(limit !== undefined, (qb) => qb.limit(limit!)),
19851985
),

packages/runtime/src/client/crud/operations/count.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { ExpressionBuilder } from 'kysely';
21
import { sql } from 'kysely';
32
import type { SchemaDef } from '../../../schema';
43
import { BaseOperationHandler } from './base';
@@ -16,8 +15,8 @@ export class CountOperationHandler<Schema extends SchemaDef> extends BaseOperati
1615
// nested query for filtering and pagination
1716

1817
let subQuery = this.dialect
19-
.buildSelectModel(eb as ExpressionBuilder<any, any>, this.model, this.model)
20-
.where((eb1) => this.dialect.buildFilter(eb1, this.model, this.model, parsedArgs?.where));
18+
.buildSelectModel(this.model, this.model)
19+
.where(() => this.dialect.buildFilter(this.model, this.model, parsedArgs?.where));
2120

2221
if (parsedArgs?.select && typeof parsedArgs.select === 'object') {
2322
// select fields

packages/runtime/src/client/crud/operations/group-by.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class GroupByOperationHandler<Schema extends SchemaDef> extends BaseOpera
1919
let subQuery = eb
2020
.selectFrom(this.model)
2121
.selectAll()
22-
.where((eb1) => this.dialect.buildFilter(eb1, this.model, this.model, parsedArgs?.where));
22+
.where(() => this.dialect.buildFilter(this.model, this.model, parsedArgs?.where));
2323

2424
// skip & take
2525
const skip = parsedArgs?.skip;
@@ -44,7 +44,7 @@ export class GroupByOperationHandler<Schema extends SchemaDef> extends BaseOpera
4444
return subQuery.as('$sub');
4545
});
4646

47-
const fieldRef = (field: string) => this.dialect.fieldRef(this.model, field, expressionBuilder(), '$sub');
47+
const fieldRef = (field: string) => this.dialect.fieldRef(this.model, field, '$sub');
4848

4949
// groupBy
5050
const bys = typeof parsedArgs.by === 'string' ? [parsedArgs.by] : (parsedArgs.by as string[]);
@@ -56,7 +56,7 @@ export class GroupByOperationHandler<Schema extends SchemaDef> extends BaseOpera
5656
}
5757

5858
if (parsedArgs.having) {
59-
query = query.having((eb) => this.dialect.buildFilter(eb, this.model, '$sub', parsedArgs.having));
59+
query = query.having(() => this.dialect.buildFilter(this.model, '$sub', parsedArgs.having));
6060
}
6161

6262
// select all by fields

0 commit comments

Comments
 (0)