Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zenstack-v3",
"version": "3.0.0-alpha.29",
"version": "3.0.0-alpha.30",
"description": "ZenStack",
"packageManager": "[email protected]",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "zenstack",
"displayName": "ZenStack CLI",
"description": "FullStack database toolkit with built-in access control and automatic API generation.",
"version": "3.0.0-alpha.29",
"version": "3.0.0-alpha.30",
"type": "module",
"author": {
"name": "ZenStack Team"
Expand Down
12 changes: 6 additions & 6 deletions packages/cli/src/plugins/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import path from 'node:path';
const plugin: CliPlugin = {
name: 'Prisma Schema Generator',
statusText: 'Generating Prisma schema',
async generate({ model, defaultOutputPath, pluginOptions }) {
let outDir = defaultOutputPath;
async generate({ model, schemaFile, defaultOutputPath, pluginOptions }) {
let outFile = path.join(defaultOutputPath, 'schema.prisma');
if (typeof pluginOptions['output'] === 'string') {
outDir = path.resolve(defaultOutputPath, pluginOptions['output']);
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir, { recursive: true });
outFile = path.resolve(path.dirname(schemaFile), pluginOptions['output']);
if (!fs.existsSync(path.dirname(outFile))) {
fs.mkdirSync(path.dirname(outFile), { recursive: true });
}
}
const prismaSchema = await new PrismaSchemaGenerator(model).generate();
fs.writeFileSync(path.join(outDir, 'schema.prisma'), prismaSchema);
fs.writeFileSync(outFile, prismaSchema);
},
};

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/test/plugins/prisma-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ model User {
const workDir = createProject(`
plugin prisma {
provider = '@core/prisma'
output = './prisma'
output = '../prisma/schema.prisma'
}

model User {
id String @id @default(cuid())
}
`);
runCli('generate', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/prisma/schema.prisma'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'prisma/schema.prisma'))).toBe(true);
});
});
2 changes: 1 addition & 1 deletion packages/common-helpers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/common-helpers",
"version": "3.0.0-alpha.29",
"version": "3.0.0-alpha.30",
"description": "ZenStack Common Helpers",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/create-zenstack/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-zenstack",
"version": "3.0.0-alpha.29",
"version": "3.0.0-alpha.30",
"description": "Create a new ZenStack project",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/dialects/sql.js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/kysely-sql-js",
"version": "3.0.0-alpha.29",
"version": "3.0.0-alpha.30",
"description": "Kysely dialect for sql.js",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/eslint-config",
"version": "3.0.0-alpha.29",
"version": "3.0.0-alpha.30",
"type": "module",
"private": true,
"license": "MIT"
Expand Down
2 changes: 1 addition & 1 deletion packages/ide/vscode/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zenstack",
"publisher": "zenstack",
"version": "3.0.0-alpha.29",
"version": "3.0.0-alpha.30",
"displayName": "ZenStack Language Tools",
"description": "VSCode extension for ZenStack ZModel language",
"private": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/language/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/language",
"description": "ZenStack ZModel language specification",
"version": "3.0.0-alpha.29",
"version": "3.0.0-alpha.30",
"license": "MIT",
"author": "ZenStack Team",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/runtime",
"version": "3.0.0-alpha.29",
"version": "3.0.0-alpha.30",
"description": "ZenStack Runtime",
"type": "module",
"scripts": {
Expand Down
43 changes: 23 additions & 20 deletions packages/runtime/src/client/crud-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
FieldIsDelegateDiscriminator,
FieldIsDelegateRelation,
FieldIsRelation,
FieldIsRelationArray,
FieldType,
ForeignKeyFields,
GetEnum,
Expand Down Expand Up @@ -218,11 +217,11 @@ export type WhereInput<
: Key]?: Key extends RelationFields<Schema, Model>
? // relation
RelationFilter<Schema, Model, Key>
: // enum
GetModelFieldType<Schema, Model, Key> extends GetEnums<Schema>
? EnumFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>>
: FieldIsArray<Schema, Model, Key> extends true
? ArrayFilter<GetModelFieldType<Schema, Model, Key>>
: FieldIsArray<Schema, Model, Key> extends true
? ArrayFilter<Schema, GetModelFieldType<Schema, Model, Key>>
: // enum
GetModelFieldType<Schema, Model, Key> extends GetEnums<Schema>
? EnumFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>>
: // primitive
PrimitiveFilter<
Schema,
Expand All @@ -247,14 +246,18 @@ type EnumFilter<Schema extends SchemaDef, T extends GetEnums<Schema>, Nullable e
not?: EnumFilter<Schema, T, Nullable>;
};

type ArrayFilter<T extends string> = {
equals?: MapBaseType<T>[];
has?: MapBaseType<T>;
hasEvery?: MapBaseType<T>[];
hasSome?: MapBaseType<T>[];
type ArrayFilter<Schema extends SchemaDef, T extends string> = {
equals?: MapScalarType<Schema, T>[];
has?: MapScalarType<Schema, T>;
hasEvery?: MapScalarType<Schema, T>[];
hasSome?: MapScalarType<Schema, T>[];
isEmpty?: boolean;
};

// map a scalar type (primitive and enum) to TS type
type MapScalarType<Schema extends SchemaDef, T extends string> =
T extends GetEnums<Schema> ? keyof GetEnum<Schema, T> : MapBaseType<T>;

type PrimitiveFilter<
Schema extends SchemaDef,
T extends string,
Expand Down Expand Up @@ -428,7 +431,7 @@ export type WhereUniqueInput<Schema extends SchemaDef, Model extends GetModels<S
>;

export type OmitInput<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
[Key in NonRelationFields<Schema, Model>]?: true;
[Key in NonRelationFields<Schema, Model>]?: boolean;
};

export type SelectIncludeOmit<Schema extends SchemaDef, Model extends GetModels<Schema>, AllowCount extends boolean> = {
Expand All @@ -443,7 +446,7 @@ export type SelectInput<
AllowCount extends boolean = true,
AllowRelation extends boolean = true,
> = {
[Key in NonRelationFields<Schema, Model>]?: true;
[Key in NonRelationFields<Schema, Model>]?: boolean;
} & (AllowRelation extends true ? IncludeInput<Schema, Model> : {}) & // relation fields
// relation count
(AllowCount extends true
Expand All @@ -454,11 +457,11 @@ export type SelectInput<
: {});

type SelectCount<Schema extends SchemaDef, Model extends GetModels<Schema>> =
| true
| boolean
| {
select: {
[Key in RelationFields<Schema, Model> as FieldIsArray<Schema, Model, Key> extends true ? Key : never]:
| true
[Key in RelationFields<Schema, Model> as FieldIsArray<Schema, Model, Key> extends true ? Key : never]?:
| boolean
| {
where: WhereInput<Schema, RelationFieldType<Schema, Model, Key>, false>;
};
Expand Down Expand Up @@ -561,9 +564,9 @@ type OptionalFieldsForCreate<Schema extends SchemaDef, Model extends GetModels<S
? Key
: FieldHasDefault<Schema, Model, Key> extends true
? Key
: GetModelField<Schema, Model, Key>['updatedAt'] extends true
: FieldIsArray<Schema, Model, Key> extends true
? Key
: FieldIsRelationArray<Schema, Model, Key> extends true
: GetModelField<Schema, Model, Key>['updatedAt'] extends true
? Key
: never]: GetModelField<Schema, Model, Key>;
};
Expand Down Expand Up @@ -694,7 +697,7 @@ type ScalarCreatePayload<
| MapModelFieldType<Schema, Model, Field>
| (FieldIsArray<Schema, Model, Field> extends true
? {
set?: MapModelFieldType<Schema, Model, Field>[];
set?: MapModelFieldType<Schema, Model, Field>;
}
: never);

Expand Down Expand Up @@ -1204,7 +1207,7 @@ type NestedUpsertInput<
Field extends RelationFields<Schema, Model>,
> = OrArray<
{
where: WhereUniqueInput<Schema, Model>;
where: WhereUniqueInput<Schema, RelationFieldType<Schema, Model, Field>>;
create: CreateInput<
Schema,
RelationFieldType<Schema, Model, Field>,
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ export type * from './crud-types';
export * from './errors';
export type { ClientOptions } from './options';
export { definePlugin } from './plugin';
export type { ZenStackPromise } from './promise';
export type { ToKysely } from './query-builder';
export { sql } from 'kysely';
2 changes: 2 additions & 0 deletions packages/runtime/test/schemas/typing/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export type Identity = $TypeDefResult<$Schema, "Identity">;
export type IdentityProvider = $TypeDefResult<$Schema, "IdentityProvider">;
export const Role = $schema.enums.Role;
export type Role = (typeof Role)[keyof typeof Role];
export const Status = $schema.enums.Status;
export type Status = (typeof Status)[keyof typeof Status];
12 changes: 11 additions & 1 deletion packages/runtime/test/schemas/typing/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { type SchemaDef, type OperandExpression, ExpressionUtils } from "../../../dist/schema";
export const schema = {
provider: {
type: "sqlite"
type: "postgresql"
},
models: {
User: {
Expand Down Expand Up @@ -49,6 +49,11 @@ export const schema = {
attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.literal("USER") }] }],
default: "USER"
},
status: {
name: "status",
type: "Status",
array: true
},
posts: {
name: "posts",
type: "Post",
Expand Down Expand Up @@ -325,6 +330,11 @@ export const schema = {
Role: {
ADMIN: "ADMIN",
USER: "USER"
},
Status: {
ACTIVE: "ACTIVE",
INACTIVE: "INACTIVE",
BANNED: "BANNED"
}
},
authType: "User",
Expand Down
11 changes: 9 additions & 2 deletions packages/runtime/test/schemas/typing/schema.zmodel
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
datasource db {
provider = "sqlite"
url = "file:./test.db"
provider = 'postgresql'
url = env('DATABASE_URL')
}

enum Role {
ADMIN
USER
}

enum Status {
ACTIVE
INACTIVE
BANNED
}

type Identity {
providers IdentityProvider[]
}
Expand All @@ -24,6 +30,7 @@ model User {
name String
email String @unique
role Role @default(USER)
status Status[]
posts Post[]
profile Profile?
postCount Int @computed
Expand Down
16 changes: 15 additions & 1 deletion packages/runtime/test/schemas/typing/typecheck.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SQLite from 'better-sqlite3';
import { SqliteDialect } from 'kysely';
import { ZenStackClient } from '../../../dist';
import { Role, type Identity, type IdentityProvider } from './models';
import { Role, Status, type Identity, type IdentityProvider } from './models';
import { schema } from './schema';

const client = new ZenStackClient(schema, {
Expand Down Expand Up @@ -35,6 +35,9 @@ async function find() {
where: {
name: 'Alex',
role: Role.USER,
status: {
has: Status.ACTIVE,
},
},
});
console.log(user1?.name);
Expand Down Expand Up @@ -77,6 +80,17 @@ async function find() {
where: { name: 'Alex' },
});

// enum array
await client.user.findFirst({
where: { status: { equals: [Status.ACTIVE] } },
});
await client.user.findFirst({
where: { status: { has: Status.ACTIVE } },
});
await client.user.findFirst({
where: { status: { hasEvery: [Status.ACTIVE] } },
});

await client.user.findMany({
skip: 1,
take: 1,
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/sdk",
"version": "3.0.0-alpha.29",
"version": "3.0.0-alpha.30",
"description": "ZenStack SDK",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/tanstack-query/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/tanstack-query",
"version": "3.0.0-alpha.29",
"version": "3.0.0-alpha.30",
"description": "",
"main": "index.js",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion packages/testtools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/testtools",
"version": "3.0.0-alpha.29",
"version": "3.0.0-alpha.30",
"description": "ZenStack Test Tools",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/typescript-config",
"version": "3.0.0-alpha.29",
"version": "3.0.0-alpha.30",
"private": true,
"license": "MIT"
}
2 changes: 1 addition & 1 deletion packages/vitest-config/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/vitest-config",
"type": "module",
"version": "3.0.0-alpha.29",
"version": "3.0.0-alpha.30",
"private": true,
"license": "MIT",
"exports": {
Expand Down
2 changes: 1 addition & 1 deletion packages/zod/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/zod",
"version": "3.0.0-alpha.29",
"version": "3.0.0-alpha.30",
"description": "",
"type": "module",
"main": "index.js",
Expand Down
Loading