@objectql/types 1.8.4 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/config.d.ts CHANGED
@@ -20,9 +20,14 @@ export interface ObjectQLConfig {
20
20
  */
21
21
  packages?: string[];
22
22
  /**
23
- * List of npm packages or presets to load.
23
+ * @deprecated Use 'modules' instead.
24
24
  */
25
25
  presets?: string[];
26
+ /**
27
+ * List of modules to load.
28
+ * Can be npm packages or local directories.
29
+ */
30
+ modules?: string[];
26
31
  /**
27
32
  * List of plugins to load.
28
33
  * Can be an instance of ObjectQLPlugin or a package name string.
package/dist/field.d.ts CHANGED
@@ -157,13 +157,23 @@ export interface FieldConfig {
157
157
  ai_context?: ValidationAiContext;
158
158
  /** Dimension of the vector for 'vector' type fields. */
159
159
  dimension?: number;
160
- /** Formula expression. */
160
+ /** Formula expression (for 'formula' type fields). */
161
161
  formula?: string;
162
+ /** Expected return data type for formula fields. */
163
+ data_type?: 'number' | 'text' | 'date' | 'datetime' | 'boolean' | 'currency' | 'percent';
164
+ /** Display format for formula results (e.g., "0.00", "YYYY-MM-DD"). */
165
+ format?: string;
166
+ /** Decimal precision for numeric formula results. */
167
+ precision?: number;
168
+ /** Treat blank/null as zero in formula calculations. */
169
+ blank_as_zero?: boolean;
170
+ /** Default value for null/undefined referenced fields in formulas. */
171
+ treat_blank_as?: string | number | boolean | Date | null;
162
172
  /** Object to summarize. */
163
173
  summary_object?: string;
164
174
  /** Field on the summary object. */
165
175
  summary_field?: string;
166
176
  /** Type of summary (count, sum, min, max, avg). */
167
177
  summary_type?: string;
168
- filters?: any[];
178
+ filters?: unknown[];
169
179
  }
@@ -0,0 +1,308 @@
1
+ /**
2
+ * Formula Engine Types
3
+ *
4
+ * Type definitions for the ObjectQL Formula Engine.
5
+ * Formulas are read-only calculated fields that derive values from other fields,
6
+ * related records, or system variables using JavaScript-style expressions.
7
+ *
8
+ * @see docs/spec/formula.md for complete specification
9
+ */
10
+ import { ObjectQLError } from './api';
11
+ /**
12
+ * Data types that formula expressions can return
13
+ */
14
+ export type FormulaDataType = 'number' | 'text' | 'date' | 'datetime' | 'boolean' | 'currency' | 'percent';
15
+ /**
16
+ * Valid formula return values based on data type
17
+ */
18
+ export type FormulaValue = number | string | boolean | Date | null | undefined;
19
+ /**
20
+ * Valid field values that can be used in formula expressions
21
+ */
22
+ export type FormulaFieldValue = string | number | boolean | Date | null | undefined | FormulaFieldValue[] | {
23
+ [key: string]: FormulaFieldValue;
24
+ };
25
+ /**
26
+ * Configuration for a formula field
27
+ *
28
+ * Formula fields are defined in object metadata and evaluated at query time.
29
+ * They are never stored in the database.
30
+ */
31
+ export interface FormulaFieldConfig {
32
+ /** Field type - must be 'formula' */
33
+ type: 'formula';
34
+ /** JavaScript expression to evaluate */
35
+ expression: string;
36
+ /** Expected return data type of the expression */
37
+ data_type: FormulaDataType;
38
+ /** Display label for UI */
39
+ label?: string;
40
+ /** Help text explaining the formula's purpose */
41
+ description?: string;
42
+ /** Display format for numbers/dates (e.g., "0.00", "YYYY-MM-DD") */
43
+ format?: string;
44
+ /** Decimal precision for numeric results */
45
+ precision?: number;
46
+ /** Treat blank/null as zero in calculations (default: false) */
47
+ blank_as_zero?: boolean;
48
+ /** Default value to use when referenced fields are null/undefined */
49
+ treat_blank_as?: FormulaValue;
50
+ /** AI-friendly context for understanding business intent */
51
+ ai_context?: FormulaAiContext;
52
+ }
53
+ /**
54
+ * AI context for formula fields
55
+ *
56
+ * Provides semantic information to help AI tools understand the
57
+ * business purpose and validation requirements of the formula.
58
+ */
59
+ export interface FormulaAiContext {
60
+ /** Business intent behind the formula */
61
+ intent?: string;
62
+ /** Business rule description in natural language */
63
+ business_rule?: string;
64
+ /** Algorithm description for complex formulas */
65
+ algorithm?: string;
66
+ /** Example calculations with inputs and expected results */
67
+ examples?: FormulaExample[];
68
+ /** Test cases for validation */
69
+ test_cases?: FormulaTestCase[];
70
+ /** Validation notes or constraints */
71
+ validation_notes?: string;
72
+ /** References to external documentation */
73
+ references?: string[];
74
+ }
75
+ /**
76
+ * Example demonstrating formula behavior
77
+ */
78
+ export interface FormulaExample {
79
+ /** Description of this example */
80
+ description: string;
81
+ /** Input field values */
82
+ inputs: Record<string, FormulaFieldValue>;
83
+ /** Expected result */
84
+ result: FormulaValue;
85
+ /** Optional explanation of the calculation */
86
+ explanation?: string;
87
+ }
88
+ /**
89
+ * Test case for formula validation
90
+ */
91
+ export interface FormulaTestCase {
92
+ /** Test case description */
93
+ description?: string;
94
+ /** Input values for the test */
95
+ input: Record<string, FormulaFieldValue>;
96
+ /** Expected output value */
97
+ expected: FormulaValue;
98
+ /** Whether this test should pass or fail */
99
+ should_pass?: boolean;
100
+ }
101
+ /**
102
+ * Runtime context for formula evaluation
103
+ *
104
+ * Provides access to record data, system variables, and user context
105
+ * during formula expression evaluation.
106
+ */
107
+ export interface FormulaContext {
108
+ /** Current record data with all field values */
109
+ record: Record<string, FormulaFieldValue>;
110
+ /** System date/time variables */
111
+ system: FormulaSystemVariables;
112
+ /** Current user context */
113
+ current_user: FormulaUserContext;
114
+ /** Record context flags */
115
+ is_new: boolean;
116
+ /** Record ID (if exists) */
117
+ record_id?: string;
118
+ }
119
+ /**
120
+ * System variables available in formula expressions
121
+ */
122
+ export interface FormulaSystemVariables {
123
+ /** Current date (YYYY-MM-DD) */
124
+ today: Date;
125
+ /** Current timestamp */
126
+ now: Date;
127
+ /** Current year */
128
+ year: number;
129
+ /** Current month (1-12) */
130
+ month: number;
131
+ /** Current day of month (1-31) */
132
+ day: number;
133
+ /** Current hour (0-23) */
134
+ hour: number;
135
+ /** Current minute (0-59) */
136
+ minute?: number;
137
+ /** Current second (0-59) */
138
+ second?: number;
139
+ }
140
+ /**
141
+ * Current user context available in formula expressions
142
+ */
143
+ export interface FormulaUserContext {
144
+ /** User ID */
145
+ id: string;
146
+ /** User's display name */
147
+ name?: string;
148
+ /** User's email address */
149
+ email?: string;
150
+ /** User's role */
151
+ role?: string;
152
+ /** Additional user properties */
153
+ [key: string]: unknown;
154
+ }
155
+ /**
156
+ * Result of formula evaluation
157
+ */
158
+ export interface FormulaEvaluationResult {
159
+ /** Computed value */
160
+ value: FormulaValue;
161
+ /** Data type of the result */
162
+ type: FormulaDataType;
163
+ /** Whether evaluation was successful */
164
+ success: boolean;
165
+ /** Error message if evaluation failed */
166
+ error?: string;
167
+ /** Stack trace for debugging (if error occurred) */
168
+ stack?: string;
169
+ /** Execution time in milliseconds */
170
+ execution_time?: number;
171
+ }
172
+ /**
173
+ * Error types that can occur during formula evaluation
174
+ */
175
+ export declare enum FormulaErrorType {
176
+ /** Syntax error in the expression */
177
+ SYNTAX_ERROR = "SYNTAX_ERROR",
178
+ /** Referenced field does not exist */
179
+ FIELD_NOT_FOUND = "FIELD_NOT_FOUND",
180
+ /** Type mismatch in operation */
181
+ TYPE_ERROR = "TYPE_ERROR",
182
+ /** Division by zero */
183
+ DIVISION_BY_ZERO = "DIVISION_BY_ZERO",
184
+ /** Null or undefined value in operation */
185
+ NULL_REFERENCE = "NULL_REFERENCE",
186
+ /** Evaluation timeout */
187
+ TIMEOUT = "TIMEOUT",
188
+ /** Security violation (restricted operation) */
189
+ SECURITY_VIOLATION = "SECURITY_VIOLATION",
190
+ /** Generic runtime error */
191
+ RUNTIME_ERROR = "RUNTIME_ERROR"
192
+ }
193
+ /**
194
+ * Context information for formula errors
195
+ */
196
+ export interface FormulaErrorContext {
197
+ /** The formula expression that caused the error */
198
+ expression?: string;
199
+ /** Field name if applicable */
200
+ field?: string;
201
+ /** Record data at time of error */
202
+ record?: Record<string, FormulaFieldValue>;
203
+ /** Additional context information */
204
+ [key: string]: unknown;
205
+ }
206
+ /**
207
+ * Custom error for formula evaluation failures
208
+ * Extends ObjectQLError to maintain consistency with ObjectQL error handling
209
+ */
210
+ export declare class FormulaError extends ObjectQLError {
211
+ readonly errorType: FormulaErrorType;
212
+ readonly expression?: string;
213
+ readonly errorContext?: FormulaErrorContext;
214
+ constructor(type: FormulaErrorType, message: string, expression?: string, context?: FormulaErrorContext);
215
+ }
216
+ /**
217
+ * Options for formula evaluation
218
+ */
219
+ export interface FormulaEvaluationOptions {
220
+ /** Maximum execution time in milliseconds (default: 1000) */
221
+ timeout?: number;
222
+ /** Enable strict mode (default: true) */
223
+ strict?: boolean;
224
+ /** Enable debug mode with detailed logging */
225
+ debug?: boolean;
226
+ /** Allow async operations (default: false) */
227
+ allow_async?: boolean;
228
+ /** Sandbox restrictions */
229
+ sandbox?: {
230
+ /** Allowed global variables */
231
+ allowed_globals?: string[];
232
+ /** Blocked operations/methods */
233
+ blocked_operations?: string[];
234
+ };
235
+ }
236
+ /**
237
+ * Metadata about a formula field for introspection
238
+ */
239
+ export interface FormulaMetadata {
240
+ /** Formula field name */
241
+ field_name: string;
242
+ /** The formula expression */
243
+ expression: string;
244
+ /** Expected return type */
245
+ data_type: FormulaDataType;
246
+ /** Fields referenced in the expression */
247
+ dependencies: string[];
248
+ /** Lookup chains used (e.g., ["account.owner.name"]) */
249
+ lookup_chains: string[];
250
+ /** System variables used */
251
+ system_variables: string[];
252
+ /** Whether the formula is valid */
253
+ is_valid: boolean;
254
+ /** Validation errors if invalid */
255
+ validation_errors?: string[];
256
+ /** Estimated complexity (simple, medium, complex) */
257
+ complexity?: 'simple' | 'medium' | 'complex';
258
+ }
259
+ /**
260
+ * Statistics about formula execution
261
+ */
262
+ export interface FormulaExecutionStats {
263
+ /** Formula field name */
264
+ field_name: string;
265
+ /** Total number of evaluations */
266
+ evaluation_count: number;
267
+ /** Number of successful evaluations */
268
+ success_count: number;
269
+ /** Number of failed evaluations */
270
+ error_count: number;
271
+ /** Average execution time in milliseconds */
272
+ avg_execution_time: number;
273
+ /** Maximum execution time in milliseconds */
274
+ max_execution_time: number;
275
+ /** Minimum execution time in milliseconds */
276
+ min_execution_time: number;
277
+ /** Most common error types */
278
+ common_errors?: Record<FormulaErrorType, number>;
279
+ }
280
+ /**
281
+ * Type for custom formula functions
282
+ * These functions can be registered in the formula engine for use in expressions
283
+ */
284
+ export type FormulaCustomFunction = (...args: FormulaFieldValue[]) => FormulaValue;
285
+ /**
286
+ * Configuration for formula engine
287
+ */
288
+ export interface FormulaEngineConfig {
289
+ /** Enable formula caching */
290
+ enable_cache?: boolean;
291
+ /** Cache TTL in seconds */
292
+ cache_ttl?: number;
293
+ /** Maximum formula execution time in milliseconds */
294
+ max_execution_time?: number;
295
+ /** Enable performance monitoring */
296
+ enable_monitoring?: boolean;
297
+ /** Custom function library */
298
+ custom_functions?: Record<string, FormulaCustomFunction>;
299
+ /** Sandbox configuration */
300
+ sandbox?: {
301
+ /** Enable sandbox mode */
302
+ enabled?: boolean;
303
+ /** Allowed global objects */
304
+ allowed_globals?: string[];
305
+ /** Blocked operations */
306
+ blocked_operations?: string[];
307
+ };
308
+ }
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ /**
3
+ * Formula Engine Types
4
+ *
5
+ * Type definitions for the ObjectQL Formula Engine.
6
+ * Formulas are read-only calculated fields that derive values from other fields,
7
+ * related records, or system variables using JavaScript-style expressions.
8
+ *
9
+ * @see docs/spec/formula.md for complete specification
10
+ */
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.FormulaError = exports.FormulaErrorType = void 0;
13
+ const api_1 = require("./api");
14
+ /**
15
+ * Error types that can occur during formula evaluation
16
+ */
17
+ var FormulaErrorType;
18
+ (function (FormulaErrorType) {
19
+ /** Syntax error in the expression */
20
+ FormulaErrorType["SYNTAX_ERROR"] = "SYNTAX_ERROR";
21
+ /** Referenced field does not exist */
22
+ FormulaErrorType["FIELD_NOT_FOUND"] = "FIELD_NOT_FOUND";
23
+ /** Type mismatch in operation */
24
+ FormulaErrorType["TYPE_ERROR"] = "TYPE_ERROR";
25
+ /** Division by zero */
26
+ FormulaErrorType["DIVISION_BY_ZERO"] = "DIVISION_BY_ZERO";
27
+ /** Null or undefined value in operation */
28
+ FormulaErrorType["NULL_REFERENCE"] = "NULL_REFERENCE";
29
+ /** Evaluation timeout */
30
+ FormulaErrorType["TIMEOUT"] = "TIMEOUT";
31
+ /** Security violation (restricted operation) */
32
+ FormulaErrorType["SECURITY_VIOLATION"] = "SECURITY_VIOLATION";
33
+ /** Generic runtime error */
34
+ FormulaErrorType["RUNTIME_ERROR"] = "RUNTIME_ERROR";
35
+ })(FormulaErrorType || (exports.FormulaErrorType = FormulaErrorType = {}));
36
+ /**
37
+ * Custom error for formula evaluation failures
38
+ * Extends ObjectQLError to maintain consistency with ObjectQL error handling
39
+ */
40
+ class FormulaError extends api_1.ObjectQLError {
41
+ constructor(type, message, expression, context) {
42
+ super({
43
+ code: type,
44
+ message,
45
+ details: {
46
+ formula_error_type: type,
47
+ expression,
48
+ ...context
49
+ }
50
+ });
51
+ this.name = 'FormulaError';
52
+ this.errorType = type;
53
+ this.expression = expression;
54
+ this.errorContext = context;
55
+ }
56
+ }
57
+ exports.FormulaError = FormulaError;
58
+ //# sourceMappingURL=formula.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formula.js","sourceRoot":"","sources":["../src/formula.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;AAEH,+BAAoD;AA0OpD;;GAEG;AACH,IAAY,gBAwBX;AAxBD,WAAY,gBAAgB;IACxB,qCAAqC;IACrC,iDAA6B,CAAA;IAE7B,sCAAsC;IACtC,uDAAmC,CAAA;IAEnC,iCAAiC;IACjC,6CAAyB,CAAA;IAEzB,uBAAuB;IACvB,yDAAqC,CAAA;IAErC,2CAA2C;IAC3C,qDAAiC,CAAA;IAEjC,yBAAyB;IACzB,uCAAmB,CAAA;IAEnB,gDAAgD;IAChD,6DAAyC,CAAA;IAEzC,4BAA4B;IAC5B,mDAA+B,CAAA;AACnC,CAAC,EAxBW,gBAAgB,gCAAhB,gBAAgB,QAwB3B;AAmBD;;;GAGG;AACH,MAAa,YAAa,SAAQ,mBAAa;IAK3C,YACI,IAAsB,EACtB,OAAe,EACf,UAAmB,EACnB,OAA6B;QAE7B,KAAK,CAAC;YACF,IAAI,EAAE,IAAc;YACpB,OAAO;YACP,OAAO,EAAE;gBACL,kBAAkB,EAAE,IAAI;gBACxB,UAAU;gBACV,GAAG,OAAO;aACb;SACJ,CAAC,CAAC;QACH,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;QAC3B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;QACtB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC;IAChC,CAAC;CACJ;AAzBD,oCAyBC"}
package/dist/index.d.ts CHANGED
@@ -22,3 +22,4 @@ export * from './view';
22
22
  export * from './workflow';
23
23
  export * from './report';
24
24
  export * from './form';
25
+ export * from './formula';
package/dist/index.js CHANGED
@@ -38,4 +38,5 @@ __exportStar(require("./view"), exports);
38
38
  __exportStar(require("./workflow"), exports);
39
39
  __exportStar(require("./report"), exports);
40
40
  __exportStar(require("./form"), exports);
41
+ __exportStar(require("./formula"), exports);
41
42
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,2CAAyB;AACzB,2CAAyB;AACzB,0CAAwB;AACxB,6CAA2B;AAC3B,yCAAuB;AACvB,2CAAyB;AACzB,+CAA6B;AAC7B,wCAAsB;AACtB,2CAAyB;AACzB,2CAAyB;AACzB,4CAA0B;AAC1B,+CAA6B;AAC7B,+CAA6B;AAC7B,yCAAuB;AACvB,2CAAyB;AACzB,gDAA8B;AAC9B,yCAAuB;AACvB,8CAA4B;AAC5B,wCAAsB;AACtB,yCAAuB;AACvB,6CAA2B;AAC3B,2CAAyB;AACzB,yCAAuB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,2CAAyB;AACzB,2CAAyB;AACzB,0CAAwB;AACxB,6CAA2B;AAC3B,yCAAuB;AACvB,2CAAyB;AACzB,+CAA6B;AAC7B,wCAAsB;AACtB,2CAAyB;AACzB,2CAAyB;AACzB,4CAA0B;AAC1B,+CAA6B;AAC7B,+CAA6B;AAC7B,yCAAuB;AACvB,2CAAyB;AACzB,gDAA8B;AAC9B,yCAAuB;AACvB,8CAA4B;AAC5B,wCAAsB;AACtB,yCAAuB;AACvB,6CAA2B;AAC3B,2CAAyB;AACzB,yCAAuB;AACvB,4CAA0B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@objectql/types",
3
- "version": "1.8.4",
3
+ "version": "1.9.0",
4
4
  "description": "Pure TypeScript type definitions and interfaces for the ObjectQL protocol - The Contract",
5
5
  "keywords": [
6
6
  "objectql",
@@ -559,6 +559,23 @@
559
559
  "$ref": "#/definitions/ValidationAiContext",
560
560
  "description": "AI context for the field. Provides semantic information for AI tools."
561
561
  },
562
+ "blank_as_zero": {
563
+ "description": "Treat blank/null as zero in formula calculations.",
564
+ "type": "boolean"
565
+ },
566
+ "data_type": {
567
+ "description": "Expected return data type for formula fields.",
568
+ "enum": [
569
+ "number",
570
+ "text",
571
+ "date",
572
+ "datetime",
573
+ "boolean",
574
+ "currency",
575
+ "percent"
576
+ ],
577
+ "type": "string"
578
+ },
562
579
  "defaultValue": {
563
580
  "description": "The default value if not provided during creation."
564
581
  },
@@ -574,8 +591,12 @@
574
591
  "items": {},
575
592
  "type": "array"
576
593
  },
594
+ "format": {
595
+ "description": "Display format for formula results (e.g., \"0.00\", \"YYYY-MM-DD\").",
596
+ "type": "string"
597
+ },
577
598
  "formula": {
578
- "description": "Formula expression.",
599
+ "description": "Formula expression (for 'formula' type fields).",
579
600
  "type": "string"
580
601
  },
581
602
  "help_text": {
@@ -649,6 +670,10 @@
649
670
  },
650
671
  "type": "array"
651
672
  },
673
+ "precision": {
674
+ "description": "Decimal precision for numeric formula results.",
675
+ "type": "number"
676
+ },
652
677
  "readonly": {
653
678
  "description": "Whether the field is read-only in UI.",
654
679
  "type": "boolean"
@@ -677,6 +702,27 @@
677
702
  "description": "Type of summary (count, sum, min, max, avg).",
678
703
  "type": "string"
679
704
  },
705
+ "treat_blank_as": {
706
+ "anyOf": [
707
+ {
708
+ "type": "string"
709
+ },
710
+ {
711
+ "type": "number"
712
+ },
713
+ {
714
+ "type": "boolean"
715
+ },
716
+ {
717
+ "format": "date-time",
718
+ "type": "string"
719
+ },
720
+ {
721
+ "type": "null"
722
+ }
723
+ ],
724
+ "description": "Default value for null/undefined referenced fields in formulas."
725
+ },
680
726
  "type": {
681
727
  "$ref": "#/definitions/FieldType",
682
728
  "description": "The data type of the field."