@zenstackhq/runtime 3.0.0-alpha.24 → 3.0.0-alpha.26

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.
@@ -1,5 +1,5 @@
1
1
  import Decimal, { Decimal as Decimal$1 } from 'decimal.js';
2
- import { SchemaDef, GetModels, ScalarFields, ForeignKeyFields, GetModelFields, FieldHasDefault, GetModelFieldType, ModelFieldIsOptional, GetModelField, NonRelationFields, RelationFields, FieldIsArray, RelationFieldType, FieldIsRelation, GetEnums, GetEnum, BuiltinType, GetModel, FieldDef, GetTypeDefs, GetTypeDefFields, GetTypeDefField, TypeDefFieldIsOptional, IsDelegateModel, GetModelDiscriminator, GetSubModels, FieldIsRelationArray, FieldIsDelegateDiscriminator, FieldType, RelationInfo, FieldIsDelegateRelation, DataSourceProviderType, ProcedureDef } from '@zenstackhq/sdk/schema';
2
+ import { SchemaDef, GetModels, ScalarFields, ForeignKeyFields, GetModelFields, FieldHasDefault, GetModelFieldType, ModelFieldIsOptional, GetModelField, NonRelationFields, RelationFields, FieldIsArray, RelationFieldType, FieldIsRelation, GetModel, FieldDef, GetEnums, GetEnum, GetTypeDefs, GetTypeDefFields, GetTypeDefField, TypeDefFieldIsOptional, BuiltinType, IsDelegateModel, GetModelDiscriminator, GetSubModels, FieldIsRelationArray, FieldIsDelegateDiscriminator, FieldType, RelationInfo, FieldIsDelegateRelation, DataSourceProviderType, ProcedureDef } from '@zenstackhq/sdk/schema';
3
3
  import { Generated, Kysely, ExpressionBuilder, OperandExpression, SqlBool, SelectQueryBuilder, Expression, ExpressionWrapper, OperationNode, RootOperationNode, QueryResult, UnknownRow, Dialect, KyselyConfig } from 'kysely';
4
4
 
5
5
  type Optional<T extends object, K extends keyof T = keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
@@ -259,9 +259,9 @@ type FindArgs<Schema extends SchemaDef, Model extends GetModels<Schema>, Collect
259
259
  skip?: number;
260
260
  take?: number;
261
261
  orderBy?: OrArray<OrderBy<Schema, Model, true, false>>;
262
- } : {}) & (AllowFilter extends true ? {
262
+ } & Distinct<Schema, Model> & Cursor<Schema, Model> : {}) & (AllowFilter extends true ? {
263
263
  where?: WhereInput<Schema, Model>;
264
- } : {}) & SelectIncludeOmit<Schema, Model, Collection> & Distinct<Schema, Model> & Cursor<Schema, Model>;
264
+ } : {}) & SelectIncludeOmit<Schema, Model, Collection>;
265
265
  type FindManyArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = FindArgs<Schema, Model, true>;
266
266
  type FindFirstArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = FindArgs<Schema, Model, false>;
267
267
  type FindUniqueArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
@@ -562,7 +562,7 @@ declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
562
562
  protected and(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): Expression<SqlBool>;
563
563
  protected or(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): Expression<SqlBool>;
564
564
  protected not(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): ExpressionWrapper<any, any, SqlBool>;
565
- fieldRef(model: string, field: string, eb: ExpressionBuilder<any, any>, modelAlias?: string): ExpressionWrapper<any, any, unknown>;
565
+ fieldRef(model: string, field: string, eb: ExpressionBuilder<any, any>, modelAlias?: string, inlineComputedField?: boolean): ExpressionWrapper<any, any, unknown>;
566
566
  abstract get provider(): DataSourceProviderType;
567
567
  /**
568
568
  * Builds selection for a relation field.
@@ -603,6 +603,7 @@ declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
603
603
  }
604
604
 
605
605
  type CrudOperation = 'findMany' | 'findUnique' | 'findFirst' | 'create' | 'createMany' | 'createManyAndReturn' | 'update' | 'updateMany' | 'updateManyAndReturn' | 'upsert' | 'delete' | 'deleteMany' | 'count' | 'aggregate' | 'groupBy';
606
+ type AllCrudOperation = CrudOperation | 'findUniqueOrThrow' | 'findFirstOrThrow';
606
607
 
607
608
  /**
608
609
  * ZenStack runtime plugin.
@@ -647,7 +648,7 @@ type OnQueryHookContext<Schema extends SchemaDef> = {
647
648
  /**
648
649
  * The operation that is being performed.
649
650
  */
650
- operation: CrudOperation;
651
+ operation: AllCrudOperation;
651
652
  /**
652
653
  * The query arguments.
653
654
  */
@@ -1,5 +1,5 @@
1
1
  import Decimal, { Decimal as Decimal$1 } from 'decimal.js';
2
- import { SchemaDef, GetModels, ScalarFields, ForeignKeyFields, GetModelFields, FieldHasDefault, GetModelFieldType, ModelFieldIsOptional, GetModelField, NonRelationFields, RelationFields, FieldIsArray, RelationFieldType, FieldIsRelation, GetEnums, GetEnum, BuiltinType, GetModel, FieldDef, GetTypeDefs, GetTypeDefFields, GetTypeDefField, TypeDefFieldIsOptional, IsDelegateModel, GetModelDiscriminator, GetSubModels, FieldIsRelationArray, FieldIsDelegateDiscriminator, FieldType, RelationInfo, FieldIsDelegateRelation, DataSourceProviderType, ProcedureDef } from '@zenstackhq/sdk/schema';
2
+ import { SchemaDef, GetModels, ScalarFields, ForeignKeyFields, GetModelFields, FieldHasDefault, GetModelFieldType, ModelFieldIsOptional, GetModelField, NonRelationFields, RelationFields, FieldIsArray, RelationFieldType, FieldIsRelation, GetModel, FieldDef, GetEnums, GetEnum, GetTypeDefs, GetTypeDefFields, GetTypeDefField, TypeDefFieldIsOptional, BuiltinType, IsDelegateModel, GetModelDiscriminator, GetSubModels, FieldIsRelationArray, FieldIsDelegateDiscriminator, FieldType, RelationInfo, FieldIsDelegateRelation, DataSourceProviderType, ProcedureDef } from '@zenstackhq/sdk/schema';
3
3
  import { Generated, Kysely, ExpressionBuilder, OperandExpression, SqlBool, SelectQueryBuilder, Expression, ExpressionWrapper, OperationNode, RootOperationNode, QueryResult, UnknownRow, Dialect, KyselyConfig } from 'kysely';
4
4
 
5
5
  type Optional<T extends object, K extends keyof T = keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
@@ -259,9 +259,9 @@ type FindArgs<Schema extends SchemaDef, Model extends GetModels<Schema>, Collect
259
259
  skip?: number;
260
260
  take?: number;
261
261
  orderBy?: OrArray<OrderBy<Schema, Model, true, false>>;
262
- } : {}) & (AllowFilter extends true ? {
262
+ } & Distinct<Schema, Model> & Cursor<Schema, Model> : {}) & (AllowFilter extends true ? {
263
263
  where?: WhereInput<Schema, Model>;
264
- } : {}) & SelectIncludeOmit<Schema, Model, Collection> & Distinct<Schema, Model> & Cursor<Schema, Model>;
264
+ } : {}) & SelectIncludeOmit<Schema, Model, Collection>;
265
265
  type FindManyArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = FindArgs<Schema, Model, true>;
266
266
  type FindFirstArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = FindArgs<Schema, Model, false>;
267
267
  type FindUniqueArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
@@ -562,7 +562,7 @@ declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
562
562
  protected and(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): Expression<SqlBool>;
563
563
  protected or(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): Expression<SqlBool>;
564
564
  protected not(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): ExpressionWrapper<any, any, SqlBool>;
565
- fieldRef(model: string, field: string, eb: ExpressionBuilder<any, any>, modelAlias?: string): ExpressionWrapper<any, any, unknown>;
565
+ fieldRef(model: string, field: string, eb: ExpressionBuilder<any, any>, modelAlias?: string, inlineComputedField?: boolean): ExpressionWrapper<any, any, unknown>;
566
566
  abstract get provider(): DataSourceProviderType;
567
567
  /**
568
568
  * Builds selection for a relation field.
@@ -603,6 +603,7 @@ declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
603
603
  }
604
604
 
605
605
  type CrudOperation = 'findMany' | 'findUnique' | 'findFirst' | 'create' | 'createMany' | 'createManyAndReturn' | 'update' | 'updateMany' | 'updateManyAndReturn' | 'upsert' | 'delete' | 'deleteMany' | 'count' | 'aggregate' | 'groupBy';
606
+ type AllCrudOperation = CrudOperation | 'findUniqueOrThrow' | 'findFirstOrThrow';
606
607
 
607
608
  /**
608
609
  * ZenStack runtime plugin.
@@ -647,7 +648,7 @@ type OnQueryHookContext<Schema extends SchemaDef> = {
647
648
  /**
648
649
  * The operation that is being performed.
649
650
  */
650
- operation: CrudOperation;
651
+ operation: AllCrudOperation;
651
652
  /**
652
653
  * The query arguments.
653
654
  */
package/dist/index.cjs CHANGED
@@ -305,11 +305,14 @@ function getIdValues(schema, model, data) {
305
305
  }), {});
306
306
  }
307
307
  __name(getIdValues, "getIdValues");
308
- function buildFieldRef(schema, model, field, options, eb, modelAlias) {
308
+ function buildFieldRef(schema, model, field, options, eb, modelAlias, inlineComputedField = true) {
309
309
  const fieldDef = requireField(schema, model, field);
310
310
  if (!fieldDef.computed) {
311
311
  return eb.ref(modelAlias ? `${modelAlias}.${field}` : field);
312
312
  } else {
313
+ if (!inlineComputedField) {
314
+ return eb.ref(modelAlias ? `${modelAlias}.${field}` : field);
315
+ }
313
316
  let computer;
314
317
  if ("computedFields" in options) {
315
318
  const computedFields = options.computedFields;
@@ -493,7 +496,6 @@ var import_kysely2 = require("kysely");
493
496
  var import_ts_pattern3 = require("ts-pattern");
494
497
 
495
498
  // src/client/constants.ts
496
- var CONTEXT_COMMENT_PREFIX = "-- $$context:";
497
499
  var NUMERIC_FIELD_TYPES = [
498
500
  "Int",
499
501
  "Float",
@@ -1080,8 +1082,8 @@ var BaseCrudDialect = class {
1080
1082
  not(eb, ...args) {
1081
1083
  return eb.not(this.and(eb, ...args));
1082
1084
  }
1083
- fieldRef(model, field, eb, modelAlias) {
1084
- return buildFieldRef(this.schema, model, field, this.options, eb, modelAlias);
1085
+ fieldRef(model, field, eb, modelAlias, inlineComputedField = true) {
1086
+ return buildFieldRef(this.schema, model, field, this.options, eb, modelAlias, inlineComputedField);
1085
1087
  }
1086
1088
  };
1087
1089
 
@@ -1176,7 +1178,7 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
1176
1178
  if (payload === true || !payload.select) {
1177
1179
  objArgs.push(...Object.entries(relationModelDef.fields).filter(([, value]) => !value.relation).filter(([name]) => !(typeof payload === "object" && payload.omit?.[name] === true)).map(([field]) => [
1178
1180
  import_kysely2.sql.lit(field),
1179
- this.fieldRef(relationModel, field, eb)
1181
+ this.fieldRef(relationModel, field, eb, void 0, false)
1180
1182
  ]).flatMap((v) => v));
1181
1183
  } else if (payload.select) {
1182
1184
  objArgs.push(...Object.entries(payload.select).filter(([, value]) => value).map(([field, value]) => {
@@ -1188,7 +1190,7 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
1188
1190
  ];
1189
1191
  } else {
1190
1192
  const fieldDef = requireField(this.schema, relationModel, field);
1191
- const fieldValue = fieldDef.relation ? eb.ref(`${parentAlias}$${relationField}$${field}.$j`) : this.fieldRef(relationModel, field, eb);
1193
+ const fieldValue = fieldDef.relation ? eb.ref(`${parentAlias}$${relationField}$${field}.$j`) : this.fieldRef(relationModel, field, eb, void 0, false);
1192
1194
  return [
1193
1195
  import_kysely2.sql.lit(field),
1194
1196
  fieldValue
@@ -1339,7 +1341,7 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
1339
1341
  if (payload === true || !payload.select) {
1340
1342
  objArgs.push(...Object.entries(relationModelDef.fields).filter(([, value]) => !value.relation).filter(([name]) => !(typeof payload === "object" && payload.omit?.[name] === true)).map(([field]) => [
1341
1343
  import_kysely3.sql.lit(field),
1342
- this.fieldRef(relationModel, field, eb)
1344
+ this.fieldRef(relationModel, field, eb, void 0, false)
1343
1345
  ]).flatMap((v) => v));
1344
1346
  } else if (payload.select) {
1345
1347
  objArgs.push(...Object.entries(payload.select).filter(([, value]) => value).map(([field, value]) => {
@@ -1360,7 +1362,7 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
1360
1362
  } else {
1361
1363
  return [
1362
1364
  import_kysely3.sql.lit(field),
1363
- this.fieldRef(relationModel, field, eb)
1365
+ this.fieldRef(relationModel, field, eb, void 0, false)
1364
1366
  ];
1365
1367
  }
1366
1368
  }
@@ -3460,8 +3462,8 @@ ${compiled.parameters.map((p) => (0, import_node_util.inspect)(p)).join("\n")}`;
3460
3462
  isNumericField(fieldDef) {
3461
3463
  return NUMERIC_FIELD_TYPES.includes(fieldDef.type) && !fieldDef.array;
3462
3464
  }
3463
- makeContextComment(context) {
3464
- return import_kysely8.sql.raw(`${CONTEXT_COMMENT_PREFIX}${JSON.stringify(context)}`);
3465
+ makeContextComment(_context) {
3466
+ return import_kysely8.sql``;
3465
3467
  }
3466
3468
  async updateMany(kysely, model, where, data, limit, returnData, filterModel) {
3467
3469
  if (typeof data !== "object") {
@@ -4504,8 +4506,7 @@ var InputValidator = class {
4504
4506
  }
4505
4507
  validateFindArgs(model, unique, args) {
4506
4508
  return this.validate(model, "find", {
4507
- unique,
4508
- collection: true
4509
+ unique
4509
4510
  }, (model2, options) => this.makeFindSchema(model2, options), args);
4510
4511
  }
4511
4512
  validateCreateArgs(model, args) {
@@ -4573,12 +4574,12 @@ var InputValidator = class {
4573
4574
  fields["select"] = this.makeSelectSchema(model).optional();
4574
4575
  fields["include"] = this.makeIncludeSchema(model).optional();
4575
4576
  fields["omit"] = this.makeOmitSchema(model).optional();
4576
- fields["distinct"] = this.makeDistinctSchema(model).optional();
4577
- fields["cursor"] = this.makeCursorSchema(model).optional();
4578
- if (options.collection) {
4577
+ if (!options.unique) {
4579
4578
  fields["skip"] = this.makeSkipSchema().optional();
4580
4579
  fields["take"] = this.makeTakeSchema().optional();
4581
4580
  fields["orderBy"] = this.orArray(this.makeOrderBySchema(model, true, false), true).optional();
4581
+ fields["cursor"] = this.makeCursorSchema(model).optional();
4582
+ fields["distinct"] = this.makeDistinctSchema(model).optional();
4582
4583
  }
4583
4584
  let result = import_zod.z.strictObject(fields);
4584
4585
  result = this.refineForSelectIncludeMutuallyExclusive(result);
@@ -4884,13 +4885,7 @@ var InputValidator = class {
4884
4885
  for (const field of Object.keys(modelDef.fields)) {
4885
4886
  const fieldDef = requireField(this.schema, model, field);
4886
4887
  if (fieldDef.relation) {
4887
- fields[field] = import_zod.z.union([
4888
- import_zod.z.literal(true),
4889
- import_zod.z.strictObject({
4890
- select: import_zod.z.lazy(() => this.makeSelectSchema(fieldDef.type)).optional(),
4891
- include: import_zod.z.lazy(() => this.makeIncludeSchema(fieldDef.type)).optional()
4892
- })
4893
- ]).optional();
4888
+ fields[field] = this.makeRelationSelectIncludeSchema(fieldDef).optional();
4894
4889
  } else {
4895
4890
  fields[field] = import_zod.z.boolean().optional();
4896
4891
  }
@@ -4914,6 +4909,28 @@ var InputValidator = class {
4914
4909
  }
4915
4910
  return import_zod.z.strictObject(fields);
4916
4911
  }
4912
+ makeRelationSelectIncludeSchema(fieldDef) {
4913
+ return import_zod.z.union([
4914
+ import_zod.z.boolean(),
4915
+ import_zod.z.strictObject({
4916
+ ...fieldDef.array || fieldDef.optional ? {
4917
+ // to-many relations and optional to-one relations are filterable
4918
+ where: import_zod.z.lazy(() => this.makeWhereSchema(fieldDef.type, false)).optional()
4919
+ } : {},
4920
+ select: import_zod.z.lazy(() => this.makeSelectSchema(fieldDef.type)).optional(),
4921
+ include: import_zod.z.lazy(() => this.makeIncludeSchema(fieldDef.type)).optional(),
4922
+ omit: import_zod.z.lazy(() => this.makeOmitSchema(fieldDef.type)).optional(),
4923
+ ...fieldDef.array ? {
4924
+ // to-many relations can be ordered, skipped, taken, and cursor-located
4925
+ orderBy: import_zod.z.lazy(() => this.makeOrderBySchema(fieldDef.type, true, false)).optional(),
4926
+ skip: this.makeSkipSchema().optional(),
4927
+ take: this.makeTakeSchema().optional(),
4928
+ cursor: this.makeCursorSchema(fieldDef.type).optional(),
4929
+ distinct: this.makeDistinctSchema(fieldDef.type).optional()
4930
+ } : {}
4931
+ })
4932
+ ]);
4933
+ }
4917
4934
  makeOmitSchema(model) {
4918
4935
  const modelDef = requireModel(this.schema, model);
4919
4936
  const fields = {};
@@ -4931,19 +4948,7 @@ var InputValidator = class {
4931
4948
  for (const field of Object.keys(modelDef.fields)) {
4932
4949
  const fieldDef = requireField(this.schema, model, field);
4933
4950
  if (fieldDef.relation) {
4934
- fields[field] = import_zod.z.union([
4935
- import_zod.z.literal(true),
4936
- import_zod.z.strictObject({
4937
- select: import_zod.z.lazy(() => this.makeSelectSchema(fieldDef.type)).optional(),
4938
- include: import_zod.z.lazy(() => this.makeIncludeSchema(fieldDef.type)).optional(),
4939
- omit: import_zod.z.lazy(() => this.makeOmitSchema(fieldDef.type)).optional(),
4940
- where: import_zod.z.lazy(() => this.makeWhereSchema(fieldDef.type, false)).optional(),
4941
- orderBy: import_zod.z.lazy(() => this.makeOrderBySchema(fieldDef.type, true, false)).optional(),
4942
- skip: this.makeSkipSchema().optional(),
4943
- take: this.makeTakeSchema().optional(),
4944
- distinct: this.makeDistinctSchema(fieldDef.type).optional()
4945
- })
4946
- ]).optional();
4951
+ fields[field] = this.makeRelationSelectIncludeSchema(fieldDef).optional();
4947
4952
  }
4948
4953
  }
4949
4954
  return import_zod.z.strictObject(fields);