Skip to content

Commit 56b68ab

Browse files
authored
feat(server): migrate rpc api handler and express adapter (#328)
* feat(server): migrate rpc api handler and express adapter * fix package.json * misc fixes * update * update ot express5 only * update * update * fix test
1 parent 67f2368 commit 56b68ab

File tree

33 files changed

+2097
-30
lines changed

33 files changed

+2097
-30
lines changed

BREAKINGCHANGES.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
1. `auth()` cannot be directly compared with a relation anymore
2-
2. `update` and `delete` policy rejection throws `NotFoundError`
3-
3. non-optional to-one relation doesn't automatically filter parent read when evaluating access policies
1+
1. `update` and `delete` policy rejection throws `NotFoundError`
2+
1. `check()` ORM api has been removed
3+
1. non-optional to-one relation doesn't automatically filter parent read when evaluating access policies

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
- [ ] DbNull vs JsonNull
8787
- [ ] Migrate to tsdown
8888
- [x] @default validation
89-
- [ ] Benchmark
89+
- [x] Benchmark
9090
- [x] Plugin
9191
- [x] Post-mutation hooks should be called after transaction is committed
9292
- [x] TypeDef and mixin

packages/common-helpers/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from './is-plain-object';
22
export * from './lower-case-first';
33
export * from './param-case';
4+
export * from './safe-json-stringify';
45
export * from './sleep';
56
export * from './tiny-invariant';
67
export * from './upper-case-first';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* A safe JSON stringify that handles bigint values.
3+
*/
4+
export function safeJSONStringify(value: unknown) {
5+
return JSON.stringify(value, (_, v) => {
6+
if (typeof v === 'bigint') {
7+
return v.toString();
8+
} else {
9+
return v;
10+
}
11+
});
12+
}

packages/language/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"build": "pnpm langium:generate && tsc --noEmit && tsup-node",
1414
"watch": "tsup-node --watch",
1515
"lint": "eslint src --ext ts",
16+
"test": "vitest run",
1617
"langium:generate": "langium generate",
1718
"langium:generate:production": "langium generate --mode=production",
1819
"pack": "pnpm pack"

packages/language/src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Supported Prisma db providers
2+
* Supported db providers
33
*/
44
export const SUPPORTED_PROVIDERS = [
55
'sqlite',

packages/plugins/policy/src/policy-handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class PolicyHandler<Schema extends SchemaDef> extends OperationNodeTransf
104104
if (constCondition === true) {
105105
needCheckPreCreate = false;
106106
} else if (constCondition === false) {
107-
throw new RejectedByPolicyError(mutationModel);
107+
throw new RejectedByPolicyError(mutationModel, RejectedByPolicyReason.NO_ACCESS);
108108
}
109109
}
110110

@@ -621,7 +621,7 @@ export class PolicyHandler<Schema extends SchemaDef> extends OperationNodeTransf
621621

622622
const result = await proceed(preCreateCheck);
623623
if (!result.rows[0]?.$condition) {
624-
throw new RejectedByPolicyError(model);
624+
throw new RejectedByPolicyError(model, RejectedByPolicyReason.NO_ACCESS);
625625
}
626626
}
627627

packages/runtime/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"dependencies": {
6565
"@paralleldrive/cuid2": "^2.2.2",
6666
"@zenstackhq/common-helpers": "workspace:*",
67-
"decimal.js": "^10.4.3",
67+
"decimal.js": "catalog:",
6868
"json-stable-stringify": "^1.3.0",
6969
"nanoid": "^5.0.9",
7070
"toposort": "^2.0.2",

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ export class PostgresCrudDialect<Schema extends SchemaDef> extends BaseCrudDiale
111111
}
112112

113113
private transformOutputBytes(value: unknown) {
114-
return Buffer.isBuffer(value) ? Uint8Array.from(value) : value;
114+
return Buffer.isBuffer(value)
115+
? Uint8Array.from(value)
116+
: // node-pg encode bytea as hex string prefixed with \x when embedded in JSON
117+
typeof value === 'string' && value.startsWith('\\x')
118+
? Uint8Array.from(Buffer.from(value.slice(2), 'hex'))
119+
: value;
115120
}
116121

117122
override buildRelationSelection(

packages/runtime/src/client/crud/validator/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ export class InputValidator<Schema extends SchemaDef> {
232232
const { error, data } = schema.safeParse(args);
233233
if (error) {
234234
throw new InputValidationError(
235+
model,
235236
`Invalid ${operation} args for model "${model}": ${formatError(error)}`,
236237
error,
237238
);

0 commit comments

Comments
 (0)