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 packages/clients/tanstack-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint": "eslint src --ext ts",
"test": "vitest run",
"pack": "pnpm pack",
"test:generate": "tsx scripts/generate.ts",
"test:generate": "tsx ../../../scripts/test-generate.ts tests",
"test:typecheck": "tsc --noEmit --project tsconfig.test.json"
},
"keywords": [
Expand Down
27 changes: 0 additions & 27 deletions packages/clients/tanstack-query/scripts/generate.ts

This file was deleted.

17 changes: 11 additions & 6 deletions packages/orm/src/client/crud-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,14 @@ export type WhereInput<
ModelFieldIsOptional<Schema, Model, Key>,
WithAggregations
>
: // primitive
PrimitiveFilter<
GetModelFieldType<Schema, Model, Key>,
ModelFieldIsOptional<Schema, Model, Key>,
WithAggregations
>;
: GetModelFieldType<Schema, Model, Key> extends GetTypeDefs<Schema>
? TypedJsonFilter
: // primitive
PrimitiveFilter<
GetModelFieldType<Schema, Model, Key>,
ModelFieldIsOptional<Schema, Model, Key>,
WithAggregations
>;
} & {
$expr?: (eb: ExpressionBuilder<ToKyselySchema<Schema>, Model>) => OperandExpression<SqlBool>;
} & {
Expand Down Expand Up @@ -460,6 +462,9 @@ export type JsonFilter = {
not?: JsonValue | JsonNullValues;
};

// TODO: extra typedef filtering
export type TypedJsonFilter = JsonFilter;

export type SortOrder = 'asc' | 'desc';
export type NullsOrder = 'first' | 'last';

Expand Down
6 changes: 6 additions & 0 deletions packages/orm/src/client/crud/dialects/base-dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
isEnum,
isInheritedField,
isRelationField,
isTypeDef,
makeDefaultOrderBy,
requireField,
requireIdFields,
Expand Down Expand Up @@ -500,6 +501,11 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
return this.buildEnumFilter(fieldRef, fieldDef, payload);
}

if (isTypeDef(this.schema, fieldDef.type)) {
// TODO: type-def field filtering
return this.buildJsonFilter(fieldRef, payload);
}

return match(fieldDef.type as BuiltinType)
.with('String', () => this.buildStringFilter(fieldRef, payload))
.with(P.union('Int', 'Float', 'Decimal', 'BigInt'), (type) =>
Expand Down
20 changes: 18 additions & 2 deletions packages/orm/src/client/crud/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,13 @@ export class InputValidator<Schema extends SchemaDef> {
}),
),
);
this.setSchemaCache(key!, schema);
return schema;

// zod doesn't preserve object field order after parsing, here we use a
// validation-only custom schema and use the original data if parsing
// is successful
const finalSchema = z.custom((v) => schema!.safeParse(v).success);
this.setSchemaCache(key!, finalSchema);
return finalSchema;
}

private makeWhereSchema(
Expand Down Expand Up @@ -434,6 +439,8 @@ export class InputValidator<Schema extends SchemaDef> {
} else if (fieldDef.array) {
// array field
fieldSchema = this.makeArrayFilterSchema(fieldDef.type as BuiltinType);
} else if (this.isTypeDefType(fieldDef.type)) {
fieldSchema = this.makeTypedJsonFilterSchema(fieldDef.type, !!fieldDef.optional);
} else {
// primitive field
fieldSchema = this.makePrimitiveFilterSchema(
Expand Down Expand Up @@ -528,6 +535,15 @@ export class InputValidator<Schema extends SchemaDef> {
return result;
}

private makeTypedJsonFilterSchema(_type: string, optional: boolean) {
// TODO: direct typed JSON filtering
return this.makeJsonFilterSchema(optional);
}

private isTypeDefType(type: string) {
return this.schema.typeDefs && type in this.schema.typeDefs;
}

private makeEnumFilterSchema(enumDef: EnumDef, optional: boolean, withAggregations: boolean) {
const baseSchema = z.enum(Object.keys(enumDef.values) as [string, ...string[]]);
const components = this.makeCommonPrimitiveFilterComponents(
Expand Down
4 changes: 4 additions & 0 deletions packages/orm/src/client/query-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ export function getEnum(schema: SchemaDef, type: string) {
return schema.enums?.[type];
}

export function isTypeDef(schema: SchemaDef, type: string) {
return !!schema.typeDefs?.[type];
}

export function buildJoinPairs(
schema: SchemaDef,
model: string,
Expand Down
24 changes: 24 additions & 0 deletions scripts/test-generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { glob } from 'glob';
import { execSync } from 'node:child_process';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const _dirname = typeof __dirname !== 'undefined' ? __dirname : path.dirname(fileURLToPath(import.meta.url));

async function main() {
const baseDir = process.argv[2] || '.';

const zmodelFiles = [...glob.sync(path.resolve(baseDir, '**/schema.zmodel'), { ignore: '**/node_modules/**' })];
for (const file of zmodelFiles) {
console.log(`Generating TS schema for: ${file}`);
await generate(file);
}
}

async function generate(schemaPath: string) {
const cliPath = path.join(_dirname, '../packages/cli/dist/index.js');
const RUNTIME = process.env.RUNTIME ?? 'node';
execSync(`${RUNTIME} ${cliPath} generate --schema ${schemaPath}`, { cwd: path.dirname(schemaPath) });
}

main();
12 changes: 0 additions & 12 deletions tests/e2e/github-repos/cal.com/cal-com.test.ts

This file was deleted.

Loading
Loading