Skip to content

Commit 0f29b39

Browse files
authored
chore: use the entire kysely dialect in client options (#130)
1 parent 043161b commit 0f29b39

File tree

20 files changed

+62
-81
lines changed

20 files changed

+62
-81
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,10 @@ Now you can use the compiled TypeScript schema to instantiate a database client.
145145
import { ZenStackClient } from '@zenstackhq/runtime';
146146
import { schema } from './zenstack/schema';
147147
import SQLite from 'better-sqlite3';
148+
import { SqliteDialect } from 'kysely';
148149

149150
const client = new ZenStackClient(schema, {
150-
dialectConfig: { database: new SQLite('./dev.db') },
151+
dialect: new SqliteDialect({ database: new SQLite('./dev.db') }),
151152
});
152153
```
153154

@@ -156,13 +157,14 @@ const client = new ZenStackClient(schema, {
156157
```ts
157158
import { ZenStackClient } from '@zenstackhq/runtime';
158159
import { schema } from './zenstack/schema';
160+
import { PostgresDialect } from 'kysely';
159161
import { Pool } from 'pg';
160162
import { parseIntoClientConfig } from 'pg-connection-string';
161163

162164
const client = new ZenStackClient(schema, {
163-
dialectConfig: {
165+
dialect: new PostgresDialect({
164166
pool: new Pool(parseIntoClientConfig(process.env.DATABASE_URL)),
165-
},
167+
}),
166168
});
167169
```
168170

packages/cli/src/actions/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import { ZenStackClient } from '@zenstackhq/runtime';
5050
import { schema } from '${outputPath}/schema';
5151
5252
const client = new ZenStackClient(schema, {
53-
dialectConfig: { ... }
53+
dialect: { ... }
5454
});
5555
\`\`\`
5656
`);

packages/cli/src/actions/templates.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ model Post {
2727
`;
2828

2929
export const STARTER_MAIN_TS = `import { ZenStackClient } from '@zenstackhq/runtime';
30-
import { schema } from './zenstack/schema';
3130
import SQLite from 'better-sqlite3';
31+
import { SqliteDialect } from 'kysely';
32+
import { schema } from './zenstack/schema';
3233
3334
async function main() {
3435
const client = new ZenStackClient(schema, {
35-
dialectConfig: {
36+
dialect: new SqliteDialect({
3637
database: new SQLite('./zenstack/dev.db'),
37-
},
38+
}),
3839
});
3940
const user = await client.user.create({
4041
data: {

packages/runtime/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@
6969
"@paralleldrive/cuid2": "^2.2.2",
7070
"decimal.js": "^10.4.3",
7171
"json-stable-stringify": "^1.3.0",
72-
"kysely": "catalog:",
7372
"nanoid": "^5.0.9",
7473
"ts-pattern": "catalog:",
7574
"ulid": "^3.0.0",
7675
"uuid": "^11.0.5"
7776
},
7877
"peerDependencies": {
7978
"better-sqlite3": "^11.8.1",
79+
"kysely": "catalog:",
8080
"pg": "^8.13.1",
8181
"zod": "catalog:"
8282
},

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

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import { invariant, lowerCaseFirst } from '@zenstackhq/common-helpers';
2-
import type { QueryExecutor, SqliteDialectConfig } from 'kysely';
2+
import type { QueryExecutor } from 'kysely';
33
import {
44
CompiledQuery,
55
DefaultConnectionProvider,
66
DefaultQueryExecutor,
77
Kysely,
88
Log,
9-
PostgresDialect,
109
sql,
11-
SqliteDialect,
1210
type KyselyProps,
13-
type PostgresDialectConfig,
1411
} from 'kysely';
15-
import { match } from 'ts-pattern';
1612
import type { GetModels, ProcedureDef, SchemaDef } from '../schema';
1713
import type { AuthType } from '../schema/auth';
1814
import type { UnwrapTuplePromises } from '../utils/type-utils';
@@ -88,18 +84,17 @@ export class ClientImpl<Schema extends SchemaDef> {
8884
this.kyselyRaw = baseClient.kyselyRaw;
8985
this.auth = baseClient.auth;
9086
} else {
91-
const dialect = this.getKyselyDialect();
92-
const driver = new ZenStackDriver(dialect.createDriver(), new Log(this.$options.log ?? []));
93-
const compiler = dialect.createQueryCompiler();
94-
const adapter = dialect.createAdapter();
87+
const driver = new ZenStackDriver(options.dialect.createDriver(), new Log(this.$options.log ?? []));
88+
const compiler = options.dialect.createQueryCompiler();
89+
const adapter = options.dialect.createAdapter();
9590
const connectionProvider = new DefaultConnectionProvider(driver);
9691

9792
this.kyselyProps = {
9893
config: {
99-
dialect,
94+
dialect: options.dialect,
10095
log: this.$options.log,
10196
},
102-
dialect,
97+
dialect: options.dialect,
10398
driver,
10499
executor: executor ?? new ZenStackQueryExecutor(this, driver, compiler, adapter, connectionProvider),
105100
};
@@ -135,21 +130,6 @@ export class ClientImpl<Schema extends SchemaDef> {
135130
return new ClientImpl(this.schema, this.$options, this, executor);
136131
}
137132

138-
private getKyselyDialect() {
139-
return match(this.schema.provider.type)
140-
.with('sqlite', () => this.makeSqliteKyselyDialect())
141-
.with('postgresql', () => this.makePostgresKyselyDialect())
142-
.exhaustive();
143-
}
144-
145-
private makePostgresKyselyDialect(): PostgresDialect {
146-
return new PostgresDialect(this.options.dialectConfig as PostgresDialectConfig);
147-
}
148-
149-
private makeSqliteKyselyDialect(): SqliteDialect {
150-
return new SqliteDialect(this.options.dialectConfig as SqliteDialectConfig);
151-
}
152-
153133
// overload for interactive transaction
154134
$transaction<T>(
155135
callback: (tx: ClientContract<Schema>) => Promise<T>,

packages/runtime/src/client/options.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
import type { Expression, ExpressionBuilder, KyselyConfig, PostgresDialectConfig, SqliteDialectConfig } from 'kysely';
2-
import type { DataSourceProvider, GetModel, GetModels, ProcedureDef, SchemaDef } from '../schema';
3-
import type { Optional, PrependParameter } from '../utils/type-utils';
1+
import type { Dialect, Expression, ExpressionBuilder, KyselyConfig } from 'kysely';
2+
import type { GetModel, GetModels, ProcedureDef, SchemaDef } from '../schema';
3+
import type { PrependParameter } from '../utils/type-utils';
44
import type { ClientContract, CRUD, ProcedureFunc } from './contract';
55
import type { BaseCrudDialect } from './crud/dialects/base';
66
import type { RuntimePlugin } from './plugin';
77
import type { ToKyselySchema } from './query-builder';
88

9-
type DialectConfig<Provider extends DataSourceProvider> = Provider['type'] extends 'sqlite'
10-
? Optional<SqliteDialectConfig, 'database'>
11-
: Provider['type'] extends 'postgresql'
12-
? Optional<PostgresDialectConfig, 'pool'>
13-
: never;
14-
159
export type ZModelFunctionContext<Schema extends SchemaDef> = {
1610
dialect: BaseCrudDialect<Schema>;
1711
model: GetModels<Schema>;
@@ -29,9 +23,9 @@ export type ZModelFunction<Schema extends SchemaDef> = (
2923
*/
3024
export type ClientOptions<Schema extends SchemaDef> = {
3125
/**
32-
* Database dialect configuration.
26+
* Kysely dialect.
3327
*/
34-
dialectConfig: DialectConfig<Schema['provider']>;
28+
dialect: Dialect;
3529

3630
/**
3731
* Custom function definitions.

packages/runtime/test/client-api/computed-fields.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import { schema } from './schema';
5555
5656
async function main() {
5757
const client = new ZenStackClient(schema, {
58-
dialectConfig: {} as any,
58+
dialect: {} as any,
5959
computedFields: {
6060
User: {
6161
upperName: (eb) => eb.fn('upper', ['name']),
@@ -122,7 +122,7 @@ import { schema } from './schema';
122122
123123
async function main() {
124124
const client = new ZenStackClient(schema, {
125-
dialectConfig: {} as any,
125+
dialect: {} as any,
126126
computedFields: {
127127
User: {
128128
upperName: (eb) => eb.lit(null),

packages/runtime/test/client-api/default-values.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isCuid } from '@paralleldrive/cuid2';
22
import SQLite from 'better-sqlite3';
3+
import { SqliteDialect } from 'kysely';
34
import { isValid as isValidUlid } from 'ulid';
45
import { validate as isValidUuid } from 'uuid';
56
import { describe, expect, it } from 'vitest';
@@ -68,7 +69,7 @@ const schema = {
6869
describe('default values tests', () => {
6970
it('supports generators', async () => {
7071
const client = new ZenStackClient(schema, {
71-
dialectConfig: { database: new SQLite(':memory:') },
72+
dialect: new SqliteDialect({ database: new SQLite(':memory:') }),
7273
});
7374
await client.$pushSchema();
7475

packages/runtime/test/client-api/name-mapping.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import SQLite from 'better-sqlite3';
2+
import { SqliteDialect } from 'kysely';
23
import { describe, expect, it } from 'vitest';
34
import { ZenStackClient } from '../../src';
45
import { type SchemaDef, ExpressionUtils } from '../../src/schema';
@@ -59,7 +60,7 @@ describe('Name mapping tests', () => {
5960

6061
it('works with model and implicit field mapping', async () => {
6162
const client = new ZenStackClient(schema, {
62-
dialectConfig: { database: new SQLite(':memory:') },
63+
dialect: new SqliteDialect({ database: new SQLite(':memory:') }),
6364
});
6465
await client.$pushSchema();
6566
const r1 = await client.foo.create({
@@ -91,7 +92,7 @@ describe('Name mapping tests', () => {
9192

9293
it('works with explicit field mapping', async () => {
9394
const client = new ZenStackClient(schema, {
94-
dialectConfig: { database: new SQLite(':memory:') },
95+
dialect: new SqliteDialect({ database: new SQLite(':memory:') }),
9596
});
9697
await client.$pushSchema();
9798
const r1 = await client.foo.create({

packages/runtime/test/plugin/kysely-on-query.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import SQLite from 'better-sqlite3';
2-
import { InsertQueryNode, Kysely, PrimitiveValueListNode, ValuesNode, type QueryResult } from 'kysely';
2+
import { InsertQueryNode, Kysely, PrimitiveValueListNode, SqliteDialect, ValuesNode, type QueryResult } from 'kysely';
33
import { beforeEach, describe, expect, it } from 'vitest';
44
import { ZenStackClient, type ClientContract } from '../../src/client';
55
import { schema } from '../schemas/basic';
@@ -9,7 +9,7 @@ describe('Kysely onQuery tests', () => {
99

1010
beforeEach(async () => {
1111
_client = new ZenStackClient(schema, {
12-
dialectConfig: { database: new SQLite(':memory:') },
12+
dialect: new SqliteDialect({ database: new SQLite(':memory:') }),
1313
});
1414
await _client.$pushSchema();
1515
});

0 commit comments

Comments
 (0)