@zenstackhq/runtime 1.3.2 → 1.4.0

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/cross/index.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Callback for @see ModelDataVisitor.
3
3
  */
4
- type ModelDataVisitorCallback = (model: string, data: any, scalarData: any) => void;
4
+ declare type ModelDataVisitorCallback = (model: string, data: any, scalarData: any) => void;
5
5
  /**
6
6
  * Visitor that traverses data returned by a Prisma query.
7
7
  */
@@ -17,7 +17,7 @@ declare class ModelDataVisitor {
17
17
  /**
18
18
  * Runtime information of a data model or field attribute
19
19
  */
20
- type RuntimeAttribute = {
20
+ declare type RuntimeAttribute = {
21
21
  name: string;
22
22
  args: Array<{
23
23
  name?: string;
@@ -27,7 +27,7 @@ type RuntimeAttribute = {
27
27
  /**
28
28
  * Runtime information of a data model field
29
29
  */
30
- type FieldInfo = {
30
+ declare type FieldInfo = {
31
31
  /**
32
32
  * Field name
33
33
  */
@@ -77,14 +77,14 @@ type FieldInfo = {
77
77
  * Metadata for a model-level unique constraint
78
78
  * e.g.: @@unique([a, b])
79
79
  */
80
- type UniqueConstraint = {
80
+ declare type UniqueConstraint = {
81
81
  name: string;
82
82
  fields: string[];
83
83
  };
84
84
  /**
85
85
  * ZModel data model metadata
86
86
  */
87
- type ModelMeta = {
87
+ declare type ModelMeta = {
88
88
  /**
89
89
  * Model fields
90
90
  */
@@ -105,7 +105,11 @@ type ModelMeta = {
105
105
  /**
106
106
  * Resolves a model field to its metadata. Returns undefined if not found.
107
107
  */
108
- declare function resolveField(modelMeta: ModelMeta, model: string, field: string): FieldInfo | undefined;
108
+ declare function resolveField(modelMeta: ModelMeta, model: string, field: string): FieldInfo;
109
+ /**
110
+ * Resolves a model field to its metadata. Throws an error if not found.
111
+ */
112
+ declare function requireField(modelMeta: ModelMeta, model: string, field: string): FieldInfo;
109
113
  /**
110
114
  * Gets all fields of a model.
111
115
  */
@@ -126,7 +130,7 @@ declare function getFields(modelMeta: ModelMeta, model: string): Record<string,
126
130
  */
127
131
  declare function applyMutation(queryModel: string, queryOp: string, queryData: any, mutationModel: string, mutationOp: PrismaWriteActionType, mutationArgs: any, modelMeta: ModelMeta, logging: boolean): Promise<any>;
128
132
 
129
- type NestedReadVisitorCallback = {
133
+ declare type NestedReadVisitorCallback = {
130
134
  field?: (model: string, field: FieldInfo | undefined, kind: 'include' | 'select' | undefined, args: unknown) => void | boolean;
131
135
  };
132
136
  /**
@@ -147,13 +151,13 @@ declare const PrismaWriteActions: readonly ["create", "createMany", "connectOrCr
147
151
  /**
148
152
  * Prisma write operation kinds
149
153
  */
150
- type PrismaWriteActionType = (typeof PrismaWriteActions)[number];
154
+ declare type PrismaWriteActionType = (typeof PrismaWriteActions)[number];
151
155
  /**
152
156
  * Maybe promise
153
157
  */
154
- type MaybePromise<T> = T | Promise<T> | PromiseLike<T>;
158
+ declare type MaybePromise<T> = T | Promise<T> | PromiseLike<T>;
155
159
 
156
- type NestingPathItem = {
160
+ declare type NestingPathItem = {
157
161
  field?: FieldInfo;
158
162
  model: string;
159
163
  where: any;
@@ -162,7 +166,7 @@ type NestingPathItem = {
162
166
  /**
163
167
  * Context for visiting
164
168
  */
165
- type NestedWriteVisitorContext = {
169
+ declare type NestedWriteVisitorContext = {
166
170
  /**
167
171
  * Parent data, can be used to replace fields
168
172
  */
@@ -181,7 +185,7 @@ type NestedWriteVisitorContext = {
181
185
  * that the visitor should continue traversing its children, or false to stop. It can also return an object
182
186
  * to let the visitor traverse it instead of its original children.
183
187
  */
184
- type NestedWriterVisitorCallback = {
188
+ declare type NestedWriterVisitorCallback = {
185
189
  create?: (model: string, args: any[], context: NestedWriteVisitorContext) => MaybePromise<boolean | object | void>;
186
190
  createMany?: (model: string, args: {
187
191
  data: any;
@@ -247,7 +251,7 @@ declare function getModelFields(data: object): string[];
247
251
  /**
248
252
  * Array or scalar
249
253
  */
250
- type Enumerable<T> = T | Array<T>;
254
+ declare type Enumerable<T> = T | Array<T>;
251
255
  /**
252
256
  * Uniformly enumerates an array or scalar.
253
257
  */
@@ -258,4 +262,4 @@ declare function enumerate<T>(x: Enumerable<T>): T[];
258
262
  declare function zip<T1, T2>(x: Enumerable<T1>, y: Enumerable<T2>): Array<[T1, T2]>;
259
263
  declare function getIdFields(modelMeta: ModelMeta, model: string, throwIfNotFound?: boolean): FieldInfo[];
260
264
 
261
- export { Enumerable, FieldInfo, MaybePromise, ModelDataVisitor, ModelDataVisitorCallback, ModelMeta, NestedReadVisitor, NestedReadVisitorCallback, NestedWriteVisitor, NestedWriteVisitorContext, NestedWriterVisitorCallback, PrismaWriteActionType, PrismaWriteActions, RuntimeAttribute, UniqueConstraint, applyMutation, enumerate, getFields, getIdFields, getModelFields, getMutatedModels, getReadModels, resolveField, zip };
265
+ export { Enumerable, FieldInfo, MaybePromise, ModelDataVisitor, ModelDataVisitorCallback, ModelMeta, NestedReadVisitor, NestedReadVisitorCallback, NestedWriteVisitor, NestedWriteVisitorContext, NestedWriterVisitorCallback, PrismaWriteActionType, PrismaWriteActions, RuntimeAttribute, UniqueConstraint, applyMutation, enumerate, getFields, getIdFields, getModelFields, getMutatedModels, getReadModels, requireField, resolveField, zip };
package/cross/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Callback for @see ModelDataVisitor.
3
3
  */
4
- type ModelDataVisitorCallback = (model: string, data: any, scalarData: any) => void;
4
+ declare type ModelDataVisitorCallback = (model: string, data: any, scalarData: any) => void;
5
5
  /**
6
6
  * Visitor that traverses data returned by a Prisma query.
7
7
  */
@@ -17,7 +17,7 @@ declare class ModelDataVisitor {
17
17
  /**
18
18
  * Runtime information of a data model or field attribute
19
19
  */
20
- type RuntimeAttribute = {
20
+ declare type RuntimeAttribute = {
21
21
  name: string;
22
22
  args: Array<{
23
23
  name?: string;
@@ -27,7 +27,7 @@ type RuntimeAttribute = {
27
27
  /**
28
28
  * Runtime information of a data model field
29
29
  */
30
- type FieldInfo = {
30
+ declare type FieldInfo = {
31
31
  /**
32
32
  * Field name
33
33
  */
@@ -77,14 +77,14 @@ type FieldInfo = {
77
77
  * Metadata for a model-level unique constraint
78
78
  * e.g.: @@unique([a, b])
79
79
  */
80
- type UniqueConstraint = {
80
+ declare type UniqueConstraint = {
81
81
  name: string;
82
82
  fields: string[];
83
83
  };
84
84
  /**
85
85
  * ZModel data model metadata
86
86
  */
87
- type ModelMeta = {
87
+ declare type ModelMeta = {
88
88
  /**
89
89
  * Model fields
90
90
  */
@@ -105,7 +105,11 @@ type ModelMeta = {
105
105
  /**
106
106
  * Resolves a model field to its metadata. Returns undefined if not found.
107
107
  */
108
- declare function resolveField(modelMeta: ModelMeta, model: string, field: string): FieldInfo | undefined;
108
+ declare function resolveField(modelMeta: ModelMeta, model: string, field: string): FieldInfo;
109
+ /**
110
+ * Resolves a model field to its metadata. Throws an error if not found.
111
+ */
112
+ declare function requireField(modelMeta: ModelMeta, model: string, field: string): FieldInfo;
109
113
  /**
110
114
  * Gets all fields of a model.
111
115
  */
@@ -126,7 +130,7 @@ declare function getFields(modelMeta: ModelMeta, model: string): Record<string,
126
130
  */
127
131
  declare function applyMutation(queryModel: string, queryOp: string, queryData: any, mutationModel: string, mutationOp: PrismaWriteActionType, mutationArgs: any, modelMeta: ModelMeta, logging: boolean): Promise<any>;
128
132
 
129
- type NestedReadVisitorCallback = {
133
+ declare type NestedReadVisitorCallback = {
130
134
  field?: (model: string, field: FieldInfo | undefined, kind: 'include' | 'select' | undefined, args: unknown) => void | boolean;
131
135
  };
132
136
  /**
@@ -147,13 +151,13 @@ declare const PrismaWriteActions: readonly ["create", "createMany", "connectOrCr
147
151
  /**
148
152
  * Prisma write operation kinds
149
153
  */
150
- type PrismaWriteActionType = (typeof PrismaWriteActions)[number];
154
+ declare type PrismaWriteActionType = (typeof PrismaWriteActions)[number];
151
155
  /**
152
156
  * Maybe promise
153
157
  */
154
- type MaybePromise<T> = T | Promise<T> | PromiseLike<T>;
158
+ declare type MaybePromise<T> = T | Promise<T> | PromiseLike<T>;
155
159
 
156
- type NestingPathItem = {
160
+ declare type NestingPathItem = {
157
161
  field?: FieldInfo;
158
162
  model: string;
159
163
  where: any;
@@ -162,7 +166,7 @@ type NestingPathItem = {
162
166
  /**
163
167
  * Context for visiting
164
168
  */
165
- type NestedWriteVisitorContext = {
169
+ declare type NestedWriteVisitorContext = {
166
170
  /**
167
171
  * Parent data, can be used to replace fields
168
172
  */
@@ -181,7 +185,7 @@ type NestedWriteVisitorContext = {
181
185
  * that the visitor should continue traversing its children, or false to stop. It can also return an object
182
186
  * to let the visitor traverse it instead of its original children.
183
187
  */
184
- type NestedWriterVisitorCallback = {
188
+ declare type NestedWriterVisitorCallback = {
185
189
  create?: (model: string, args: any[], context: NestedWriteVisitorContext) => MaybePromise<boolean | object | void>;
186
190
  createMany?: (model: string, args: {
187
191
  data: any;
@@ -247,7 +251,7 @@ declare function getModelFields(data: object): string[];
247
251
  /**
248
252
  * Array or scalar
249
253
  */
250
- type Enumerable<T> = T | Array<T>;
254
+ declare type Enumerable<T> = T | Array<T>;
251
255
  /**
252
256
  * Uniformly enumerates an array or scalar.
253
257
  */
@@ -258,4 +262,4 @@ declare function enumerate<T>(x: Enumerable<T>): T[];
258
262
  declare function zip<T1, T2>(x: Enumerable<T1>, y: Enumerable<T2>): Array<[T1, T2]>;
259
263
  declare function getIdFields(modelMeta: ModelMeta, model: string, throwIfNotFound?: boolean): FieldInfo[];
260
264
 
261
- export { Enumerable, FieldInfo, MaybePromise, ModelDataVisitor, ModelDataVisitorCallback, ModelMeta, NestedReadVisitor, NestedReadVisitorCallback, NestedWriteVisitor, NestedWriteVisitorContext, NestedWriterVisitorCallback, PrismaWriteActionType, PrismaWriteActions, RuntimeAttribute, UniqueConstraint, applyMutation, enumerate, getFields, getIdFields, getModelFields, getMutatedModels, getReadModels, resolveField, zip };
265
+ export { Enumerable, FieldInfo, MaybePromise, ModelDataVisitor, ModelDataVisitorCallback, ModelMeta, NestedReadVisitor, NestedReadVisitorCallback, NestedWriteVisitor, NestedWriteVisitorContext, NestedWriterVisitorCallback, PrismaWriteActionType, PrismaWriteActions, RuntimeAttribute, UniqueConstraint, applyMutation, enumerate, getFields, getIdFields, getModelFields, getMutatedModels, getReadModels, requireField, resolveField, zip };
package/cross/index.js CHANGED
@@ -78,6 +78,7 @@ __export(cross_exports, {
78
78
  getModelFields: () => getModelFields,
79
79
  getMutatedModels: () => getMutatedModels,
80
80
  getReadModels: () => getReadModels,
81
+ requireField: () => requireField,
81
82
  resolveField: () => resolveField,
82
83
  zip: () => zip
83
84
  });
@@ -120,6 +121,13 @@ function resolveField(modelMeta, model, field) {
120
121
  var _a;
121
122
  return (_a = modelMeta.fields[(0, import_lower_case_first.lowerCaseFirst)(model)]) == null ? void 0 : _a[field];
122
123
  }
124
+ function requireField(modelMeta, model, field) {
125
+ const f = resolveField(modelMeta, model, field);
126
+ if (!f) {
127
+ throw new Error(`Field ${model}.${field} cannot be resolved`);
128
+ }
129
+ return f;
130
+ }
123
131
  function getFields(modelMeta, model) {
124
132
  return modelMeta.fields[(0, import_lower_case_first.lowerCaseFirst)(model)];
125
133
  }
@@ -156,17 +164,21 @@ function applyMutation(queryModel, queryOp, queryData, mutationModel, mutationOp
156
164
  }
157
165
  },
158
166
  update: (model, args) => {
159
- const r = updateMutate(queryModel, resultData, model, args, modelMeta, logging);
160
- if (r) {
161
- resultData = r;
162
- updated = true;
167
+ if (model === queryModel) {
168
+ const r = updateMutate(queryModel, resultData, model, args, modelMeta, logging);
169
+ if (r) {
170
+ resultData = r;
171
+ updated = true;
172
+ }
163
173
  }
164
174
  },
165
175
  delete: (model, args) => {
166
- const r = deleteMutate(queryModel, resultData, model, args, modelMeta, logging);
167
- if (r) {
168
- resultData = r;
169
- updated = true;
176
+ if (model === queryModel) {
177
+ const r = deleteMutate(queryModel, resultData, model, args, modelMeta, logging);
178
+ if (r) {
179
+ resultData = r;
180
+ updated = true;
181
+ }
170
182
  }
171
183
  }
172
184
  });
@@ -209,12 +221,12 @@ function createMutate(queryModel, queryOp, currentData, newData, modelMeta, logg
209
221
  idFields.forEach((f) => {
210
222
  if (insert[f.name] === void 0) {
211
223
  if (f.type === "Int" || f.type === "BigInt") {
212
- const currMax = Math.max(
224
+ const currMax = Array.isArray(currentData) ? Math.max(
213
225
  ...[...currentData].map((item) => {
214
226
  const idv = parseInt(item[f.name]);
215
227
  return isNaN(idv) ? 0 : idv;
216
228
  })
217
- );
229
+ ) : 0;
218
230
  insert[f.name] = currMax + 1;
219
231
  } else {
220
232
  insert[f.name] = (0, import_uuid.v4)();
@@ -225,7 +237,7 @@ function createMutate(queryModel, queryOp, currentData, newData, modelMeta, logg
225
237
  if (logging) {
226
238
  console.log(`Optimistic create for ${queryModel}:`, insert);
227
239
  }
228
- return [insert, ...currentData];
240
+ return [insert, ...Array.isArray(currentData) ? currentData : []];
229
241
  }
230
242
  function updateMutate(queryModel, currentData, mutateModel, mutateArgs, modelMeta, logging) {
231
243
  if (!currentData) {
@@ -436,54 +448,55 @@ var NestedWriteVisitor = class {
436
448
  if (!data) {
437
449
  return;
438
450
  }
439
- const context = { parent, field, nestingPath: [...nestingPath] };
440
451
  const toplevel = field == void 0;
452
+ const context = { parent, field, nestingPath: [...nestingPath] };
453
+ const pushNewContext = (field2, model2, where, unique = false) => {
454
+ return __spreadProps(__spreadValues({}, context), { nestingPath: [...context.nestingPath, { field: field2, model: model2, where, unique }] });
455
+ };
441
456
  switch (action) {
442
457
  case "create":
443
- context.nestingPath.push({ field, model, where: {}, unique: false });
444
458
  for (const item of enumerate(data)) {
459
+ const newContext = pushNewContext(field, model, {});
445
460
  let callbackResult;
446
461
  if (this.callback.create) {
447
- callbackResult = yield this.callback.create(model, item, context);
462
+ callbackResult = yield this.callback.create(model, item, newContext);
448
463
  }
449
464
  if (callbackResult !== false) {
450
465
  const subPayload = typeof callbackResult === "object" ? callbackResult : item;
451
- yield this.visitSubPayload(model, action, subPayload, context.nestingPath);
466
+ yield this.visitSubPayload(model, action, subPayload, newContext.nestingPath);
452
467
  }
453
468
  }
454
469
  break;
455
470
  case "createMany":
456
471
  if (data) {
457
- context.nestingPath.push({ field, model, where: {}, unique: false });
472
+ const newContext = pushNewContext(field, model, {});
458
473
  let callbackResult;
459
474
  if (this.callback.createMany) {
460
- callbackResult = yield this.callback.createMany(model, data, context);
475
+ callbackResult = yield this.callback.createMany(model, data, newContext);
461
476
  }
462
477
  if (callbackResult !== false) {
463
478
  const subPayload = typeof callbackResult === "object" ? callbackResult : data.data;
464
- yield this.visitSubPayload(model, action, subPayload, context.nestingPath);
479
+ yield this.visitSubPayload(model, action, subPayload, newContext.nestingPath);
465
480
  }
466
481
  }
467
482
  break;
468
483
  case "connectOrCreate":
469
- context.nestingPath.push({ field, model, where: data.where, unique: false });
470
484
  for (const item of enumerate(data)) {
485
+ const newContext = pushNewContext(field, model, item.where);
471
486
  let callbackResult;
472
487
  if (this.callback.connectOrCreate) {
473
- callbackResult = yield this.callback.connectOrCreate(model, item, context);
488
+ callbackResult = yield this.callback.connectOrCreate(model, item, newContext);
474
489
  }
475
490
  if (callbackResult !== false) {
476
491
  const subPayload = typeof callbackResult === "object" ? callbackResult : item.create;
477
- yield this.visitSubPayload(model, action, subPayload, context.nestingPath);
492
+ yield this.visitSubPayload(model, action, subPayload, newContext.nestingPath);
478
493
  }
479
494
  }
480
495
  break;
481
496
  case "connect":
482
497
  if (this.callback.connect) {
483
498
  for (const item of enumerate(data)) {
484
- const newContext = __spreadProps(__spreadValues({}, context), {
485
- nestingPath: [...context.nestingPath, { field, model, where: item, unique: true }]
486
- });
499
+ const newContext = pushNewContext(field, model, item, true);
487
500
  yield this.callback.connect(model, item, newContext);
488
501
  }
489
502
  }
@@ -491,61 +504,56 @@ var NestedWriteVisitor = class {
491
504
  case "disconnect":
492
505
  if (this.callback.disconnect) {
493
506
  for (const item of enumerate(data)) {
494
- const newContext = __spreadProps(__spreadValues({}, context), {
495
- nestingPath: [
496
- ...context.nestingPath,
497
- { field, model, where: item, unique: typeof item === "object" }
498
- ]
499
- });
507
+ const newContext = pushNewContext(field, model, item, typeof item === "object");
500
508
  yield this.callback.disconnect(model, item, newContext);
501
509
  }
502
510
  }
503
511
  break;
504
512
  case "set":
505
513
  if (this.callback.set) {
506
- context.nestingPath.push({ field, model, where: {}, unique: false });
507
- yield this.callback.set(model, data, context);
514
+ const newContext = pushNewContext(field, model, {});
515
+ yield this.callback.set(model, data, newContext);
508
516
  }
509
517
  break;
510
518
  case "update":
511
- context.nestingPath.push({ field, model, where: data.where, unique: false });
512
519
  for (const item of enumerate(data)) {
520
+ const newContext = pushNewContext(field, model, item.where);
513
521
  let callbackResult;
514
522
  if (this.callback.update) {
515
- callbackResult = yield this.callback.update(model, item, context);
523
+ callbackResult = yield this.callback.update(model, item, newContext);
516
524
  }
517
525
  if (callbackResult !== false) {
518
526
  const subPayload = typeof callbackResult === "object" ? callbackResult : typeof item.data === "object" ? item.data : item;
519
- yield this.visitSubPayload(model, action, subPayload, context.nestingPath);
527
+ yield this.visitSubPayload(model, action, subPayload, newContext.nestingPath);
520
528
  }
521
529
  }
522
530
  break;
523
531
  case "updateMany":
524
- context.nestingPath.push({ field, model, where: data.where, unique: false });
525
532
  for (const item of enumerate(data)) {
533
+ const newContext = pushNewContext(field, model, item.where);
526
534
  let callbackResult;
527
535
  if (this.callback.updateMany) {
528
- callbackResult = yield this.callback.updateMany(model, item, context);
536
+ callbackResult = yield this.callback.updateMany(model, item, newContext);
529
537
  }
530
538
  if (callbackResult !== false) {
531
539
  const subPayload = typeof callbackResult === "object" ? callbackResult : item;
532
- yield this.visitSubPayload(model, action, subPayload, context.nestingPath);
540
+ yield this.visitSubPayload(model, action, subPayload, newContext.nestingPath);
533
541
  }
534
542
  }
535
543
  break;
536
544
  case "upsert": {
537
- context.nestingPath.push({ field, model, where: data.where, unique: false });
538
545
  for (const item of enumerate(data)) {
546
+ const newContext = pushNewContext(field, model, item.where);
539
547
  let callbackResult;
540
548
  if (this.callback.upsert) {
541
- callbackResult = yield this.callback.upsert(model, item, context);
549
+ callbackResult = yield this.callback.upsert(model, item, newContext);
542
550
  }
543
551
  if (callbackResult !== false) {
544
552
  if (typeof callbackResult === "object") {
545
- yield this.visitSubPayload(model, action, callbackResult, context.nestingPath);
553
+ yield this.visitSubPayload(model, action, callbackResult, newContext.nestingPath);
546
554
  } else {
547
- yield this.visitSubPayload(model, action, item.create, context.nestingPath);
548
- yield this.visitSubPayload(model, action, item.update, context.nestingPath);
555
+ yield this.visitSubPayload(model, action, item.create, newContext.nestingPath);
556
+ yield this.visitSubPayload(model, action, item.update, newContext.nestingPath);
549
557
  }
550
558
  }
551
559
  }
@@ -554,12 +562,7 @@ var NestedWriteVisitor = class {
554
562
  case "delete": {
555
563
  if (this.callback.delete) {
556
564
  for (const item of enumerate(data)) {
557
- const newContext = __spreadProps(__spreadValues({}, context), {
558
- nestingPath: [
559
- ...context.nestingPath,
560
- { field, model, where: toplevel ? item.where : item, unique: false }
561
- ]
562
- });
565
+ const newContext = pushNewContext(field, model, toplevel ? item.where : item);
563
566
  yield this.callback.delete(model, item, newContext);
564
567
  }
565
568
  }
@@ -568,12 +571,7 @@ var NestedWriteVisitor = class {
568
571
  case "deleteMany":
569
572
  if (this.callback.deleteMany) {
570
573
  for (const item of enumerate(data)) {
571
- const newContext = __spreadProps(__spreadValues({}, context), {
572
- nestingPath: [
573
- ...context.nestingPath,
574
- { field, model, where: toplevel ? item.where : item, unique: false }
575
- ]
576
- });
574
+ const newContext = pushNewContext(field, model, toplevel ? item.where : item);
577
575
  yield this.callback.deleteMany(model, item, newContext);
578
576
  }
579
577
  }
@@ -692,6 +690,7 @@ function collectDeleteCascades(model, modelMeta, result, visited) {
692
690
  getModelFields,
693
691
  getMutatedModels,
694
692
  getReadModels,
693
+ requireField,
695
694
  resolveField,
696
695
  zip
697
696
  });