Skip to content

Commit 50ec1be

Browse files
authored
fix(orm): update to kysely 0.28.x and simplify typing (#380)
* fix(orm): update to kysely 0.28.x and simplify typing * fix deprecation warning * fix `orderBy` deprecation * fix tests
1 parent 0d1faf1 commit 50ec1be

File tree

19 files changed

+272
-275
lines changed

19 files changed

+272
-275
lines changed

packages/orm/src/client/client-impl.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
type KyselyProps,
1212
} from 'kysely';
1313
import type { GetModels, ProcedureDef, SchemaDef } from '../schema';
14+
import type { AnyKysely } from '../utils/kysely-utils';
1415
import type { UnwrapTuplePromises } from '../utils/type-utils';
1516
import type {
1617
AuthType,
@@ -53,7 +54,7 @@ export const ZenStackClient = function <Schema extends SchemaDef>(
5354

5455
export class ClientImpl<Schema extends SchemaDef> {
5556
private kysely: ToKysely<Schema>;
56-
private kyselyRaw: ToKysely<any>;
57+
private kyselyRaw: AnyKysely;
5758
public readonly $options: ClientOptions<Schema>;
5859
public readonly $schema: Schema;
5960
readonly kyselyProps: KyselyProps;
@@ -192,7 +193,7 @@ export class ClientImpl<Schema extends SchemaDef> {
192193
arg: ZenStackPromise<Schema, any>[],
193194
options?: { isolationLevel?: TransactionIsolationLevel },
194195
) {
195-
const execute = async (tx: Kysely<any>) => {
196+
const execute = async (tx: AnyKysely) => {
196197
const txClient = new ClientImpl<Schema>(this.schema, this.$options, this);
197198
txClient.kysely = tx;
198199
const result: any[] = [];
@@ -210,7 +211,7 @@ export class ClientImpl<Schema extends SchemaDef> {
210211
if (options?.isolationLevel) {
211212
txBuilder = txBuilder.setIsolationLevel(options.isolationLevel);
212213
}
213-
return txBuilder.execute((tx) => execute(tx as Kysely<any>));
214+
return txBuilder.execute((tx) => execute(tx as AnyKysely));
214215
}
215216
}
216217

packages/orm/src/client/contract.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type Decimal from 'decimal.js';
22
import { type GetModels, type IsDelegateModel, type ProcedureDef, type SchemaDef } from '../schema';
3+
import type { AnyKysely } from '../utils/kysely-utils';
34
import type { OrUndefinedIf, Simplify, UnwrapTuplePromises } from '../utils/type-utils';
45
import type { TRANSACTION_UNSUPPORTED_METHODS } from './constants';
56
import type {
@@ -116,7 +117,7 @@ export type ClientContract<Schema extends SchemaDef> = {
116117
/**
117118
* The raw Kysely query builder without any ZenStack enhancements.
118119
*/
119-
readonly $qbRaw: ToKysely<any>;
120+
readonly $qbRaw: AnyKysely;
120121

121122
/**
122123
* Starts an interactive transaction.

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
6464
}
6565

6666
buildFilterSortTake(
67-
model: GetModels<Schema>,
67+
model: string,
6868
args: FindArgs<Schema, GetModels<Schema>, true>,
6969
query: SelectQueryBuilder<any, any, {}>,
7070
modelAlias: string,
@@ -767,7 +767,7 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
767767
invariant(v === 'asc' || v === 'desc', `invalid orderBy value for field "${field}"`);
768768
result = result.orderBy(
769769
(eb) => aggregate(eb, buildFieldRef(model, k, modelAlias), field as AGGREGATE_OPERATORS),
770-
sql.raw(this.negateSort(v, negated)),
770+
this.negateSort(v, negated),
771771
);
772772
}
773773
continue;
@@ -780,7 +780,7 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
780780
invariant(v === 'asc' || v === 'desc', `invalid orderBy value for field "${field}"`);
781781
result = result.orderBy(
782782
(eb) => eb.fn.count(buildFieldRef(model, k, modelAlias)),
783-
sql.raw(this.negateSort(v, negated)),
783+
this.negateSort(v, negated),
784784
);
785785
}
786786
continue;
@@ -803,10 +803,12 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
803803
(value.sort === 'asc' || value.sort === 'desc') &&
804804
(value.nulls === 'first' || value.nulls === 'last')
805805
) {
806-
result = result.orderBy(
807-
fieldRef,
808-
sql.raw(`${this.negateSort(value.sort, negated)} nulls ${value.nulls}`),
809-
);
806+
result = result.orderBy(fieldRef, (ob) => {
807+
const dir = this.negateSort(value.sort, negated);
808+
ob = dir === 'asc' ? ob.asc() : ob.desc();
809+
ob = value.nulls === 'first' ? ob.nullsFirst() : ob.nullsLast();
810+
return ob;
811+
});
810812
}
811813
} else {
812814
// order by relation

0 commit comments

Comments
 (0)