@murumets-ee/entity 0.10.0 → 0.11.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.
@@ -36,78 +36,6 @@ interface CountCacheLike {
36
36
  invalidate(prefix: string): void;
37
37
  }
38
38
  //#endregion
39
- //#region src/cursor.d.ts
40
- /** Cursor input for keyset pagination. */
41
- interface CursorInput {
42
- /** Sort field name (e.g. 'createdAt'). Must be a real column on the entity. */
43
- field: string;
44
- /** Last seen value of the sort field. */
45
- value: string | number;
46
- /** Sort direction — must match the ORDER BY direction. */
47
- direction: 'asc' | 'desc';
48
- /** Tie-breaker: last seen entity ID. Required for non-unique sort fields. */
49
- id?: string;
50
- }
51
- //#endregion
52
- //#region src/admin-config.d.ts
53
- /**
54
- * Optional admin UI configuration for entities.
55
- * Controls how entities appear in the admin sidebar, list pages, and forms.
56
- */
57
- interface EntityAdminConfig {
58
- /** Sidebar section: 'content' | 'structure' | custom string. Default: 'content' */
59
- group?: string;
60
- /** Plural display name for sidebar + list pages. Default: title-cased pluralized entity name */
61
- label?: string;
62
- /** Singular label for "New X" button. Default: title-cased entity name */
63
- labelSingular?: string;
64
- /** Lucide icon name as string, e.g. 'file-text' */
65
- icon?: string;
66
- /** Description shown on list page */
67
- description?: string;
68
- /** Form layout. Default: 'single' */
69
- layout?: 'single' | 'two-column';
70
- /** Fields to hide in the form */
71
- hiddenFields?: string[];
72
- /** Columns to hide in the list */
73
- hiddenColumns?: string[];
74
- /** Per-field label/description/placeholder overrides */
75
- fieldOverrides?: Record<string, {
76
- label?: string;
77
- description?: string;
78
- placeholder?: string;
79
- }>;
80
- /** Default list sort field. Default: 'createdAt' */
81
- defaultSort?: string;
82
- /** Default list sort direction. Default: 'desc' */
83
- defaultSortDirection?: 'asc' | 'desc';
84
- /** List page size. Default: 20 */
85
- pageSize?: number;
86
- /** For block editor: fields to show in Puck root config */
87
- rootFields?: string[];
88
- /** Suppress "New" button in the list page */
89
- disableCreate?: boolean;
90
- /** Order within sidebar group. Default: 0 */
91
- sortOrder?: number;
92
- /**
93
- * Hide the entity from BOTH dashboard and sidebar.
94
- * Use for system entities that should not appear in any UI.
95
- * Shorthand for `hideFromMenu: true` + `hideFromDashboard: true`.
96
- */
97
- hidden?: boolean;
98
- /**
99
- * Hide from sidebar menu only. Entity remains accessible via dashboard, direct URL,
100
- * and the entity API. Use for storage models accessed through specialized UI
101
- * (e.g., Ticket entities accessed via the inbox/board, not as a CRUD table).
102
- */
103
- hideFromMenu?: boolean;
104
- /**
105
- * Hide from dashboard widget grid only. Entity still appears in sidebar.
106
- * Use for entities that don't have meaningful counts/stats to show.
107
- */
108
- hideFromDashboard?: boolean;
109
- }
110
- //#endregion
111
39
  //#region src/fields/base.d.ts
112
40
  /**
113
41
  * Field type definitions
@@ -119,6 +47,23 @@ interface BaseFieldConfig {
119
47
  translatable?: boolean;
120
48
  indexed?: boolean;
121
49
  unique?: boolean;
50
+ /**
51
+ * Marks the field as **system-managed**: stored as a real column, populated
52
+ * by behavior hooks or trusted server-side transitions, but NOT writable
53
+ * through the public `AdminClient.create/update` surface.
54
+ *
55
+ * - Caller-supplied values for internal fields are silently stripped before
56
+ * hooks run (so an HTTP PATCH cannot poison a workflow state).
57
+ * - `beforeCreate` / `beforeUpdate` hooks may still set them (they run after
58
+ * the strip), and the values are preserved through validation.
59
+ * - Trusted server code that needs to write internals directly — e.g.
60
+ * workflow transitions invoked from an authorized admin route — must use
61
+ * `AdminClient.updateInternal()`, which bypasses the strip.
62
+ *
63
+ * Use this flag on any field added by a behavior whose value represents a
64
+ * controlled state machine (e.g. `_workflowStatus`), not user input.
65
+ */
66
+ internal?: boolean;
122
67
  access?: {
123
68
  view?: string;
124
69
  edit?: string;
@@ -199,12 +144,142 @@ interface BlocksField extends BaseFieldConfig {
199
144
  }
200
145
  type FieldConfig = IdField | TextField | NumberField | BooleanField | DateField | SelectField | ReferenceField | MediaField | RichTextField | SlugField | JsonField | BlocksField;
201
146
  //#endregion
147
+ //#region src/types/infer.d.ts
148
+ /**
149
+ * Recursive JSON-compatible value, for `field.json()` (jsonb column).
150
+ * Mirrors what Postgres jsonb can store: primitives, arrays, or objects.
151
+ */
152
+ type JsonValue = string | number | boolean | null | JsonValue[] | {
153
+ [key: string]: JsonValue;
154
+ };
155
+ /**
156
+ * Maps a single FieldConfig to its TypeScript output type.
157
+ * Each branch is a shallow comparison — no recursion.
158
+ */
159
+ type FieldToTS<F extends FieldConfig> = F extends IdField ? string : F extends TextField ? string : F extends NumberField ? number : F extends BooleanField ? boolean : F extends DateField ? Date | string : F extends SelectField ? F['options'][number] : F extends ReferenceField ? F['cardinality'] extends 'many' ? string[] : string : F extends MediaField ? string : F extends RichTextField ? Record<string, unknown>[] : F extends SlugField ? string : F extends JsonField ? JsonValue : F extends BlocksField ? Array<{
160
+ _block: string;
161
+ _id: string;
162
+ [key: string]: unknown;
163
+ }> : never;
164
+ /**
165
+ * Maps a full field record to its TypeScript output type.
166
+ * Required fields are non-nullable; optional fields are `T | null | undefined`.
167
+ *
168
+ * The `id` field is always `string` and always present.
169
+ * The `id` key from Fields is excluded to avoid duplication since
170
+ * we hardcode `{ id: string }` at the front.
171
+ */
172
+ type InferEntityDTO<Fields extends Record<string, FieldConfig>> = {
173
+ id: string;
174
+ } & { [K in keyof Fields as K extends 'id' ? never : Fields[K]['required'] extends true ? K : never]: FieldToTS<Fields[K]> } & { [K in keyof Fields as Fields[K]['required'] extends true ? never : K]?: FieldToTS<Fields[K]> | null };
175
+ /** Fields that are auto-generated and should not appear in create input. */
176
+ type AutoGeneratedFields = 'id' | 'createdAt' | 'updatedAt' | 'createdBy' | 'updatedBy' | '_version';
177
+ /**
178
+ * The input type for creating an entity.
179
+ * - Omits auto-generated fields (id, timestamps, version)
180
+ * - Required fields stay required; optional fields stay optional
181
+ */
182
+ type InferCreateInput<Fields extends Record<string, FieldConfig>> = Omit<{ [K in keyof Fields as K extends 'id' ? never : Fields[K]['required'] extends true ? K : never]: FieldToTS<Fields[K]> } & { [K in keyof Fields as Fields[K]['required'] extends true ? never : K]?: FieldToTS<Fields[K]> | null }, AutoGeneratedFields>;
183
+ /** Fields that cannot be changed after creation. */
184
+ type ImmutableFields = 'id' | 'createdAt' | 'createdBy';
185
+ type InferUpdateInput<Fields extends Record<string, FieldConfig>> = { [K in keyof Fields as K extends ImmutableFields ? never : K]?: FieldToTS<Fields[K]> | null };
186
+ //#endregion
187
+ //#region src/cursor.d.ts
188
+ /** Cursor input for keyset pagination. */
189
+ interface CursorInput {
190
+ /** Sort field name (e.g. 'createdAt'). Must be a real column on the entity. */
191
+ field: string;
192
+ /** Last seen value of the sort field. */
193
+ value: string | number;
194
+ /** Sort direction — must match the ORDER BY direction. */
195
+ direction: 'asc' | 'desc';
196
+ /** Tie-breaker: last seen entity ID. Required for non-unique sort fields. */
197
+ id?: string;
198
+ }
199
+ //#endregion
200
+ //#region src/admin-config.d.ts
201
+ /**
202
+ * Optional admin UI configuration for entities.
203
+ * Controls how entities appear in the admin sidebar, list pages, and forms.
204
+ */
205
+ interface EntityAdminConfig {
206
+ /** Sidebar section: 'content' | 'structure' | custom string. Default: 'content' */
207
+ group?: string;
208
+ /** Plural display name for sidebar + list pages. Default: title-cased pluralized entity name */
209
+ label?: string;
210
+ /** Singular label for "New X" button. Default: title-cased entity name */
211
+ labelSingular?: string;
212
+ /** Lucide icon name as string, e.g. 'file-text' */
213
+ icon?: string;
214
+ /** Description shown on list page */
215
+ description?: string;
216
+ /** Form layout. Default: 'single' */
217
+ layout?: 'single' | 'two-column';
218
+ /** Fields to hide in the form */
219
+ hiddenFields?: string[];
220
+ /** Columns to hide in the list */
221
+ hiddenColumns?: string[];
222
+ /** Per-field label/description/placeholder overrides */
223
+ fieldOverrides?: Record<string, {
224
+ label?: string;
225
+ description?: string;
226
+ placeholder?: string;
227
+ }>;
228
+ /** Default list sort field. Default: 'createdAt' */
229
+ defaultSort?: string;
230
+ /** Default list sort direction. Default: 'desc' */
231
+ defaultSortDirection?: 'asc' | 'desc';
232
+ /** List page size. Default: 20 */
233
+ pageSize?: number;
234
+ /** For block editor: fields to show in Puck root config */
235
+ rootFields?: string[];
236
+ /** Suppress "New" button in the list page */
237
+ disableCreate?: boolean;
238
+ /** Order within sidebar group. Default: 0 */
239
+ sortOrder?: number;
240
+ /**
241
+ * Hide the entity from BOTH dashboard and sidebar.
242
+ * Use for system entities that should not appear in any UI.
243
+ * Shorthand for `hideFromMenu: true` + `hideFromDashboard: true`.
244
+ */
245
+ hidden?: boolean;
246
+ /**
247
+ * Hide from sidebar menu only. Entity remains accessible via dashboard, direct URL,
248
+ * and the entity API. Use for storage models accessed through specialized UI
249
+ * (e.g., Ticket entities accessed via the inbox/board, not as a CRUD table).
250
+ */
251
+ hideFromMenu?: boolean;
252
+ /**
253
+ * Hide from dashboard widget grid only. Entity still appears in sidebar.
254
+ * Use for entities that don't have meaningful counts/stats to show.
255
+ */
256
+ hideFromDashboard?: boolean;
257
+ }
258
+ //#endregion
202
259
  //#region src/behaviors/types.d.ts
203
260
  /**
204
261
  * Context passed to behavior hooks. Resolved once per request by AdminClient
205
262
  * from its `contextResolver` and forwarded into every hook so behaviors never
206
263
  * have to reach into AsyncLocalStorage themselves — that pattern breaks under
207
264
  * bundlers (e.g. Turbopack) that duplicate module instances across boundaries.
265
+ *
266
+ * `loadCurrent` is a lazy, memoized loader for the *current* entity row,
267
+ * provided by AdminClient on update/delete codepaths. Hooks that need to
268
+ * validate against the existing state (e.g. workflowable's transition
269
+ * checker) call it; behaviors that don't, don't pay for the round trip.
270
+ * Returns `null` when no entity matches the id (rare — usually means the
271
+ * row was deleted concurrently). On create, `loadCurrent` is undefined.
272
+ *
273
+ * `viaInternal` is `true` when the hook is running on an `AdminClient.updateInternal`
274
+ * call (the trusted server-side path; public PATCH always sets it false).
275
+ * Hooks SHOULD treat this as informational only — the route layer has
276
+ * authorized the *capability*, but the hook is still responsible for
277
+ * enforcing structural invariants. Workflowable, for example, validates
278
+ * the `_workflowStatus` transition table on every update regardless of
279
+ * `viaInternal` so that even a route-layer bug cannot push the workflow
280
+ * row into an illegal state. Use `viaInternal` when a hook genuinely
281
+ * needs to differentiate (e.g. side effects that should only fire when
282
+ * a real user — not the seed loader — initiates the change).
208
283
  */
209
284
  interface BehaviorContext {
210
285
  user?: {
@@ -212,6 +287,8 @@ interface BehaviorContext {
212
287
  name?: string;
213
288
  email?: string;
214
289
  };
290
+ loadCurrent?: () => Promise<Record<string, unknown> | null>;
291
+ viaInternal?: boolean;
215
292
  }
216
293
  interface Behavior<F extends Record<string, FieldConfig> = {}> {
217
294
  name: string;
@@ -277,46 +354,6 @@ interface SecurityContext {
277
354
  */
278
355
  type ContextResolver = () => SecurityContext | undefined | Promise<SecurityContext | undefined>;
279
356
  //#endregion
280
- //#region src/types/infer.d.ts
281
- /**
282
- * Recursive JSON-compatible value, for `field.json()` (jsonb column).
283
- * Mirrors what Postgres jsonb can store: primitives, arrays, or objects.
284
- */
285
- type JsonValue = string | number | boolean | null | JsonValue[] | {
286
- [key: string]: JsonValue;
287
- };
288
- /**
289
- * Maps a single FieldConfig to its TypeScript output type.
290
- * Each branch is a shallow comparison — no recursion.
291
- */
292
- type FieldToTS<F extends FieldConfig> = F extends IdField ? string : F extends TextField ? string : F extends NumberField ? number : F extends BooleanField ? boolean : F extends DateField ? Date | string : F extends SelectField ? F['options'][number] : F extends ReferenceField ? F['cardinality'] extends 'many' ? string[] : string : F extends MediaField ? string : F extends RichTextField ? Record<string, unknown>[] : F extends SlugField ? string : F extends JsonField ? JsonValue : F extends BlocksField ? Array<{
293
- _block: string;
294
- _id: string;
295
- [key: string]: unknown;
296
- }> : never;
297
- /**
298
- * Maps a full field record to its TypeScript output type.
299
- * Required fields are non-nullable; optional fields are `T | null | undefined`.
300
- *
301
- * The `id` field is always `string` and always present.
302
- * The `id` key from Fields is excluded to avoid duplication since
303
- * we hardcode `{ id: string }` at the front.
304
- */
305
- type InferEntityDTO<Fields extends Record<string, FieldConfig>> = {
306
- id: string;
307
- } & { [K in keyof Fields as K extends 'id' ? never : Fields[K]['required'] extends true ? K : never]: FieldToTS<Fields[K]> } & { [K in keyof Fields as Fields[K]['required'] extends true ? never : K]?: FieldToTS<Fields[K]> | null };
308
- /** Fields that are auto-generated and should not appear in create input. */
309
- type AutoGeneratedFields = 'id' | 'createdAt' | 'updatedAt' | 'createdBy' | 'updatedBy' | '_version';
310
- /**
311
- * The input type for creating an entity.
312
- * - Omits auto-generated fields (id, timestamps, version)
313
- * - Required fields stay required; optional fields stay optional
314
- */
315
- type InferCreateInput<Fields extends Record<string, FieldConfig>> = Omit<{ [K in keyof Fields as K extends 'id' ? never : Fields[K]['required'] extends true ? K : never]: FieldToTS<Fields[K]> } & { [K in keyof Fields as Fields[K]['required'] extends true ? never : K]?: FieldToTS<Fields[K]> | null }, AutoGeneratedFields>;
316
- /** Fields that cannot be changed after creation. */
317
- type ImmutableFields = 'id' | 'createdAt' | 'createdBy';
318
- type InferUpdateInput<Fields extends Record<string, FieldConfig>> = { [K in keyof Fields as K extends ImmutableFields ? never : K]?: FieldToTS<Fields[K]> | null };
319
- //#endregion
320
357
  //#region src/types/logger.d.ts
321
358
  /**
322
359
  * Minimal logger interface compatible with Pino.
@@ -377,6 +414,12 @@ interface FindManyOptions {
377
414
  * The cursor `field` must be a real column on the entity table.
378
415
  */
379
416
  cursor?: CursorInput;
417
+ /**
418
+ * Include fields marked `internal: true` (and legacy `_`-prefixed
419
+ * infrastructure columns) in the returned DTOs. Use only on trusted
420
+ * server-side reads that need to inspect state-machine fields.
421
+ */
422
+ includeInternal?: boolean;
380
423
  }
381
424
  interface CountOptions {
382
425
  where?: SQL | undefined;
@@ -406,8 +449,14 @@ declare class AdminClient<AllFields extends Record<string, FieldConfig> = Record
406
449
  private entity;
407
450
  private db;
408
451
  private logger?;
452
+ /** Public-surface create schema — internal fields excluded. */
409
453
  private createSchema;
454
+ /** Public-surface update schema — internal fields excluded. */
410
455
  private updateSchema;
456
+ /** Trusted-surface update schema — internal fields included. Used by `updateInternal()`. */
457
+ private updateInternalSchema;
458
+ /** Names of fields marked `internal: true`. Cached for O(1) strip / pick. */
459
+ private internalFieldNames;
411
460
  private table;
412
461
  private countCache?;
413
462
  private contextResolver?;
@@ -415,6 +464,19 @@ declare class AdminClient<AllFields extends Record<string, FieldConfig> = Record
415
464
  /** Shared context for entity-data-ops functions. */
416
465
  private get ctx();
417
466
  constructor(config: AdminClientConfig<AllFields>);
467
+ /**
468
+ * Strip caller-provided values for internal fields from the input.
469
+ *
470
+ * Returns a NEW object — does not mutate the caller's input. Hooks run
471
+ * AFTER this strip, so they can still set internal fields legitimately;
472
+ * those values are then preserved through validation by `pickInternalFields`.
473
+ */
474
+ private stripCallerInternals;
475
+ /**
476
+ * Pick the internal-field values that hooks set on `data`.
477
+ * Used to re-attach them after schema validation strips unknown keys.
478
+ */
479
+ private pickInternalFields;
418
480
  /**
419
481
  * Create a new entity
420
482
  *
@@ -428,11 +490,16 @@ declare class AdminClient<AllFields extends Record<string, FieldConfig> = Record
428
490
  */
429
491
  create(data: InferCreateInput<AllFields>): Promise<InferEntityDTO<AllFields>>;
430
492
  /**
431
- * Find entity by ID
493
+ * Find entity by ID.
494
+ *
495
+ * Pass `includeInternal: true` from trusted server code (workflow
496
+ * transitions, behavior implementations) when you need to read fields
497
+ * marked `internal: true` — by default they are stripped from the DTO.
432
498
  */
433
499
  findById(id: string, options?: {
434
500
  locale?: string;
435
501
  defaultLocale?: string;
502
+ includeInternal?: boolean;
436
503
  }): Promise<InferEntityDTO<AllFields> | null>;
437
504
  /**
438
505
  * Find multiple entities
@@ -456,6 +523,27 @@ declare class AdminClient<AllFields extends Record<string, FieldConfig> = Record
456
523
  update(id: string, data: InferUpdateInput<AllFields>, options?: {
457
524
  locale?: string;
458
525
  }): Promise<InferEntityDTO<AllFields>>;
526
+ /**
527
+ * Update entity by ID, allowing writes to fields marked `internal: true`.
528
+ *
529
+ * Use this from trusted server code that has already authorized the
530
+ * transition out-of-band — typical example: workflow transitions invoked
531
+ * from an admin route that has already checked the `publish` permission.
532
+ * The HTTP `PATCH` surface uses the public {@link update} method, which
533
+ * silently strips internal fields so untrusted callers cannot poison
534
+ * state-machine values like `_workflowStatus`.
535
+ *
536
+ * The validation schema for this method INCLUDES internal fields — values
537
+ * are still type-checked and constrained (e.g. select-field options).
538
+ *
539
+ * **Security**: never call this from a code path that forwards request body
540
+ * fields directly. The caller must construct the internal-field values
541
+ * server-side from authorized state transitions.
542
+ */
543
+ updateInternal(id: string, data: Record<string, unknown>, options?: {
544
+ locale?: string;
545
+ }): Promise<InferEntityDTO<AllFields>>;
546
+ private updateImpl;
459
547
  /**
460
548
  * Delete entity by ID.
461
549
  *
@@ -545,6 +633,7 @@ declare class AdminClient<AllFields extends Record<string, FieldConfig> = Record
545
633
  */
546
634
  updateMany(where: SQL, data: Partial<InferUpdateInput<AllFields>>, options?: {
547
635
  expressions?: Record<string, SQL>;
636
+ allowInternal?: boolean;
548
637
  }): Promise<number>;
549
638
  /**
550
639
  * Run an aggregate query on this entity's table.
@@ -659,5 +748,5 @@ declare class AdminClient<AllFields extends Record<string, FieldConfig> = Record
659
748
  private invalidateCountCache;
660
749
  }
661
750
  //#endregion
662
- export { AdminClient, type AdminClientConfig, type CountCacheLike, type CountOptions, type FindManyOptions };
751
+ export { AdminClient, type AdminClientConfig, type CountCacheLike, type CountOptions, type FindManyOptions, type InferCreateInput, type InferEntityDTO, type InferUpdateInput };
663
752
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../../src/count-cache.ts","../../src/cursor.ts","../../src/admin-config.ts","../../src/fields/base.ts","../../src/behaviors/types.ts","../../src/define-entity.ts","../../src/shared/entity-data-ops.ts","../../src/types/infer.ts","../../src/types/logger.ts","../../src/admin/client.ts"],"mappings":";;;;;;;;;;AAiBA;;;;;;;;;;;UAAiB,cAAA;EACf,GAAA,CAAI,GAAA;EACJ,GAAA,CAAI,GAAA,UAAa,KAAA;EAYjB;;;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,CAAiB,SAAA,IAC/B,OAAA;IAAY,WAAA,GAAc,MAAA,SAAe,GAAA;EAAA,IACxC,OAAA;EN9tBD;;;;;;;;;;;;;;;;ACtFJ;;;;;;;;;;EK+3BQ,SAAA,iBAA0B,MAAA,oBAA0B,MAAA,kBAAA,CAAyB,OAAA;IL13B5D,iGK43BrB,MAAA,EAAQ,MAAA,SAAe,GAAA,GAAM,UAAA,QAAkB,EAAA,IL53BA;IK83B/C,OAAA,GAAU,GAAA,IL53BH;IK83BP,KAAA,GAAQ,GAAA,EL13BA;IK43BR,OAAA,GAAU,GAAA,GAAM,GAAA,IL33BX;IK63BL,KAAA;EAAA,IACE,OAAA,CAAQ,OAAA;EL73BgE;;;;;;;;;;;;;;;;;EKy6B5E,QAAA,CAAA,GAAY,kBAAA;EL/6BZ;;;;;;;;EK27BA,KAAA,CAAA,GAAS,kBAAA;ELt7BM;;;EAAA,QK67BP,oBAAA;EL57BgD;;;;EAAA,QKi9BhD,oBAAA;EL98BE;;;;EKg+BJ,eAAA,CACJ,QAAA,UACA,MAAA,UACA,YAAA,EAAc,MAAA,oBACb,OAAA;ELl+BY;;;;EKghCT,iBAAA,CAAkB,QAAA,UAAkB,MAAA,YAAkB,OAAA;EL/gCV;;;;;;;EK8iC5C,qBAAA,CACJ,QAAA,UACA,MAAA,UACA,IAAA,EAAM,MAAA,oBACL,OAAA;ELhjCc;;;;;EK8nCX,mBAAA,CACJ,QAAA,UACA,MAAA,UACA,IAAA,EAAM,MAAA,oBACL,OAAA;;;;AJrnCL;UImoCgB,UAAA;EJnoCO;;;;;;;;EAAA,QImsCP,QAAA;EJnrCH;;;EAAA,QIwuCH,oBAAA;AAAA"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../../src/count-cache.ts","../../src/fields/base.ts","../../src/types/infer.ts","../../src/cursor.ts","../../src/admin-config.ts","../../src/behaviors/types.ts","../../src/define-entity.ts","../../src/shared/entity-data-ops.ts","../../src/types/logger.ts","../../src/admin/client.ts"],"m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EAAA,QO2/B7D,oBAAA;EPngCiE;;;;EOqhCnE,eAAA,CACJ,QAAA,UACA,MAAA,UACA,YAAA,EAAc,MAAA,oBACb,OAAA;EPzhCsD;;;;EOukCnD,iBAAA,CAAkB,QAAA,UAAkB,MAAA,YAAkB,OAAA;EPnkCtD;;;;;;;EOkmCA,qBAAA,CACJ,QAAA,UACA,MAAA,UACA,IAAA,EAAM,MAAA,oBACL,OAAA;EPlmCqB;;;;;EOgrClB,mBAAA,CACJ,QAAA,UACA,MAAA,UACA,IAAA,EAAM,MAAA,oBACL,OAAA;EPhrCH;;;AACD;EADC,QO8rCc,UAAA;;;;APhrChB;;;;;UOgvCgB,QAAA;EP/uCQ;;;EAAA,QOoyCd,oBAAA;AAAA"}
@@ -1,2 +1,2 @@
1
- import{schemaRegistry as e}from"@murumets-ee/db";import{and as t,eq as n,getTableColumns as r,gt as i,inArray as a,isNull as o,lt as s,or as c,sql as l}from"drizzle-orm";import{index as u,pgTable as d,unique as f,uuid as p,varchar as m}from"drizzle-orm/pg-core";import{z as h}from"zod";function g(e,a){let o=r(e),l=o[a.field];if(!l)return null;let u=a.direction===`desc`?s:i,d=u(l,a.value);if(!a.id)return d;let f=o.id;if(!f)return d;let p=u(f,a.id);return c(d,t(n(l,a.value),p))??d}function _(e,t,n){if(!t)return null;let r={},i=n?.select||Object.keys(e.allFields);for(let a of i){if(!n?.includeInternal&&a.startsWith(`_`))continue;let i=e.allFields[a];i&&i.type!==`blocks`&&(r[a]=t[a])}return r}function v(e,t,n){return t.map(t=>_(e,t,n))}var y=class extends Error{entityName;entityId;usages;constructor(e,t,n){let r=n.length;super(`Cannot delete ${e} '${t}': referenced by ${r} other entit${r===1?`y`:`ies`}`),this.name=`ReferencedEntityError`,this.entityName=e,this.entityId=t,this.usages=n}};const b=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function x(e,t){let n=[],r=new Set;function i(e,t,i){if(!b.test(i))return;let a=`${e}|${t}|${i}`;r.has(a)||(r.add(a),n.push({sourceField:e,targetEntity:t,targetId:i}))}for(let[n,r]of Object.entries(e)){let e=t[n];if(e!=null)switch(r.type){case`media`:typeof e==`string`&&i(n,`media`,e);break;case`reference`:{let t=r;if(t.cardinality===`many`&&Array.isArray(e))for(let r of e)typeof r==`string`&&i(n,t.entity,r);else typeof e==`string`&&i(n,t.entity,e);break}case`blocks`:{let t=r;if(!Array.isArray(e))break;for(let r of e){let e=r,a=e._block;if(!a)continue;let o=t.blocks.find(e=>e.slug===a);if(o)for(let[t,r]of Object.entries(o.fields)){let a=e[t];if(a!=null&&(r.type===`media`&&typeof a==`string`&&i(n,`media`,a),r.type===`reference`)){let e=r;if(e.cardinality===`many`&&Array.isArray(a))for(let t of a)typeof t==`string`&&i(n,e.entity,t);else typeof a==`string`&&i(n,e.entity,a)}}}break}}}return n}const S=d(`entity_refs`,{sourceEntity:m(`source_entity`,{length:100}).notNull(),sourceId:p(`source_id`).notNull(),sourceField:m(`source_field`,{length:100}).notNull(),targetEntity:m(`target_entity`,{length:100}).notNull(),targetId:p(`target_id`).notNull()},e=>[f(`uq_entity_refs`).on(e.sourceEntity,e.sourceId,e.sourceField,e.targetEntity,e.targetId),u(`idx_entity_refs_target`).on(e.targetEntity,e.targetId),u(`idx_entity_refs_source`).on(e.sourceEntity,e.sourceId)]);function C(e){return e.entity.allFields}function w(e){return Object.entries(C(e)).filter(([e,t])=>t.type===`blocks`).map(([e,t])=>({name:e,config:t}))}async function T(r,i,o){if(!i.length)return i;let s=`${r.entity.name}_translations`,c=e.get(s);if(!c)return i;let l=i.map(e=>e.id),u=await r.db.select().from(c).where(t(a(c.entityId,l),n(c.locale,o))),d=Object.entries(C(r)).filter(([e,t])=>t.translatable).map(([e])=>e),f=new Map;for(let e of u){let t={};for(let n of d){let r=e[n];r!=null&&(t[n]=r)}f.set(e.entityId,t)}return i.map(e=>{let t=f.get(e.id);return t?{...e,...t}:e})}async function E(r,i,s,l){let u=w(r);if(u.length===0||i.length===0)return new Map;let d=e.get(`${r.entity.name}_layout`);if(!d)return new Map;let f=u.filter(({config:e})=>!(`localized`in e&&e.localized)),p=u.filter(({config:e})=>`localized`in e&&e.localized),m=new Map;if(f.length>0){let c=await r.db.select().from(d).where(t(a(d.entityId,i),o(d.locale))).orderBy(d.sortOrder),u;if(s&&c.length>0){let i=e.get(`${r.entity.name}_layout_translations`);if(i){let e=c.map(e=>e.id),o=await r.db.select().from(i).where(t(a(i.layoutId,e),n(i.locale,s)));u=new Map;for(let e of o)u.set(e.layoutId,e.fields??{})}}let p;if(l?.strictTranslations){p=new Map;for(let{config:e}of f)if(e.type===`blocks`)for(let t of e.blocks)p.set(t.slug,t.fields)}for(let e of c){let t=e.entityId,n=e.fieldName,r=m.get(t);r||(r={},m.set(t,r)),r[n]||(r[n]=[]);let i=e.data??{},a=u?.get(e.id),o={_block:e.blockType,_id:e.id,...i,...a??{}};if(s&&p){let t=e.blockType,n=p.get(t);if(n)for(let[e,t]of Object.entries(n))t.translatable&&!a?.[e]&&(o[e]=``)}r[n].push(o)}}if(p.length>0){let e=!s||s===l?.defaultLocale,u;u=s?e?c(n(d.locale,s),o(d.locale))??n(d.locale,s):n(d.locale,s):o(d.locale);let f=await r.db.select().from(d).where(t(a(d.entityId,i),u)).orderBy(d.sortOrder),p=new Map;for(let e of f){let t=`${e.entityId}::${e.fieldName}`,n=p.get(t);n||(n={localeRows:[],nullRows:[]},p.set(t,n)),e.locale?n.localeRows.push(e):n.nullRows.push(e)}for(let[,{localeRows:e,nullRows:t}]of p){let n=e.length>0?e:t;for(let e of n){let t=e.entityId,n=e.fieldName,r=m.get(t);r||(r={},m.set(t,r)),r[n]||(r[n]=[]);let i=e.data??{};r[n].push({_block:e.blockType,_id:e.id,...i})}}}return m}function D(e,t,n){let r=w(e);if(r.length!==0)for(let e of t){let t=e.id,i=n.get(t)??{};for(let{name:t}of r)e[t]=i[t]??[]}}function O(e,t,n,r){let i=n?`${n}:${e}`:e;return r&&(i=`${i}@${r}`),t?`${i}:${String(t)}`:i}var k=class extends Error{constructor(e){super(e),this.name=`ForbiddenError`}};async function A(e){if(!e)throw new k(`No context resolver configured on AdminClient/QueryClient. Use createAdminClient() from @murumets-ee/core/clients, or pass a contextResolver in config.`);let t=await e();if(!t)throw new k(`Context resolver returned no context. Ensure auth is configured in your request handler, or use runAsCli() for CLI scripts.`);return t}async function j(e,t,n){let r=await A(t),i=r.user.groups[0];if(!i)throw new k(`User '${r.user.id}' has no role assigned.`);if(!r.checker(i,e.name,n))throw new k(`Forbidden: role '${i}' cannot ${n} '${e.name}'`)}function M(e,t){return n(e.table._scopeId,t)}async function N(e,t,n,r){let i=await A(t),a=i.user.groups[0];if(!a)throw new k(`User '${i.user.id}' has no role assigned.`);if(!i.checker(a,e.name,n))throw new k(`Forbidden: role '${a}' cannot ${n} '${e.name}'`);let o;if(e.scope&&e.scope!==`global`&&(o=i.scope?.id,!o&&r?.strictScope))throw new k(`Entity '${e.name}' requires scope '${e.scope}' but no scope is set in context.`);let s={user:{id:i.user.id,name:i.user.name,email:i.user.email}};return{security:i,scopeId:o,behaviorCtx:s}}function P(e){let t;switch(e.type){case`id`:t=h.string().uuid();break;case`text`:{let n=h.string();e.maxLength&&(n=n.max(e.maxLength)),e.minLength&&(n=n.min(e.minLength)),e.pattern&&(n=n.regex(e.pattern)),t=n;break}case`number`:{let n=h.number();e.integer&&(n=n.int()),e.min!==void 0&&(n=n.min(e.min)),e.max!==void 0&&(n=n.max(e.max)),t=n;break}case`boolean`:t=h.boolean();break;case`date`:t=h.date().or(h.string().datetime());break;case`select`:t=h.enum(e.options);break;case`reference`:t=e.cardinality===`many`?h.array(h.string().uuid()):h.string().uuid();break;case`media`:t=h.string().uuid();break;case`richtext`:t=h.union([h.string(),h.array(h.record(h.unknown()))]);break;case`slug`:t=h.string().regex(/^[a-z0-9-]+$/);break;case`json`:t=h.union([h.record(h.unknown()),h.array(h.unknown()),h.string(),h.number(),h.boolean()]);break;case`blocks`:t=h.array(h.object({_block:h.string()}).passthrough());break;default:t=h.unknown()}return e.required||(t=t.nullable().optional()),e.default!==void 0&&(t=t.default(e.default)),t}function F(e){let t={},n=new Set([`id`,`createdAt`,`updatedAt`,`createdBy`,`_version`]);for(let[r,i]of Object.entries(e.allFields))n.has(r)||(t[r]=P(i));return h.object(t)}function I(e){let t={},n=new Set([`id`,`createdAt`,`createdBy`,`updatedAt`,`updatedBy`,`_version`]);for(let[r,i]of Object.entries(e.allFields))n.has(r)||(t[r]=P(i).optional());return h.object(t)}var L=class r{entity;db;logger;createSchema;updateSchema;table;countCache;contextResolver;entityResolver;get ctx(){return{entity:this.entity,db:this.db,table:this.table,resolveContext:this.contextResolver}}constructor(t){if(typeof window<`u`)throw Error(`AdminClient cannot be used in browser code. Use QueryClient for frontend data access.`);this.entity=t.entity,this.db=t.db,this.logger=t.logger,this.countCache=t.countCache,this.contextResolver=t.contextResolver,this.entityResolver=t.entityResolver,this.createSchema=F(t.entity),this.updateSchema=I(t.entity);let n=e.get(t.entity.name);if(!n)throw Error(`Schema for entity '${t.entity.name}' not found in registry. Ensure schemas are generated and registered before creating AdminClient.`);this.table=n}async create(e){this.logger?.info({entity:this.entity.name},`Creating entity`);let{scopeId:t,behaviorCtx:n}=await N(this.entity,this.contextResolver,`create`,{strictScope:!0}),r=e;for(let e of this.entity.behaviors??[])if(e.hooks?.beforeCreate){let t=await e.hooks.beforeCreate(r,n);t!==void 0&&(r=t)}let i={};for(let e of[`createdAt`,`updatedAt`,`createdBy`,`updatedBy`])r[e]!==void 0&&(i[e]=r[e]);r={...this.createSchema.parse(r),...i},t&&(r._scopeId=t);let a=this.prepareDataForInsert(r),[o]=await this.db.insert(this.table).values(a).returning();await this.saveBlocks(o.id,r),await this.syncRefs(o.id,r,`create`);for(let e of this.entity.behaviors??[])e.hooks?.afterCreate&&await e.hooks.afterCreate(o,n);this.invalidateCountCache();let s=_(this.entity,o);if(s&&w(this.ctx).length>0){let e=await E(this.ctx,[s.id],void 0,{strictTranslations:!0});D(this.ctx,[s],e)}return s}async findById(e,r){this.logger?.info({entity:this.entity.name,id:e},`Finding entity by ID`);let{scopeId:i}=await N(this.entity,this.contextResolver,`view`),a=[n(this.table.id,e)];i&&a.push(M(this.ctx,i));let[o]=await this.db.select().from(this.table).where(t(...a));if(!o)return null;let s=_(this.entity,o);if(!s)return null;if(w(this.ctx).length>0){let e=await E(this.ctx,[s.id],r?.locale,{defaultLocale:r?.defaultLocale,strictTranslations:!0});D(this.ctx,[s],e)}if(r?.locale){let[e]=await T(this.ctx,[s],r.locale);return e}return s}async findMany(e){this.logger?.info({entity:this.entity.name,options:e},`Finding entities`);let{scopeId:n}=await N(this.entity,this.contextResolver,`view`),r=this.db.select().from(this.table).$dynamic(),i=[];if(n&&i.push(M(this.ctx,n)),e?.where&&i.push(e.where),e?.cursor){let t=C(this.ctx);if(!(e.cursor.field in t)&&e.cursor.field!==`id`)throw Error(`Invalid cursor field: '${e.cursor.field}' is not a field on '${this.entity.name}'`);let n=g(this.table,e.cursor);n&&i.push(n)}if(i.length>0&&(r=r.where(t(...i))),e?.limit&&(r=r.limit(e.limit)),e?.offset&&!e?.cursor&&(r=r.offset(e.offset)),e?.orderBy){let t=Array.isArray(e.orderBy)?e.orderBy:[e.orderBy];r=r.orderBy(...t)}let a=await r,o=v(this.entity,a).filter(e=>e!==null);if(w(this.ctx).length>0&&o.length>0){let t=o.map(e=>e.id),n=await E(this.ctx,t,e?.locale,{defaultLocale:e?.defaultLocale,strictTranslations:!0});D(this.ctx,o,n)}return e?.locale?await T(this.ctx,o,e.locale):o}async count(e){this.logger?.info({entity:this.entity.name,options:e},`Counting entities`);let{scopeId:n}=await N(this.entity,this.contextResolver,`view`),r=O(this.entity.name,e?.where,void 0,n),i=async()=>{let r=this.db.select({count:l`count(*)`}).from(this.table).$dynamic(),i=[];n&&i.push(M(this.ctx,n)),e?.where&&i.push(e.where),i.length>0&&(r=r.where(t(...i)));let[a]=await r;return Number(a.count)};return this.countCache?this.countCache.getOrCompute(r,i):i()}async update(e,r,i){this.logger?.info({entity:this.entity.name,id:e},`Updating entity`);let{scopeId:a,behaviorCtx:o}=await N(this.entity,this.contextResolver,`update`,{strictScope:!0}),s=r;for(let t of this.entity.behaviors??[])if(t.hooks?.beforeUpdate){let n=await t.hooks.beforeUpdate(e,s,o);n!==void 0&&(s=n)}let c={};for(let e of[`updatedAt`,`updatedBy`])s[e]!==void 0&&(c[e]=s[e]);s={...this.updateSchema.parse(s),...c};let l=this.prepareDataForUpdate(s),u=n(this.table.id,e),d=a?t(u,M(this.ctx,a))??u:u,f;if(Object.keys(l).length>0){let[e]=await this.db.update(this.table).set(l).where(d).returning();f=e}else{let[e]=await this.db.select().from(this.table).where(d);f=e}await this.saveBlocks(e,s,i),await this.syncRefs(e,s,`update`);for(let e of this.entity.behaviors??[])e.hooks?.afterUpdate&&await e.hooks.afterUpdate(f,o);let p=_(this.entity,f);if(p&&w(this.ctx).length>0){let t=await E(this.ctx,[e],void 0,{strictTranslations:!0});D(this.ctx,[p],t)}return p}async delete(e){this.logger?.info({entity:this.entity.name,id:e},`Deleting entity`);let{scopeId:r,behaviorCtx:i}=await N(this.entity,this.contextResolver,`delete`,{strictScope:!0});if(r){let[i]=await this.db.select({id:this.table.id}).from(this.table).where(t(n(this.table.id,e),M(this.ctx,r)));if(!i)return}await this.db.transaction(async r=>{await this.handleIncomingRefsForDelete(r,[e]);for(let t of this.entity.behaviors??[])t.hooks?.beforeDelete&&await t.hooks.beforeDelete(e,i);await r.delete(this.table).where(n(this.table.id,e)),await r.delete(S).where(t(n(S.sourceEntity,this.entity.name),n(S.sourceId,e)))});for(let t of this.entity.behaviors??[])t.hooks?.afterDelete&&await t.hooks.afterDelete(e,i);this.invalidateCountCache()}async deleteMany(e){this.logger?.info({entity:this.entity.name},`Bulk deleting entities`);let{scopeId:r,behaviorCtx:i}=await N(this.entity,this.contextResolver,`delete`,{strictScope:!0}),o=[e];r&&o.push(M(this.ctx,r));let s=(await this.db.select({id:this.table.id}).from(this.table).where(t(...o))).map(e=>e.id);if(s.length===0)return 0;await this.db.transaction(async e=>{await this.handleIncomingRefsForDelete(e,s);for(let e of s)for(let t of this.entity.behaviors??[])t.hooks?.beforeDelete&&await t.hooks.beforeDelete(e,i);await e.delete(this.table).where(a(this.table.id,s)),await e.delete(S).where(t(n(S.sourceEntity,this.entity.name),a(S.sourceId,s)))});for(let e of s)for(let t of this.entity.behaviors??[])t.hooks?.afterDelete&&await t.hooks.afterDelete(e,i);return this.invalidateCountCache(),s.length}static MAX_CASCADE_DEPTH=10;async handleIncomingRefsForDelete(i,o,s=0){if(s>=r.MAX_CASCADE_DEPTH)throw Error(`Cascade delete exceeded maximum depth of ${r.MAX_CASCADE_DEPTH} while deleting '${this.entity.name}'. This likely indicates a circular reference chain.`);let c=await i.select({sourceEntity:S.sourceEntity,sourceId:S.sourceId,sourceField:S.sourceField}).from(S).where(t(n(S.targetEntity,this.entity.name),a(S.targetId,o)));if(c.length===0)return;let l=new Map;for(let e of c){let t=`${e.sourceEntity}:${e.sourceField}`,n=l.get(t);n||(n={sourceEntity:e.sourceEntity,sourceField:e.sourceField,sourceIds:[]},l.set(t,n)),n.sourceIds.push(e.sourceId)}let u=this.entityResolver?.();for(let[,c]of l){let l=u?.get(c.sourceEntity),d=l?.fields[c.sourceField]?.onDelete??`restrict`;if(d===`restrict`)throw new y(this.entity.name,o[0],c.sourceIds.map(e=>({sourceEntity:c.sourceEntity,sourceId:e,sourceField:c.sourceField})));if(d===`cascade`){let n=e.get(c.sourceEntity);if(n&&l){let e=new r({entity:l,db:i,logger:this.logger,contextResolver:this.contextResolver}),o=await N(l,this.contextResolver,`delete`),u=a(n.id,c.sourceIds);if(o.scopeId){let e={entity:l,db:i,table:n,resolveContext:this.contextResolver},r=t(u,M(e,o.scopeId));r&&(u=r)}await e.deleteManyInTx(i,u,s+1,o.behaviorCtx)}}else if(d===`set-null`){let r=e.get(c.sourceEntity);if(r&&l&&r[c.sourceField]){let e=(await N(l,this.contextResolver,`update`)).scopeId,o=a(r.id,c.sourceIds);if(e){let n={entity:l,db:i,table:r,resolveContext:this.contextResolver},a=t(o,M(n,e));a&&(o=a)}await i.update(r).set({[c.sourceField]:null}).where(o),await i.delete(S).where(t(n(S.sourceEntity,c.sourceEntity),a(S.sourceId,c.sourceIds),n(S.sourceField,c.sourceField)))}}}}async deleteManyInTx(e,r,i,o){let s=(await e.select({id:this.table.id}).from(this.table).where(r)).map(e=>e.id);if(s.length===0)return 0;await this.handleIncomingRefsForDelete(e,s,i);for(let e of s)for(let t of this.entity.behaviors??[])t.hooks?.beforeDelete&&await t.hooks.beforeDelete(e,o);return await e.delete(this.table).where(a(this.table.id,s)),await e.delete(S).where(t(n(S.sourceEntity,this.entity.name),a(S.sourceId,s))),this.invalidateCountCache(),s.length}async updateMany(e,n,r){this.logger?.info({entity:this.entity.name},`Bulk updating entities`);let{scopeId:i}=await N(this.entity,this.contextResolver,`update`,{strictScope:!0}),a=this.updateSchema.parse(n),o=this.prepareDataForUpdate(a);if(r?.expressions){let e=C(this.ctx);for(let[t,n]of Object.entries(r.expressions)){if(t in o)throw Error(`updateMany: field '${t}' appears in both \`data\` and \`expressions\`. Choose one — expressions silently overriding validated values is unsafe.`);if(!(t in e)&&t!==`_scopeId`)throw Error(`updateMany.expressions: unknown column '${t}' on entity '${this.entity.name}'`);o[t]=n}}let s=[e];i&&s.push(M(this.ctx,i));let c=await this.db.update(this.table).set(o).where(t(...s)).returning({id:this.table.id});return this.invalidateCountCache(),c.length}async aggregate(e){this.logger?.info({entity:this.entity.name},`Aggregate query`);let{scopeId:n}=await N(this.entity,this.contextResolver,`view`),r=this.db.select(e.select).from(this.table).$dynamic(),i=[];if(n&&i.push(M(this.ctx,n)),e.where&&i.push(e.where),i.length>0&&(r=r.where(t(...i))),e.groupBy&&e.groupBy.length>0&&(r=r.groupBy(...e.groupBy)),e.orderBy){let t=Array.isArray(e.orderBy)?e.orderBy:[e.orderBy];r=r.orderBy(...t)}return e.limit&&(r=r.limit(e.limit)),await r}getTable(){return this.table}getDb(){return this.db}prepareDataForInsert(e){let t={};for(let[n,r]of Object.entries(e)){let e=C(this.ctx)[n];e&&n!==`id`&&e.type!==`blocks`&&(t[n]=r)}return e._scopeId!==void 0&&(t._scopeId=e._scopeId),t}prepareDataForUpdate(e){let t={};for(let[n,r]of Object.entries(e)){let e=C(this.ctx)[n];e&&n!==`id`&&e.type!==`blocks`&&(t[n]=r)}return t}async saveTranslation(t,n,r){await j(this.entity,this.contextResolver,`update`),this.logger?.info({entity:this.entity.name,entityId:t,locale:n},`Saving translation`);let i=`${this.entity.name}_translations`,a=e.get(i);if(!a)throw Error(`Translation table ${i} not found in schema registry`);let o=Object.entries(C(this.ctx)).filter(([e,t])=>t.translatable).map(([e])=>e);if(o.length===0)throw Error(`Entity ${this.entity.name} has no translatable fields`);let s={entityId:t,locale:n},c={};for(let e of o)r[e]!==void 0&&(s[e]=r[e],c[e]=r[e]);await this.db.insert(a).values(s).onConflictDoUpdate({target:[a.entityId,a.locale],set:c}),this.logger?.info({entity:this.entity.name,entityId:t,locale:n},`Translation saved`)}async deleteTranslation(r,i){await j(this.entity,this.contextResolver,`update`),this.logger?.info({entity:this.entity.name,entityId:r,locale:i},`Deleting translation`);let a=`${this.entity.name}_translations`,o=e.get(a);if(!o)throw Error(`Translation table ${a} not found in schema registry`);i?(await this.db.delete(o).where(t(n(o.entityId,r),n(o.locale,i))),this.logger?.info({entity:this.entity.name,entityId:r,locale:i},`Translation deleted`)):(await this.db.delete(o).where(n(o.entityId,r)),this.logger?.info({entity:this.entity.name,entityId:r},`All translations deleted`))}async saveBlockTranslations(r,i,a){await j(this.entity,this.contextResolver,`update`),this.logger?.info({entity:this.entity.name,entityId:r,locale:i},`Saving block translations`);let s=w(this.ctx);if(s.length===0)return;let c=e.get(`${this.entity.name}_layout_translations`);if(!c)return;let l=e.get(`${this.entity.name}_layout`);if(l){for(let{name:e,config:u}of s){let s=a[e];if(!Array.isArray(s)||u.type!==`blocks`)continue;let d=u.blocks;if(!d)continue;let f=await this.db.select({id:l.id,blockType:l.blockType}).from(l).where(t(n(l.entityId,r),n(l.fieldName,e),o(l.locale))).orderBy(l.sortOrder);for(let e=0;e<s.length;e++){let t=s[e],n=t._block;if(!n)continue;let r=f[e];if(!r||r.blockType!==n)continue;let a=d.find(e=>e.slug===n);if(!a)continue;let o={};for(let[e,n]of Object.entries(a.fields))n.translatable&&t[e]!==void 0&&(o[e]=t[e]);Object.keys(o).length!==0&&await this.db.insert(c).values({layoutId:r.id,locale:i,fields:o}).onConflictDoUpdate({target:[c.layoutId,c.locale],set:{fields:o}})}}this.logger?.info({entity:this.entity.name,entityId:r,locale:i},`Block translations saved`)}}async saveLocalizedBlocks(e,t,n){await j(this.entity,this.contextResolver,`update`),this.logger?.info({entity:this.entity.name,entityId:e,locale:t},`Saving localized blocks`),await this.saveBlocks(e,n,{locale:t})}async saveBlocks(r,i,a){let s=w(this.ctx);if(s.length===0)return;let c=e.get(`${this.entity.name}_layout`);if(c)for(let{name:e,config:l}of s){let s=i[e];if(!Array.isArray(s))continue;let u=`localized`in l&&l.localized===!0,d=u?a?.locale??null:null,f=[n(c.entityId,r),n(c.fieldName,e)];d?f.push(n(c.locale,d)):u||f.push(o(c.locale)),await this.db.delete(c).where(t(...f));for(let t=0;t<s.length;t++){let n=s[t],i=n._block;if(!i)continue;let{_block:a,_id:o,...l}=n;await this.db.insert(c).values({entityId:r,fieldName:e,blockType:i,sortOrder:t,data:l,locale:d})}}}async syncRefs(r,i,o){if(!e.has(`entity_refs`))return;let s=C(this.ctx);if(o===`update`){let e=Object.keys(i).filter(e=>{let t=s[e];return t&&(t.type===`media`||t.type===`reference`||t.type===`blocks`)});e.length>0&&await this.db.delete(S).where(t(n(S.sourceEntity,this.entity.name),n(S.sourceId,r),a(S.sourceField,e)))}let c=x(s,i);c.length>0&&await this.db.insert(S).values(c.map(e=>({sourceEntity:this.entity.name,sourceId:r,sourceField:e.sourceField,targetEntity:e.targetEntity,targetId:e.targetId}))).onConflictDoNothing()}invalidateCountCache(){this.countCache?.invalidate(this.entity.name)}};export{L as AdminClient};
1
+ import{schemaRegistry as e}from"@murumets-ee/db";import{and as t,eq as n,getTableColumns as r,gt as i,inArray as a,isNull as o,lt as s,or as c,sql as l}from"drizzle-orm";import{index as u,pgTable as d,unique as f,uuid as p,varchar as m}from"drizzle-orm/pg-core";import{z as h}from"zod";function g(e,a){let o=r(e),l=o[a.field];if(!l)return null;let u=a.direction===`desc`?s:i,d=u(l,a.value);if(!a.id)return d;let f=o.id;if(!f)return d;let p=u(f,a.id);return c(d,t(n(l,a.value),p))??d}function _(e,t,n){if(!t)return null;let r={},i=n?.select||Object.keys(e.allFields);for(let a of i){let i=e.allFields[a];if(!n?.includeInternal){let e=a.startsWith(`_`),t=i?.internal===!0;if(e||t)continue}i&&i.type!==`blocks`&&(r[a]=t[a])}return r}function v(e,t,n){return t.map(t=>_(e,t,n))}var y=class extends Error{entityName;entityId;usages;constructor(e,t,n){let r=n.length;super(`Cannot delete ${e} '${t}': referenced by ${r} other entit${r===1?`y`:`ies`}`),this.name=`ReferencedEntityError`,this.entityName=e,this.entityId=t,this.usages=n}};const b=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function x(e,t){let n=[],r=new Set;function i(e,t,i){if(!b.test(i))return;let a=`${e}|${t}|${i}`;r.has(a)||(r.add(a),n.push({sourceField:e,targetEntity:t,targetId:i}))}for(let[n,r]of Object.entries(e)){let e=t[n];if(e!=null)switch(r.type){case`media`:typeof e==`string`&&i(n,`media`,e);break;case`reference`:{let t=r;if(t.cardinality===`many`&&Array.isArray(e))for(let r of e)typeof r==`string`&&i(n,t.entity,r);else typeof e==`string`&&i(n,t.entity,e);break}case`blocks`:{let t=r;if(!Array.isArray(e))break;for(let r of e){let e=r,a=e._block;if(!a)continue;let o=t.blocks.find(e=>e.slug===a);if(o)for(let[t,r]of Object.entries(o.fields)){let a=e[t];if(a!=null&&(r.type===`media`&&typeof a==`string`&&i(n,`media`,a),r.type===`reference`)){let e=r;if(e.cardinality===`many`&&Array.isArray(a))for(let t of a)typeof t==`string`&&i(n,e.entity,t);else typeof a==`string`&&i(n,e.entity,a)}}}break}}}return n}const S=d(`entity_refs`,{sourceEntity:m(`source_entity`,{length:100}).notNull(),sourceId:p(`source_id`).notNull(),sourceField:m(`source_field`,{length:100}).notNull(),targetEntity:m(`target_entity`,{length:100}).notNull(),targetId:p(`target_id`).notNull()},e=>[f(`uq_entity_refs`).on(e.sourceEntity,e.sourceId,e.sourceField,e.targetEntity,e.targetId),u(`idx_entity_refs_target`).on(e.targetEntity,e.targetId),u(`idx_entity_refs_source`).on(e.sourceEntity,e.sourceId)]);function C(e){return e.entity.allFields}function w(e){return Object.entries(C(e)).filter(([e,t])=>t.type===`blocks`).map(([e,t])=>({name:e,config:t}))}async function T(r,i,o){if(!i.length)return i;let s=`${r.entity.name}_translations`,c=e.get(s);if(!c)return i;let l=i.map(e=>e.id),u=await r.db.select().from(c).where(t(a(c.entityId,l),n(c.locale,o))),d=Object.entries(C(r)).filter(([e,t])=>t.translatable).map(([e])=>e),f=new Map;for(let e of u){let t={};for(let n of d){let r=e[n];r!=null&&(t[n]=r)}f.set(e.entityId,t)}return i.map(e=>{let t=f.get(e.id);return t?{...e,...t}:e})}async function E(r,i,s,l){let u=w(r);if(u.length===0||i.length===0)return new Map;let d=e.get(`${r.entity.name}_layout`);if(!d)return new Map;let f=u.filter(({config:e})=>!(`localized`in e&&e.localized)),p=u.filter(({config:e})=>`localized`in e&&e.localized),m=new Map;if(f.length>0){let c=await r.db.select().from(d).where(t(a(d.entityId,i),o(d.locale))).orderBy(d.sortOrder),u;if(s&&c.length>0){let i=e.get(`${r.entity.name}_layout_translations`);if(i){let e=c.map(e=>e.id),o=await r.db.select().from(i).where(t(a(i.layoutId,e),n(i.locale,s)));u=new Map;for(let e of o)u.set(e.layoutId,e.fields??{})}}let p;if(l?.strictTranslations){p=new Map;for(let{config:e}of f)if(e.type===`blocks`)for(let t of e.blocks)p.set(t.slug,t.fields)}for(let e of c){let t=e.entityId,n=e.fieldName,r=m.get(t);r||(r={},m.set(t,r)),r[n]||(r[n]=[]);let i=e.data??{},a=u?.get(e.id),o={_block:e.blockType,_id:e.id,...i,...a??{}};if(s&&p){let t=e.blockType,n=p.get(t);if(n)for(let[e,t]of Object.entries(n))t.translatable&&!a?.[e]&&(o[e]=``)}r[n].push(o)}}if(p.length>0){let e=!s||s===l?.defaultLocale,u;u=s?e?c(n(d.locale,s),o(d.locale))??n(d.locale,s):n(d.locale,s):o(d.locale);let f=await r.db.select().from(d).where(t(a(d.entityId,i),u)).orderBy(d.sortOrder),p=new Map;for(let e of f){let t=`${e.entityId}::${e.fieldName}`,n=p.get(t);n||(n={localeRows:[],nullRows:[]},p.set(t,n)),e.locale?n.localeRows.push(e):n.nullRows.push(e)}for(let[,{localeRows:e,nullRows:t}]of p){let n=e.length>0?e:t;for(let e of n){let t=e.entityId,n=e.fieldName,r=m.get(t);r||(r={},m.set(t,r)),r[n]||(r[n]=[]);let i=e.data??{};r[n].push({_block:e.blockType,_id:e.id,...i})}}}return m}function D(e,t,n){let r=w(e);if(r.length!==0)for(let e of t){let t=e.id,i=n.get(t)??{};for(let{name:t}of r)e[t]=i[t]??[]}}function O(e,t,n,r){let i=n?`${n}:${e}`:e;return r&&(i=`${i}@${r}`),t?`${i}:${String(t)}`:i}var k=class extends Error{constructor(e){super(e),this.name=`ForbiddenError`}};async function A(e){if(!e)throw new k(`No context resolver configured on AdminClient/QueryClient. Use createAdminClient() from @murumets-ee/core/clients, or pass a contextResolver in config.`);let t=await e();if(!t)throw new k(`Context resolver returned no context. Ensure auth is configured in your request handler, or use runAsCli() for CLI scripts.`);return t}async function j(e,t,n){let r=await A(t),i=r.user.groups[0];if(!i)throw new k(`User '${r.user.id}' has no role assigned.`);if(!r.checker(i,e.name,n))throw new k(`Forbidden: role '${i}' cannot ${n} '${e.name}'`)}function M(e,t){return n(e.table._scopeId,t)}async function N(e,t,n,r){let i=await A(t),a=i.user.groups[0];if(!a)throw new k(`User '${i.user.id}' has no role assigned.`);if(!i.checker(a,e.name,n))throw new k(`Forbidden: role '${a}' cannot ${n} '${e.name}'`);let o;if(e.scope&&e.scope!==`global`&&(o=i.scope?.id,!o&&r?.strictScope))throw new k(`Entity '${e.name}' requires scope '${e.scope}' but no scope is set in context.`);let s={user:{id:i.user.id,name:i.user.name,email:i.user.email}};return{security:i,scopeId:o,behaviorCtx:s}}function P(e){let t;switch(e.type){case`id`:t=h.string().uuid();break;case`text`:{let n=h.string();e.maxLength&&(n=n.max(e.maxLength)),e.minLength&&(n=n.min(e.minLength)),e.pattern&&(n=n.regex(e.pattern)),t=n;break}case`number`:{let n=h.number();e.integer&&(n=n.int()),e.min!==void 0&&(n=n.min(e.min)),e.max!==void 0&&(n=n.max(e.max)),t=n;break}case`boolean`:t=h.boolean();break;case`date`:t=h.date().or(h.string().datetime());break;case`select`:t=h.enum(e.options);break;case`reference`:t=e.cardinality===`many`?h.array(h.string().uuid()):h.string().uuid();break;case`media`:t=h.string().uuid();break;case`richtext`:t=h.union([h.string(),h.array(h.record(h.unknown()))]);break;case`slug`:t=h.string().regex(/^[a-z0-9-]+$/);break;case`json`:t=h.union([h.record(h.unknown()),h.array(h.unknown()),h.string(),h.number(),h.boolean()]);break;case`blocks`:t=h.array(h.object({_block:h.string()}).passthrough());break;default:t=h.unknown()}return e.required||(t=t.nullable().optional()),e.default!==void 0&&(t=t.default(e.default)),t}function F(e){return e===`id`||e===`createdAt`||e===`createdBy`||e===`updatedAt`||e===`updatedBy`||e===`_version`}function I(e){return e===`id`||e===`createdAt`||e===`updatedAt`||e===`createdBy`||e===`_version`}function L(e,t={}){let n={};for(let[r,i]of Object.entries(e.allFields))I(r)||i.internal&&!t.includeInternal||(n[r]=P(i));return h.object(n)}function R(e,t={}){let n={};for(let[r,i]of Object.entries(e.allFields))F(r)||i.internal&&!t.includeInternal||(n[r]=P(i).optional());return h.object(n)}var z=class r{entity;db;logger;createSchema;updateSchema;updateInternalSchema;internalFieldNames;table;countCache;contextResolver;entityResolver;get ctx(){return{entity:this.entity,db:this.db,table:this.table,resolveContext:this.contextResolver}}constructor(t){if(typeof window<`u`)throw Error(`AdminClient cannot be used in browser code. Use QueryClient for frontend data access.`);this.entity=t.entity,this.db=t.db,this.logger=t.logger,this.countCache=t.countCache,this.contextResolver=t.contextResolver,this.entityResolver=t.entityResolver,this.createSchema=L(t.entity),this.updateSchema=R(t.entity),this.updateInternalSchema=R(t.entity,{includeInternal:!0}),this.internalFieldNames=new Set(Object.entries(t.entity.allFields).filter(([,e])=>e.internal).map(([e])=>e));let n=e.get(t.entity.name);if(!n)throw Error(`Schema for entity '${t.entity.name}' not found in registry. Ensure schemas are generated and registered before creating AdminClient.`);this.table=n}stripCallerInternals(e){if(this.internalFieldNames.size===0)return e;let t={};for(let[n,r]of Object.entries(e))this.internalFieldNames.has(n)||(t[n]=r);return t}pickInternalFields(e){if(this.internalFieldNames.size===0)return{};let t={};for(let n of this.internalFieldNames)e[n]!==void 0&&(t[n]=e[n]);return t}async create(e){this.logger?.info({entity:this.entity.name},`Creating entity`);let{scopeId:t,behaviorCtx:n}=await N(this.entity,this.contextResolver,`create`,{strictScope:!0}),r=this.stripCallerInternals(e);for(let e of this.entity.behaviors??[])if(e.hooks?.beforeCreate){let t=await e.hooks.beforeCreate(r,n);t!==void 0&&(r=t)}let i={};for(let e of[`createdAt`,`updatedAt`,`createdBy`,`updatedBy`])r[e]!==void 0&&(i[e]=r[e]);let a=this.pickInternalFields(r);r={...this.createSchema.parse(r),...i,...a},t&&(r._scopeId=t);let o=this.prepareDataForInsert(r),[s]=await this.db.insert(this.table).values(o).returning();await this.saveBlocks(s.id,r),await this.syncRefs(s.id,r,`create`);for(let e of this.entity.behaviors??[])e.hooks?.afterCreate&&await e.hooks.afterCreate(s,n);this.invalidateCountCache();let c=_(this.entity,s);if(c&&w(this.ctx).length>0){let e=await E(this.ctx,[c.id],void 0,{strictTranslations:!0});D(this.ctx,[c],e)}return c}async findById(e,r){this.logger?.info({entity:this.entity.name,id:e},`Finding entity by ID`);let{scopeId:i}=await N(this.entity,this.contextResolver,`view`),a=[n(this.table.id,e)];i&&a.push(M(this.ctx,i));let[o]=await this.db.select().from(this.table).where(t(...a));if(!o)return null;let s=r?.includeInternal?{includeInternal:!0}:void 0,c=_(this.entity,o,s);if(!c)return null;if(w(this.ctx).length>0){let e=await E(this.ctx,[c.id],r?.locale,{defaultLocale:r?.defaultLocale,strictTranslations:!0});D(this.ctx,[c],e)}if(r?.locale){let[e]=await T(this.ctx,[c],r.locale);return e}return c}async findMany(e){this.logger?.info({entity:this.entity.name,options:e},`Finding entities`);let{scopeId:n}=await N(this.entity,this.contextResolver,`view`),r=this.db.select().from(this.table).$dynamic(),i=[];if(n&&i.push(M(this.ctx,n)),e?.where&&i.push(e.where),e?.cursor){let t=C(this.ctx);if(!(e.cursor.field in t)&&e.cursor.field!==`id`)throw Error(`Invalid cursor field: '${e.cursor.field}' is not a field on '${this.entity.name}'`);let n=g(this.table,e.cursor);n&&i.push(n)}if(i.length>0&&(r=r.where(t(...i))),e?.limit&&(r=r.limit(e.limit)),e?.offset&&!e?.cursor&&(r=r.offset(e.offset)),e?.orderBy){let t=Array.isArray(e.orderBy)?e.orderBy:[e.orderBy];r=r.orderBy(...t)}let a=await r,o=e?.includeInternal?{includeInternal:!0}:void 0,s=v(this.entity,a,o).filter(e=>e!==null);if(w(this.ctx).length>0&&s.length>0){let t=s.map(e=>e.id),n=await E(this.ctx,t,e?.locale,{defaultLocale:e?.defaultLocale,strictTranslations:!0});D(this.ctx,s,n)}return e?.locale?await T(this.ctx,s,e.locale):s}async count(e){this.logger?.info({entity:this.entity.name,options:e},`Counting entities`);let{scopeId:n}=await N(this.entity,this.contextResolver,`view`),r=O(this.entity.name,e?.where,void 0,n),i=async()=>{let r=this.db.select({count:l`count(*)`}).from(this.table).$dynamic(),i=[];n&&i.push(M(this.ctx,n)),e?.where&&i.push(e.where),i.length>0&&(r=r.where(t(...i)));let[a]=await r;return Number(a.count)};return this.countCache?this.countCache.getOrCompute(r,i):i()}async update(e,t,n){return this.updateImpl(e,t,n,{allowInternal:!1})}async updateInternal(e,t,n){return this.updateImpl(e,t,n,{allowInternal:!0})}async updateImpl(e,r,i,a){this.logger?.info({entity:this.entity.name,id:e,internal:a.allowInternal||void 0},`Updating entity`);let{scopeId:o,behaviorCtx:s}=await N(this.entity,this.contextResolver,`update`,{strictScope:!0}),c=a.allowInternal?r:this.stripCallerInternals(r),l,u=async()=>(l===void 0&&(l=await this.findById(e)),l),d={...s,loadCurrent:u,viaInternal:a.allowInternal};for(let t of this.entity.behaviors??[])if(t.hooks?.beforeUpdate){let n=await t.hooks.beforeUpdate(e,c,d);n!==void 0&&(c=n)}let f={};for(let e of[`updatedAt`,`updatedBy`])c[e]!==void 0&&(f[e]=c[e]);let p=a.allowInternal?this.updateInternalSchema:this.updateSchema,m=a.allowInternal?{}:this.pickInternalFields(c);c={...p.parse(c),...f,...m};let h=this.prepareDataForUpdate(c),g=n(this.table.id,e),v=o?t(g,M(this.ctx,o))??g:g,y;if(Object.keys(h).length>0){let[e]=await this.db.update(this.table).set(h).where(v).returning();y=e}else{let[e]=await this.db.select().from(this.table).where(v);y=e}await this.saveBlocks(e,c,i),await this.syncRefs(e,c,`update`);for(let e of this.entity.behaviors??[])e.hooks?.afterUpdate&&await e.hooks.afterUpdate(y,s);let b=a.allowInternal?{includeInternal:!0}:void 0,x=_(this.entity,y,b);if(x&&w(this.ctx).length>0){let t=await E(this.ctx,[e],void 0,{strictTranslations:!0});D(this.ctx,[x],t)}return x}async delete(e){this.logger?.info({entity:this.entity.name,id:e},`Deleting entity`);let{scopeId:r,behaviorCtx:i}=await N(this.entity,this.contextResolver,`delete`,{strictScope:!0});if(r){let[i]=await this.db.select({id:this.table.id}).from(this.table).where(t(n(this.table.id,e),M(this.ctx,r)));if(!i)return}await this.db.transaction(async r=>{await this.handleIncomingRefsForDelete(r,[e]);for(let t of this.entity.behaviors??[])t.hooks?.beforeDelete&&await t.hooks.beforeDelete(e,i);await r.delete(this.table).where(n(this.table.id,e)),await r.delete(S).where(t(n(S.sourceEntity,this.entity.name),n(S.sourceId,e)))});for(let t of this.entity.behaviors??[])t.hooks?.afterDelete&&await t.hooks.afterDelete(e,i);this.invalidateCountCache()}async deleteMany(e){this.logger?.info({entity:this.entity.name},`Bulk deleting entities`);let{scopeId:r,behaviorCtx:i}=await N(this.entity,this.contextResolver,`delete`,{strictScope:!0}),o=[e];r&&o.push(M(this.ctx,r));let s=(await this.db.select({id:this.table.id}).from(this.table).where(t(...o))).map(e=>e.id);if(s.length===0)return 0;await this.db.transaction(async e=>{await this.handleIncomingRefsForDelete(e,s);for(let e of s)for(let t of this.entity.behaviors??[])t.hooks?.beforeDelete&&await t.hooks.beforeDelete(e,i);await e.delete(this.table).where(a(this.table.id,s)),await e.delete(S).where(t(n(S.sourceEntity,this.entity.name),a(S.sourceId,s)))});for(let e of s)for(let t of this.entity.behaviors??[])t.hooks?.afterDelete&&await t.hooks.afterDelete(e,i);return this.invalidateCountCache(),s.length}static MAX_CASCADE_DEPTH=10;async handleIncomingRefsForDelete(i,o,s=0){if(s>=r.MAX_CASCADE_DEPTH)throw Error(`Cascade delete exceeded maximum depth of ${r.MAX_CASCADE_DEPTH} while deleting '${this.entity.name}'. This likely indicates a circular reference chain.`);let c=await i.select({sourceEntity:S.sourceEntity,sourceId:S.sourceId,sourceField:S.sourceField}).from(S).where(t(n(S.targetEntity,this.entity.name),a(S.targetId,o)));if(c.length===0)return;let l=new Map;for(let e of c){let t=`${e.sourceEntity}:${e.sourceField}`,n=l.get(t);n||(n={sourceEntity:e.sourceEntity,sourceField:e.sourceField,sourceIds:[]},l.set(t,n)),n.sourceIds.push(e.sourceId)}let u=this.entityResolver?.();for(let[,c]of l){let l=u?.get(c.sourceEntity),d=l?.fields[c.sourceField]?.onDelete??`restrict`;if(d===`restrict`)throw new y(this.entity.name,o[0],c.sourceIds.map(e=>({sourceEntity:c.sourceEntity,sourceId:e,sourceField:c.sourceField})));if(d===`cascade`){let n=e.get(c.sourceEntity);if(n&&l){let e=new r({entity:l,db:i,logger:this.logger,contextResolver:this.contextResolver}),o=await N(l,this.contextResolver,`delete`),u=a(n.id,c.sourceIds);if(o.scopeId){let e={entity:l,db:i,table:n,resolveContext:this.contextResolver},r=t(u,M(e,o.scopeId));r&&(u=r)}await e.deleteManyInTx(i,u,s+1,o.behaviorCtx)}}else if(d===`set-null`){let r=e.get(c.sourceEntity);if(r&&l&&r[c.sourceField]){let e=(await N(l,this.contextResolver,`update`)).scopeId,o=a(r.id,c.sourceIds);if(e){let n={entity:l,db:i,table:r,resolveContext:this.contextResolver},a=t(o,M(n,e));a&&(o=a)}await i.update(r).set({[c.sourceField]:null}).where(o),await i.delete(S).where(t(n(S.sourceEntity,c.sourceEntity),a(S.sourceId,c.sourceIds),n(S.sourceField,c.sourceField)))}}}}async deleteManyInTx(e,r,i,o){let s=(await e.select({id:this.table.id}).from(this.table).where(r)).map(e=>e.id);if(s.length===0)return 0;await this.handleIncomingRefsForDelete(e,s,i);for(let e of s)for(let t of this.entity.behaviors??[])t.hooks?.beforeDelete&&await t.hooks.beforeDelete(e,o);return await e.delete(this.table).where(a(this.table.id,s)),await e.delete(S).where(t(n(S.sourceEntity,this.entity.name),a(S.sourceId,s))),this.invalidateCountCache(),s.length}async updateMany(e,n,r){this.logger?.info({entity:this.entity.name},`Bulk updating entities`);let{scopeId:i}=await N(this.entity,this.contextResolver,`update`,{strictScope:!0}),a=r?.allowInternal?n:this.stripCallerInternals(n),o=(r?.allowInternal?this.updateInternalSchema:this.updateSchema).parse(a),s=this.prepareDataForUpdate(o);if(r?.expressions){let e=C(this.ctx);for(let[t,n]of Object.entries(r.expressions)){if(t in s)throw Error(`updateMany: field '${t}' appears in both \`data\` and \`expressions\`. Choose one — expressions silently overriding validated values is unsafe.`);if(!(t in e)&&t!==`_scopeId`)throw Error(`updateMany.expressions: unknown column '${t}' on entity '${this.entity.name}'`);if(!r.allowInternal&&this.internalFieldNames.has(t))throw Error(`updateMany.expressions: '${t}' is internal — pass \`allowInternal: true\` from a trusted server context that has authorized the transition out-of-band.`);s[t]=n}}let c=[e];i&&c.push(M(this.ctx,i));let l=await this.db.update(this.table).set(s).where(t(...c)).returning({id:this.table.id});return this.invalidateCountCache(),l.length}async aggregate(e){this.logger?.info({entity:this.entity.name},`Aggregate query`);let{scopeId:n}=await N(this.entity,this.contextResolver,`view`),r=this.db.select(e.select).from(this.table).$dynamic(),i=[];if(n&&i.push(M(this.ctx,n)),e.where&&i.push(e.where),i.length>0&&(r=r.where(t(...i))),e.groupBy&&e.groupBy.length>0&&(r=r.groupBy(...e.groupBy)),e.orderBy){let t=Array.isArray(e.orderBy)?e.orderBy:[e.orderBy];r=r.orderBy(...t)}return e.limit&&(r=r.limit(e.limit)),await r}getTable(){return this.table}getDb(){return this.db}prepareDataForInsert(e){let t={};for(let[n,r]of Object.entries(e)){let e=C(this.ctx)[n];e&&n!==`id`&&e.type!==`blocks`&&(t[n]=r)}return e._scopeId!==void 0&&(t._scopeId=e._scopeId),t}prepareDataForUpdate(e){let t={};for(let[n,r]of Object.entries(e)){let e=C(this.ctx)[n];e&&n!==`id`&&e.type!==`blocks`&&(t[n]=r)}return t}async saveTranslation(t,n,r){await j(this.entity,this.contextResolver,`update`),this.logger?.info({entity:this.entity.name,entityId:t,locale:n},`Saving translation`);let i=`${this.entity.name}_translations`,a=e.get(i);if(!a)throw Error(`Translation table ${i} not found in schema registry`);let o=Object.entries(C(this.ctx)).filter(([e,t])=>t.translatable).map(([e])=>e);if(o.length===0)throw Error(`Entity ${this.entity.name} has no translatable fields`);let s={entityId:t,locale:n},c={};for(let e of o)r[e]!==void 0&&(s[e]=r[e],c[e]=r[e]);await this.db.insert(a).values(s).onConflictDoUpdate({target:[a.entityId,a.locale],set:c}),this.logger?.info({entity:this.entity.name,entityId:t,locale:n},`Translation saved`)}async deleteTranslation(r,i){await j(this.entity,this.contextResolver,`update`),this.logger?.info({entity:this.entity.name,entityId:r,locale:i},`Deleting translation`);let a=`${this.entity.name}_translations`,o=e.get(a);if(!o)throw Error(`Translation table ${a} not found in schema registry`);i?(await this.db.delete(o).where(t(n(o.entityId,r),n(o.locale,i))),this.logger?.info({entity:this.entity.name,entityId:r,locale:i},`Translation deleted`)):(await this.db.delete(o).where(n(o.entityId,r)),this.logger?.info({entity:this.entity.name,entityId:r},`All translations deleted`))}async saveBlockTranslations(r,i,a){await j(this.entity,this.contextResolver,`update`),this.logger?.info({entity:this.entity.name,entityId:r,locale:i},`Saving block translations`);let s=w(this.ctx);if(s.length===0)return;let c=e.get(`${this.entity.name}_layout_translations`);if(!c)return;let l=e.get(`${this.entity.name}_layout`);if(l){for(let{name:e,config:u}of s){let s=a[e];if(!Array.isArray(s)||u.type!==`blocks`)continue;let d=u.blocks;if(!d)continue;let f=await this.db.select({id:l.id,blockType:l.blockType}).from(l).where(t(n(l.entityId,r),n(l.fieldName,e),o(l.locale))).orderBy(l.sortOrder);for(let e=0;e<s.length;e++){let t=s[e],n=t._block;if(!n)continue;let r=f[e];if(!r||r.blockType!==n)continue;let a=d.find(e=>e.slug===n);if(!a)continue;let o={};for(let[e,n]of Object.entries(a.fields))n.translatable&&t[e]!==void 0&&(o[e]=t[e]);Object.keys(o).length!==0&&await this.db.insert(c).values({layoutId:r.id,locale:i,fields:o}).onConflictDoUpdate({target:[c.layoutId,c.locale],set:{fields:o}})}}this.logger?.info({entity:this.entity.name,entityId:r,locale:i},`Block translations saved`)}}async saveLocalizedBlocks(e,t,n){await j(this.entity,this.contextResolver,`update`),this.logger?.info({entity:this.entity.name,entityId:e,locale:t},`Saving localized blocks`),await this.saveBlocks(e,n,{locale:t})}async saveBlocks(r,i,a){let s=w(this.ctx);if(s.length===0)return;let c=e.get(`${this.entity.name}_layout`);if(c)for(let{name:e,config:l}of s){let s=i[e];if(!Array.isArray(s))continue;let u=`localized`in l&&l.localized===!0,d=u?a?.locale??null:null,f=[n(c.entityId,r),n(c.fieldName,e)];d?f.push(n(c.locale,d)):u||f.push(o(c.locale)),await this.db.delete(c).where(t(...f));for(let t=0;t<s.length;t++){let n=s[t],i=n._block;if(!i)continue;let{_block:a,_id:o,...l}=n;await this.db.insert(c).values({entityId:r,fieldName:e,blockType:i,sortOrder:t,data:l,locale:d})}}}async syncRefs(r,i,o){if(!e.has(`entity_refs`))return;let s=C(this.ctx);if(o===`update`){let e=Object.keys(i).filter(e=>{let t=s[e];return t&&(t.type===`media`||t.type===`reference`||t.type===`blocks`)});e.length>0&&await this.db.delete(S).where(t(n(S.sourceEntity,this.entity.name),n(S.sourceId,r),a(S.sourceField,e)))}let c=x(s,i);c.length>0&&await this.db.insert(S).values(c.map(e=>({sourceEntity:this.entity.name,sourceId:r,sourceField:e.sourceField,targetEntity:e.targetEntity,targetId:e.targetId}))).onConflictDoNothing()}invalidateCountCache(){this.countCache?.invalidate(this.entity.name)}};export{z as AdminClient};
2
2
  //# sourceMappingURL=index.mjs.map