Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 ZenStack

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 1 addition & 1 deletion NEW-FEATURES.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
- Cross-field comparison (for read and mutations)
- Custom policy functions
- Computed fields
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"dependencies": {
"@types/node": "^20.0.0",
"@zenstackhq/language": "workspace:*",
"@zenstackhq/runtime": "workspace:*",
"@zenstackhq/sdk": "workspace:*",
"async-exit-hook": "^2.0.1",
"colors": "1.4.0",
Expand All @@ -50,6 +49,7 @@
"@types/better-sqlite3": "^7.6.13",
"@types/semver": "^7.3.13",
"@types/tmp": "^0.2.6",
"@zenstackhq/runtime": "workspace:*",
"@zenstackhq/testtools": "workspace:*",
"better-sqlite3": "^11.8.1",
"tmp": "^0.2.3"
Expand Down
10 changes: 6 additions & 4 deletions packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"description": "ZenStack Runtime",
"type": "module",
"scripts": {
"build": "tsup-node && pnpm test-typecheck",
"test-typecheck": "tsc --project tsconfig.test.json",
"build": "tsup-node",
"watch": "tsup-node --watch",
"lint": "eslint src --ext ts",
"test": "vitest",
"test": "vitest && pnpm test:generate && pnpm test:typecheck",
"test:generate": "tsx test/typing/generate.ts",
"test:typecheck": "tsc --project tsconfig.test.json",
"pack": "pnpm pack"
},
"keywords": [],
Expand Down Expand Up @@ -117,6 +118,7 @@
"@zenstackhq/language": "workspace:*",
"@zenstackhq/sdk": "workspace:*",
"@zenstackhq/testtools": "workspace:*",
"tmp": "^0.2.3"
"tmp": "^0.2.3",
"tsx": "^4.19.2"
}
}
57 changes: 37 additions & 20 deletions packages/runtime/src/client/crud-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,19 @@ type ModelSelectResult<
Select,
Omit
> = {
[Key in keyof Select & GetFields<Schema, Model> as Select[Key] extends
| false
| undefined
[Key in keyof Select as Select[Key] extends false | undefined
? never
: Key extends keyof Omit
? Omit[Key] extends true
? never
: Key
: Key]: Key extends NonRelationFields<Schema, Model>
: Key extends '_count'
? Select[Key] extends SelectCount<Schema, Model>
? Key
: never
: Key]: Key extends '_count'
? SelectCountResult<Select[Key]>
: Key extends NonRelationFields<Schema, Model>
? MapFieldType<Schema, Model, Key>
: Key extends RelationFields<Schema, Model>
? Select[Key] extends FindArgs<
Expand All @@ -97,6 +101,12 @@ type ModelSelectResult<
: never;
};

type SelectCountResult<C> = C extends true
? number
: C extends { select: infer S }
? { [Key in keyof S]: number }
: never;

export type ModelResult<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
Expand Down Expand Up @@ -366,7 +376,7 @@ export type SelectIncludeOmit<
Model extends GetModels<Schema>,
AllowCount extends boolean
> = {
select?: Select<Schema, Model, AllowCount>;
select?: Select<Schema, Model, AllowCount, boolean>;
include?: Include<Schema, Model>;
omit?: OmitFields<Schema, Model>;
};
Expand All @@ -382,14 +392,15 @@ type Cursor<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
type Select<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
AllowCount extends Boolean
AllowCount extends Boolean,
AllowRelation extends boolean = true
> = {
[Key in NonRelationFields<Schema, Model>]?: true;
} & Include<Schema, Model> &
} & (AllowRelation extends true ? Include<Schema, Model> : {}) & // relation fields
// relation count
(AllowCount extends true ? { _count?: RelationCount<Schema, Model> } : {});
(AllowCount extends true ? { _count?: SelectCount<Schema, Model> } : {});

type RelationCount<Schema extends SchemaDef, Model extends GetModels<Schema>> =
type SelectCount<Schema extends SchemaDef, Model extends GetModels<Schema>> =
| true
| {
select: {
Expand Down Expand Up @@ -619,8 +630,10 @@ export type CreateManyArgs<
export type CreateManyAndReturnArgs<
Schema extends SchemaDef,
Model extends GetModels<Schema>
> = CreateManyPayload<Schema, Model> &
Omit<SelectIncludeOmit<Schema, Model, false>, 'include'>;
> = CreateManyPayload<Schema, Model> & {
select?: Select<Schema, Model, false, false>;
omit?: OmitFields<Schema, Model>;
};

type OptionalWrap<
Schema extends SchemaDef,
Expand Down Expand Up @@ -712,10 +725,11 @@ type CreateWithRelationInput<

type ConnectOrCreatePayload<
Schema extends SchemaDef,
Model extends GetModels<Schema>
Model extends GetModels<Schema>,
Without extends string = never
> = {
where: WhereUniqueInput<Schema, Model>;
create: CreateInput<Schema, Model>;
create: CreateInput<Schema, Model, Without>;
};

type CreateManyPayload<
Expand Down Expand Up @@ -1132,10 +1146,15 @@ type ConnectOrCreateInput<
? OrArray<
ConnectOrCreatePayload<
Schema,
RelationFieldType<Schema, Model, Field>
RelationFieldType<Schema, Model, Field>,
OppositeRelationAndFK<Schema, Model, Field>
>
>
: ConnectOrCreatePayload<Schema, RelationFieldType<Schema, Model, Field>>;
: ConnectOrCreatePayload<
Schema,
RelationFieldType<Schema, Model, Field>,
OppositeRelationAndFK<Schema, Model, Field>
>;

type DisconnectInput<
Schema extends SchemaDef,
Expand Down Expand Up @@ -1280,11 +1299,9 @@ export type ModelOperations<

createMany(args?: CreateManyPayload<Schema, Model>): Promise<BatchResult>;

createManyAndReturn(
args?: CreateManyAndReturnArgs<Schema, Model>
): Promise<
ModelResult<Schema, Model, CreateManyAndReturnArgs<Schema, Model>>[]
>;
createManyAndReturn<T extends CreateManyAndReturnArgs<Schema, Model>>(
args?: SelectSubset<T, CreateManyAndReturnArgs<Schema, Model>>
): Promise<ModelResult<Schema, Model, T>[]>;

update<T extends UpdateArgs<Schema, Model>>(
args: SelectSubset<T, UpdateArgs<Schema, Model>>
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/client/crud/operations/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class CreateOperationHandler<
});
});

if (!result) {
if (!result && this.hasPolicyEnabled) {
throw new RejectedByPolicyError(
this.model,
`result is not allowed to be read back`
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/client/crud/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class UpdateOperationHandler<
});
});

if (!result) {
if (!result && this.hasPolicyEnabled) {
throw new RejectedByPolicyError(
this.model,
'result is not allowed to be read back'
Expand Down
4 changes: 3 additions & 1 deletion packages/runtime/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export { ZenStackClient } from './client-impl';
export type * from './crud-types';
export type { ClientConstructor, ClientContract } from './contract';
export type * from './crud-types';
export * from './errors';
export type { ClientOptions } from './options';
export type { CliGenerator } from './plugin';
export type { ToKysely } from './query-builder';
1 change: 1 addition & 0 deletions packages/runtime/src/plugins/policy/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './errors';
export * from './plugin';
3 changes: 0 additions & 3 deletions packages/runtime/test/client-api/many-to-many.test.ts

This file was deleted.

7 changes: 5 additions & 2 deletions packages/runtime/test/test-schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Sqlite from 'better-sqlite3';
import type { DataSourceProviderType, SchemaDef } from '../src/schema';
import { ExpressionUtils } from '../src/schema/expression';
import {
ExpressionUtils,
type DataSourceProviderType,
type SchemaDef,
} from '../src/schema';

export const schema = {
provider: {
Expand Down
22 changes: 22 additions & 0 deletions packages/runtime/test/typing/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { TsSchemaGenerator } from '@zenstackhq/sdk';
import path from 'node:path';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';

async function main() {
const generator = new TsSchemaGenerator();
const dir = path.dirname(fileURLToPath(import.meta.url));
const zmodelPath = path.join(dir, 'typing-test.zmodel');
const tsPath = path.join(dir, 'schema.ts');
await generator.generate(zmodelPath, [], tsPath);

const content = fs.readFileSync(tsPath, 'utf-8');
fs.writeFileSync(
tsPath,
content.replace(/@zenstackhq\/runtime/g, '../../dist')
);

console.log('TS schema generated at:', tsPath);
}

main();
Loading