@zenstackhq/runtime 3.0.0-alpha.3 → 3.0.0-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.cjs +266 -240
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +2 -4
- package/dist/client.d.ts +2 -4
- package/dist/client.js +61 -36
- package/dist/client.js.map +1 -1
- package/dist/{contract-0F-AwA7Z.d.cts → contract-DW8XGrtV.d.cts} +725 -697
- package/dist/{contract-0F-AwA7Z.d.ts → contract-DW8XGrtV.d.ts} +725 -697
- package/dist/index.cjs +256 -238
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -5
- package/dist/index.d.ts +3 -5
- package/dist/index.js +53 -35
- package/dist/index.js.map +1 -1
- package/dist/plugins/policy.cjs +34 -45
- package/dist/plugins/policy.cjs.map +1 -1
- package/dist/plugins/policy.d.cts +2 -4
- package/dist/plugins/policy.d.ts +2 -4
- package/dist/plugins/policy.js +6 -7
- package/dist/plugins/policy.js.map +1 -1
- package/dist/schema.cjs.map +1 -1
- package/dist/schema.js.map +1 -1
- package/package.json +12 -14
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { SchemaDef, GetModels, ScalarFields, ForeignKeyFields, GetFields, FieldHasDefault, GetFieldType, FieldIsOptional, RelationFields, RelationFieldType, FieldIsArray, GetEnums, GetEnum, BuiltinType, NonRelationFields, GetModel, FieldDef, GetField, FieldIsRelationArray, RelationInfo, FieldType, FieldIsRelation, DataSourceProviderType, ProcedureDef, DataSourceProvider } from '@zenstackhq/sdk/schema';
|
|
2
1
|
import Decimal, { Decimal as Decimal$1 } from 'decimal.js';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { Model } from '@zenstackhq/language/ast';
|
|
2
|
+
import { SchemaDef, GetModels, ScalarFields, ForeignKeyFields, GetFields, FieldHasDefault, GetFieldType, FieldIsOptional, NonRelationFields, RelationFields, FieldIsArray, RelationFieldType, GetField, FieldIsRelation, GetEnums, GetEnum, BuiltinType, GetModel, FieldDef, FieldIsRelationArray, FieldType, RelationInfo, DataSourceProviderType, DataSourceProvider, ProcedureDef } from '@zenstackhq/sdk/schema';
|
|
3
|
+
import { Generated, Kysely, ExpressionBuilder, OperandExpression, SqlBool, SelectQueryBuilder, Expression, ExpressionWrapper, RootOperationNode, QueryResult, UnknownRow, OperationNode, SqliteDialectConfig, PostgresDialectConfig, KyselyConfig } from 'kysely';
|
|
6
4
|
|
|
5
|
+
type Optional<T extends object, K extends keyof T = keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
7
6
|
type NullableIf<T, Condition extends boolean> = Condition extends true ? T | null : T;
|
|
8
7
|
type WrapType<T, Optional = false, Array = false> = Optional extends true ? T | null : Array extends true ? T[] : T;
|
|
9
8
|
type MapBaseType$1<T> = T extends 'String' ? string : T extends 'Boolean' ? boolean : T extends 'Int' | 'Float' ? number : T extends 'BigInt' ? bigint : T extends 'Decimal' ? Decimal : T extends 'DateTime' ? Date : T extends 'Json' ? JsonValue : unknown;
|
|
@@ -24,7 +23,7 @@ type AtLeast<O extends object, K extends string> = NoExpand<O extends unknown ?
|
|
|
24
23
|
type Without<T, U> = {
|
|
25
24
|
[P in Exclude<keyof T, keyof U>]?: never;
|
|
26
25
|
};
|
|
27
|
-
type XOR<T, U> = T extends object ? U extends object ? (Without<T, U> & U) | (Without<U, T> & T) : U : T;
|
|
26
|
+
type XOR<T, U> = T extends object ? (U extends object ? (Without<T, U> & U) | (Without<U, T> & T) : U) : T;
|
|
28
27
|
type MaybePromise<T> = T | Promise<T>;
|
|
29
28
|
type PrependParameter<Param, Func> = Func extends (...args: any[]) => infer R ? (p: Param, ...args: Parameters<Func>) => R : never;
|
|
30
29
|
type OrUndefinedIf<T, Condition extends boolean> = Condition extends true ? T | undefined : T;
|
|
@@ -157,7 +156,7 @@ type Distinct<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
|
157
156
|
type Cursor<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
158
157
|
cursor?: WhereUniqueInput<Schema, Model>;
|
|
159
158
|
};
|
|
160
|
-
type Select<Schema extends SchemaDef, Model extends GetModels<Schema>, AllowCount extends
|
|
159
|
+
type Select<Schema extends SchemaDef, Model extends GetModels<Schema>, AllowCount extends boolean, AllowRelation extends boolean = true> = {
|
|
161
160
|
[Key in NonRelationFields<Schema, Model>]?: true;
|
|
162
161
|
} & (AllowRelation extends true ? Include<Schema, Model> : {}) & // relation fields
|
|
163
162
|
(AllowCount extends true ? {
|
|
@@ -202,7 +201,7 @@ type OptionalFieldsForCreate<Schema extends SchemaDef, Model extends GetModels<S
|
|
|
202
201
|
};
|
|
203
202
|
type GetRelation<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetFields<Schema, Model>> = GetField<Schema, Model, Field>['relation'];
|
|
204
203
|
type OppositeRelation<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetFields<Schema, Model>, FT = FieldType<Schema, Model, Field>> = FT extends GetModels<Schema> ? GetRelation<Schema, Model, Field> extends RelationInfo ? GetRelation<Schema, Model, Field>['opposite'] extends GetFields<Schema, FT> ? Schema['models'][FT]['fields'][GetRelation<Schema, Model, Field>['opposite']]['relation'] : never : never : never;
|
|
205
|
-
type OppositeRelationFields<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetFields<Schema, Model>, Opposite = OppositeRelation<Schema, Model, Field>> = Opposite extends RelationInfo ? Opposite['fields'] extends string[] ? Opposite['fields'] : [] : [];
|
|
204
|
+
type OppositeRelationFields<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetFields<Schema, Model>, Opposite = OppositeRelation<Schema, Model, Field>> = Opposite extends RelationInfo ? (Opposite['fields'] extends string[] ? Opposite['fields'] : []) : [];
|
|
206
205
|
type OppositeRelationAndFK<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetFields<Schema, Model>, FT = FieldType<Schema, Model, Field>, Relation = GetField<Schema, Model, Field>['relation'], Opposite = Relation extends RelationInfo ? Relation['opposite'] : never> = FT extends GetModels<Schema> ? Opposite extends GetFields<Schema, FT> ? Opposite | OppositeRelationFields<Schema, Model, Field>[number] : never : never;
|
|
207
206
|
type FindArgs<Schema extends SchemaDef, Model extends GetModels<Schema>, Collection extends boolean, AllowFilter extends boolean = true> = (Collection extends true ? {
|
|
208
207
|
skip?: number;
|
|
@@ -448,603 +447,82 @@ type NestedUpsertInput<Schema extends SchemaDef, Model extends GetModels<Schema>
|
|
|
448
447
|
type NestedUpdateManyInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> = OrArray<UpdateManyPayload<Schema, RelationFieldType<Schema, Model, Field>, OppositeRelationAndFK<Schema, Model, Field>>>;
|
|
449
448
|
type NestedDeleteInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> = FieldIsArray<Schema, Model, Field> extends true ? OrArray<WhereUniqueInput<Schema, RelationFieldType<Schema, Model, Field>>, true> : boolean | WhereInput<Schema, RelationFieldType<Schema, Model, Field>>;
|
|
450
449
|
type NestedDeleteManyInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> = OrArray<WhereInput<Schema, RelationFieldType<Schema, Model, Field>, true>>;
|
|
451
|
-
|
|
450
|
+
|
|
451
|
+
type AuthType<Schema extends SchemaDef> = string extends GetModels<Schema> ? Record<string, unknown> : Schema['authType'] extends GetModels<Schema> ? Partial<ModelResult<Schema, Schema['authType']>> : never;
|
|
452
|
+
|
|
453
|
+
declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
|
|
454
|
+
protected readonly schema: Schema;
|
|
455
|
+
protected readonly options: ClientOptions<Schema>;
|
|
456
|
+
constructor(schema: Schema, options: ClientOptions<Schema>);
|
|
457
|
+
abstract get provider(): DataSourceProviderType;
|
|
458
|
+
transformPrimitive(value: unknown, _type: BuiltinType): unknown;
|
|
459
|
+
abstract buildRelationSelection(query: SelectQueryBuilder<any, any, any>, model: string, relationField: string, parentAlias: string, payload: true | FindArgs<Schema, GetModels<Schema>, true>): SelectQueryBuilder<any, any, any>;
|
|
460
|
+
abstract buildSkipTake(query: SelectQueryBuilder<any, any, any>, skip: number | undefined, take: number | undefined): SelectQueryBuilder<any, any, any>;
|
|
461
|
+
buildFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, where: boolean | object | undefined): Expression<SqlBool>;
|
|
462
|
+
protected buildCompositeFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, key: 'AND' | 'OR' | 'NOT', payload: any): Expression<SqlBool>;
|
|
463
|
+
private buildRelationFilter;
|
|
464
|
+
private buildToOneRelationFilter;
|
|
465
|
+
private buildToManyRelationFilter;
|
|
466
|
+
private buildArrayFilter;
|
|
467
|
+
buildPrimitiveFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, field: string, fieldDef: FieldDef, payload: any): Expression<SqlBool>;
|
|
468
|
+
private buildLiteralFilter;
|
|
469
|
+
private buildStandardFilter;
|
|
470
|
+
private buildStringFilter;
|
|
471
|
+
private prepStringCasing;
|
|
472
|
+
private buildNumberFilter;
|
|
473
|
+
private buildBooleanFilter;
|
|
474
|
+
private buildDateTimeFilter;
|
|
475
|
+
private buildBytesFilter;
|
|
476
|
+
private buildEnumFilter;
|
|
477
|
+
buildOrderBy(query: SelectQueryBuilder<any, any, any>, model: string, modelAlias: string, orderBy: OrArray<OrderBy<Schema, GetModels<Schema>, boolean, boolean>> | undefined, useDefaultIfEmpty: boolean, negated: boolean): SelectQueryBuilder<any, any, any>;
|
|
478
|
+
private negateSort;
|
|
479
|
+
true(eb: ExpressionBuilder<any, any>): Expression<SqlBool>;
|
|
480
|
+
false(eb: ExpressionBuilder<any, any>): Expression<SqlBool>;
|
|
481
|
+
isTrue(expression: Expression<SqlBool>): boolean;
|
|
482
|
+
isFalse(expression: Expression<SqlBool>): boolean;
|
|
483
|
+
protected and(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): Expression<SqlBool>;
|
|
484
|
+
protected or(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): Expression<SqlBool>;
|
|
485
|
+
protected not(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): ExpressionWrapper<any, any, SqlBool>;
|
|
452
486
|
/**
|
|
453
|
-
*
|
|
454
|
-
* @param args - query args
|
|
455
|
-
* @returns a list of entities
|
|
456
|
-
*
|
|
457
|
-
* @example
|
|
458
|
-
* ```ts
|
|
459
|
-
* // find all users and return all scalar fields
|
|
460
|
-
* await client.user.findMany();
|
|
461
|
-
*
|
|
462
|
-
* // find all users with name 'Alex'
|
|
463
|
-
* await client.user.findMany({
|
|
464
|
-
* where: {
|
|
465
|
-
* name: 'Alex'
|
|
466
|
-
* }
|
|
467
|
-
* });
|
|
468
|
-
*
|
|
469
|
-
* // select fields
|
|
470
|
-
* await client.user.findMany({
|
|
471
|
-
* select: {
|
|
472
|
-
* name: true,
|
|
473
|
-
* email: true,
|
|
474
|
-
* }
|
|
475
|
-
* }); // result: `Array<{ name: string, email: string }>`
|
|
476
|
-
*
|
|
477
|
-
* // omit fields
|
|
478
|
-
* await client.user.findMany({
|
|
479
|
-
* omit: {
|
|
480
|
-
* name: true,
|
|
481
|
-
* }
|
|
482
|
-
* }); // result: `Array<{ id: number; email: string; ... }>`
|
|
483
|
-
*
|
|
484
|
-
* // include relations (and all scalar fields)
|
|
485
|
-
* await client.user.findMany({
|
|
486
|
-
* include: {
|
|
487
|
-
* posts: true,
|
|
488
|
-
* }
|
|
489
|
-
* }); // result: `Array<{ ...; posts: Post[] }>`
|
|
490
|
-
*
|
|
491
|
-
* // include relations with filter
|
|
492
|
-
* await client.user.findMany({
|
|
493
|
-
* include: {
|
|
494
|
-
* posts: {
|
|
495
|
-
* where: {
|
|
496
|
-
* published: true
|
|
497
|
-
* }
|
|
498
|
-
* }
|
|
499
|
-
* }
|
|
500
|
-
* });
|
|
501
|
-
*
|
|
502
|
-
* // pagination and sorting
|
|
503
|
-
* await client.user.findMany({
|
|
504
|
-
* skip: 10,
|
|
505
|
-
* take: 10,
|
|
506
|
-
* orderBy: [{ name: 'asc' }, { email: 'desc' }],
|
|
507
|
-
* });
|
|
508
|
-
*
|
|
509
|
-
* // pagination with cursor (https://www.prisma.io/docs/orm/prisma-client/queries/pagination#cursor-based-pagination)
|
|
510
|
-
* await client.user.findMany({
|
|
511
|
-
* cursor: { id: 10 },
|
|
512
|
-
* skip: 1,
|
|
513
|
-
* take: 10,
|
|
514
|
-
* orderBy: { id: 'asc' },
|
|
515
|
-
* });
|
|
516
|
-
*
|
|
517
|
-
* // distinct
|
|
518
|
-
* await client.user.findMany({
|
|
519
|
-
* distinct: ['name']
|
|
520
|
-
* });
|
|
521
|
-
*
|
|
522
|
-
* // count all relations
|
|
523
|
-
* await client.user.findMany({
|
|
524
|
-
* _count: true,
|
|
525
|
-
* }); // result: `{ _count: { posts: number; ... } }`
|
|
526
|
-
*
|
|
527
|
-
* // count selected relations
|
|
528
|
-
* await client.user.findMany({
|
|
529
|
-
* _count: { select: { posts: true } },
|
|
530
|
-
* }); // result: `{ _count: { posts: number } }`
|
|
531
|
-
* ```
|
|
487
|
+
* Builds an Kysely expression that returns a JSON object for the given key-value pairs.
|
|
532
488
|
*/
|
|
533
|
-
|
|
489
|
+
abstract buildJsonObject(eb: ExpressionBuilder<any, any>, value: Record<string, Expression<unknown>>): ExpressionWrapper<any, any, unknown>;
|
|
534
490
|
/**
|
|
535
|
-
*
|
|
536
|
-
* @param args - query args
|
|
537
|
-
* @returns a single entity or null if not found
|
|
538
|
-
* @see {@link findMany}
|
|
491
|
+
* Builds an Kysely expression that returns the length of an array.
|
|
539
492
|
*/
|
|
540
|
-
|
|
493
|
+
abstract buildArrayLength(eb: ExpressionBuilder<any, any>, array: Expression<unknown>): ExpressionWrapper<any, any, number>;
|
|
541
494
|
/**
|
|
542
|
-
*
|
|
543
|
-
* @param args - query args
|
|
544
|
-
* @returns a single entity
|
|
545
|
-
* @see {@link findMany}
|
|
495
|
+
* Builds an array literal SQL string for the given values.
|
|
546
496
|
*/
|
|
547
|
-
|
|
497
|
+
abstract buildArrayLiteralSQL(values: unknown[]): string;
|
|
548
498
|
/**
|
|
549
|
-
*
|
|
550
|
-
* @param args - query args
|
|
551
|
-
* @returns a single entity or null if not found
|
|
552
|
-
* @see {@link findMany}
|
|
499
|
+
* Whether the dialect supports updating with a limit on the number of updated rows.
|
|
553
500
|
*/
|
|
554
|
-
|
|
501
|
+
abstract get supportsUpdateWithLimit(): boolean;
|
|
555
502
|
/**
|
|
556
|
-
*
|
|
557
|
-
* @param args - query args
|
|
558
|
-
* @returns a single entity
|
|
559
|
-
* @see {@link findMany}
|
|
503
|
+
* Whether the dialect supports deleting with a limit on the number of deleted rows.
|
|
560
504
|
*/
|
|
561
|
-
|
|
505
|
+
abstract get supportsDeleteWithLimit(): boolean;
|
|
562
506
|
/**
|
|
563
|
-
*
|
|
564
|
-
* @param args - create args
|
|
565
|
-
* @returns the created entity
|
|
566
|
-
*
|
|
567
|
-
* @example
|
|
568
|
-
* ```ts
|
|
569
|
-
* // simple create
|
|
570
|
-
* await client.user.create({
|
|
571
|
-
* data: { name: 'Alex', email: 'alex@zenstack.dev' }
|
|
572
|
-
* });
|
|
573
|
-
*
|
|
574
|
-
* // nested create with relation
|
|
575
|
-
* await client.user.create({
|
|
576
|
-
* data: {
|
|
577
|
-
* email: 'alex@zenstack.dev',
|
|
578
|
-
* posts: { create: { title: 'Hello World' } }
|
|
579
|
-
* }
|
|
580
|
-
* });
|
|
581
|
-
*
|
|
582
|
-
* // you can use `select`, `omit`, and `include` to control
|
|
583
|
-
* // the fields returned by the query, as with `findMany`
|
|
584
|
-
* await client.user.create({
|
|
585
|
-
* data: {
|
|
586
|
-
* email: 'alex@zenstack.dev',
|
|
587
|
-
* posts: { create: { title: 'Hello World' } }
|
|
588
|
-
* },
|
|
589
|
-
* include: { posts: true }
|
|
590
|
-
* }); // result: `{ id: number; posts: Post[] }`
|
|
591
|
-
*
|
|
592
|
-
* // connect relations
|
|
593
|
-
* await client.user.create({
|
|
594
|
-
* data: {
|
|
595
|
-
* email: 'alex@zenstack.dev',
|
|
596
|
-
* posts: { connect: { id: 1 } }
|
|
597
|
-
* }
|
|
598
|
-
* });
|
|
599
|
-
*
|
|
600
|
-
* // connect relations, and create if not found
|
|
601
|
-
* await client.user.create({
|
|
602
|
-
* data: {
|
|
603
|
-
* email: 'alex@zenstack.dev',
|
|
604
|
-
* posts: {
|
|
605
|
-
* connectOrCreate: {
|
|
606
|
-
* where: { id: 1 },
|
|
607
|
-
* create: { title: 'Hello World' }
|
|
608
|
-
* }
|
|
609
|
-
* }
|
|
610
|
-
* }
|
|
611
|
-
* });
|
|
612
|
-
* ```
|
|
507
|
+
* Whether the dialect supports DISTINCT ON.
|
|
613
508
|
*/
|
|
614
|
-
|
|
509
|
+
abstract get supportsDistinctOn(): boolean;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
type CrudOperation = 'findMany' | 'findUnique' | 'findFirst' | 'create' | 'createMany' | 'createManyAndReturn' | 'update' | 'updateMany' | 'updateManyAndReturn' | 'upsert' | 'delete' | 'deleteMany' | 'count' | 'aggregate' | 'groupBy';
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* The result of the hooks interception filter.
|
|
516
|
+
*/
|
|
517
|
+
type MutationInterceptionFilterResult = {
|
|
615
518
|
/**
|
|
616
|
-
*
|
|
617
|
-
* @param args - create args
|
|
618
|
-
* @returns count of created entities: `{ count: number }`
|
|
619
|
-
*
|
|
620
|
-
* @example
|
|
621
|
-
* ```ts
|
|
622
|
-
* // create multiple entities
|
|
623
|
-
* await client.user.createMany({
|
|
624
|
-
* data: [
|
|
625
|
-
* { name: 'Alex', email: 'alex@zenstack.dev' },
|
|
626
|
-
* { name: 'John', email: 'john@zenstack.dev' }
|
|
627
|
-
* ]
|
|
628
|
-
* });
|
|
629
|
-
*
|
|
630
|
-
* // skip items that cause unique constraint violation
|
|
631
|
-
* await client.user.createMany({
|
|
632
|
-
* data: [
|
|
633
|
-
* { name: 'Alex', email: 'alex@zenstack.dev' },
|
|
634
|
-
* { name: 'John', email: 'john@zenstack.dev' }
|
|
635
|
-
* ],
|
|
636
|
-
* skipDuplicates: true
|
|
637
|
-
* });
|
|
638
|
-
* ```
|
|
519
|
+
* Whether to intercept the mutation or not.
|
|
639
520
|
*/
|
|
640
|
-
|
|
521
|
+
intercept: boolean;
|
|
641
522
|
/**
|
|
642
|
-
*
|
|
643
|
-
* @param args - create args. See {@link createMany} for input. Use
|
|
644
|
-
* `select` and `omit` to control the fields returned.
|
|
645
|
-
* @returns the created entities
|
|
646
|
-
*
|
|
647
|
-
* @example
|
|
648
|
-
* ```ts
|
|
649
|
-
* // create multiple entities and return selected fields
|
|
650
|
-
* await client.user.createManyAndReturn({
|
|
651
|
-
* data: [
|
|
652
|
-
* { name: 'Alex', email: 'alex@zenstack.dev' },
|
|
653
|
-
* { name: 'John', email: 'john@zenstack.dev' }
|
|
654
|
-
* ],
|
|
655
|
-
* select: { id: true, email: true }
|
|
656
|
-
* });
|
|
657
|
-
* ```
|
|
523
|
+
* Whether to use a transaction for the mutation.
|
|
658
524
|
*/
|
|
659
|
-
|
|
660
|
-
/**
|
|
661
|
-
* Updates a uniquely identified entity.
|
|
662
|
-
* @param args - update args. See {@link findMany} for how to control
|
|
663
|
-
* fields and relations returned.
|
|
664
|
-
* @returns the updated entity. Throws `NotFoundError` if the entity is not found.
|
|
665
|
-
*
|
|
666
|
-
* @example
|
|
667
|
-
* ```ts
|
|
668
|
-
* // update fields
|
|
669
|
-
* await client.user.update({
|
|
670
|
-
* where: { id: 1 },
|
|
671
|
-
* data: { name: 'Alex' }
|
|
672
|
-
* });
|
|
673
|
-
*
|
|
674
|
-
* // connect a relation
|
|
675
|
-
* await client.user.update({
|
|
676
|
-
* where: { id: 1 },
|
|
677
|
-
* data: { posts: { connect: { id: 1 } } }
|
|
678
|
-
* });
|
|
679
|
-
*
|
|
680
|
-
* // connect relation, and create if not found
|
|
681
|
-
* await client.user.update({
|
|
682
|
-
* where: { id: 1 },
|
|
683
|
-
* data: {
|
|
684
|
-
* posts: {
|
|
685
|
-
* connectOrCreate: {
|
|
686
|
-
* where: { id: 1 },
|
|
687
|
-
* create: { title: 'Hello World' }
|
|
688
|
-
* }
|
|
689
|
-
* }
|
|
690
|
-
* }
|
|
691
|
-
* });
|
|
692
|
-
*
|
|
693
|
-
* // create many related entities (only available for one-to-many relations)
|
|
694
|
-
* await client.user.update({
|
|
695
|
-
* where: { id: 1 },
|
|
696
|
-
* data: {
|
|
697
|
-
* posts: {
|
|
698
|
-
* createMany: {
|
|
699
|
-
* data: [{ title: 'Hello World' }, { title: 'Hello World 2' }],
|
|
700
|
-
* }
|
|
701
|
-
* }
|
|
702
|
-
* }
|
|
703
|
-
* });
|
|
704
|
-
*
|
|
705
|
-
* // disconnect a one-to-many relation
|
|
706
|
-
* await client.user.update({
|
|
707
|
-
* where: { id: 1 },
|
|
708
|
-
* data: { posts: { disconnect: { id: 1 } } }
|
|
709
|
-
* });
|
|
710
|
-
*
|
|
711
|
-
* // disconnect a one-to-one relation
|
|
712
|
-
* await client.user.update({
|
|
713
|
-
* where: { id: 1 },
|
|
714
|
-
* data: { profile: { disconnect: true } }
|
|
715
|
-
* });
|
|
716
|
-
*
|
|
717
|
-
* // replace a relation (only available for one-to-many relations)
|
|
718
|
-
* await client.user.update({
|
|
719
|
-
* where: { id: 1 },
|
|
720
|
-
* data: {
|
|
721
|
-
* posts: {
|
|
722
|
-
* set: [{ id: 1 }, { id: 2 }]
|
|
723
|
-
* }
|
|
724
|
-
* }
|
|
725
|
-
* });
|
|
726
|
-
*
|
|
727
|
-
* // update a relation
|
|
728
|
-
* await client.user.update({
|
|
729
|
-
* where: { id: 1 },
|
|
730
|
-
* data: {
|
|
731
|
-
* posts: {
|
|
732
|
-
* update: { where: { id: 1 }, data: { title: 'Hello World' } }
|
|
733
|
-
* }
|
|
734
|
-
* }
|
|
735
|
-
* });
|
|
736
|
-
*
|
|
737
|
-
* // upsert a relation
|
|
738
|
-
* await client.user.update({
|
|
739
|
-
* where: { id: 1 },
|
|
740
|
-
* data: {
|
|
741
|
-
* posts: {
|
|
742
|
-
* upsert: {
|
|
743
|
-
* where: { id: 1 },
|
|
744
|
-
* create: { title: 'Hello World' },
|
|
745
|
-
* update: { title: 'Hello World' }
|
|
746
|
-
* }
|
|
747
|
-
* }
|
|
748
|
-
* }
|
|
749
|
-
* });
|
|
750
|
-
*
|
|
751
|
-
* // update many related entities (only available for one-to-many relations)
|
|
752
|
-
* await client.user.update({
|
|
753
|
-
* where: { id: 1 },
|
|
754
|
-
* data: {
|
|
755
|
-
* posts: {
|
|
756
|
-
* updateMany: {
|
|
757
|
-
* where: { published: true },
|
|
758
|
-
* data: { title: 'Hello World' }
|
|
759
|
-
* }
|
|
760
|
-
* }
|
|
761
|
-
* }
|
|
762
|
-
* });
|
|
763
|
-
*
|
|
764
|
-
* // delete a one-to-many relation
|
|
765
|
-
* await client.user.update({
|
|
766
|
-
* where: { id: 1 },
|
|
767
|
-
* data: { posts: { delete: { id: 1 } } }
|
|
768
|
-
* });
|
|
769
|
-
*
|
|
770
|
-
* // delete a one-to-one relation
|
|
771
|
-
* await client.user.update({
|
|
772
|
-
* where: { id: 1 },
|
|
773
|
-
* data: { profile: { delete: true } }
|
|
774
|
-
* });
|
|
775
|
-
* ```
|
|
776
|
-
*/
|
|
777
|
-
update<T extends UpdateArgs<Schema, Model>>(args: SelectSubset<T, UpdateArgs<Schema, Model>>): Promise<ModelResult<Schema, Model, T>>;
|
|
778
|
-
/**
|
|
779
|
-
* Updates multiple entities.
|
|
780
|
-
* @param args - update args. Only scalar fields are allowed for data.
|
|
781
|
-
* @returns count of updated entities: `{ count: number }`
|
|
782
|
-
*
|
|
783
|
-
* @example
|
|
784
|
-
* ```ts
|
|
785
|
-
* // update many entities
|
|
786
|
-
* await client.user.updateMany({
|
|
787
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
788
|
-
* data: { role: 'ADMIN' }
|
|
789
|
-
* });
|
|
790
|
-
*
|
|
791
|
-
* // limit the number of updated entities
|
|
792
|
-
* await client.user.updateMany({
|
|
793
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
794
|
-
* data: { role: 'ADMIN' },
|
|
795
|
-
* limit: 10
|
|
796
|
-
* });
|
|
797
|
-
*/
|
|
798
|
-
updateMany<T extends UpdateManyArgs<Schema, Model>>(args: Subset<T, UpdateManyArgs<Schema, Model>>): Promise<BatchResult>;
|
|
799
|
-
/**
|
|
800
|
-
* Updates multiple entities and returns them.
|
|
801
|
-
* @param args - update args. Only scalar fields are allowed for data.
|
|
802
|
-
* @returns the updated entities
|
|
803
|
-
*
|
|
804
|
-
* @example
|
|
805
|
-
* ```ts
|
|
806
|
-
* // update many entities and return selected fields
|
|
807
|
-
* await client.user.updateManyAndReturn({
|
|
808
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
809
|
-
* data: { role: 'ADMIN' },
|
|
810
|
-
* select: { id: true, email: true }
|
|
811
|
-
* }); // result: `Array<{ id: string; email: string }>`
|
|
812
|
-
*
|
|
813
|
-
* // limit the number of updated entities
|
|
814
|
-
* await client.user.updateManyAndReturn({
|
|
815
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
816
|
-
* data: { role: 'ADMIN' },
|
|
817
|
-
* limit: 10
|
|
818
|
-
* });
|
|
819
|
-
* ```
|
|
820
|
-
*/
|
|
821
|
-
updateManyAndReturn<T extends UpdateManyAndReturnArgs<Schema, Model>>(args: Subset<T, UpdateManyAndReturnArgs<Schema, Model>>): Promise<ModelResult<Schema, Model, T>[]>;
|
|
822
|
-
/**
|
|
823
|
-
* Creates or updates an entity.
|
|
824
|
-
* @param args - upsert args
|
|
825
|
-
* @returns the upserted entity
|
|
826
|
-
*
|
|
827
|
-
* @example
|
|
828
|
-
* ```ts
|
|
829
|
-
* // upsert an entity
|
|
830
|
-
* await client.user.upsert({
|
|
831
|
-
* // `where` clause is used to find the entity
|
|
832
|
-
* where: { id: 1 },
|
|
833
|
-
* // `create` clause is used if the entity is not found
|
|
834
|
-
* create: { email: 'alex@zenstack.dev', name: 'Alex' },
|
|
835
|
-
* // `update` clause is used if the entity is found
|
|
836
|
-
* update: { name: 'Alex-new' },
|
|
837
|
-
* // `select` and `omit` can be used to control the returned fields
|
|
838
|
-
* ...
|
|
839
|
-
* });
|
|
840
|
-
* ```
|
|
841
|
-
*/
|
|
842
|
-
upsert<T extends UpsertArgs<Schema, Model>>(args: SelectSubset<T, UpsertArgs<Schema, Model>>): Promise<ModelResult<Schema, Model, T>>;
|
|
843
|
-
/**
|
|
844
|
-
* Deletes a uniquely identifiable entity.
|
|
845
|
-
* @param args - delete args
|
|
846
|
-
* @returns the deleted entity. Throws `NotFoundError` if the entity is not found.
|
|
847
|
-
*
|
|
848
|
-
* @example
|
|
849
|
-
* ```ts
|
|
850
|
-
* // delete an entity
|
|
851
|
-
* await client.user.delete({
|
|
852
|
-
* where: { id: 1 }
|
|
853
|
-
* });
|
|
854
|
-
*
|
|
855
|
-
* // delete an entity and return selected fields
|
|
856
|
-
* await client.user.delete({
|
|
857
|
-
* where: { id: 1 },
|
|
858
|
-
* select: { id: true, email: true }
|
|
859
|
-
* }); // result: `{ id: string; email: string }`
|
|
860
|
-
* ```
|
|
861
|
-
*/
|
|
862
|
-
delete<T extends DeleteArgs<Schema, Model>>(args: SelectSubset<T, DeleteArgs<Schema, Model>>): Promise<ModelResult<Schema, Model>>;
|
|
863
|
-
/**
|
|
864
|
-
* Deletes multiple entities.
|
|
865
|
-
* @param args - delete args
|
|
866
|
-
* @returns count of deleted entities: `{ count: number }`
|
|
867
|
-
*
|
|
868
|
-
* @example
|
|
869
|
-
* ```ts
|
|
870
|
-
* // delete many entities
|
|
871
|
-
* await client.user.deleteMany({
|
|
872
|
-
* where: { email: { endsWith: '@zenstack.dev' } }
|
|
873
|
-
* });
|
|
874
|
-
*
|
|
875
|
-
* // limit the number of deleted entities
|
|
876
|
-
* await client.user.deleteMany({
|
|
877
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
878
|
-
* limit: 10
|
|
879
|
-
* });
|
|
880
|
-
* ```
|
|
881
|
-
*/
|
|
882
|
-
deleteMany<T extends DeleteManyArgs<Schema, Model>>(args?: Subset<T, DeleteManyArgs<Schema, Model>>): Promise<BatchResult>;
|
|
883
|
-
/**
|
|
884
|
-
* Counts rows or field values.
|
|
885
|
-
* @param args - count args
|
|
886
|
-
* @returns `number`, or an object containing count of selected relations
|
|
887
|
-
*
|
|
888
|
-
* @example
|
|
889
|
-
* ```ts
|
|
890
|
-
* // count all
|
|
891
|
-
* await client.user.count();
|
|
892
|
-
*
|
|
893
|
-
* // count with a filter
|
|
894
|
-
* await client.user.count({ where: { email: { endsWith: '@zenstack.dev' } } });
|
|
895
|
-
*
|
|
896
|
-
* // count rows and field values
|
|
897
|
-
* await client.user.count({
|
|
898
|
-
* select: { _all: true, email: true }
|
|
899
|
-
* }); // result: `{ _all: number, email: number }`
|
|
900
|
-
*/
|
|
901
|
-
count<T extends CountArgs<Schema, Model>>(args?: Subset<T, CountArgs<Schema, Model>>): Promise<CountResult<Schema, Model, T>>;
|
|
902
|
-
/**
|
|
903
|
-
* Aggregates rows.
|
|
904
|
-
* @param args - aggregation args
|
|
905
|
-
* @returns an object containing aggregated values
|
|
906
|
-
*
|
|
907
|
-
* @example
|
|
908
|
-
* ```ts
|
|
909
|
-
* // aggregate rows
|
|
910
|
-
* await client.profile.aggregate({
|
|
911
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
912
|
-
* _count: true,
|
|
913
|
-
* _avg: { age: true },
|
|
914
|
-
* _sum: { age: true },
|
|
915
|
-
* _min: { age: true },
|
|
916
|
-
* _max: { age: true }
|
|
917
|
-
* }); // result: `{ _count: number, _avg: { age: number }, ... }`
|
|
918
|
-
*/
|
|
919
|
-
aggregate<T extends AggregateArgs<Schema, Model>>(args: Subset<T, AggregateArgs<Schema, Model>>): Promise<AggregateResult<Schema, Model, T>>;
|
|
920
|
-
/**
|
|
921
|
-
* Groups rows by columns.
|
|
922
|
-
* @param args - groupBy args
|
|
923
|
-
* @returns an object containing grouped values
|
|
924
|
-
*
|
|
925
|
-
* @example
|
|
926
|
-
* ```ts
|
|
927
|
-
* // group by a field
|
|
928
|
-
* await client.profile.groupBy({
|
|
929
|
-
* by: 'country',
|
|
930
|
-
* _count: true
|
|
931
|
-
* }); // result: `Array<{ country: string, _count: number }>`
|
|
932
|
-
*
|
|
933
|
-
* // group by multiple fields
|
|
934
|
-
* await client.profile.groupBy({
|
|
935
|
-
* by: ['country', 'city'],
|
|
936
|
-
* _count: true
|
|
937
|
-
* }); // result: `Array<{ country: string, city: string, _count: number }>`
|
|
938
|
-
*
|
|
939
|
-
* // group by with sorting, the `orderBy` fields must be in the `by` list
|
|
940
|
-
* await client.profile.groupBy({
|
|
941
|
-
* by: 'country',
|
|
942
|
-
* orderBy: { country: 'desc' }
|
|
943
|
-
* });
|
|
944
|
-
*
|
|
945
|
-
* // group by with having (post-aggregation filter), the `having` fields must
|
|
946
|
-
* // be in the `by` list
|
|
947
|
-
* await client.profile.groupBy({
|
|
948
|
-
* by: 'country',
|
|
949
|
-
* having: { country: 'US' }
|
|
950
|
-
* });
|
|
951
|
-
*/
|
|
952
|
-
groupBy<T extends GroupByArgs<Schema, Model>>(args: Subset<T, GroupByArgs<Schema, Model>>): Promise<GroupByResult<Schema, Model, T>>;
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
type AuthType<Schema extends SchemaDef> = string extends GetModels<Schema> ? Record<string, unknown> : Schema['authType'] extends GetModels<Schema> ? Partial<ModelResult<Schema, Schema['authType']>> : never;
|
|
956
|
-
|
|
957
|
-
declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
|
|
958
|
-
protected readonly schema: Schema;
|
|
959
|
-
protected readonly options: ClientOptions<Schema>;
|
|
960
|
-
constructor(schema: Schema, options: ClientOptions<Schema>);
|
|
961
|
-
abstract get provider(): DataSourceProviderType;
|
|
962
|
-
transformPrimitive(value: unknown, _type: BuiltinType): unknown;
|
|
963
|
-
abstract buildRelationSelection(query: SelectQueryBuilder<any, any, any>, model: string, relationField: string, parentAlias: string, payload: true | FindArgs<Schema, GetModels<Schema>, true>): SelectQueryBuilder<any, any, any>;
|
|
964
|
-
abstract buildSkipTake(query: SelectQueryBuilder<any, any, any>, skip: number | undefined, take: number | undefined): SelectQueryBuilder<any, any, any>;
|
|
965
|
-
buildFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, where: boolean | object | undefined): Expression<SqlBool>;
|
|
966
|
-
protected buildCompositeFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, key: 'AND' | 'OR' | 'NOT', payload: any): Expression<SqlBool>;
|
|
967
|
-
private buildRelationFilter;
|
|
968
|
-
private buildToOneRelationFilter;
|
|
969
|
-
private buildToManyRelationFilter;
|
|
970
|
-
private buildArrayFilter;
|
|
971
|
-
buildPrimitiveFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, field: string, fieldDef: FieldDef, payload: any): Expression<SqlBool>;
|
|
972
|
-
private buildLiteralFilter;
|
|
973
|
-
private buildStandardFilter;
|
|
974
|
-
private buildStringFilter;
|
|
975
|
-
private prepStringCasing;
|
|
976
|
-
private buildNumberFilter;
|
|
977
|
-
private buildBooleanFilter;
|
|
978
|
-
private buildDateTimeFilter;
|
|
979
|
-
private buildBytesFilter;
|
|
980
|
-
private buildEnumFilter;
|
|
981
|
-
buildOrderBy(query: SelectQueryBuilder<any, any, any>, model: string, modelAlias: string, orderBy: OrArray<OrderBy<Schema, GetModels<Schema>, boolean, boolean>> | undefined, useDefaultIfEmpty: boolean, negated: boolean): SelectQueryBuilder<any, any, any>;
|
|
982
|
-
private negateSort;
|
|
983
|
-
true(eb: ExpressionBuilder<any, any>): Expression<SqlBool>;
|
|
984
|
-
false(eb: ExpressionBuilder<any, any>): Expression<SqlBool>;
|
|
985
|
-
isTrue(expression: Expression<SqlBool>): boolean;
|
|
986
|
-
isFalse(expression: Expression<SqlBool>): boolean;
|
|
987
|
-
protected and(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): Expression<SqlBool>;
|
|
988
|
-
protected or(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): Expression<SqlBool>;
|
|
989
|
-
protected not(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): ExpressionWrapper<any, any, SqlBool>;
|
|
990
|
-
/**
|
|
991
|
-
* Builds an Kysely expression that returns a JSON object for the given key-value pairs.
|
|
992
|
-
*/
|
|
993
|
-
abstract buildJsonObject(eb: ExpressionBuilder<any, any>, value: Record<string, Expression<unknown>>): ExpressionWrapper<any, any, unknown>;
|
|
994
|
-
/**
|
|
995
|
-
* Builds an Kysely expression that returns the length of an array.
|
|
996
|
-
*/
|
|
997
|
-
abstract buildArrayLength(eb: ExpressionBuilder<any, any>, array: Expression<unknown>): ExpressionWrapper<any, any, number>;
|
|
998
|
-
/**
|
|
999
|
-
* Builds an array literal SQL string for the given values.
|
|
1000
|
-
*/
|
|
1001
|
-
abstract buildArrayLiteralSQL(values: unknown[]): string;
|
|
1002
|
-
/**
|
|
1003
|
-
* Whether the dialect supports updating with a limit on the number of updated rows.
|
|
1004
|
-
*/
|
|
1005
|
-
abstract get supportsUpdateWithLimit(): boolean;
|
|
1006
|
-
/**
|
|
1007
|
-
* Whether the dialect supports deleting with a limit on the number of deleted rows.
|
|
1008
|
-
*/
|
|
1009
|
-
abstract get supportsDeleteWithLimit(): boolean;
|
|
1010
|
-
/**
|
|
1011
|
-
* Whether the dialect supports DISTINCT ON.
|
|
1012
|
-
*/
|
|
1013
|
-
abstract get supportsDistinctOn(): boolean;
|
|
1014
|
-
}
|
|
1015
|
-
|
|
1016
|
-
type CrudOperation = 'findMany' | 'findUnique' | 'findFirst' | 'create' | 'createMany' | 'createManyAndReturn' | 'update' | 'updateMany' | 'updateManyAndReturn' | 'upsert' | 'delete' | 'deleteMany' | 'count' | 'aggregate' | 'groupBy';
|
|
1017
|
-
|
|
1018
|
-
type QueryContext<Schema extends SchemaDef> = {
|
|
1019
|
-
/**
|
|
1020
|
-
* The ZenStack client that's invoking the plugin.
|
|
1021
|
-
*/
|
|
1022
|
-
client: ClientContract<Schema>;
|
|
1023
|
-
/**
|
|
1024
|
-
* The model that is being queried.
|
|
1025
|
-
*/
|
|
1026
|
-
model: GetModels<Schema>;
|
|
1027
|
-
/**
|
|
1028
|
-
* The query operation that is being performed.
|
|
1029
|
-
*/
|
|
1030
|
-
operation: CrudOperation;
|
|
1031
|
-
/**
|
|
1032
|
-
* The query arguments.
|
|
1033
|
-
*/
|
|
1034
|
-
queryArgs: unknown;
|
|
1035
|
-
};
|
|
1036
|
-
/**
|
|
1037
|
-
* The result of the hooks interception filter.
|
|
1038
|
-
*/
|
|
1039
|
-
type MutationInterceptionFilterResult = {
|
|
1040
|
-
/**
|
|
1041
|
-
* Whether to intercept the mutation or not.
|
|
1042
|
-
*/
|
|
1043
|
-
intercept: boolean;
|
|
1044
|
-
/**
|
|
1045
|
-
* Whether to use a transaction for the mutation.
|
|
1046
|
-
*/
|
|
1047
|
-
useTransactionForMutation?: boolean;
|
|
525
|
+
useTransactionForMutation?: boolean;
|
|
1048
526
|
/**
|
|
1049
527
|
* Whether entities should be loaded before the mutation.
|
|
1050
528
|
*/
|
|
@@ -1068,9 +546,6 @@ type MutationHooksArgs<Schema extends SchemaDef> = {
|
|
|
1068
546
|
*/
|
|
1069
547
|
queryNode: OperationNode;
|
|
1070
548
|
};
|
|
1071
|
-
type OnQueryArgs<Schema extends SchemaDef> = QueryContext<Schema> & {
|
|
1072
|
-
proceed: ProceedQueryFunction<Schema>;
|
|
1073
|
-
};
|
|
1074
549
|
type PluginBeforeEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
|
|
1075
550
|
entities?: Record<string, unknown>[];
|
|
1076
551
|
};
|
|
@@ -1078,7 +553,6 @@ type PluginAfterEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs
|
|
|
1078
553
|
beforeMutationEntities?: Record<string, unknown>[];
|
|
1079
554
|
afterMutationEntities?: Record<string, unknown>[];
|
|
1080
555
|
};
|
|
1081
|
-
type ProceedQueryFunction<Schema extends SchemaDef> = (queryArgs: unknown, tx?: ClientContract<Schema>) => Promise<unknown>;
|
|
1082
556
|
type OnKyselyQueryTransactionCallback = (proceed: ProceedKyselyQueryFunction) => Promise<QueryResult<any>>;
|
|
1083
557
|
type OnKyselyQueryTransaction = (callback: OnKyselyQueryTransactionCallback) => Promise<QueryResult<any>>;
|
|
1084
558
|
type OnKyselyQueryArgs<Schema extends SchemaDef> = {
|
|
@@ -1109,7 +583,7 @@ interface RuntimePlugin<Schema extends SchemaDef = SchemaDef> {
|
|
|
1109
583
|
/**
|
|
1110
584
|
* Intercepts an ORM query.
|
|
1111
585
|
*/
|
|
1112
|
-
onQuery?:
|
|
586
|
+
onQuery?: OnQueryHooks<Schema>;
|
|
1113
587
|
/**
|
|
1114
588
|
* Intercepts a Kysely query.
|
|
1115
589
|
*/
|
|
@@ -1132,141 +606,695 @@ interface RuntimePlugin<Schema extends SchemaDef = SchemaDef> {
|
|
|
1132
606
|
* @param args.afterMutationEntity Only available if `loadAfterMutationEntity` is set to true in the
|
|
1133
607
|
* return value of {@link RuntimePlugin.mutationInterceptionFilter}.
|
|
1134
608
|
*/
|
|
1135
|
-
afterEntityMutation?: (args: PluginAfterEntityMutationArgs<Schema>) => MaybePromise<void>;
|
|
1136
|
-
}
|
|
1137
|
-
type
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
};
|
|
1142
|
-
type
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
609
|
+
afterEntityMutation?: (args: PluginAfterEntityMutationArgs<Schema>) => MaybePromise<void>;
|
|
610
|
+
}
|
|
611
|
+
type OnQueryHooks<Schema extends SchemaDef = SchemaDef> = {
|
|
612
|
+
[Model in GetModels<Schema> as Uncapitalize<Model>]?: OnQueryOperationHooks<Schema, Model>;
|
|
613
|
+
} & {
|
|
614
|
+
$allModels?: OnQueryOperationHooks<Schema, GetModels<Schema>>;
|
|
615
|
+
};
|
|
616
|
+
type OnQueryOperationHooks<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
617
|
+
[Operation in keyof ModelOperations<Schema, Model>]?: (ctx: OnQueryHookContext<Schema, Model, Operation>) => ReturnType<ModelOperations<Schema, Model>[Operation]>;
|
|
618
|
+
} & {
|
|
619
|
+
$allOperations?: (ctx: {
|
|
620
|
+
model: Model;
|
|
621
|
+
operation: CrudOperation;
|
|
622
|
+
args: unknown;
|
|
623
|
+
query: (args: unknown) => Promise<unknown>;
|
|
624
|
+
}) => MaybePromise<unknown>;
|
|
625
|
+
};
|
|
626
|
+
type OnQueryHookContext<Schema extends SchemaDef, Model extends GetModels<Schema>, Operation extends keyof ModelOperations<Schema, Model>> = {
|
|
627
|
+
/**
|
|
628
|
+
* The model that is being queried.
|
|
629
|
+
*/
|
|
630
|
+
model: Model;
|
|
631
|
+
/**
|
|
632
|
+
* The operation that is being performed.
|
|
633
|
+
*/
|
|
634
|
+
operation: Operation;
|
|
635
|
+
/**
|
|
636
|
+
* The query arguments.
|
|
637
|
+
*/
|
|
638
|
+
args: Parameters<ModelOperations<Schema, Model>[Operation]>[0];
|
|
639
|
+
/**
|
|
640
|
+
* The query function to proceed with the original query.
|
|
641
|
+
* It takes the same arguments as the operation method.
|
|
642
|
+
*
|
|
643
|
+
* @param args The query arguments.
|
|
644
|
+
* @param tx Optional transaction client to use for the query.
|
|
645
|
+
*/
|
|
646
|
+
query: (args: Parameters<ModelOperations<Schema, Model>[Operation]>[0], tx?: ClientContract<Schema>) => ReturnType<ModelOperations<Schema, Model>[Operation]>;
|
|
647
|
+
/**
|
|
648
|
+
* The ZenStack client that is performing the operation.
|
|
649
|
+
*/
|
|
650
|
+
client: ClientContract<Schema>;
|
|
651
|
+
};
|
|
652
|
+
/**
|
|
653
|
+
* Defines a ZenStack runtime plugin.
|
|
654
|
+
*/
|
|
655
|
+
declare function definePlugin<Schema extends SchemaDef>(plugin: RuntimePlugin<Schema>): RuntimePlugin<Schema>;
|
|
656
|
+
|
|
657
|
+
type DialectConfig<Provider extends DataSourceProvider> = Provider['type'] extends 'sqlite' ? Optional<SqliteDialectConfig, 'database'> : Provider['type'] extends 'postgresql' ? Optional<PostgresDialectConfig, 'pool'> : never;
|
|
658
|
+
type ZModelFunctionContext<Schema extends SchemaDef> = {
|
|
659
|
+
dialect: BaseCrudDialect<Schema>;
|
|
660
|
+
model: GetModels<Schema>;
|
|
661
|
+
operation: CRUD;
|
|
662
|
+
};
|
|
663
|
+
type ZModelFunction<Schema extends SchemaDef> = (eb: ExpressionBuilder<ToKyselySchema<Schema>, keyof ToKyselySchema<Schema>>, args: Expression<any>[], context: ZModelFunctionContext<Schema>) => Expression<unknown>;
|
|
664
|
+
/**
|
|
665
|
+
* ZenStack client options.
|
|
666
|
+
*/
|
|
667
|
+
type ClientOptions<Schema extends SchemaDef> = {
|
|
668
|
+
/**
|
|
669
|
+
* Database dialect configuration.
|
|
670
|
+
*/
|
|
671
|
+
dialectConfig: DialectConfig<Schema['provider']>;
|
|
672
|
+
/**
|
|
673
|
+
* Custom function definitions.
|
|
674
|
+
*/
|
|
675
|
+
functions?: Record<string, ZModelFunction<Schema>>;
|
|
676
|
+
/**
|
|
677
|
+
* Plugins.
|
|
678
|
+
*/
|
|
679
|
+
plugins?: RuntimePlugin<Schema>[];
|
|
680
|
+
/**
|
|
681
|
+
* Logging configuration.
|
|
682
|
+
*/
|
|
683
|
+
log?: KyselyConfig['log'];
|
|
684
|
+
} & (HasComputedFields<Schema> extends true ? {
|
|
685
|
+
/**
|
|
686
|
+
* Computed field definitions.
|
|
687
|
+
*/
|
|
688
|
+
computedFields: ComputedFieldsOptions<Schema>;
|
|
689
|
+
} : {}) & (HasProcedures<Schema> extends true ? {
|
|
690
|
+
/**
|
|
691
|
+
* Custom procedure definitions.
|
|
692
|
+
*/
|
|
693
|
+
procedures: ProceduresOptions<Schema>;
|
|
694
|
+
} : {});
|
|
695
|
+
type ComputedFieldsOptions<Schema extends SchemaDef> = {
|
|
696
|
+
[Model in GetModels<Schema> as 'computedFields' extends keyof GetModel<Schema, Model> ? Model : never]: {
|
|
697
|
+
[Field in keyof Schema['models'][Model]['computedFields']]: PrependParameter<ExpressionBuilder<ToKyselySchema<Schema>, Model>, Schema['models'][Model]['computedFields'][Field]>;
|
|
698
|
+
};
|
|
699
|
+
};
|
|
700
|
+
type HasComputedFields<Schema extends SchemaDef> = string extends GetModels<Schema> ? false : keyof ComputedFieldsOptions<Schema> extends never ? false : true;
|
|
701
|
+
type ProceduresOptions<Schema extends SchemaDef> = Schema extends {
|
|
702
|
+
procedures: Record<string, ProcedureDef>;
|
|
703
|
+
} ? {
|
|
704
|
+
[Key in keyof Schema['procedures']]: PrependParameter<ClientContract<Schema>, ProcedureFunc<Schema, Schema['procedures'][Key]>>;
|
|
705
|
+
} : {};
|
|
706
|
+
type HasProcedures<Schema extends SchemaDef> = Schema extends {
|
|
707
|
+
procedures: Record<string, ProcedureDef>;
|
|
708
|
+
} ? true : false;
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* ZenStack client interface.
|
|
712
|
+
*/
|
|
713
|
+
type ClientContract<Schema extends SchemaDef> = {
|
|
714
|
+
readonly $schema: Schema;
|
|
715
|
+
/**
|
|
716
|
+
* The client options.
|
|
717
|
+
*/
|
|
718
|
+
readonly $options: ClientOptions<Schema>;
|
|
719
|
+
/**
|
|
720
|
+
* The current user identity.
|
|
721
|
+
*/
|
|
722
|
+
get $auth(): AuthType<Schema> | undefined;
|
|
723
|
+
/**
|
|
724
|
+
* Sets the current user identity.
|
|
725
|
+
*/
|
|
726
|
+
$setAuth(auth: AuthType<Schema> | undefined): ClientContract<Schema>;
|
|
727
|
+
/**
|
|
728
|
+
* The Kysely query builder instance.
|
|
729
|
+
*/
|
|
730
|
+
readonly $qb: ToKysely<Schema>;
|
|
731
|
+
/**
|
|
732
|
+
* The raw Kysely query builder without any ZenStack enhancements.
|
|
733
|
+
*/
|
|
734
|
+
readonly $qbRaw: ToKysely<any>;
|
|
735
|
+
/**
|
|
736
|
+
* Starts a transaction.
|
|
737
|
+
*/
|
|
738
|
+
$transaction<T>(callback: (tx: ClientContract<Schema>) => Promise<T>): Promise<T>;
|
|
739
|
+
/**
|
|
740
|
+
* Returns a new client with the specified plugin installed.
|
|
741
|
+
*/
|
|
742
|
+
$use(plugin: RuntimePlugin<Schema>): ClientContract<Schema>;
|
|
743
|
+
/**
|
|
744
|
+
* Returns a new client with the specified plugin removed.
|
|
745
|
+
*/
|
|
746
|
+
$unuse(pluginId: string): ClientContract<Schema>;
|
|
747
|
+
/**
|
|
748
|
+
* Returns a new client with all plugins removed.
|
|
749
|
+
*/
|
|
750
|
+
$unuseAll(): ClientContract<Schema>;
|
|
751
|
+
/**
|
|
752
|
+
* Disconnects the underlying Kysely instance from the database.
|
|
753
|
+
*/
|
|
754
|
+
$disconnect(): Promise<void>;
|
|
755
|
+
/**
|
|
756
|
+
* Pushes the schema to the database. For testing purposes only.
|
|
757
|
+
* @private
|
|
758
|
+
*/
|
|
759
|
+
$pushSchema(): Promise<void>;
|
|
760
|
+
} & {
|
|
761
|
+
[Key in GetModels<Schema> as Uncapitalize<Key>]: ModelOperations<Schema, Key>;
|
|
762
|
+
} & Procedures<Schema>;
|
|
763
|
+
type _TypeMap = {
|
|
764
|
+
String: string;
|
|
765
|
+
Int: number;
|
|
766
|
+
Float: number;
|
|
767
|
+
BigInt: bigint;
|
|
768
|
+
Decimal: Decimal$1;
|
|
769
|
+
Boolean: boolean;
|
|
770
|
+
DateTime: Date;
|
|
771
|
+
};
|
|
772
|
+
type MapType<Schema extends SchemaDef, T extends string> = T extends keyof _TypeMap ? _TypeMap[T] : T extends GetModels<Schema> ? ModelResult<Schema, T> : unknown;
|
|
773
|
+
type Procedures<Schema extends SchemaDef> = Schema['procedures'] extends Record<string, ProcedureDef> ? {
|
|
774
|
+
$procedures: {
|
|
775
|
+
[Key in keyof Schema['procedures']]: ProcedureFunc<Schema, Schema['procedures'][Key]>;
|
|
776
|
+
};
|
|
777
|
+
} : {};
|
|
778
|
+
type ProcedureFunc<Schema extends SchemaDef, Proc extends ProcedureDef> = (...args: MapProcedureParams<Schema, Proc['params']>) => Promise<MapType<Schema, Proc['returnType']>>;
|
|
779
|
+
type MapProcedureParams<Schema extends SchemaDef, Params> = {
|
|
780
|
+
[P in keyof Params]: Params[P] extends {
|
|
781
|
+
type: infer U;
|
|
782
|
+
} ? OrUndefinedIf<MapType<Schema, U & string>, Params[P] extends {
|
|
783
|
+
optional: true;
|
|
784
|
+
} ? true : false> : never;
|
|
785
|
+
};
|
|
786
|
+
/**
|
|
787
|
+
* Creates a new ZenStack client instance.
|
|
788
|
+
*/
|
|
789
|
+
interface ClientConstructor {
|
|
790
|
+
new <Schema extends SchemaDef>(schema: Schema, options: ClientOptions<Schema>): ClientContract<Schema>;
|
|
791
|
+
}
|
|
792
|
+
/**
|
|
793
|
+
* CRUD operations.
|
|
794
|
+
*/
|
|
795
|
+
type CRUD = 'create' | 'read' | 'update' | 'delete';
|
|
796
|
+
interface ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>> {
|
|
797
|
+
/**
|
|
798
|
+
* Returns a list of entities.
|
|
799
|
+
* @param args - query args
|
|
800
|
+
* @returns a list of entities
|
|
801
|
+
*
|
|
802
|
+
* @example
|
|
803
|
+
* ```ts
|
|
804
|
+
* // find all users and return all scalar fields
|
|
805
|
+
* await client.user.findMany();
|
|
806
|
+
*
|
|
807
|
+
* // find all users with name 'Alex'
|
|
808
|
+
* await client.user.findMany({
|
|
809
|
+
* where: {
|
|
810
|
+
* name: 'Alex'
|
|
811
|
+
* }
|
|
812
|
+
* });
|
|
813
|
+
*
|
|
814
|
+
* // select fields
|
|
815
|
+
* await client.user.findMany({
|
|
816
|
+
* select: {
|
|
817
|
+
* name: true,
|
|
818
|
+
* email: true,
|
|
819
|
+
* }
|
|
820
|
+
* }); // result: `Array<{ name: string, email: string }>`
|
|
821
|
+
*
|
|
822
|
+
* // omit fields
|
|
823
|
+
* await client.user.findMany({
|
|
824
|
+
* omit: {
|
|
825
|
+
* name: true,
|
|
826
|
+
* }
|
|
827
|
+
* }); // result: `Array<{ id: number; email: string; ... }>`
|
|
828
|
+
*
|
|
829
|
+
* // include relations (and all scalar fields)
|
|
830
|
+
* await client.user.findMany({
|
|
831
|
+
* include: {
|
|
832
|
+
* posts: true,
|
|
833
|
+
* }
|
|
834
|
+
* }); // result: `Array<{ ...; posts: Post[] }>`
|
|
835
|
+
*
|
|
836
|
+
* // include relations with filter
|
|
837
|
+
* await client.user.findMany({
|
|
838
|
+
* include: {
|
|
839
|
+
* posts: {
|
|
840
|
+
* where: {
|
|
841
|
+
* published: true
|
|
842
|
+
* }
|
|
843
|
+
* }
|
|
844
|
+
* }
|
|
845
|
+
* });
|
|
846
|
+
*
|
|
847
|
+
* // pagination and sorting
|
|
848
|
+
* await client.user.findMany({
|
|
849
|
+
* skip: 10,
|
|
850
|
+
* take: 10,
|
|
851
|
+
* orderBy: [{ name: 'asc' }, { email: 'desc' }],
|
|
852
|
+
* });
|
|
853
|
+
*
|
|
854
|
+
* // pagination with cursor (https://www.prisma.io/docs/orm/prisma-client/queries/pagination#cursor-based-pagination)
|
|
855
|
+
* await client.user.findMany({
|
|
856
|
+
* cursor: { id: 10 },
|
|
857
|
+
* skip: 1,
|
|
858
|
+
* take: 10,
|
|
859
|
+
* orderBy: { id: 'asc' },
|
|
860
|
+
* });
|
|
861
|
+
*
|
|
862
|
+
* // distinct
|
|
863
|
+
* await client.user.findMany({
|
|
864
|
+
* distinct: ['name']
|
|
865
|
+
* });
|
|
866
|
+
*
|
|
867
|
+
* // count all relations
|
|
868
|
+
* await client.user.findMany({
|
|
869
|
+
* _count: true,
|
|
870
|
+
* }); // result: `{ _count: { posts: number; ... } }`
|
|
871
|
+
*
|
|
872
|
+
* // count selected relations
|
|
873
|
+
* await client.user.findMany({
|
|
874
|
+
* _count: { select: { posts: true } },
|
|
875
|
+
* }); // result: `{ _count: { posts: number } }`
|
|
876
|
+
* ```
|
|
877
|
+
*/
|
|
878
|
+
findMany<T extends FindArgs<Schema, Model, true>>(args?: SelectSubset<T, FindArgs<Schema, Model, true>>): Promise<ModelResult<Schema, Model, T>[]>;
|
|
1155
879
|
/**
|
|
1156
|
-
*
|
|
880
|
+
* Returns a uniquely identified entity.
|
|
881
|
+
* @param args - query args
|
|
882
|
+
* @returns a single entity or null if not found
|
|
883
|
+
* @see {@link findMany}
|
|
1157
884
|
*/
|
|
1158
|
-
|
|
885
|
+
findUnique<T extends FindUniqueArgs<Schema, Model>>(args?: SelectSubset<T, FindUniqueArgs<Schema, Model>>): Promise<ModelResult<Schema, Model, T> | null>;
|
|
1159
886
|
/**
|
|
1160
|
-
*
|
|
887
|
+
* Returns a uniquely identified entity or throws `NotFoundError` if not found.
|
|
888
|
+
* @param args - query args
|
|
889
|
+
* @returns a single entity
|
|
890
|
+
* @see {@link findMany}
|
|
1161
891
|
*/
|
|
1162
|
-
|
|
892
|
+
findUniqueOrThrow<T extends FindUniqueArgs<Schema, Model>>(args?: SelectSubset<T, FindUniqueArgs<Schema, Model>>): Promise<ModelResult<Schema, Model, T>>;
|
|
1163
893
|
/**
|
|
1164
|
-
*
|
|
894
|
+
* Returns the first entity.
|
|
895
|
+
* @param args - query args
|
|
896
|
+
* @returns a single entity or null if not found
|
|
897
|
+
* @see {@link findMany}
|
|
1165
898
|
*/
|
|
1166
|
-
|
|
899
|
+
findFirst<T extends FindArgs<Schema, Model, true>>(args?: SelectSubset<T, FindArgs<Schema, Model, true>>): Promise<ModelResult<Schema, Model, T> | null>;
|
|
1167
900
|
/**
|
|
1168
|
-
*
|
|
901
|
+
* Returns the first entity or throws `NotFoundError` if not found.
|
|
902
|
+
* @param args - query args
|
|
903
|
+
* @returns a single entity
|
|
904
|
+
* @see {@link findMany}
|
|
1169
905
|
*/
|
|
1170
|
-
|
|
1171
|
-
} & (HasComputedFields<Schema> extends true ? {
|
|
906
|
+
findFirstOrThrow<T extends FindArgs<Schema, Model, true>>(args?: SelectSubset<T, FindArgs<Schema, Model, true>>): Promise<ModelResult<Schema, Model, T>>;
|
|
1172
907
|
/**
|
|
1173
|
-
*
|
|
908
|
+
* Creates a new entity.
|
|
909
|
+
* @param args - create args
|
|
910
|
+
* @returns the created entity
|
|
911
|
+
*
|
|
912
|
+
* @example
|
|
913
|
+
* ```ts
|
|
914
|
+
* // simple create
|
|
915
|
+
* await client.user.create({
|
|
916
|
+
* data: { name: 'Alex', email: 'alex@zenstack.dev' }
|
|
917
|
+
* });
|
|
918
|
+
*
|
|
919
|
+
* // nested create with relation
|
|
920
|
+
* await client.user.create({
|
|
921
|
+
* data: {
|
|
922
|
+
* email: 'alex@zenstack.dev',
|
|
923
|
+
* posts: { create: { title: 'Hello World' } }
|
|
924
|
+
* }
|
|
925
|
+
* });
|
|
926
|
+
*
|
|
927
|
+
* // you can use `select`, `omit`, and `include` to control
|
|
928
|
+
* // the fields returned by the query, as with `findMany`
|
|
929
|
+
* await client.user.create({
|
|
930
|
+
* data: {
|
|
931
|
+
* email: 'alex@zenstack.dev',
|
|
932
|
+
* posts: { create: { title: 'Hello World' } }
|
|
933
|
+
* },
|
|
934
|
+
* include: { posts: true }
|
|
935
|
+
* }); // result: `{ id: number; posts: Post[] }`
|
|
936
|
+
*
|
|
937
|
+
* // connect relations
|
|
938
|
+
* await client.user.create({
|
|
939
|
+
* data: {
|
|
940
|
+
* email: 'alex@zenstack.dev',
|
|
941
|
+
* posts: { connect: { id: 1 } }
|
|
942
|
+
* }
|
|
943
|
+
* });
|
|
944
|
+
*
|
|
945
|
+
* // connect relations, and create if not found
|
|
946
|
+
* await client.user.create({
|
|
947
|
+
* data: {
|
|
948
|
+
* email: 'alex@zenstack.dev',
|
|
949
|
+
* posts: {
|
|
950
|
+
* connectOrCreate: {
|
|
951
|
+
* where: { id: 1 },
|
|
952
|
+
* create: { title: 'Hello World' }
|
|
953
|
+
* }
|
|
954
|
+
* }
|
|
955
|
+
* }
|
|
956
|
+
* });
|
|
957
|
+
* ```
|
|
1174
958
|
*/
|
|
1175
|
-
|
|
1176
|
-
} : {}) & (HasProcedures<Schema> extends true ? {
|
|
959
|
+
create<T extends CreateArgs<Schema, Model>>(args: SelectSubset<T, CreateArgs<Schema, Model>>): Promise<ModelResult<Schema, Model, T>>;
|
|
1177
960
|
/**
|
|
1178
|
-
*
|
|
961
|
+
* Creates multiple entities. Only scalar fields are allowed.
|
|
962
|
+
* @param args - create args
|
|
963
|
+
* @returns count of created entities: `{ count: number }`
|
|
964
|
+
*
|
|
965
|
+
* @example
|
|
966
|
+
* ```ts
|
|
967
|
+
* // create multiple entities
|
|
968
|
+
* await client.user.createMany({
|
|
969
|
+
* data: [
|
|
970
|
+
* { name: 'Alex', email: 'alex@zenstack.dev' },
|
|
971
|
+
* { name: 'John', email: 'john@zenstack.dev' }
|
|
972
|
+
* ]
|
|
973
|
+
* });
|
|
974
|
+
*
|
|
975
|
+
* // skip items that cause unique constraint violation
|
|
976
|
+
* await client.user.createMany({
|
|
977
|
+
* data: [
|
|
978
|
+
* { name: 'Alex', email: 'alex@zenstack.dev' },
|
|
979
|
+
* { name: 'John', email: 'john@zenstack.dev' }
|
|
980
|
+
* ],
|
|
981
|
+
* skipDuplicates: true
|
|
982
|
+
* });
|
|
983
|
+
* ```
|
|
1179
984
|
*/
|
|
1180
|
-
|
|
1181
|
-
} : {});
|
|
1182
|
-
type ComputedFieldsOptions<Schema extends SchemaDef> = {
|
|
1183
|
-
[Model in GetModels<Schema> as 'computedFields' extends keyof GetModel<Schema, Model> ? Model : never]: {
|
|
1184
|
-
[Field in keyof Schema['models'][Model]['computedFields']]: PrependParameter<ExpressionBuilder<ToKyselySchema<Schema>, Model>, Schema['models'][Model]['computedFields'][Field]>;
|
|
1185
|
-
};
|
|
1186
|
-
};
|
|
1187
|
-
type HasComputedFields<Schema extends SchemaDef> = string extends GetModels<Schema> ? false : keyof ComputedFieldsOptions<Schema> extends never ? false : true;
|
|
1188
|
-
type ProceduresOptions<Schema extends SchemaDef> = Schema extends {
|
|
1189
|
-
procedures: Record<string, ProcedureDef>;
|
|
1190
|
-
} ? {
|
|
1191
|
-
[Key in keyof Schema['procedures']]: PrependParameter<ClientContract<Schema>, ProcedureFunc<Schema, Schema['procedures'][Key]>>;
|
|
1192
|
-
} : {};
|
|
1193
|
-
type HasProcedures<Schema extends SchemaDef> = Schema extends {
|
|
1194
|
-
procedures: Record<string, ProcedureDef>;
|
|
1195
|
-
} ? true : false;
|
|
1196
|
-
|
|
1197
|
-
/**
|
|
1198
|
-
* ZenStack client interface.
|
|
1199
|
-
*/
|
|
1200
|
-
type ClientContract<Schema extends SchemaDef> = {
|
|
1201
|
-
readonly $schema: Schema;
|
|
985
|
+
createMany<T extends CreateManyArgs<Schema, Model>>(args?: SelectSubset<T, CreateManyArgs<Schema, Model>>): Promise<BatchResult>;
|
|
1202
986
|
/**
|
|
1203
|
-
*
|
|
987
|
+
* Creates multiple entities and returns them.
|
|
988
|
+
* @param args - create args. See {@link createMany} for input. Use
|
|
989
|
+
* `select` and `omit` to control the fields returned.
|
|
990
|
+
* @returns the created entities
|
|
991
|
+
*
|
|
992
|
+
* @example
|
|
993
|
+
* ```ts
|
|
994
|
+
* // create multiple entities and return selected fields
|
|
995
|
+
* await client.user.createManyAndReturn({
|
|
996
|
+
* data: [
|
|
997
|
+
* { name: 'Alex', email: 'alex@zenstack.dev' },
|
|
998
|
+
* { name: 'John', email: 'john@zenstack.dev' }
|
|
999
|
+
* ],
|
|
1000
|
+
* select: { id: true, email: true }
|
|
1001
|
+
* });
|
|
1002
|
+
* ```
|
|
1204
1003
|
*/
|
|
1205
|
-
|
|
1004
|
+
createManyAndReturn<T extends CreateManyAndReturnArgs<Schema, Model>>(args?: SelectSubset<T, CreateManyAndReturnArgs<Schema, Model>>): Promise<ModelResult<Schema, Model, T>[]>;
|
|
1206
1005
|
/**
|
|
1207
|
-
*
|
|
1006
|
+
* Updates a uniquely identified entity.
|
|
1007
|
+
* @param args - update args. See {@link findMany} for how to control
|
|
1008
|
+
* fields and relations returned.
|
|
1009
|
+
* @returns the updated entity. Throws `NotFoundError` if the entity is not found.
|
|
1010
|
+
*
|
|
1011
|
+
* @example
|
|
1012
|
+
* ```ts
|
|
1013
|
+
* // update fields
|
|
1014
|
+
* await client.user.update({
|
|
1015
|
+
* where: { id: 1 },
|
|
1016
|
+
* data: { name: 'Alex' }
|
|
1017
|
+
* });
|
|
1018
|
+
*
|
|
1019
|
+
* // connect a relation
|
|
1020
|
+
* await client.user.update({
|
|
1021
|
+
* where: { id: 1 },
|
|
1022
|
+
* data: { posts: { connect: { id: 1 } } }
|
|
1023
|
+
* });
|
|
1024
|
+
*
|
|
1025
|
+
* // connect relation, and create if not found
|
|
1026
|
+
* await client.user.update({
|
|
1027
|
+
* where: { id: 1 },
|
|
1028
|
+
* data: {
|
|
1029
|
+
* posts: {
|
|
1030
|
+
* connectOrCreate: {
|
|
1031
|
+
* where: { id: 1 },
|
|
1032
|
+
* create: { title: 'Hello World' }
|
|
1033
|
+
* }
|
|
1034
|
+
* }
|
|
1035
|
+
* }
|
|
1036
|
+
* });
|
|
1037
|
+
*
|
|
1038
|
+
* // create many related entities (only available for one-to-many relations)
|
|
1039
|
+
* await client.user.update({
|
|
1040
|
+
* where: { id: 1 },
|
|
1041
|
+
* data: {
|
|
1042
|
+
* posts: {
|
|
1043
|
+
* createMany: {
|
|
1044
|
+
* data: [{ title: 'Hello World' }, { title: 'Hello World 2' }],
|
|
1045
|
+
* }
|
|
1046
|
+
* }
|
|
1047
|
+
* }
|
|
1048
|
+
* });
|
|
1049
|
+
*
|
|
1050
|
+
* // disconnect a one-to-many relation
|
|
1051
|
+
* await client.user.update({
|
|
1052
|
+
* where: { id: 1 },
|
|
1053
|
+
* data: { posts: { disconnect: { id: 1 } } }
|
|
1054
|
+
* });
|
|
1055
|
+
*
|
|
1056
|
+
* // disconnect a one-to-one relation
|
|
1057
|
+
* await client.user.update({
|
|
1058
|
+
* where: { id: 1 },
|
|
1059
|
+
* data: { profile: { disconnect: true } }
|
|
1060
|
+
* });
|
|
1061
|
+
*
|
|
1062
|
+
* // replace a relation (only available for one-to-many relations)
|
|
1063
|
+
* await client.user.update({
|
|
1064
|
+
* where: { id: 1 },
|
|
1065
|
+
* data: {
|
|
1066
|
+
* posts: {
|
|
1067
|
+
* set: [{ id: 1 }, { id: 2 }]
|
|
1068
|
+
* }
|
|
1069
|
+
* }
|
|
1070
|
+
* });
|
|
1071
|
+
*
|
|
1072
|
+
* // update a relation
|
|
1073
|
+
* await client.user.update({
|
|
1074
|
+
* where: { id: 1 },
|
|
1075
|
+
* data: {
|
|
1076
|
+
* posts: {
|
|
1077
|
+
* update: { where: { id: 1 }, data: { title: 'Hello World' } }
|
|
1078
|
+
* }
|
|
1079
|
+
* }
|
|
1080
|
+
* });
|
|
1081
|
+
*
|
|
1082
|
+
* // upsert a relation
|
|
1083
|
+
* await client.user.update({
|
|
1084
|
+
* where: { id: 1 },
|
|
1085
|
+
* data: {
|
|
1086
|
+
* posts: {
|
|
1087
|
+
* upsert: {
|
|
1088
|
+
* where: { id: 1 },
|
|
1089
|
+
* create: { title: 'Hello World' },
|
|
1090
|
+
* update: { title: 'Hello World' }
|
|
1091
|
+
* }
|
|
1092
|
+
* }
|
|
1093
|
+
* }
|
|
1094
|
+
* });
|
|
1095
|
+
*
|
|
1096
|
+
* // update many related entities (only available for one-to-many relations)
|
|
1097
|
+
* await client.user.update({
|
|
1098
|
+
* where: { id: 1 },
|
|
1099
|
+
* data: {
|
|
1100
|
+
* posts: {
|
|
1101
|
+
* updateMany: {
|
|
1102
|
+
* where: { published: true },
|
|
1103
|
+
* data: { title: 'Hello World' }
|
|
1104
|
+
* }
|
|
1105
|
+
* }
|
|
1106
|
+
* }
|
|
1107
|
+
* });
|
|
1108
|
+
*
|
|
1109
|
+
* // delete a one-to-many relation
|
|
1110
|
+
* await client.user.update({
|
|
1111
|
+
* where: { id: 1 },
|
|
1112
|
+
* data: { posts: { delete: { id: 1 } } }
|
|
1113
|
+
* });
|
|
1114
|
+
*
|
|
1115
|
+
* // delete a one-to-one relation
|
|
1116
|
+
* await client.user.update({
|
|
1117
|
+
* where: { id: 1 },
|
|
1118
|
+
* data: { profile: { delete: true } }
|
|
1119
|
+
* });
|
|
1120
|
+
* ```
|
|
1208
1121
|
*/
|
|
1209
|
-
|
|
1122
|
+
update<T extends UpdateArgs<Schema, Model>>(args: SelectSubset<T, UpdateArgs<Schema, Model>>): Promise<ModelResult<Schema, Model, T>>;
|
|
1210
1123
|
/**
|
|
1211
|
-
*
|
|
1124
|
+
* Updates multiple entities.
|
|
1125
|
+
* @param args - update args. Only scalar fields are allowed for data.
|
|
1126
|
+
* @returns count of updated entities: `{ count: number }`
|
|
1127
|
+
*
|
|
1128
|
+
* @example
|
|
1129
|
+
* ```ts
|
|
1130
|
+
* // update many entities
|
|
1131
|
+
* await client.user.updateMany({
|
|
1132
|
+
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1133
|
+
* data: { role: 'ADMIN' }
|
|
1134
|
+
* });
|
|
1135
|
+
*
|
|
1136
|
+
* // limit the number of updated entities
|
|
1137
|
+
* await client.user.updateMany({
|
|
1138
|
+
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1139
|
+
* data: { role: 'ADMIN' },
|
|
1140
|
+
* limit: 10
|
|
1141
|
+
* });
|
|
1212
1142
|
*/
|
|
1213
|
-
|
|
1143
|
+
updateMany<T extends UpdateManyArgs<Schema, Model>>(args: Subset<T, UpdateManyArgs<Schema, Model>>): Promise<BatchResult>;
|
|
1214
1144
|
/**
|
|
1215
|
-
*
|
|
1145
|
+
* Updates multiple entities and returns them.
|
|
1146
|
+
* @param args - update args. Only scalar fields are allowed for data.
|
|
1147
|
+
* @returns the updated entities
|
|
1148
|
+
*
|
|
1149
|
+
* @example
|
|
1150
|
+
* ```ts
|
|
1151
|
+
* // update many entities and return selected fields
|
|
1152
|
+
* await client.user.updateManyAndReturn({
|
|
1153
|
+
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1154
|
+
* data: { role: 'ADMIN' },
|
|
1155
|
+
* select: { id: true, email: true }
|
|
1156
|
+
* }); // result: `Array<{ id: string; email: string }>`
|
|
1157
|
+
*
|
|
1158
|
+
* // limit the number of updated entities
|
|
1159
|
+
* await client.user.updateManyAndReturn({
|
|
1160
|
+
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1161
|
+
* data: { role: 'ADMIN' },
|
|
1162
|
+
* limit: 10
|
|
1163
|
+
* });
|
|
1164
|
+
* ```
|
|
1216
1165
|
*/
|
|
1217
|
-
|
|
1166
|
+
updateManyAndReturn<T extends UpdateManyAndReturnArgs<Schema, Model>>(args: Subset<T, UpdateManyAndReturnArgs<Schema, Model>>): Promise<ModelResult<Schema, Model, T>[]>;
|
|
1218
1167
|
/**
|
|
1219
|
-
*
|
|
1168
|
+
* Creates or updates an entity.
|
|
1169
|
+
* @param args - upsert args
|
|
1170
|
+
* @returns the upserted entity
|
|
1171
|
+
*
|
|
1172
|
+
* @example
|
|
1173
|
+
* ```ts
|
|
1174
|
+
* // upsert an entity
|
|
1175
|
+
* await client.user.upsert({
|
|
1176
|
+
* // `where` clause is used to find the entity
|
|
1177
|
+
* where: { id: 1 },
|
|
1178
|
+
* // `create` clause is used if the entity is not found
|
|
1179
|
+
* create: { email: 'alex@zenstack.dev', name: 'Alex' },
|
|
1180
|
+
* // `update` clause is used if the entity is found
|
|
1181
|
+
* update: { name: 'Alex-new' },
|
|
1182
|
+
* // `select` and `omit` can be used to control the returned fields
|
|
1183
|
+
* ...
|
|
1184
|
+
* });
|
|
1185
|
+
* ```
|
|
1220
1186
|
*/
|
|
1221
|
-
|
|
1187
|
+
upsert<T extends UpsertArgs<Schema, Model>>(args: SelectSubset<T, UpsertArgs<Schema, Model>>): Promise<ModelResult<Schema, Model, T>>;
|
|
1222
1188
|
/**
|
|
1223
|
-
*
|
|
1189
|
+
* Deletes a uniquely identifiable entity.
|
|
1190
|
+
* @param args - delete args
|
|
1191
|
+
* @returns the deleted entity. Throws `NotFoundError` if the entity is not found.
|
|
1192
|
+
*
|
|
1193
|
+
* @example
|
|
1194
|
+
* ```ts
|
|
1195
|
+
* // delete an entity
|
|
1196
|
+
* await client.user.delete({
|
|
1197
|
+
* where: { id: 1 }
|
|
1198
|
+
* });
|
|
1199
|
+
*
|
|
1200
|
+
* // delete an entity and return selected fields
|
|
1201
|
+
* await client.user.delete({
|
|
1202
|
+
* where: { id: 1 },
|
|
1203
|
+
* select: { id: true, email: true }
|
|
1204
|
+
* }); // result: `{ id: string; email: string }`
|
|
1205
|
+
* ```
|
|
1224
1206
|
*/
|
|
1225
|
-
|
|
1207
|
+
delete<T extends DeleteArgs<Schema, Model>>(args: SelectSubset<T, DeleteArgs<Schema, Model>>): Promise<ModelResult<Schema, Model>>;
|
|
1226
1208
|
/**
|
|
1227
|
-
*
|
|
1209
|
+
* Deletes multiple entities.
|
|
1210
|
+
* @param args - delete args
|
|
1211
|
+
* @returns count of deleted entities: `{ count: number }`
|
|
1212
|
+
*
|
|
1213
|
+
* @example
|
|
1214
|
+
* ```ts
|
|
1215
|
+
* // delete many entities
|
|
1216
|
+
* await client.user.deleteMany({
|
|
1217
|
+
* where: { email: { endsWith: '@zenstack.dev' } }
|
|
1218
|
+
* });
|
|
1219
|
+
*
|
|
1220
|
+
* // limit the number of deleted entities
|
|
1221
|
+
* await client.user.deleteMany({
|
|
1222
|
+
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1223
|
+
* limit: 10
|
|
1224
|
+
* });
|
|
1225
|
+
* ```
|
|
1228
1226
|
*/
|
|
1229
|
-
|
|
1227
|
+
deleteMany<T extends DeleteManyArgs<Schema, Model>>(args?: Subset<T, DeleteManyArgs<Schema, Model>>): Promise<BatchResult>;
|
|
1230
1228
|
/**
|
|
1231
|
-
*
|
|
1229
|
+
* Counts rows or field values.
|
|
1230
|
+
* @param args - count args
|
|
1231
|
+
* @returns `number`, or an object containing count of selected relations
|
|
1232
|
+
*
|
|
1233
|
+
* @example
|
|
1234
|
+
* ```ts
|
|
1235
|
+
* // count all
|
|
1236
|
+
* await client.user.count();
|
|
1237
|
+
*
|
|
1238
|
+
* // count with a filter
|
|
1239
|
+
* await client.user.count({ where: { email: { endsWith: '@zenstack.dev' } } });
|
|
1240
|
+
*
|
|
1241
|
+
* // count rows and field values
|
|
1242
|
+
* await client.user.count({
|
|
1243
|
+
* select: { _all: true, email: true }
|
|
1244
|
+
* }); // result: `{ _all: number, email: number }`
|
|
1232
1245
|
*/
|
|
1233
|
-
|
|
1246
|
+
count<T extends CountArgs<Schema, Model>>(args?: Subset<T, CountArgs<Schema, Model>>): Promise<CountResult<Schema, Model, T>>;
|
|
1234
1247
|
/**
|
|
1235
|
-
*
|
|
1248
|
+
* Aggregates rows.
|
|
1249
|
+
* @param args - aggregation args
|
|
1250
|
+
* @returns an object containing aggregated values
|
|
1251
|
+
*
|
|
1252
|
+
* @example
|
|
1253
|
+
* ```ts
|
|
1254
|
+
* // aggregate rows
|
|
1255
|
+
* await client.profile.aggregate({
|
|
1256
|
+
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1257
|
+
* _count: true,
|
|
1258
|
+
* _avg: { age: true },
|
|
1259
|
+
* _sum: { age: true },
|
|
1260
|
+
* _min: { age: true },
|
|
1261
|
+
* _max: { age: true }
|
|
1262
|
+
* }); // result: `{ _count: number, _avg: { age: number }, ... }`
|
|
1236
1263
|
*/
|
|
1237
|
-
|
|
1264
|
+
aggregate<T extends AggregateArgs<Schema, Model>>(args: Subset<T, AggregateArgs<Schema, Model>>): Promise<AggregateResult<Schema, Model, T>>;
|
|
1238
1265
|
/**
|
|
1239
|
-
*
|
|
1240
|
-
* @
|
|
1266
|
+
* Groups rows by columns.
|
|
1267
|
+
* @param args - groupBy args
|
|
1268
|
+
* @returns an object containing grouped values
|
|
1269
|
+
*
|
|
1270
|
+
* @example
|
|
1271
|
+
* ```ts
|
|
1272
|
+
* // group by a field
|
|
1273
|
+
* await client.profile.groupBy({
|
|
1274
|
+
* by: 'country',
|
|
1275
|
+
* _count: true
|
|
1276
|
+
* }); // result: `Array<{ country: string, _count: number }>`
|
|
1277
|
+
*
|
|
1278
|
+
* // group by multiple fields
|
|
1279
|
+
* await client.profile.groupBy({
|
|
1280
|
+
* by: ['country', 'city'],
|
|
1281
|
+
* _count: true
|
|
1282
|
+
* }); // result: `Array<{ country: string, city: string, _count: number }>`
|
|
1283
|
+
*
|
|
1284
|
+
* // group by with sorting, the `orderBy` fields must be in the `by` list
|
|
1285
|
+
* await client.profile.groupBy({
|
|
1286
|
+
* by: 'country',
|
|
1287
|
+
* orderBy: { country: 'desc' }
|
|
1288
|
+
* });
|
|
1289
|
+
*
|
|
1290
|
+
* // group by with having (post-aggregation filter), the `having` fields must
|
|
1291
|
+
* // be in the `by` list
|
|
1292
|
+
* await client.profile.groupBy({
|
|
1293
|
+
* by: 'country',
|
|
1294
|
+
* having: { country: 'US' }
|
|
1295
|
+
* });
|
|
1241
1296
|
*/
|
|
1242
|
-
|
|
1243
|
-
} & {
|
|
1244
|
-
[Key in GetModels<Schema> as Uncapitalize<Key>]: ModelOperations<Schema, Key>;
|
|
1245
|
-
} & Procedures<Schema>;
|
|
1246
|
-
type MapType<Schema extends SchemaDef, T extends string> = T extends 'String' ? string : T extends 'Int' ? number : T extends 'Float' ? number : T extends 'BigInt' ? bigint : T extends 'Decimal' ? Decimal$1 : T extends 'Boolean' ? boolean : T extends 'DateTime' ? Date : T extends GetModels<Schema> ? ModelResult<Schema, T> : unknown;
|
|
1247
|
-
type Procedures<Schema extends SchemaDef> = Schema['procedures'] extends Record<string, ProcedureDef> ? {
|
|
1248
|
-
$procedures: {
|
|
1249
|
-
[Key in keyof Schema['procedures']]: ProcedureFunc<Schema, Schema['procedures'][Key]>;
|
|
1250
|
-
};
|
|
1251
|
-
} : {};
|
|
1252
|
-
type ProcedureFunc<Schema extends SchemaDef, Proc extends ProcedureDef> = (...args: MapProcedureParams<Schema, Proc['params']>) => Promise<MapType<Schema, Proc['returnType']>>;
|
|
1253
|
-
type MapProcedureParams<Schema extends SchemaDef, Params> = {
|
|
1254
|
-
[P in keyof Params]: Params[P] extends {
|
|
1255
|
-
type: infer U;
|
|
1256
|
-
} ? OrUndefinedIf<MapType<Schema, U & string>, Params[P] extends {
|
|
1257
|
-
optional: true;
|
|
1258
|
-
} ? true : false> : never;
|
|
1259
|
-
};
|
|
1260
|
-
/**
|
|
1261
|
-
* Creates a new ZenStack client instance.
|
|
1262
|
-
*/
|
|
1263
|
-
interface ClientConstructor {
|
|
1264
|
-
new <Schema extends SchemaDef>(schema: HasComputedFields<Schema> extends false ? Schema : never): ClientContract<Schema>;
|
|
1265
|
-
new <Schema extends SchemaDef>(schema: Schema, options: ClientOptions<Schema>): ClientContract<Schema>;
|
|
1297
|
+
groupBy<T extends GroupByArgs<Schema, Model>>(args: Subset<T, GroupByArgs<Schema, Model>>): Promise<GroupByResult<Schema, Model, T>>;
|
|
1266
1298
|
}
|
|
1267
|
-
/**
|
|
1268
|
-
* CRUD operations.
|
|
1269
|
-
*/
|
|
1270
|
-
type CRUD = 'create' | 'read' | 'update' | 'delete';
|
|
1271
1299
|
|
|
1272
|
-
export type
|
|
1300
|
+
export { type UpdateRelationInput as A, type BatchResult as B, type ClientConstructor as C, type DateTimeFilter as D, type UpdateInput as E, type FindArgs as F, type DeleteArgs as G, type DeleteManyArgs as H, type CountArgs as I, type CountAggregateInput as J, type CountResult as K, type AggregateArgs as L, type ModelResult as M, type NumberFilter as N, type OrderBy as O, type AggregateResult as P, type GroupByArgs as Q, type GroupByResult as R, type StringFilter as S, type ToKysely as T, type UpdateArgs as U, type RuntimePlugin as V, type WhereInput as W, type OnKyselyQueryArgs as X, type ClientContract as a, type ClientOptions as b, type CommonPrimitiveFilter as c, definePlugin as d, type BytesFilter as e, type BooleanFilter as f, type SortOrder as g, type NullsOrder as h, type WhereUniqueInput as i, type SelectIncludeOmit as j, type Subset as k, type SelectSubset as l, type MapFieldType as m, type OptionalFieldsForCreate as n, type OppositeRelationFields as o, type OppositeRelationAndFK as p, type FindUniqueArgs as q, type CreateArgs as r, type CreateManyArgs as s, type CreateManyAndReturnArgs as t, type CreateManyPayload as u, type CreateInput as v, type UpdateManyArgs as w, type UpdateManyAndReturnArgs as x, type UpsertArgs as y, type UpdateScalarInput as z };
|