Skip to content

Commit 1667ce1

Browse files
authored
perf: improve type checking performance (magically...) (#122)
1 parent 330be71 commit 1667ce1

File tree

6 files changed

+40
-23
lines changed

6 files changed

+40
-23
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,23 +255,33 @@ export class ClientImpl<Schema extends SchemaDef> {
255255
}
256256

257257
$use(plugin: RuntimePlugin<Schema>) {
258-
const newOptions = {
258+
// tsc perf
259+
const newPlugins: RuntimePlugin<Schema>[] = [...(this.$options.plugins ?? []), plugin];
260+
const newOptions: ClientOptions<Schema> = {
259261
...this.options,
260-
plugins: [...(this.options.plugins ?? []), plugin],
262+
plugins: newPlugins,
261263
};
262264
return new ClientImpl<Schema>(this.schema, newOptions, this);
263265
}
264266

265267
$unuse(pluginId: string) {
266-
const newOptions = {
268+
// tsc perf
269+
const newPlugins: RuntimePlugin<Schema>[] = [];
270+
for (const plugin of this.options.plugins ?? []) {
271+
if (plugin.id !== pluginId) {
272+
newPlugins.push(plugin);
273+
}
274+
}
275+
const newOptions: ClientOptions<Schema> = {
267276
...this.options,
268-
plugins: this.options.plugins?.filter((p) => p.id !== pluginId),
277+
plugins: newPlugins,
269278
};
270279
return new ClientImpl<Schema>(this.schema, newOptions, this);
271280
}
272281

273282
$unuseAll() {
274-
const newOptions = {
283+
// tsc perf
284+
const newOptions: ClientOptions<Schema> = {
275285
...this.options,
276286
plugins: [] as RuntimePlugin<Schema>[],
277287
};
@@ -388,7 +398,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
388398
for (const plugin of plugins) {
389399
if (plugin.onQuery && typeof plugin.onQuery === 'object') {
390400
// for each model key or "$allModels"
391-
for (const [_model, modelHooks] of Object.entries(plugin.onQuery)) {
401+
for (const [_model, modelHooks] of Object.entries<any>(plugin.onQuery)) {
392402
if (_model === lowerCaseFirst(model) || _model === '$allModels') {
393403
if (modelHooks && typeof modelHooks === 'object') {
394404
// for each operation key or "$allOperations"

packages/runtime/src/client/executor/zenstack-query-executor.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import type { GetModels, SchemaDef } from '../../schema';
2525
import { type ClientImpl } from '../client-impl';
2626
import type { ClientContract } from '../contract';
2727
import { InternalError, QueryError } from '../errors';
28-
import type { MutationInterceptionFilterResult } from '../plugin';
28+
import type { MutationInterceptionFilterResult, OnKyselyQueryCallback } from '../plugin';
2929
import { QueryNameMapper } from './name-mapper';
3030
import type { ZenStackDriver } from './zenstack-driver';
3131

@@ -113,10 +113,13 @@ export class ZenStackQueryExecutor<Schema extends SchemaDef> extends DefaultQuer
113113
// return this.executeWithTransaction(() => callback(p));
114114
// };
115115

116-
const hooks =
117-
this.options.plugins
118-
?.filter((plugin) => typeof plugin.onKyselyQuery === 'function')
119-
.map((plugin) => plugin.onKyselyQuery!.bind(plugin)) ?? [];
116+
const hooks: OnKyselyQueryCallback<Schema>[] = [];
117+
// tsc perf
118+
for (const plugin of this.client.$options.plugins ?? []) {
119+
if (plugin.onKyselyQuery) {
120+
hooks.push(plugin.onKyselyQuery.bind(plugin));
121+
}
122+
}
120123

121124
for (const hook of hooks) {
122125
const _proceed = proceed;

packages/runtime/src/client/plugin.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ export type OnKyselyQueryArgs<Schema extends SchemaDef> = {
9696

9797
export type ProceedKyselyQueryFunction = (query: RootOperationNode) => Promise<QueryResult<any>>;
9898

99+
export type OnKyselyQueryCallback<Schema extends SchemaDef> = (
100+
args: OnKyselyQueryArgs<Schema>,
101+
) => Promise<QueryResult<UnknownRow>>;
102+
99103
/**
100104
* ZenStack runtime plugin.
101105
*/
@@ -123,7 +127,7 @@ export interface RuntimePlugin<Schema extends SchemaDef = SchemaDef> {
123127
/**
124128
* Intercepts a Kysely query.
125129
*/
126-
onKyselyQuery?: (args: OnKyselyQueryArgs<Schema>) => Promise<QueryResult<UnknownRow>>;
130+
onKyselyQuery?: OnKyselyQueryCallback<Schema>;
127131

128132
/**
129133
* This callback determines whether a mutation should be intercepted, and if so,

packages/runtime/test/client-api/client-specs.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ export function createClientSpecs(dbName: string, logQueries = false, providers:
1515
{
1616
provider: 'sqlite' as const,
1717
schema: getSchema('sqlite'),
18-
createClient: async () => {
19-
const client = await makeSqliteClient(getSchema('sqlite'), {
18+
createClient: async (): Promise<ClientContract<typeof schema>> => {
19+
// tsc perf
20+
return makeSqliteClient<any>(getSchema('sqlite'), {
2021
log: logQueries ? logger('sqlite') : undefined,
21-
});
22-
return client as ClientContract<typeof schema>;
22+
}) as unknown as ClientContract<typeof schema>;
2323
},
2424
},
2525
]
@@ -29,11 +29,11 @@ export function createClientSpecs(dbName: string, logQueries = false, providers:
2929
{
3030
provider: 'postgresql' as const,
3131
schema: getSchema('postgresql'),
32-
createClient: async () => {
33-
const client = await makePostgresClient(getSchema('postgresql'), dbName, {
32+
createClient: async (): Promise<ClientContract<typeof schema>> => {
33+
// tsc perf
34+
return makePostgresClient<any>(getSchema('postgresql'), dbName, {
3435
log: logQueries ? logger('postgresql') : undefined,
35-
});
36-
return client as unknown as ClientContract<typeof schema>;
36+
}) as unknown as ClientContract<typeof schema>;
3737
},
3838
},
3939
]

packages/runtime/test/policy/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ export async function createPolicyTestClient<Schema extends SchemaDef>(
2020
{
2121
...options,
2222
plugins: [new PolicyPlugin()],
23-
} as CreateTestClientOptions<SchemaDef>,
23+
} as any,
2424
);
2525
}

packages/runtime/test/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type PostgresSchema = SchemaDef & { provider: { type: 'postgresql' } };
1717
export async function makeSqliteClient<Schema extends SqliteSchema>(
1818
schema: Schema,
1919
extraOptions?: Partial<ClientOptions<Schema>>,
20-
) {
20+
): Promise<ClientContract<Schema>> {
2121
const client = new ZenStackClient(schema, {
2222
...extraOptions,
2323
dialectConfig: { database: new SQLite(':memory:') },
@@ -37,7 +37,7 @@ export async function makePostgresClient<Schema extends PostgresSchema>(
3737
schema: Schema,
3838
dbName: string,
3939
extraOptions?: Partial<ClientOptions<Schema>>,
40-
) {
40+
): Promise<ClientContract<Schema>> {
4141
invariant(dbName, 'dbName is required');
4242
const pgClient = new PGClient(TEST_PG_CONFIG);
4343
await pgClient.connect();

0 commit comments

Comments
 (0)