Skip to content

Commit f9c45f7

Browse files
committed
chore: simplify plugin contract to improve tsc performance
1 parent ac43d58 commit f9c45f7

File tree

13 files changed

+1127
-1248
lines changed

13 files changed

+1127
-1248
lines changed

.vscode/launch.json

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@
1010
"request": "launch",
1111
"skipFiles": ["<node_internals>/**"],
1212
"type": "node",
13-
"args": [
14-
"generate",
15-
"--schema",
16-
"${workspaceFolder}/samples/blog/zenstack/schema.zmodel"
17-
]
13+
"args": ["generate", "--schema", "${workspaceFolder}/samples/blog/zenstack/schema.zmodel"]
1814
},
1915
{
2016
"name": "Debug with TSX",
@@ -44,15 +40,14 @@
4440

4541
// Ignore all dependencies (optional)
4642
"${workspaceFolder}/node_modules/**"
47-
]
43+
],
44+
"cwd": "${fileDirname}"
4845
},
4946
{
5047
"name": "Run Extension",
5148
"type": "extensionHost",
5249
"request": "launch",
53-
"args": [
54-
"--extensionDevelopmentPath=${workspaceFolder}/packages/ide/vscode"
55-
],
50+
"args": ["--extensionDevelopmentPath=${workspaceFolder}/packages/ide/vscode"],
5651
"sourceMaps": true,
5752
"outFiles": ["${workspaceFolder}/packages/ide/vscode/dist/**/*.js"]
5853
},

README.md

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,11 @@ ORM query interception allows you to intercept the high-level ORM API calls. The
289289
```ts
290290
db.$use({
291291
id: 'cost-logger',
292-
onQuery: {
293-
$allModels: {
294-
$allOperations: async ({ model, operation, args, query }) => {
295-
const start = Date.now();
296-
const result = await query(args);
297-
console.log(`[cost] ${model} ${operation} took ${Date.now() - start}ms`);
298-
return result;
299-
},
300-
},
292+
onQuery: async ({ model, operation, args, query }) => {
293+
const start = Date.now();
294+
const result = await query(args);
295+
console.log(`[cost] ${model} ${operation} took ${Date.now() - start}ms`);
296+
return result;
301297
},
302298
});
303299
```
@@ -333,11 +329,14 @@ Another popular interception use case is, instead of intercepting calls, "listen
333329
```ts
334330
db.$use({
335331
id: 'mutation-hook-plugin',
336-
beforeEntityMutation({ model, action }) {
337-
console.log(`Before ${model} ${action}`);
338-
},
339-
afterEntityMutation({ model, action }) {
340-
console.log(`After ${model} ${action}`);
332+
onEntityMutation: {
333+
beforeEntityMutation({ model, action }) {
334+
console.log(`Before ${model} ${action}`);
335+
},
336+
337+
afterEntityMutation({ model, action }) {
338+
console.log(`After ${model} ${action}`);
339+
},
341340
},
342341
});
343342
```
@@ -347,20 +346,24 @@ You can provide an extra `mutationInterceptionFilter` to control what to interce
347346
```ts
348347
db.$use({
349348
id: 'mutation-hook-plugin',
350-
mutationInterceptionFilter: ({ model }) => {
351-
return {
352-
intercept: model === 'User',
353-
// load entities affected before the mutation (defaults to false)
354-
loadBeforeMutationEntities: true,
355-
// load entities affected after the mutation (defaults to false)
356-
loadAfterMutationEntities: true,
357-
};
358-
},
359-
beforeEntityMutation({ model, action, entities }) {
360-
console.log(`Before ${model} ${action}: ${entities}`);
361-
},
362-
afterEntityMutation({ model, action, afterMutationEntities }) {
363-
console.log(`After ${model} ${action}: ${afterMutationEntities}`);
349+
onEntityMutation: {
350+
mutationInterceptionFilter: ({ model }) => {
351+
return {
352+
intercept: model === 'User',
353+
// load entities affected before the mutation (defaults to false)
354+
loadBeforeMutationEntities: true,
355+
// load entities affected after the mutation (defaults to false)
356+
loadAfterMutationEntities: true,
357+
};
358+
},
359+
360+
beforeEntityMutation({ model, action, entities }) {
361+
console.log(`Before ${model} ${action}: ${entities}`);
362+
},
363+
364+
afterEntityMutation({ model, action, afterMutationEntities }) {
365+
console.log(`After ${model} ${action}: ${afterMutationEntities}`);
366+
},
364367
},
365368
});
366369
```

TODO.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
- [ ] Short-circuit pre-create check for scalar-field only policies
100100
- [ ] Inject "replace into"
101101
- [ ] Inject "on conflict do update"
102+
- [ ] Inject "insert into select from"
102103
- [x] Migration
103104
- [ ] Databases
104105
- [x] SQLite

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

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { invariant, lowerCaseFirst } from '@zenstackhq/common-helpers';
1+
import { invariant } from '@zenstackhq/common-helpers';
22
import type { QueryExecutor } from 'kysely';
33
import {
44
CompiledQuery,
@@ -376,30 +376,10 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
376376
// apply plugins
377377
const plugins = [...(client.$options.plugins ?? [])];
378378
for (const plugin of plugins) {
379-
if (plugin.onQuery && typeof plugin.onQuery === 'object') {
380-
// for each model key or "$allModels"
381-
for (const [_model, modelHooks] of Object.entries<any>(plugin.onQuery)) {
382-
if (_model === lowerCaseFirst(model) || _model === '$allModels') {
383-
if (modelHooks && typeof modelHooks === 'object') {
384-
// for each operation key or "$allOperations"
385-
for (const [op, opHooks] of Object.entries(modelHooks)) {
386-
if (op === operation || op === '$allOperations') {
387-
if (typeof opHooks === 'function') {
388-
const _proceed = proceed;
389-
proceed = () =>
390-
opHooks({
391-
client,
392-
model,
393-
operation,
394-
args,
395-
query: _proceed,
396-
}) as Promise<unknown>;
397-
}
398-
}
399-
}
400-
}
401-
}
402-
}
379+
const onQuery = plugin.onQuery;
380+
if (onQuery) {
381+
const _proceed = proceed;
382+
proceed = () => onQuery({ client, model, operation, args, query: _proceed }) as Promise<unknown>;
403383
}
404384
}
405385

0 commit comments

Comments
 (0)