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
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.18",
"version": "3.0.0-alpha.19",
"description": "ZenStack",
"packageManager": "pnpm@10.12.1",
"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.18",
"version": "3.0.0-alpha.19",
"type": "module",
"author": {
"name": "ZenStack Team"
Expand Down
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.18",
"version": "3.0.0-alpha.19",
"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.18",
"version": "3.0.0-alpha.19",
"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.18",
"version": "3.0.0-alpha.19",
"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.18",
"version": "3.0.0-alpha.19",
"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.18",
"version": "3.0.0-alpha.19",
"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.18",
"version": "3.0.0-alpha.19",
"license": "MIT",
"author": "ZenStack Team",
"files": [
Expand Down
4 changes: 2 additions & 2 deletions 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.18",
"version": "3.0.0-alpha.19",
"description": "ZenStack Runtime",
"type": "module",
"scripts": {
Expand Down Expand Up @@ -75,7 +75,7 @@
"uuid": "^11.0.5"
},
"peerDependencies": {
"better-sqlite3": "^11.8.1",
"better-sqlite3": "^12.2.0",
"kysely": "catalog:",
"pg": "^8.13.1",
"zod": "catalog:"
Expand Down
52 changes: 35 additions & 17 deletions packages/runtime/src/client/crud-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export type WhereInput<
: FieldIsArray<Schema, Model, Key> extends true
? ArrayFilter<GetModelFieldType<Schema, Model, Key>>
: // primitive
PrimitiveFilter<GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>>;
PrimitiveFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>>;
} & {
$expr?: (eb: ExpressionBuilder<ToKyselySchema<Schema>, Model>) => OperandExpression<SqlBool>;
} & {
Expand All @@ -249,47 +249,52 @@ type ArrayFilter<T extends string> = {
isEmpty?: boolean;
};

type PrimitiveFilter<T extends string, Nullable extends boolean> = T extends 'String'
? StringFilter<Nullable>
type PrimitiveFilter<Schema extends SchemaDef, T extends string, Nullable extends boolean> = T extends 'String'
? StringFilter<Schema, Nullable>
: T extends 'Int' | 'Float' | 'Decimal' | 'BigInt'
? NumberFilter<T, Nullable>
? NumberFilter<Schema, T, Nullable>
: T extends 'Boolean'
? BooleanFilter<Nullable>
: T extends 'DateTime'
? DateTimeFilter<Nullable>
? DateTimeFilter<Schema, Nullable>
: T extends 'Bytes'
? BytesFilter<Nullable>
: T extends 'Json'
? 'Not implemented yet' // TODO: Json filter
: never;

type CommonPrimitiveFilter<DataType, T extends BuiltinType, Nullable extends boolean> = {
type CommonPrimitiveFilter<Schema extends SchemaDef, DataType, T extends BuiltinType, Nullable extends boolean> = {
equals?: NullableIf<DataType, Nullable>;
in?: DataType[];
notIn?: DataType[];
lt?: DataType;
lte?: DataType;
gt?: DataType;
gte?: DataType;
not?: PrimitiveFilter<T, Nullable>;
not?: PrimitiveFilter<Schema, T, Nullable>;
};

export type StringFilter<Nullable extends boolean> =
export type StringFilter<Schema extends SchemaDef, Nullable extends boolean> =
| NullableIf<string, Nullable>
| (CommonPrimitiveFilter<string, 'String', Nullable> & {
| (CommonPrimitiveFilter<Schema, string, 'String', Nullable> & {
contains?: string;
startsWith?: string;
endsWith?: string;
mode?: 'default' | 'insensitive';
});
} & (ProviderSupportsCaseSensitivity<Schema> extends true
? {
mode?: 'default' | 'insensitive';
}
: {}));

export type NumberFilter<T extends 'Int' | 'Float' | 'Decimal' | 'BigInt', Nullable extends boolean> =
| NullableIf<number | bigint, Nullable>
| CommonPrimitiveFilter<number, T, Nullable>;
export type NumberFilter<
Schema extends SchemaDef,
T extends 'Int' | 'Float' | 'Decimal' | 'BigInt',
Nullable extends boolean,
> = NullableIf<number | bigint, Nullable> | CommonPrimitiveFilter<Schema, number, T, Nullable>;

export type DateTimeFilter<Nullable extends boolean> =
export type DateTimeFilter<Schema extends SchemaDef, Nullable extends boolean> =
| NullableIf<Date | string, Nullable>
| CommonPrimitiveFilter<Date | string, 'DateTime', Nullable>;
| CommonPrimitiveFilter<Schema, Date | string, 'DateTime', Nullable>;

export type BytesFilter<Nullable extends boolean> =
| NullableIf<Uint8Array | Buffer, Nullable>
Expand Down Expand Up @@ -393,7 +398,12 @@ export type SelectInput<
[Key in NonRelationFields<Schema, Model>]?: true;
} & (AllowRelation extends true ? IncludeInput<Schema, Model> : {}) & // relation fields
// relation count
(AllowCount extends true ? { _count?: SelectCount<Schema, Model> } : {});
(AllowCount extends true
? // _count is only allowed if the model has to-many relations
HasToManyRelations<Schema, Model> extends true
? { _count?: SelectCount<Schema, Model> }
: {}
: {});

type SelectCount<Schema extends SchemaDef, Model extends GetModels<Schema>> =
| true
Expand Down Expand Up @@ -1181,4 +1191,12 @@ type NonOwnedRelationFields<Schema extends SchemaDef, Model extends GetModels<Sc
: Key]: true;
};

type HasToManyRelations<Schema extends SchemaDef, Model extends GetModels<Schema>> = keyof {
[Key in RelationFields<Schema, Model> as FieldIsArray<Schema, Model, Key> extends true ? Key : never]: true;
} extends never
? false
: true;

type ProviderSupportsCaseSensitivity<Schema extends SchemaDef> = Schema['provider'] extends 'postgresql' ? true : false;

// #endregion
113 changes: 89 additions & 24 deletions packages/runtime/src/client/crud/dialects/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
recurse: (value: unknown) => Expression<SqlBool>,
throwIfInvalid = false,
onlyForKeys: string[] | undefined = undefined,
excludeKeys: string[] = [],
) {
if (payload === null || !isPlainObject(payload)) {
return {
Expand All @@ -472,6 +473,9 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
if (onlyForKeys && !onlyForKeys.includes(op)) {
continue;
}
if (excludeKeys.includes(op)) {
continue;
}
const rhs = Array.isArray(value) ? value.map(getRhs) : getRhs(value);
const condition = match(op)
.with('equals', () => (rhs === null ? eb(lhs, 'is', null) : eb(lhs, '=', rhs)))
Expand Down Expand Up @@ -513,20 +517,23 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
return { conditions, consumedKeys };
}

private buildStringFilter(eb: ExpressionBuilder<any, any>, fieldRef: Expression<any>, payload: StringFilter<true>) {
let insensitive = false;
if (payload && typeof payload === 'object' && 'mode' in payload && payload.mode === 'insensitive') {
insensitive = true;
fieldRef = eb.fn('lower', [fieldRef]);
private buildStringFilter(
eb: ExpressionBuilder<any, any>,
fieldRef: Expression<any>,
payload: StringFilter<Schema, true>,
) {
let mode: 'default' | 'insensitive' | undefined;
if (payload && typeof payload === 'object' && 'mode' in payload) {
mode = payload.mode;
}

const { conditions, consumedKeys } = this.buildStandardFilter(
eb,
'String',
payload,
fieldRef,
(value) => this.prepStringCasing(eb, value, insensitive),
(value) => this.buildStringFilter(eb, fieldRef, value as StringFilter<true>),
mode === 'insensitive' ? eb.fn('lower', [fieldRef]) : fieldRef,
(value) => this.prepStringCasing(eb, value, mode),
(value) => this.buildStringFilter(eb, fieldRef, value as StringFilter<Schema, true>),
);

if (payload && typeof payload === 'object') {
Expand All @@ -538,22 +545,22 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {

const condition = match(key)
.with('contains', () =>
insensitive
? eb(fieldRef, 'ilike', sql.lit(`%${value}%`))
: eb(fieldRef, 'like', sql.lit(`%${value}%`)),
mode === 'insensitive'
? eb(fieldRef, 'ilike', sql.val(`%${value}%`))
: eb(fieldRef, 'like', sql.val(`%${value}%`)),
)
.with('startsWith', () =>
insensitive
? eb(fieldRef, 'ilike', sql.lit(`${value}%`))
: eb(fieldRef, 'like', sql.lit(`${value}%`)),
mode === 'insensitive'
? eb(fieldRef, 'ilike', sql.val(`${value}%`))
: eb(fieldRef, 'like', sql.val(`${value}%`)),
)
.with('endsWith', () =>
insensitive
? eb(fieldRef, 'ilike', sql.lit(`%${value}`))
: eb(fieldRef, 'like', sql.lit(`%${value}`)),
mode === 'insensitive'
? eb(fieldRef, 'ilike', sql.val(`%${value}`))
: eb(fieldRef, 'like', sql.val(`%${value}`)),
)
.otherwise(() => {
throw new Error(`Invalid string filter key: ${key}`);
throw new QueryError(`Invalid string filter key: ${key}`);
});

if (condition) {
Expand All @@ -565,13 +572,21 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
return this.and(eb, ...conditions);
}

private prepStringCasing(eb: ExpressionBuilder<any, any>, value: unknown, toLower: boolean = true): any {
private prepStringCasing(
eb: ExpressionBuilder<any, any>,
value: unknown,
mode: 'default' | 'insensitive' | undefined,
): any {
if (!mode || mode === 'default') {
return value === null ? value : sql.val(value);
}

if (typeof value === 'string') {
return toLower ? eb.fn('lower', [sql.lit(value)]) : sql.lit(value);
return eb.fn('lower', [sql.val(value)]);
} else if (Array.isArray(value)) {
return value.map((v) => this.prepStringCasing(eb, v, toLower));
return value.map((v) => this.prepStringCasing(eb, v, mode));
} else {
return value === null ? null : sql.lit(value);
return value === null ? null : sql.val(value);
}
}

Expand Down Expand Up @@ -613,15 +628,15 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
private buildDateTimeFilter(
eb: ExpressionBuilder<any, any>,
fieldRef: Expression<any>,
payload: DateTimeFilter<true>,
payload: DateTimeFilter<Schema, true>,
) {
const { conditions } = this.buildStandardFilter(
eb,
'DateTime',
payload,
fieldRef,
(value) => this.transformPrimitive(value, 'DateTime', false),
(value) => this.buildDateTimeFilter(eb, fieldRef, value as DateTimeFilter<true>),
(value) => this.buildDateTimeFilter(eb, fieldRef, value as DateTimeFilter<Schema, true>),
true,
);
return this.and(eb, ...conditions);
Expand Down Expand Up @@ -847,6 +862,56 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
return query;
}

buildCountJson(model: string, eb: ExpressionBuilder<any, any>, parentAlias: string, payload: any) {
const modelDef = requireModel(this.schema, model);
const toManyRelations = Object.entries(modelDef.fields).filter(([, field]) => field.relation && field.array);

const selections =
payload === true
? {
select: toManyRelations.reduce(
(acc, [field]) => {
acc[field] = true;
return acc;
},
{} as Record<string, boolean>,
),
}
: payload;

const jsonObject: Record<string, Expression<any>> = {};

for (const [field, value] of Object.entries(selections.select)) {
const fieldDef = requireField(this.schema, model, field);
const fieldModel = fieldDef.type;
const joinPairs = buildJoinPairs(this.schema, model, parentAlias, field, fieldModel);

// build a nested query to count the number of records in the relation
let fieldCountQuery = eb.selectFrom(fieldModel).select(eb.fn.countAll().as(`_count$${field}`));

// join conditions
for (const [left, right] of joinPairs) {
fieldCountQuery = fieldCountQuery.whereRef(left, '=', right);
}

// merge _count filter
if (
value &&
typeof value === 'object' &&
'where' in value &&
value.where &&
typeof value.where === 'object'
) {
const filter = this.buildFilter(eb, fieldModel, fieldModel, value.where);
fieldCountQuery = fieldCountQuery.where(filter);
}

jsonObject[field] = fieldCountQuery;
}

return this.buildJsonObject(eb, jsonObject);
}

// #endregion

// #region utils
Expand Down
Loading