Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions packages/orm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"@paralleldrive/cuid2": "^2.2.2",
"@zenstackhq/common-helpers": "workspace:*",
"@zenstackhq/schema": "workspace:*",
"@zenstackhq/zod": "workspace:*",
"cuid": "^3.0.0",
"decimal.js": "catalog:",
"json-stable-stringify": "^1.3.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/orm/src/client/client-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ export class ClientImpl {
return this.kyselyRaw;
}

get $zod() {
return this.inputValidator.zodFactory;
}

get isTransaction() {
return this.kysely.isTransaction;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/orm/src/client/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* The comment prefix for annotation generated Kysely queries with context information.
*/
export const CONTEXT_COMMENT_PREFIX = '-- $$context:';
// /**
// * The comment prefix for annotation generated Kysely queries with context information.
// */
// export const CONTEXT_COMMENT_PREFIX = '-- $$context:';

/**
* The types of fields that are numeric.
Expand Down
131 changes: 45 additions & 86 deletions packages/orm/src/client/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,45 +39,15 @@ import type {
UpdateManyArgs,
UpsertArgs,
} from './crud-types';
import type {
CoreCreateOperations,
CoreCrudOperations,
CoreDeleteOperations,
CoreReadOperations,
CoreUpdateOperations,
} from './crud/operations/base';
import type { ClientOptions, QueryOptions } from './options';
import type { ExtClientMembersBase, ExtQueryArgsBase, RuntimePlugin } from './plugin';
import type { ZenStackPromise } from './promise';
import type { ToKysely } from './query-builder';
import type { GetSlicedModels, GetSlicedOperations, GetSlicedProcedures } from './type-utils';
import type { ZodSchemaFactory } from './zod/factory';

type TransactionUnsupportedMethods = (typeof TRANSACTION_UNSUPPORTED_METHODS)[number];

/**
* Extracts extended query args for a specific operation.
*/
type ExtractExtQueryArgs<ExtQueryArgs, Operation extends CoreCrudOperations> = (Operation extends keyof ExtQueryArgs
? ExtQueryArgs[Operation]
: {}) &
('$create' extends keyof ExtQueryArgs
? Operation extends CoreCreateOperations
? ExtQueryArgs['$create']
: {}
: {}) &
('$read' extends keyof ExtQueryArgs ? (Operation extends CoreReadOperations ? ExtQueryArgs['$read'] : {}) : {}) &
('$update' extends keyof ExtQueryArgs
? Operation extends CoreUpdateOperations
? ExtQueryArgs['$update']
: {}
: {}) &
('$delete' extends keyof ExtQueryArgs
? Operation extends CoreDeleteOperations
? ExtQueryArgs['$delete']
: {}
: {}) &
('$all' extends keyof ExtQueryArgs ? ExtQueryArgs['$all'] : {});

/**
* Transaction isolation levels.
*/
Expand Down Expand Up @@ -232,6 +202,11 @@ export type ClientContract<
*/
$disconnect(): Promise<void>;

/**
* Factory for creating zod schemas to validate query args.
*/
get $zod(): ZodSchemaFactory<Schema, Options, ExtQueryArgs>;

/**
* Pushes the schema to the database. For testing purposes only.
* @private
Expand Down Expand Up @@ -317,7 +292,7 @@ export type AllModelOperations<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
Options extends QueryOptions<Schema>,
ExtQueryArgs,
ExtQueryArgs extends ExtQueryArgsBase,
> = CommonModelOperations<Schema, Model, Options, ExtQueryArgs> &
// provider-specific operations
(Schema['provider']['type'] extends 'mysql'
Expand All @@ -341,15 +316,8 @@ export type AllModelOperations<
* });
* ```
*/
createManyAndReturn<
T extends CreateManyAndReturnArgs<Schema, Model, Options> &
ExtractExtQueryArgs<ExtQueryArgs, 'createManyAndReturn'>,
>(
args?: SelectSubset<
T,
CreateManyAndReturnArgs<Schema, Model, Options> &
ExtractExtQueryArgs<ExtQueryArgs, 'createManyAndReturn'>
>,
createManyAndReturn<T extends CreateManyAndReturnArgs<Schema, Model, Options, ExtQueryArgs>>(
args?: SelectSubset<T, CreateManyAndReturnArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, SimplifiedPlainResult<Schema, Model, T, Options>[]>;

/**
Expand All @@ -374,23 +342,16 @@ export type AllModelOperations<
* });
* ```
*/
updateManyAndReturn<
T extends UpdateManyAndReturnArgs<Schema, Model, Options> &
ExtractExtQueryArgs<ExtQueryArgs, 'updateManyAndReturn'>,
>(
args: Subset<
T,
UpdateManyAndReturnArgs<Schema, Model, Options> &
ExtractExtQueryArgs<ExtQueryArgs, 'updateManyAndReturn'>
>,
updateManyAndReturn<T extends UpdateManyAndReturnArgs<Schema, Model, Options, ExtQueryArgs>>(
args: Subset<T, UpdateManyAndReturnArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, SimplifiedPlainResult<Schema, Model, T, Options>[]>;
});

type CommonModelOperations<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
Options extends QueryOptions<Schema>,
ExtQueryArgs,
ExtQueryArgs extends ExtQueryArgsBase,
> = {
/**
* Returns a list of entities.
Expand Down Expand Up @@ -473,8 +434,8 @@ type CommonModelOperations<
* }); // result: `{ _count: { posts: number } }`
* ```
*/
findMany<T extends FindManyArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'findMany'>>(
args?: SelectSubset<T, FindManyArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'findMany'>>,
findMany<T extends FindManyArgs<Schema, Model, Options, ExtQueryArgs>>(
args?: SelectSubset<T, FindManyArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, SimplifiedPlainResult<Schema, Model, T, Options>[]>;

/**
Expand All @@ -483,8 +444,8 @@ type CommonModelOperations<
* @returns a single entity or null if not found
* @see {@link findMany}
*/
findUnique<T extends FindUniqueArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'findUnique'>>(
args: SelectSubset<T, FindUniqueArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'findUnique'>>,
findUnique<T extends FindUniqueArgs<Schema, Model, Options, ExtQueryArgs>>(
args: SelectSubset<T, FindUniqueArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, SimplifiedPlainResult<Schema, Model, T, Options> | null>;

/**
Expand All @@ -493,10 +454,8 @@ type CommonModelOperations<
* @returns a single entity
* @see {@link findMany}
*/
findUniqueOrThrow<
T extends FindUniqueArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'findUnique'>,
>(
args: SelectSubset<T, FindUniqueArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'findUnique'>>,
findUniqueOrThrow<T extends FindUniqueArgs<Schema, Model, Options, ExtQueryArgs>>(
args: SelectSubset<T, FindUniqueArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, SimplifiedPlainResult<Schema, Model, T, Options>>;

/**
Expand All @@ -505,8 +464,8 @@ type CommonModelOperations<
* @returns a single entity or null if not found
* @see {@link findMany}
*/
findFirst<T extends FindFirstArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'findFirst'>>(
args?: SelectSubset<T, FindFirstArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'findFirst'>>,
findFirst<T extends FindFirstArgs<Schema, Model, Options, ExtQueryArgs>>(
args?: SelectSubset<T, FindFirstArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, SimplifiedPlainResult<Schema, Model, T, Options> | null>;

/**
Expand All @@ -515,8 +474,8 @@ type CommonModelOperations<
* @returns a single entity
* @see {@link findMany}
*/
findFirstOrThrow<T extends FindFirstArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'findFirst'>>(
args?: SelectSubset<T, FindFirstArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'findFirst'>>,
findFirstOrThrow<T extends FindFirstArgs<Schema, Model, Options, ExtQueryArgs>>(
args?: SelectSubset<T, FindFirstArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, SimplifiedPlainResult<Schema, Model, T, Options>>;

/**
Expand Down Expand Up @@ -571,8 +530,8 @@ type CommonModelOperations<
* });
* ```
*/
create<T extends CreateArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'create'>>(
args: SelectSubset<T, CreateArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'create'>>,
create<T extends CreateArgs<Schema, Model, Options, ExtQueryArgs>>(
args: SelectSubset<T, CreateArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, SimplifiedPlainResult<Schema, Model, T, Options>>;

/**
Expand Down Expand Up @@ -600,8 +559,8 @@ type CommonModelOperations<
* });
* ```
*/
createMany<T extends CreateManyArgs<Schema, Model> & ExtractExtQueryArgs<ExtQueryArgs, 'createMany'>>(
args?: SelectSubset<T, CreateManyArgs<Schema, Model> & ExtractExtQueryArgs<ExtQueryArgs, 'createMany'>>,
createMany<T extends CreateManyArgs<Schema, Model, Options, ExtQueryArgs>>(
args?: SelectSubset<T, CreateManyArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, BatchResult>;

/**
Expand Down Expand Up @@ -721,8 +680,8 @@ type CommonModelOperations<
* });
* ```
*/
update<T extends UpdateArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'update'>>(
args: SelectSubset<T, UpdateArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'update'>>,
update<T extends UpdateArgs<Schema, Model, Options, ExtQueryArgs>>(
args: SelectSubset<T, UpdateArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, SimplifiedPlainResult<Schema, Model, T, Options>>;

/**
Expand All @@ -745,8 +704,8 @@ type CommonModelOperations<
* limit: 10
* });
*/
updateMany<T extends UpdateManyArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'updateMany'>>(
args: Subset<T, UpdateManyArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'updateMany'>>,
updateMany<T extends UpdateManyArgs<Schema, Model, Options, ExtQueryArgs>>(
args: Subset<T, UpdateManyArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, BatchResult>;

/**
Expand All @@ -769,8 +728,8 @@ type CommonModelOperations<
* });
* ```
*/
upsert<T extends UpsertArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'upsert'>>(
args: SelectSubset<T, UpsertArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'upsert'>>,
upsert<T extends UpsertArgs<Schema, Model, Options, ExtQueryArgs>>(
args: SelectSubset<T, UpsertArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, SimplifiedPlainResult<Schema, Model, T, Options>>;

/**
Expand All @@ -792,8 +751,8 @@ type CommonModelOperations<
* }); // result: `{ id: string; email: string }`
* ```
*/
delete<T extends DeleteArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'delete'>>(
args: SelectSubset<T, DeleteArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'delete'>>,
delete<T extends DeleteArgs<Schema, Model, Options, ExtQueryArgs>>(
args: SelectSubset<T, DeleteArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, SimplifiedPlainResult<Schema, Model, T, Options>>;

/**
Expand All @@ -815,8 +774,8 @@ type CommonModelOperations<
* });
* ```
*/
deleteMany<T extends DeleteManyArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'deleteMany'>>(
args?: Subset<T, DeleteManyArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'deleteMany'>>,
deleteMany<T extends DeleteManyArgs<Schema, Model, Options, ExtQueryArgs>>(
args?: Subset<T, DeleteManyArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, BatchResult>;

/**
Expand All @@ -837,8 +796,8 @@ type CommonModelOperations<
* select: { _all: true, email: true }
* }); // result: `{ _all: number, email: number }`
*/
count<T extends CountArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'count'>>(
args?: Subset<T, CountArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'count'>>,
count<T extends CountArgs<Schema, Model, Options, ExtQueryArgs>>(
args?: Subset<T, CountArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, Simplify<CountResult<Schema, Model, T>>>;

/**
Expand All @@ -858,8 +817,8 @@ type CommonModelOperations<
* _max: { age: true }
* }); // result: `{ _count: number, _avg: { age: number }, ... }`
*/
aggregate<T extends AggregateArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'aggregate'>>(
args: Subset<T, AggregateArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'aggregate'>>,
aggregate<T extends AggregateArgs<Schema, Model, Options, ExtQueryArgs>>(
args: Subset<T, AggregateArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, Simplify<AggregateResult<Schema, Model, T>>>;

/**
Expand Down Expand Up @@ -895,8 +854,8 @@ type CommonModelOperations<
* having: { country: 'US', age: { _avg: { gte: 18 } } }
* });
*/
groupBy<T extends GroupByArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'groupBy'>>(
args: Subset<T, GroupByArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'groupBy'>>,
groupBy<T extends GroupByArgs<Schema, Model, Options, ExtQueryArgs>>(
args: Subset<T, GroupByArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, Simplify<GroupByResult<Schema, Model, T>>>;

/**
Expand All @@ -916,8 +875,8 @@ type CommonModelOperations<
* where: { posts: { some: { published: true } } },
* }); // result: `boolean`
*/
exists<T extends ExistsArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'exists'>>(
args?: Subset<T, ExistsArgs<Schema, Model, Options> & ExtractExtQueryArgs<ExtQueryArgs, 'exists'>>,
exists<T extends ExistsArgs<Schema, Model, Options, ExtQueryArgs>>(
args?: Subset<T, ExistsArgs<Schema, Model, Options, ExtQueryArgs>>,
): ZenStackPromise<Schema, boolean>;
};

Expand All @@ -927,7 +886,7 @@ export type ModelOperations<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
Options extends ClientOptions<Schema> = ClientOptions<Schema>,
ExtQueryArgs = {},
ExtQueryArgs extends ExtQueryArgsBase = {},
> = SliceOperations<AllModelOperations<Schema, Model, Options, ExtQueryArgs>, Schema, Model, Options>;

//#endregion
Expand Down
Loading
Loading