@objectstack/spec 0.1.1 → 0.1.2

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.
Files changed (136) hide show
  1. package/README.md +73 -1
  2. package/dist/api/contract.zod.d.ts +1733 -0
  3. package/dist/api/contract.zod.d.ts.map +1 -0
  4. package/dist/api/contract.zod.js +138 -0
  5. package/dist/data/dataset.zod.d.ts +2 -2
  6. package/dist/data/field.zod.d.ts +1648 -10
  7. package/dist/data/field.zod.d.ts.map +1 -1
  8. package/dist/data/field.zod.js +149 -8
  9. package/dist/data/mapping.zod.d.ts +215 -2
  10. package/dist/data/mapping.zod.d.ts.map +1 -1
  11. package/dist/data/object.zod.d.ts +505 -25
  12. package/dist/data/object.zod.d.ts.map +1 -1
  13. package/dist/data/object.zod.js +32 -5
  14. package/dist/data/query.zod.d.ts +349 -0
  15. package/dist/data/query.zod.d.ts.map +1 -1
  16. package/dist/data/query.zod.js +77 -1
  17. package/dist/data/trigger.zod.d.ts +354 -0
  18. package/dist/data/trigger.zod.d.ts.map +1 -0
  19. package/dist/data/trigger.zod.js +195 -0
  20. package/dist/data/validation.zod.d.ts +83 -43
  21. package/dist/data/validation.zod.d.ts.map +1 -1
  22. package/dist/data/validation.zod.js +51 -5
  23. package/dist/index.d.ts +8 -0
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +9 -0
  26. package/dist/system/api.zod.d.ts +130 -4
  27. package/dist/system/api.zod.d.ts.map +1 -1
  28. package/dist/system/api.zod.js +4 -1
  29. package/dist/system/datasource.zod.d.ts +89 -6
  30. package/dist/system/datasource.zod.d.ts.map +1 -1
  31. package/dist/system/datasource.zod.js +33 -5
  32. package/dist/system/discovery.zod.d.ts +174 -0
  33. package/dist/system/discovery.zod.d.ts.map +1 -0
  34. package/dist/system/discovery.zod.js +53 -0
  35. package/dist/system/driver.zod.d.ts +1525 -0
  36. package/dist/system/driver.zod.d.ts.map +1 -0
  37. package/dist/system/driver.zod.js +290 -0
  38. package/dist/system/license.zod.d.ts +2 -2
  39. package/dist/system/manifest.zod.d.ts +323 -52
  40. package/dist/system/manifest.zod.d.ts.map +1 -1
  41. package/dist/system/manifest.zod.js +91 -17
  42. package/dist/system/plugin.zod.d.ts +3516 -0
  43. package/dist/system/plugin.zod.d.ts.map +1 -0
  44. package/dist/system/plugin.zod.js +226 -0
  45. package/dist/system/territory.zod.d.ts +1 -1
  46. package/dist/system/webhook.zod.d.ts +3 -3
  47. package/dist/ui/action.zod.d.ts +19 -12
  48. package/dist/ui/action.zod.d.ts.map +1 -1
  49. package/dist/ui/action.zod.js +7 -1
  50. package/dist/ui/app.zod.d.ts +109 -3
  51. package/dist/ui/app.zod.d.ts.map +1 -1
  52. package/dist/ui/app.zod.js +13 -2
  53. package/dist/ui/dashboard.zod.d.ts +9 -3
  54. package/dist/ui/dashboard.zod.d.ts.map +1 -1
  55. package/dist/ui/dashboard.zod.js +7 -1
  56. package/dist/ui/page.zod.d.ts +6 -6
  57. package/dist/ui/report.zod.d.ts +9 -0
  58. package/dist/ui/report.zod.d.ts.map +1 -1
  59. package/dist/ui/report.zod.js +7 -1
  60. package/dist/ui/theme.zod.d.ts +1221 -0
  61. package/dist/ui/theme.zod.d.ts.map +1 -0
  62. package/dist/ui/theme.zod.js +202 -0
  63. package/dist/ui/widget.zod.d.ts +350 -0
  64. package/dist/ui/widget.zod.d.ts.map +1 -0
  65. package/dist/ui/widget.zod.js +66 -0
  66. package/json-schema/Action.json +8 -2
  67. package/json-schema/ActionParam.json +8 -2
  68. package/json-schema/Address.json +40 -0
  69. package/json-schema/AggregationFunction.json +19 -0
  70. package/json-schema/AggregationNode.json +42 -0
  71. package/json-schema/Animation.json +56 -0
  72. package/json-schema/ApiCapabilities.json +28 -0
  73. package/json-schema/ApiError.json +27 -0
  74. package/json-schema/ApiRoutes.json +41 -0
  75. package/json-schema/App.json +13 -2
  76. package/json-schema/AsyncValidation.json +70 -0
  77. package/json-schema/BaseResponse.json +63 -0
  78. package/json-schema/BorderRadius.json +44 -0
  79. package/json-schema/Breakpoints.json +36 -0
  80. package/json-schema/BulkRequest.json +29 -0
  81. package/json-schema/BulkResponse.json +108 -0
  82. package/json-schema/ColorPalette.json +83 -0
  83. package/json-schema/ConditionalValidation.json +793 -0
  84. package/json-schema/CreateRequest.json +20 -0
  85. package/json-schema/CrossFieldValidation.json +56 -0
  86. package/json-schema/CustomValidator.json +57 -0
  87. package/json-schema/Datasource.json +0 -18
  88. package/json-schema/DeleteResponse.json +68 -0
  89. package/json-schema/Discovery.json +114 -0
  90. package/json-schema/DriverCapabilities.json +39 -0
  91. package/json-schema/DriverDefinition.json +66 -0
  92. package/json-schema/DriverInterface.json +58 -0
  93. package/json-schema/DriverOptions.json +23 -0
  94. package/json-schema/DriverType.json +1 -18
  95. package/json-schema/ExportRequest.json +786 -0
  96. package/json-schema/Field.json +75 -4
  97. package/json-schema/FieldType.json +8 -2
  98. package/json-schema/FieldWidgetProps.json +327 -0
  99. package/json-schema/I18nContext.json +12 -0
  100. package/json-schema/JoinNode.json +455 -0
  101. package/json-schema/JoinType.json +15 -0
  102. package/json-schema/ListRecordResponse.json +103 -0
  103. package/json-schema/LocationCoordinates.json +36 -0
  104. package/json-schema/Logger.json +25 -0
  105. package/json-schema/Manifest.json +243 -18
  106. package/json-schema/Mapping.json +328 -0
  107. package/json-schema/ModificationResult.json +46 -0
  108. package/json-schema/Object.json +103 -6
  109. package/json-schema/ObjectCapabilities.json +26 -0
  110. package/json-schema/ObjectQLClient.json +12 -0
  111. package/json-schema/Plugin.json +20 -0
  112. package/json-schema/PluginContext.json +91 -0
  113. package/json-schema/PluginLifecycle.json +11 -0
  114. package/json-schema/Query.json +328 -0
  115. package/json-schema/RecordData.json +11 -0
  116. package/json-schema/Router.json +12 -0
  117. package/json-schema/Scheduler.json +12 -0
  118. package/json-schema/ScopedStorage.json +12 -0
  119. package/json-schema/Shadow.json +44 -0
  120. package/json-schema/SingleRecordResponse.json +69 -0
  121. package/json-schema/Spacing.json +64 -0
  122. package/json-schema/SystemAPI.json +12 -0
  123. package/json-schema/Theme.json +543 -0
  124. package/json-schema/ThemeMode.json +14 -0
  125. package/json-schema/Trigger.json +73 -0
  126. package/json-schema/TriggerAction.json +14 -0
  127. package/json-schema/TriggerContext.json +61 -0
  128. package/json-schema/TriggerTiming.json +13 -0
  129. package/json-schema/Typography.json +142 -0
  130. package/json-schema/UpdateRequest.json +20 -0
  131. package/json-schema/ValidationRule.json +583 -0
  132. package/json-schema/WindowFunction.json +24 -0
  133. package/json-schema/WindowFunctionNode.json +104 -0
  134. package/json-schema/WindowSpec.json +65 -0
  135. package/json-schema/ZIndex.json +44 -0
  136. package/package.json +8 -3
@@ -0,0 +1,354 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * Trigger Action Enum
4
+ *
5
+ * Defines the database operation that triggered the execution.
6
+ */
7
+ export declare const TriggerAction: z.ZodEnum<["insert", "update", "delete"]>;
8
+ /**
9
+ * Trigger Timing Enum
10
+ *
11
+ * Defines when the trigger executes relative to the database operation.
12
+ */
13
+ export declare const TriggerTiming: z.ZodEnum<["before", "after"]>;
14
+ /**
15
+ * Trigger Context Schema
16
+ *
17
+ * This defines the runtime context available to trigger code during execution.
18
+ * Standardizes how trigger code is written and enables AI code generation.
19
+ *
20
+ * Triggers are business logic hooks that execute before or after database operations.
21
+ * They can validate data, set defaults, update related records, or prevent operations.
22
+ *
23
+ * @example Before Insert Trigger
24
+ * export default {
25
+ * timing: 'before',
26
+ * action: 'insert',
27
+ * execute: async (context: TriggerContext) => {
28
+ * // Set default values
29
+ * if (!context.doc.status) {
30
+ * context.doc.status = 'active';
31
+ * }
32
+ *
33
+ * // Validation
34
+ * if (!context.doc.email) {
35
+ * context.addError('Email is required');
36
+ * }
37
+ * }
38
+ * };
39
+ *
40
+ * @example After Update Trigger
41
+ * export default {
42
+ * timing: 'after',
43
+ * action: 'update',
44
+ * execute: async (context: TriggerContext) => {
45
+ * // Update related records
46
+ * if (context.getOldValue('status') !== context.doc.status) {
47
+ * await context.ql.object('activity_log').create({
48
+ * record_id: context.doc.id,
49
+ * message: `Status changed from ${context.getOldValue('status')} to ${context.doc.status}`,
50
+ * user_id: context.userId,
51
+ * });
52
+ * }
53
+ * }
54
+ * };
55
+ */
56
+ export declare const TriggerContextSchema: z.ZodObject<{
57
+ /**
58
+ * The database operation that triggered execution.
59
+ * One of: 'insert', 'update', 'delete'
60
+ */
61
+ action: z.ZodEnum<["insert", "update", "delete"]>;
62
+ /**
63
+ * When the trigger executes relative to the operation.
64
+ * - 'before': Execute before database operation (can modify doc, prevent operation)
65
+ * - 'after': Execute after database operation (can trigger side effects)
66
+ */
67
+ timing: z.ZodEnum<["before", "after"]>;
68
+ /**
69
+ * The current document/record being operated on.
70
+ *
71
+ * For 'before' triggers: Can be modified to change what gets saved.
72
+ * For 'after' triggers: Contains the final saved state (read-only).
73
+ * For 'delete' triggers: Contains the record being deleted.
74
+ */
75
+ doc: z.ZodRecord<z.ZodString, z.ZodAny>;
76
+ /**
77
+ * The document state before the current operation.
78
+ *
79
+ * Only available for 'update' and 'delete' operations.
80
+ * Null for 'insert' operations.
81
+ *
82
+ * Use this to detect what changed in an update trigger.
83
+ */
84
+ previousDoc: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
85
+ /**
86
+ * ID of the user performing the operation.
87
+ */
88
+ userId: z.ZodString;
89
+ /**
90
+ * Complete user record of the user performing the operation.
91
+ * Contains fields like name, email, roles, etc.
92
+ */
93
+ user: z.ZodRecord<z.ZodString, z.ZodAny>;
94
+ /**
95
+ * ObjectQL data access API.
96
+ * Use this to query or modify other records.
97
+ *
98
+ * @example
99
+ * await context.ql.object('account').findOne(context.doc.account_id);
100
+ * await context.ql.object('activity').create({ ... });
101
+ */
102
+ ql: z.ZodAny;
103
+ /**
104
+ * Logging interface.
105
+ * Use this for debugging and auditing.
106
+ *
107
+ * @example
108
+ * context.logger.info('Trigger executed', { recordId: context.doc.id });
109
+ * context.logger.error('Validation failed', { error });
110
+ */
111
+ logger: z.ZodAny;
112
+ /**
113
+ * Add a validation error.
114
+ * For 'before' triggers only - prevents the operation from completing.
115
+ *
116
+ * @param message - Error message to display
117
+ * @param field - Optional field name the error relates to
118
+ *
119
+ * @example
120
+ * if (context.doc.amount < 0) {
121
+ * context.addError('Amount must be positive', 'amount');
122
+ * }
123
+ */
124
+ addError: z.ZodFunction<z.ZodTuple<[z.ZodString, z.ZodOptional<z.ZodString>], z.ZodUnknown>, z.ZodVoid>;
125
+ /**
126
+ * Get the old value of a field.
127
+ * Helper function for 'update' triggers to easily compare old vs new values.
128
+ *
129
+ * @param fieldName - Name of the field
130
+ * @returns Previous value of the field, or undefined if not available
131
+ *
132
+ * @example
133
+ * if (context.getOldValue('status') !== context.doc.status) {
134
+ * // Status changed
135
+ * }
136
+ */
137
+ getOldValue: z.ZodFunction<z.ZodTuple<[z.ZodString], z.ZodUnknown>, z.ZodAny>;
138
+ }, "strip", z.ZodTypeAny, {
139
+ user: Record<string, any>;
140
+ action: "update" | "delete" | "insert";
141
+ timing: "before" | "after";
142
+ doc: Record<string, any>;
143
+ userId: string;
144
+ addError: (args_0: string, args_1: string | undefined, ...args: unknown[]) => void;
145
+ getOldValue: (args_0: string, ...args: unknown[]) => any;
146
+ previousDoc?: Record<string, any> | undefined;
147
+ ql?: any;
148
+ logger?: any;
149
+ }, {
150
+ user: Record<string, any>;
151
+ action: "update" | "delete" | "insert";
152
+ timing: "before" | "after";
153
+ doc: Record<string, any>;
154
+ userId: string;
155
+ addError: (args_0: string, args_1: string | undefined, ...args: unknown[]) => void;
156
+ getOldValue: (args_0: string, ...args: unknown[]) => any;
157
+ previousDoc?: Record<string, any> | undefined;
158
+ ql?: any;
159
+ logger?: any;
160
+ }>;
161
+ /**
162
+ * Trigger Definition Schema
163
+ *
164
+ * Complete definition of a trigger including metadata and execution function.
165
+ */
166
+ export declare const TriggerSchema: z.ZodObject<{
167
+ /**
168
+ * Unique trigger name.
169
+ */
170
+ name: z.ZodString;
171
+ /**
172
+ * Object this trigger is attached to.
173
+ */
174
+ object: z.ZodString;
175
+ /**
176
+ * Trigger timing.
177
+ */
178
+ timing: z.ZodEnum<["before", "after"]>;
179
+ /**
180
+ * Trigger action(s).
181
+ * Can be a single action or array of actions.
182
+ */
183
+ action: z.ZodUnion<[z.ZodEnum<["insert", "update", "delete"]>, z.ZodArray<z.ZodEnum<["insert", "update", "delete"]>, "many">]>;
184
+ /**
185
+ * Trigger execution function.
186
+ * Receives TriggerContext and performs the business logic.
187
+ */
188
+ execute: z.ZodFunction<z.ZodTuple<[z.ZodObject<{
189
+ /**
190
+ * The database operation that triggered execution.
191
+ * One of: 'insert', 'update', 'delete'
192
+ */
193
+ action: z.ZodEnum<["insert", "update", "delete"]>;
194
+ /**
195
+ * When the trigger executes relative to the operation.
196
+ * - 'before': Execute before database operation (can modify doc, prevent operation)
197
+ * - 'after': Execute after database operation (can trigger side effects)
198
+ */
199
+ timing: z.ZodEnum<["before", "after"]>;
200
+ /**
201
+ * The current document/record being operated on.
202
+ *
203
+ * For 'before' triggers: Can be modified to change what gets saved.
204
+ * For 'after' triggers: Contains the final saved state (read-only).
205
+ * For 'delete' triggers: Contains the record being deleted.
206
+ */
207
+ doc: z.ZodRecord<z.ZodString, z.ZodAny>;
208
+ /**
209
+ * The document state before the current operation.
210
+ *
211
+ * Only available for 'update' and 'delete' operations.
212
+ * Null for 'insert' operations.
213
+ *
214
+ * Use this to detect what changed in an update trigger.
215
+ */
216
+ previousDoc: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
217
+ /**
218
+ * ID of the user performing the operation.
219
+ */
220
+ userId: z.ZodString;
221
+ /**
222
+ * Complete user record of the user performing the operation.
223
+ * Contains fields like name, email, roles, etc.
224
+ */
225
+ user: z.ZodRecord<z.ZodString, z.ZodAny>;
226
+ /**
227
+ * ObjectQL data access API.
228
+ * Use this to query or modify other records.
229
+ *
230
+ * @example
231
+ * await context.ql.object('account').findOne(context.doc.account_id);
232
+ * await context.ql.object('activity').create({ ... });
233
+ */
234
+ ql: z.ZodAny;
235
+ /**
236
+ * Logging interface.
237
+ * Use this for debugging and auditing.
238
+ *
239
+ * @example
240
+ * context.logger.info('Trigger executed', { recordId: context.doc.id });
241
+ * context.logger.error('Validation failed', { error });
242
+ */
243
+ logger: z.ZodAny;
244
+ /**
245
+ * Add a validation error.
246
+ * For 'before' triggers only - prevents the operation from completing.
247
+ *
248
+ * @param message - Error message to display
249
+ * @param field - Optional field name the error relates to
250
+ *
251
+ * @example
252
+ * if (context.doc.amount < 0) {
253
+ * context.addError('Amount must be positive', 'amount');
254
+ * }
255
+ */
256
+ addError: z.ZodFunction<z.ZodTuple<[z.ZodString, z.ZodOptional<z.ZodString>], z.ZodUnknown>, z.ZodVoid>;
257
+ /**
258
+ * Get the old value of a field.
259
+ * Helper function for 'update' triggers to easily compare old vs new values.
260
+ *
261
+ * @param fieldName - Name of the field
262
+ * @returns Previous value of the field, or undefined if not available
263
+ *
264
+ * @example
265
+ * if (context.getOldValue('status') !== context.doc.status) {
266
+ * // Status changed
267
+ * }
268
+ */
269
+ getOldValue: z.ZodFunction<z.ZodTuple<[z.ZodString], z.ZodUnknown>, z.ZodAny>;
270
+ }, "strip", z.ZodTypeAny, {
271
+ user: Record<string, any>;
272
+ action: "update" | "delete" | "insert";
273
+ timing: "before" | "after";
274
+ doc: Record<string, any>;
275
+ userId: string;
276
+ addError: (args_0: string, args_1: string | undefined, ...args: unknown[]) => void;
277
+ getOldValue: (args_0: string, ...args: unknown[]) => any;
278
+ previousDoc?: Record<string, any> | undefined;
279
+ ql?: any;
280
+ logger?: any;
281
+ }, {
282
+ user: Record<string, any>;
283
+ action: "update" | "delete" | "insert";
284
+ timing: "before" | "after";
285
+ doc: Record<string, any>;
286
+ userId: string;
287
+ addError: (args_0: string, args_1: string | undefined, ...args: unknown[]) => void;
288
+ getOldValue: (args_0: string, ...args: unknown[]) => any;
289
+ previousDoc?: Record<string, any> | undefined;
290
+ ql?: any;
291
+ logger?: any;
292
+ }>], z.ZodUnknown>, z.ZodPromise<z.ZodVoid>>;
293
+ /**
294
+ * Optional description of what the trigger does.
295
+ */
296
+ description: z.ZodOptional<z.ZodString>;
297
+ /**
298
+ * Whether the trigger is active.
299
+ */
300
+ active: z.ZodDefault<z.ZodBoolean>;
301
+ /**
302
+ * Execution order when multiple triggers are defined.
303
+ * Lower numbers execute first.
304
+ */
305
+ order: z.ZodDefault<z.ZodNumber>;
306
+ }, "strip", z.ZodTypeAny, {
307
+ object: string;
308
+ name: string;
309
+ active: boolean;
310
+ order: number;
311
+ action: "update" | "delete" | "insert" | ("update" | "delete" | "insert")[];
312
+ timing: "before" | "after";
313
+ execute: (args_0: {
314
+ user: Record<string, any>;
315
+ action: "update" | "delete" | "insert";
316
+ timing: "before" | "after";
317
+ doc: Record<string, any>;
318
+ userId: string;
319
+ addError: (args_0: string, args_1: string | undefined, ...args: unknown[]) => void;
320
+ getOldValue: (args_0: string, ...args: unknown[]) => any;
321
+ previousDoc?: Record<string, any> | undefined;
322
+ ql?: any;
323
+ logger?: any;
324
+ }, ...args: unknown[]) => Promise<void>;
325
+ description?: string | undefined;
326
+ }, {
327
+ object: string;
328
+ name: string;
329
+ action: "update" | "delete" | "insert" | ("update" | "delete" | "insert")[];
330
+ timing: "before" | "after";
331
+ execute: (args_0: {
332
+ user: Record<string, any>;
333
+ action: "update" | "delete" | "insert";
334
+ timing: "before" | "after";
335
+ doc: Record<string, any>;
336
+ userId: string;
337
+ addError: (args_0: string, args_1: string | undefined, ...args: unknown[]) => void;
338
+ getOldValue: (args_0: string, ...args: unknown[]) => any;
339
+ previousDoc?: Record<string, any> | undefined;
340
+ ql?: any;
341
+ logger?: any;
342
+ }, ...args: unknown[]) => Promise<void>;
343
+ description?: string | undefined;
344
+ active?: boolean | undefined;
345
+ order?: number | undefined;
346
+ }>;
347
+ /**
348
+ * TypeScript types
349
+ */
350
+ export type TriggerAction = z.infer<typeof TriggerAction>;
351
+ export type TriggerTiming = z.infer<typeof TriggerTiming>;
352
+ export type TriggerContext = z.infer<typeof TriggerContextSchema>;
353
+ export type Trigger = z.infer<typeof TriggerSchema>;
354
+ //# sourceMappingURL=trigger.zod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trigger.zod.d.ts","sourceRoot":"","sources":["../../src/data/trigger.zod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;GAIG;AACH,eAAO,MAAM,aAAa,2CAAyC,CAAC;AAEpE;;;;GAIG;AACH,eAAO,MAAM,aAAa,gCAA8B,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,eAAO,MAAM,oBAAoB;IAC/B;;;OAGG;;IAGH;;;;OAIG;;IAGH;;;;;;OAMG;;IAGH;;;;;;;OAOG;;IAGH;;OAEG;;IAGH;;;OAGG;;IAGH;;;;;;;OAOG;;IAGH;;;;;;;OAOG;;IAGH;;;;;;;;;;;OAWG;;IAMH;;;;;;;;;;;OAWG;;;;;;;;;;;;;;;;;;;;;;;;EAKH,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,aAAa;IACxB;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAGH;;;OAGG;;IAMH;;;OAGG;;QAnIH;;;WAGG;;QAGH;;;;WAIG;;QAGH;;;;;;WAMG;;QAGH;;;;;;;WAOG;;QAGH;;WAEG;;QAGH;;;WAGG;;QAGH;;;;;;;WAOG;;QAGH;;;;;;;WAOG;;QAGH;;;;;;;;;;;WAWG;;QAMH;;;;;;;;;;;WAWG;;;;;;;;;;;;;;;;;;;;;;;;;IA8CH;;OAEG;;IAGH;;OAEG;;IAGH;;;OAGG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAEH,CAAC;AAEH;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAC;AAC1D,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAC;AAC1D,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAClE,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAC"}
@@ -0,0 +1,195 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TriggerSchema = exports.TriggerContextSchema = exports.TriggerTiming = exports.TriggerAction = void 0;
4
+ const zod_1 = require("zod");
5
+ /**
6
+ * Trigger Action Enum
7
+ *
8
+ * Defines the database operation that triggered the execution.
9
+ */
10
+ exports.TriggerAction = zod_1.z.enum(['insert', 'update', 'delete']);
11
+ /**
12
+ * Trigger Timing Enum
13
+ *
14
+ * Defines when the trigger executes relative to the database operation.
15
+ */
16
+ exports.TriggerTiming = zod_1.z.enum(['before', 'after']);
17
+ /**
18
+ * Trigger Context Schema
19
+ *
20
+ * This defines the runtime context available to trigger code during execution.
21
+ * Standardizes how trigger code is written and enables AI code generation.
22
+ *
23
+ * Triggers are business logic hooks that execute before or after database operations.
24
+ * They can validate data, set defaults, update related records, or prevent operations.
25
+ *
26
+ * @example Before Insert Trigger
27
+ * export default {
28
+ * timing: 'before',
29
+ * action: 'insert',
30
+ * execute: async (context: TriggerContext) => {
31
+ * // Set default values
32
+ * if (!context.doc.status) {
33
+ * context.doc.status = 'active';
34
+ * }
35
+ *
36
+ * // Validation
37
+ * if (!context.doc.email) {
38
+ * context.addError('Email is required');
39
+ * }
40
+ * }
41
+ * };
42
+ *
43
+ * @example After Update Trigger
44
+ * export default {
45
+ * timing: 'after',
46
+ * action: 'update',
47
+ * execute: async (context: TriggerContext) => {
48
+ * // Update related records
49
+ * if (context.getOldValue('status') !== context.doc.status) {
50
+ * await context.ql.object('activity_log').create({
51
+ * record_id: context.doc.id,
52
+ * message: `Status changed from ${context.getOldValue('status')} to ${context.doc.status}`,
53
+ * user_id: context.userId,
54
+ * });
55
+ * }
56
+ * }
57
+ * };
58
+ */
59
+ exports.TriggerContextSchema = zod_1.z.object({
60
+ /**
61
+ * The database operation that triggered execution.
62
+ * One of: 'insert', 'update', 'delete'
63
+ */
64
+ action: exports.TriggerAction.describe('Database operation type'),
65
+ /**
66
+ * When the trigger executes relative to the operation.
67
+ * - 'before': Execute before database operation (can modify doc, prevent operation)
68
+ * - 'after': Execute after database operation (can trigger side effects)
69
+ */
70
+ timing: exports.TriggerTiming.describe('Trigger execution timing'),
71
+ /**
72
+ * The current document/record being operated on.
73
+ *
74
+ * For 'before' triggers: Can be modified to change what gets saved.
75
+ * For 'after' triggers: Contains the final saved state (read-only).
76
+ * For 'delete' triggers: Contains the record being deleted.
77
+ */
78
+ doc: zod_1.z.record(zod_1.z.any()).describe('Current document/record'),
79
+ /**
80
+ * The document state before the current operation.
81
+ *
82
+ * Only available for 'update' and 'delete' operations.
83
+ * Null for 'insert' operations.
84
+ *
85
+ * Use this to detect what changed in an update trigger.
86
+ */
87
+ previousDoc: zod_1.z.record(zod_1.z.any()).optional().describe('Previous document state'),
88
+ /**
89
+ * ID of the user performing the operation.
90
+ */
91
+ userId: zod_1.z.string().describe('Current user ID'),
92
+ /**
93
+ * Complete user record of the user performing the operation.
94
+ * Contains fields like name, email, roles, etc.
95
+ */
96
+ user: zod_1.z.record(zod_1.z.any()).describe('Current user record'),
97
+ /**
98
+ * ObjectQL data access API.
99
+ * Use this to query or modify other records.
100
+ *
101
+ * @example
102
+ * await context.ql.object('account').findOne(context.doc.account_id);
103
+ * await context.ql.object('activity').create({ ... });
104
+ */
105
+ ql: zod_1.z.any().describe('ObjectQL data access API'),
106
+ /**
107
+ * Logging interface.
108
+ * Use this for debugging and auditing.
109
+ *
110
+ * @example
111
+ * context.logger.info('Trigger executed', { recordId: context.doc.id });
112
+ * context.logger.error('Validation failed', { error });
113
+ */
114
+ logger: zod_1.z.any().describe('Logging interface'),
115
+ /**
116
+ * Add a validation error.
117
+ * For 'before' triggers only - prevents the operation from completing.
118
+ *
119
+ * @param message - Error message to display
120
+ * @param field - Optional field name the error relates to
121
+ *
122
+ * @example
123
+ * if (context.doc.amount < 0) {
124
+ * context.addError('Amount must be positive', 'amount');
125
+ * }
126
+ */
127
+ addError: zod_1.z.function()
128
+ .args(zod_1.z.string(), zod_1.z.string().optional())
129
+ .returns(zod_1.z.void())
130
+ .describe('Add validation error'),
131
+ /**
132
+ * Get the old value of a field.
133
+ * Helper function for 'update' triggers to easily compare old vs new values.
134
+ *
135
+ * @param fieldName - Name of the field
136
+ * @returns Previous value of the field, or undefined if not available
137
+ *
138
+ * @example
139
+ * if (context.getOldValue('status') !== context.doc.status) {
140
+ * // Status changed
141
+ * }
142
+ */
143
+ getOldValue: zod_1.z.function()
144
+ .args(zod_1.z.string())
145
+ .returns(zod_1.z.any())
146
+ .describe('Get previous field value'),
147
+ });
148
+ /**
149
+ * Trigger Definition Schema
150
+ *
151
+ * Complete definition of a trigger including metadata and execution function.
152
+ */
153
+ exports.TriggerSchema = zod_1.z.object({
154
+ /**
155
+ * Unique trigger name.
156
+ */
157
+ name: zod_1.z.string().regex(/^[a-z_][a-z0-9_]*$/).describe('Trigger name (snake_case)'),
158
+ /**
159
+ * Object this trigger is attached to.
160
+ */
161
+ object: zod_1.z.string().describe('Target object name'),
162
+ /**
163
+ * Trigger timing.
164
+ */
165
+ timing: exports.TriggerTiming.describe('Execution timing'),
166
+ /**
167
+ * Trigger action(s).
168
+ * Can be a single action or array of actions.
169
+ */
170
+ action: zod_1.z.union([
171
+ exports.TriggerAction,
172
+ zod_1.z.array(exports.TriggerAction),
173
+ ]).describe('Database operation(s) to trigger on'),
174
+ /**
175
+ * Trigger execution function.
176
+ * Receives TriggerContext and performs the business logic.
177
+ */
178
+ execute: zod_1.z.function()
179
+ .args(exports.TriggerContextSchema)
180
+ .returns(zod_1.z.promise(zod_1.z.void()))
181
+ .describe('Trigger execution function'),
182
+ /**
183
+ * Optional description of what the trigger does.
184
+ */
185
+ description: zod_1.z.string().optional().describe('Trigger description'),
186
+ /**
187
+ * Whether the trigger is active.
188
+ */
189
+ active: zod_1.z.boolean().default(true).describe('Is trigger active'),
190
+ /**
191
+ * Execution order when multiple triggers are defined.
192
+ * Lower numbers execute first.
193
+ */
194
+ order: zod_1.z.number().default(0).describe('Execution order'),
195
+ });