@inixiative/json-rules 1.3.4 → 2.0.3
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/README.md +168 -0
- package/dist/index.cjs +18 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +159 -42
- package/dist/index.d.ts +159 -42
- package/dist/index.js +18 -2
- package/dist/index.js.map +1 -1
- package/package.json +4 -9
package/dist/index.d.cts
CHANGED
|
@@ -86,7 +86,7 @@ type StrictRangeRule = (RuleBase<OperatorValues['between']> & ValueSource<[Order
|
|
|
86
86
|
type StrictPresenceRule = (RuleBase<OperatorValues['isEmpty']> & NoValueSource) | (RuleBase<OperatorValues['notEmpty']> & NoValueSource) | (RuleBase<OperatorValues['exists']> & NoValueSource) | (RuleBase<OperatorValues['notExists']> & NoValueSource);
|
|
87
87
|
type StrictRule<TValue = RuleValue> = StrictEqualityRule<TValue> | StrictOrderedComparisonRule | StrictMembershipRule<TValue> | StrictContainsRule<TValue> | StrictPatternRule | StrictStringBoundaryRule | StrictRangeRule | StrictPresenceRule;
|
|
88
88
|
type ArrayRuleBase<TOperator extends ArrayOperator> = {
|
|
89
|
-
field
|
|
89
|
+
field?: string;
|
|
90
90
|
arrayOperator: TOperator;
|
|
91
91
|
error?: string;
|
|
92
92
|
};
|
|
@@ -164,7 +164,7 @@ type Rule<TValue = RuleValue> = {
|
|
|
164
164
|
error?: string;
|
|
165
165
|
};
|
|
166
166
|
type ArrayRule<TRuleValue = RuleValue, TDateValue = DateRuleValue> = {
|
|
167
|
-
field
|
|
167
|
+
field?: string;
|
|
168
168
|
arrayOperator: ArrayOperator;
|
|
169
169
|
condition?: Condition<TRuleValue, TDateValue>;
|
|
170
170
|
count?: number;
|
|
@@ -208,11 +208,16 @@ type StrictIfThenElse<TRuleValue = RuleValue, TDateValue = DateRuleValue> = {
|
|
|
208
208
|
};
|
|
209
209
|
type StrictCondition<TRuleValue = RuleValue, TDateValue = DateRuleValue> = StrictRule<TRuleValue> | StrictAggregateRule<TRuleValue, TDateValue> | StrictArrayRule<TRuleValue, TDateValue> | StrictDateRule | StrictAll<TRuleValue, TDateValue> | StrictAny<TRuleValue, TDateValue> | StrictIfThenElse<TRuleValue, TDateValue> | boolean;
|
|
210
210
|
|
|
211
|
-
|
|
211
|
+
type Row$1 = Record<string, unknown>;
|
|
212
|
+
type CheckData = Row$1 | unknown[];
|
|
213
|
+
type CheckOptions = {
|
|
214
|
+
context?: CheckData;
|
|
215
|
+
};
|
|
216
|
+
declare const check: <TData extends CheckData>(conditions: Condition, data: TData, options?: CheckOptions) => boolean | string;
|
|
212
217
|
|
|
213
218
|
type PrismaWhere = Record<string, unknown>;
|
|
214
219
|
type FieldMapEntry = {
|
|
215
|
-
kind: 'scalar' | 'object' | 'enum';
|
|
220
|
+
kind: 'scalar' | 'object' | 'enum' | 'bridge';
|
|
216
221
|
type: string;
|
|
217
222
|
isList?: boolean;
|
|
218
223
|
fromFields?: string[];
|
|
@@ -249,11 +254,157 @@ type ToPrismaResult = {
|
|
|
249
254
|
steps: PrismaStep[];
|
|
250
255
|
};
|
|
251
256
|
type BuildOptions = {
|
|
252
|
-
map?: FieldMap;
|
|
257
|
+
map?: FieldMap | FieldMapSet;
|
|
258
|
+
mapName?: string;
|
|
253
259
|
model?: string;
|
|
254
260
|
context?: Record<string, unknown>;
|
|
255
261
|
};
|
|
256
262
|
|
|
263
|
+
type BridgeEndpoint = {
|
|
264
|
+
fieldMap: string;
|
|
265
|
+
model: string;
|
|
266
|
+
on: string;
|
|
267
|
+
};
|
|
268
|
+
type BridgeCardinality = 'oneToOne' | 'oneToMany';
|
|
269
|
+
/**
|
|
270
|
+
* A cross-source edge between two endpoints.
|
|
271
|
+
*
|
|
272
|
+
* Endpoint ordering convention for `oneToMany`:
|
|
273
|
+
* - `endpoints[0]` is the "one" side — its `on` field must be unique per row
|
|
274
|
+
* (typically a primary key).
|
|
275
|
+
* - `endpoints[1]` is the "many" side — its `on` field may repeat across rows
|
|
276
|
+
* (typically a foreign key).
|
|
277
|
+
*
|
|
278
|
+
* Mis-ordering produces wrong `isList` flags during stitching and silent
|
|
279
|
+
* row-dedup when building bridge dictionaries. `buildBridgeDictionary` throws
|
|
280
|
+
* at runtime if endpoint[0]'s data has duplicate `on` values to catch this.
|
|
281
|
+
*
|
|
282
|
+
* For `oneToOne`, both `on` fields must be unique; endpoint order is symmetric.
|
|
283
|
+
*/
|
|
284
|
+
type Bridge = {
|
|
285
|
+
endpoints: [BridgeEndpoint, BridgeEndpoint];
|
|
286
|
+
cardinality: BridgeCardinality;
|
|
287
|
+
};
|
|
288
|
+
type FieldMapSet = {
|
|
289
|
+
maps: Record<string, FieldMap>;
|
|
290
|
+
bridges?: Bridge[];
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
type Row = Record<string, unknown>;
|
|
294
|
+
type BridgeDictionary = Record<string, // map name
|
|
295
|
+
Record<string, // model name
|
|
296
|
+
Record<string, Record<string, Row | Row[]>>>>;
|
|
297
|
+
declare const buildBridgeDictionary: (set: FieldMapSet, rawData: Record<string, Row[]>) => BridgeDictionary;
|
|
298
|
+
|
|
299
|
+
declare const stitchFieldMaps: (set: FieldMapSet) => FieldMapSet;
|
|
300
|
+
|
|
301
|
+
declare const validateFieldMapSet: (set: FieldMapSet) => void;
|
|
302
|
+
declare const validateFieldMap: (fieldMap: FieldMap, mapName?: string) => void;
|
|
303
|
+
|
|
304
|
+
type Lens = FieldMapSet & {
|
|
305
|
+
mapName: string;
|
|
306
|
+
model: string;
|
|
307
|
+
};
|
|
308
|
+
type ModelNarrowing = {
|
|
309
|
+
picks?: string[];
|
|
310
|
+
omits?: string[];
|
|
311
|
+
relations?: Record<string, ModelNarrowing>;
|
|
312
|
+
};
|
|
313
|
+
type MapNarrowing = {
|
|
314
|
+
models: Record<string, ModelNarrowing>;
|
|
315
|
+
};
|
|
316
|
+
type LensNarrowing = {
|
|
317
|
+
parent: Lens | LensNarrowing;
|
|
318
|
+
maps: Record<string, MapNarrowing>;
|
|
319
|
+
constrains?: Condition;
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
declare const applyLens: (rule: Condition, narrowing: Lens | LensNarrowing) => Condition;
|
|
323
|
+
|
|
324
|
+
type RuleLensViolation = {
|
|
325
|
+
path: string;
|
|
326
|
+
reason: string;
|
|
327
|
+
};
|
|
328
|
+
type RuleLensCheck = {
|
|
329
|
+
ok: boolean;
|
|
330
|
+
violations: RuleLensViolation[];
|
|
331
|
+
};
|
|
332
|
+
declare const checkRuleAgainstLens: (rule: Condition, lensOrNarrowing: Lens | LensNarrowing) => RuleLensCheck;
|
|
333
|
+
|
|
334
|
+
type CreateLensInput = {
|
|
335
|
+
maps: Record<string, FieldMap>;
|
|
336
|
+
bridges?: Bridge[];
|
|
337
|
+
mapName: string;
|
|
338
|
+
model: string;
|
|
339
|
+
};
|
|
340
|
+
declare const createLens: (input: CreateLensInput) => Lens;
|
|
341
|
+
|
|
342
|
+
declare const validateNarrowing: (narrowing: LensNarrowing) => void;
|
|
343
|
+
|
|
344
|
+
declare const projectNarrowing: (lensOrNarrowing: Lens | LensNarrowing) => FieldMapSet;
|
|
345
|
+
|
|
346
|
+
declare const FieldKind: {
|
|
347
|
+
readonly String: "String";
|
|
348
|
+
readonly Boolean: "Boolean";
|
|
349
|
+
readonly Int: "Int";
|
|
350
|
+
readonly BigInt: "BigInt";
|
|
351
|
+
readonly Float: "Float";
|
|
352
|
+
readonly Decimal: "Decimal";
|
|
353
|
+
readonly DateTime: "DateTime";
|
|
354
|
+
readonly Json: "Json";
|
|
355
|
+
readonly Bytes: "Bytes";
|
|
356
|
+
readonly Enum: "Enum";
|
|
357
|
+
};
|
|
358
|
+
type FieldKind = (typeof FieldKind)[keyof typeof FieldKind];
|
|
359
|
+
declare const NUMERIC_KINDS: readonly FieldKind[];
|
|
360
|
+
declare const ORDERABLE_KINDS: readonly FieldKind[];
|
|
361
|
+
declare const STRINGY_KINDS: readonly FieldKind[];
|
|
362
|
+
declare const EQUATABLE_KINDS: readonly FieldKind[];
|
|
363
|
+
declare const ALL_KINDS: readonly FieldKind[];
|
|
364
|
+
declare const RuleTarget: {
|
|
365
|
+
readonly check: "check";
|
|
366
|
+
readonly toPrisma: "toPrisma";
|
|
367
|
+
readonly toSql: "toSql";
|
|
368
|
+
};
|
|
369
|
+
type RuleTarget = (typeof RuleTarget)[keyof typeof RuleTarget];
|
|
370
|
+
declare const ValueShape: {
|
|
371
|
+
readonly none: "none";
|
|
372
|
+
readonly scalar: "scalar";
|
|
373
|
+
readonly ordered: "ordered";
|
|
374
|
+
readonly array: "array";
|
|
375
|
+
readonly string: "string";
|
|
376
|
+
readonly pattern: "pattern";
|
|
377
|
+
readonly range: "range";
|
|
378
|
+
readonly dateValue: "dateValue";
|
|
379
|
+
readonly dateRange: "dateRange";
|
|
380
|
+
readonly dayList: "dayList";
|
|
381
|
+
readonly count: "count";
|
|
382
|
+
readonly predicate: "predicate";
|
|
383
|
+
};
|
|
384
|
+
type ValueShape = (typeof ValueShape)[keyof typeof ValueShape];
|
|
385
|
+
type CatalogEntry = {
|
|
386
|
+
kinds: readonly FieldKind[];
|
|
387
|
+
targets: readonly RuleTarget[];
|
|
388
|
+
valueShape: ValueShape;
|
|
389
|
+
};
|
|
390
|
+
declare const FIELD_OPERATOR_CATALOG: Record<Operator, CatalogEntry>;
|
|
391
|
+
declare const DATE_OPERATOR_CATALOG: Record<DateOperator, CatalogEntry>;
|
|
392
|
+
type ArrayCatalogEntry = {
|
|
393
|
+
targets: readonly RuleTarget[];
|
|
394
|
+
valueShape: ValueShape;
|
|
395
|
+
};
|
|
396
|
+
declare const ARRAY_OPERATOR_CATALOG: Record<ArrayOperator, ArrayCatalogEntry>;
|
|
397
|
+
declare const AGGREGATE_OPERATORS: readonly Operator[];
|
|
398
|
+
declare const isAggregateSingleOperator: (operator: Operator) => boolean;
|
|
399
|
+
declare const isAggregateRangeOperator: (operator: Operator) => boolean;
|
|
400
|
+
declare const getValueShape: (operator: Operator | DateOperator | ArrayOperator) => ValueShape;
|
|
401
|
+
declare const isOperatorSupportedForTarget: (operator: Operator | DateOperator | ArrayOperator, target: RuleTarget) => boolean;
|
|
402
|
+
declare const getOperatorsForKind: (kind: FieldKind, target?: RuleTarget) => {
|
|
403
|
+
field: Operator[];
|
|
404
|
+
date: DateOperator[];
|
|
405
|
+
};
|
|
406
|
+
declare const getArrayOperators: (target?: RuleTarget) => ArrayOperator[];
|
|
407
|
+
|
|
257
408
|
/**
|
|
258
409
|
* Execute a Prisma query plan produced by toPrisma().
|
|
259
410
|
*
|
|
@@ -321,42 +472,8 @@ type SqlBuildOptions = {
|
|
|
321
472
|
alias?: string;
|
|
322
473
|
context?: Record<string, unknown>;
|
|
323
474
|
};
|
|
324
|
-
/**
|
|
325
|
-
* Convert a json-rules Condition to a PostgreSQL WHERE clause.
|
|
326
|
-
*
|
|
327
|
-
* @param condition - The rule condition to convert
|
|
328
|
-
* @param options - Optional map/model/alias for JOIN generation; context for path refs
|
|
329
|
-
* @returns Object with `sql`, `params`, and `joins` (LEFT JOIN clauses)
|
|
330
|
-
*
|
|
331
|
-
* @example
|
|
332
|
-
* ```typescript
|
|
333
|
-
* // Simple field
|
|
334
|
-
* const { sql, params } = toSql({ field: 'status', operator: Operator.equals, value: 'active' });
|
|
335
|
-
* // sql: '"status" = $1'
|
|
336
|
-
*
|
|
337
|
-
* // Relation traversal with JOINs (map required)
|
|
338
|
-
* const { sql, params, joins } = toSql(
|
|
339
|
-
* { field: 'author.email', operator: Operator.equals, value: 'a@b.com' },
|
|
340
|
-
* { map, model: 'Post', alias: 't0' }
|
|
341
|
-
* );
|
|
342
|
-
* // sql: '"t1"."email" = $1'
|
|
343
|
-
* // joins: ['LEFT JOIN "User" AS "t1" ON "t1"."id" = "t0"."authorId"']
|
|
344
|
-
*
|
|
345
|
-
* // Same-record field comparison ($.field)
|
|
346
|
-
* const { sql: sql2 } = toSql({ field: 'endDate', operator: Operator.greaterThan, path: '$.startDate' });
|
|
347
|
-
* // sql2: '"endDate" > "startDate"'
|
|
348
|
-
*
|
|
349
|
-
* // External context ref
|
|
350
|
-
* const { sql: sql3 } = toSql(
|
|
351
|
-
* { field: 'userId', operator: Operator.equals, path: 'currentUser.id' },
|
|
352
|
-
* { context: { currentUser: { id: '123' } } }
|
|
353
|
-
* );
|
|
354
|
-
* // sql3: '"userId" = $1' params: ['123']
|
|
355
|
-
* ```
|
|
356
|
-
*/
|
|
357
475
|
declare const toSql: (condition: Condition, options?: SqlBuildOptions) => SqlResult;
|
|
358
476
|
|
|
359
|
-
type RuleValidationTarget = 'check' | 'toPrisma' | 'toSql';
|
|
360
477
|
type ValidationIssue = {
|
|
361
478
|
path: string;
|
|
362
479
|
message: string;
|
|
@@ -367,10 +484,10 @@ type ValidationResult = {
|
|
|
367
484
|
errors: ValidationIssue[];
|
|
368
485
|
};
|
|
369
486
|
declare const validateRule: (condition: unknown, options?: {
|
|
370
|
-
target?:
|
|
487
|
+
target?: RuleTarget;
|
|
371
488
|
}) => ValidationResult;
|
|
372
489
|
declare const assertValidRule: (condition: unknown, options?: {
|
|
373
|
-
target?:
|
|
490
|
+
target?: RuleTarget;
|
|
374
491
|
}) => asserts condition is Condition;
|
|
375
492
|
|
|
376
|
-
export { type AggregateMode, type AggregateRule, type All, type Any, ArrayOperator, type ArrayRule, type BuildOptions, type Condition, type DateInputValue, DateOperator, type DateRule, type DateRuleValue, type FieldMap, type FieldMapEntry, type GroupByStep, type IfThenElse, Operator, type OrderedRuleValue, type PrismaStep, type PrismaWhere, type Rule, type
|
|
493
|
+
export { AGGREGATE_OPERATORS, ALL_KINDS, ARRAY_OPERATOR_CATALOG, type AggregateMode, type AggregateRule, type All, type Any, type ArrayCatalogEntry, ArrayOperator, type ArrayRule, type Bridge, type BridgeCardinality, type BridgeDictionary, type BridgeEndpoint, type BuildOptions, type CatalogEntry, type CheckOptions, type Condition, type CreateLensInput, DATE_OPERATOR_CATALOG, type DateInputValue, DateOperator, type DateRule, type DateRuleValue, EQUATABLE_KINDS, FIELD_OPERATOR_CATALOG, FieldKind, type FieldMap, type FieldMapEntry, type FieldMapSet, type GroupByStep, type IfThenElse, type Lens, type LensNarrowing, type MapNarrowing, type ModelNarrowing, NUMERIC_KINDS, ORDERABLE_KINDS, Operator, type OrderedRuleValue, type PrismaStep, type PrismaWhere, type Rule, type RuleLensCheck, type RuleLensViolation, type RuleScalar, RuleTarget, type RuleValue, STRINGY_KINDS, type SqlResult, type StepRef, type StrictAggregateRule, type StrictAll, type StrictAny, type StrictArrayCountRule, type StrictArrayPredicateRule, type StrictArrayPresenceRule, type StrictArrayRule, type StrictCondition, type StrictContainsRule, type StrictDateComparisonRule, type StrictDateDayRule, type StrictDateRangeRule, type StrictDateRule, type StrictEqualityRule, type StrictIfThenElse, type StrictMembershipRule, type StrictOrderedComparisonRule, type StrictPatternRule, type StrictPresenceRule, type StrictRangeRule, type StrictRule, type StrictStringBoundaryRule, type ToPrismaResult, type ValidationIssue, type ValidationResult, ValueShape, type WhereStep, applyLens, assertValidRule, buildBridgeDictionary, check, checkRuleAgainstLens, createLens, executePrismaQueryPlan, getArrayOperators, getOperatorsForKind, getValueShape, isAggregateRangeOperator, isAggregateSingleOperator, isOperatorSupportedForTarget, projectNarrowing, stitchFieldMaps, toPrisma, toSql, validateFieldMap, validateFieldMapSet, validateNarrowing, validateRule };
|
package/dist/index.d.ts
CHANGED
|
@@ -86,7 +86,7 @@ type StrictRangeRule = (RuleBase<OperatorValues['between']> & ValueSource<[Order
|
|
|
86
86
|
type StrictPresenceRule = (RuleBase<OperatorValues['isEmpty']> & NoValueSource) | (RuleBase<OperatorValues['notEmpty']> & NoValueSource) | (RuleBase<OperatorValues['exists']> & NoValueSource) | (RuleBase<OperatorValues['notExists']> & NoValueSource);
|
|
87
87
|
type StrictRule<TValue = RuleValue> = StrictEqualityRule<TValue> | StrictOrderedComparisonRule | StrictMembershipRule<TValue> | StrictContainsRule<TValue> | StrictPatternRule | StrictStringBoundaryRule | StrictRangeRule | StrictPresenceRule;
|
|
88
88
|
type ArrayRuleBase<TOperator extends ArrayOperator> = {
|
|
89
|
-
field
|
|
89
|
+
field?: string;
|
|
90
90
|
arrayOperator: TOperator;
|
|
91
91
|
error?: string;
|
|
92
92
|
};
|
|
@@ -164,7 +164,7 @@ type Rule<TValue = RuleValue> = {
|
|
|
164
164
|
error?: string;
|
|
165
165
|
};
|
|
166
166
|
type ArrayRule<TRuleValue = RuleValue, TDateValue = DateRuleValue> = {
|
|
167
|
-
field
|
|
167
|
+
field?: string;
|
|
168
168
|
arrayOperator: ArrayOperator;
|
|
169
169
|
condition?: Condition<TRuleValue, TDateValue>;
|
|
170
170
|
count?: number;
|
|
@@ -208,11 +208,16 @@ type StrictIfThenElse<TRuleValue = RuleValue, TDateValue = DateRuleValue> = {
|
|
|
208
208
|
};
|
|
209
209
|
type StrictCondition<TRuleValue = RuleValue, TDateValue = DateRuleValue> = StrictRule<TRuleValue> | StrictAggregateRule<TRuleValue, TDateValue> | StrictArrayRule<TRuleValue, TDateValue> | StrictDateRule | StrictAll<TRuleValue, TDateValue> | StrictAny<TRuleValue, TDateValue> | StrictIfThenElse<TRuleValue, TDateValue> | boolean;
|
|
210
210
|
|
|
211
|
-
|
|
211
|
+
type Row$1 = Record<string, unknown>;
|
|
212
|
+
type CheckData = Row$1 | unknown[];
|
|
213
|
+
type CheckOptions = {
|
|
214
|
+
context?: CheckData;
|
|
215
|
+
};
|
|
216
|
+
declare const check: <TData extends CheckData>(conditions: Condition, data: TData, options?: CheckOptions) => boolean | string;
|
|
212
217
|
|
|
213
218
|
type PrismaWhere = Record<string, unknown>;
|
|
214
219
|
type FieldMapEntry = {
|
|
215
|
-
kind: 'scalar' | 'object' | 'enum';
|
|
220
|
+
kind: 'scalar' | 'object' | 'enum' | 'bridge';
|
|
216
221
|
type: string;
|
|
217
222
|
isList?: boolean;
|
|
218
223
|
fromFields?: string[];
|
|
@@ -249,11 +254,157 @@ type ToPrismaResult = {
|
|
|
249
254
|
steps: PrismaStep[];
|
|
250
255
|
};
|
|
251
256
|
type BuildOptions = {
|
|
252
|
-
map?: FieldMap;
|
|
257
|
+
map?: FieldMap | FieldMapSet;
|
|
258
|
+
mapName?: string;
|
|
253
259
|
model?: string;
|
|
254
260
|
context?: Record<string, unknown>;
|
|
255
261
|
};
|
|
256
262
|
|
|
263
|
+
type BridgeEndpoint = {
|
|
264
|
+
fieldMap: string;
|
|
265
|
+
model: string;
|
|
266
|
+
on: string;
|
|
267
|
+
};
|
|
268
|
+
type BridgeCardinality = 'oneToOne' | 'oneToMany';
|
|
269
|
+
/**
|
|
270
|
+
* A cross-source edge between two endpoints.
|
|
271
|
+
*
|
|
272
|
+
* Endpoint ordering convention for `oneToMany`:
|
|
273
|
+
* - `endpoints[0]` is the "one" side — its `on` field must be unique per row
|
|
274
|
+
* (typically a primary key).
|
|
275
|
+
* - `endpoints[1]` is the "many" side — its `on` field may repeat across rows
|
|
276
|
+
* (typically a foreign key).
|
|
277
|
+
*
|
|
278
|
+
* Mis-ordering produces wrong `isList` flags during stitching and silent
|
|
279
|
+
* row-dedup when building bridge dictionaries. `buildBridgeDictionary` throws
|
|
280
|
+
* at runtime if endpoint[0]'s data has duplicate `on` values to catch this.
|
|
281
|
+
*
|
|
282
|
+
* For `oneToOne`, both `on` fields must be unique; endpoint order is symmetric.
|
|
283
|
+
*/
|
|
284
|
+
type Bridge = {
|
|
285
|
+
endpoints: [BridgeEndpoint, BridgeEndpoint];
|
|
286
|
+
cardinality: BridgeCardinality;
|
|
287
|
+
};
|
|
288
|
+
type FieldMapSet = {
|
|
289
|
+
maps: Record<string, FieldMap>;
|
|
290
|
+
bridges?: Bridge[];
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
type Row = Record<string, unknown>;
|
|
294
|
+
type BridgeDictionary = Record<string, // map name
|
|
295
|
+
Record<string, // model name
|
|
296
|
+
Record<string, Record<string, Row | Row[]>>>>;
|
|
297
|
+
declare const buildBridgeDictionary: (set: FieldMapSet, rawData: Record<string, Row[]>) => BridgeDictionary;
|
|
298
|
+
|
|
299
|
+
declare const stitchFieldMaps: (set: FieldMapSet) => FieldMapSet;
|
|
300
|
+
|
|
301
|
+
declare const validateFieldMapSet: (set: FieldMapSet) => void;
|
|
302
|
+
declare const validateFieldMap: (fieldMap: FieldMap, mapName?: string) => void;
|
|
303
|
+
|
|
304
|
+
type Lens = FieldMapSet & {
|
|
305
|
+
mapName: string;
|
|
306
|
+
model: string;
|
|
307
|
+
};
|
|
308
|
+
type ModelNarrowing = {
|
|
309
|
+
picks?: string[];
|
|
310
|
+
omits?: string[];
|
|
311
|
+
relations?: Record<string, ModelNarrowing>;
|
|
312
|
+
};
|
|
313
|
+
type MapNarrowing = {
|
|
314
|
+
models: Record<string, ModelNarrowing>;
|
|
315
|
+
};
|
|
316
|
+
type LensNarrowing = {
|
|
317
|
+
parent: Lens | LensNarrowing;
|
|
318
|
+
maps: Record<string, MapNarrowing>;
|
|
319
|
+
constrains?: Condition;
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
declare const applyLens: (rule: Condition, narrowing: Lens | LensNarrowing) => Condition;
|
|
323
|
+
|
|
324
|
+
type RuleLensViolation = {
|
|
325
|
+
path: string;
|
|
326
|
+
reason: string;
|
|
327
|
+
};
|
|
328
|
+
type RuleLensCheck = {
|
|
329
|
+
ok: boolean;
|
|
330
|
+
violations: RuleLensViolation[];
|
|
331
|
+
};
|
|
332
|
+
declare const checkRuleAgainstLens: (rule: Condition, lensOrNarrowing: Lens | LensNarrowing) => RuleLensCheck;
|
|
333
|
+
|
|
334
|
+
type CreateLensInput = {
|
|
335
|
+
maps: Record<string, FieldMap>;
|
|
336
|
+
bridges?: Bridge[];
|
|
337
|
+
mapName: string;
|
|
338
|
+
model: string;
|
|
339
|
+
};
|
|
340
|
+
declare const createLens: (input: CreateLensInput) => Lens;
|
|
341
|
+
|
|
342
|
+
declare const validateNarrowing: (narrowing: LensNarrowing) => void;
|
|
343
|
+
|
|
344
|
+
declare const projectNarrowing: (lensOrNarrowing: Lens | LensNarrowing) => FieldMapSet;
|
|
345
|
+
|
|
346
|
+
declare const FieldKind: {
|
|
347
|
+
readonly String: "String";
|
|
348
|
+
readonly Boolean: "Boolean";
|
|
349
|
+
readonly Int: "Int";
|
|
350
|
+
readonly BigInt: "BigInt";
|
|
351
|
+
readonly Float: "Float";
|
|
352
|
+
readonly Decimal: "Decimal";
|
|
353
|
+
readonly DateTime: "DateTime";
|
|
354
|
+
readonly Json: "Json";
|
|
355
|
+
readonly Bytes: "Bytes";
|
|
356
|
+
readonly Enum: "Enum";
|
|
357
|
+
};
|
|
358
|
+
type FieldKind = (typeof FieldKind)[keyof typeof FieldKind];
|
|
359
|
+
declare const NUMERIC_KINDS: readonly FieldKind[];
|
|
360
|
+
declare const ORDERABLE_KINDS: readonly FieldKind[];
|
|
361
|
+
declare const STRINGY_KINDS: readonly FieldKind[];
|
|
362
|
+
declare const EQUATABLE_KINDS: readonly FieldKind[];
|
|
363
|
+
declare const ALL_KINDS: readonly FieldKind[];
|
|
364
|
+
declare const RuleTarget: {
|
|
365
|
+
readonly check: "check";
|
|
366
|
+
readonly toPrisma: "toPrisma";
|
|
367
|
+
readonly toSql: "toSql";
|
|
368
|
+
};
|
|
369
|
+
type RuleTarget = (typeof RuleTarget)[keyof typeof RuleTarget];
|
|
370
|
+
declare const ValueShape: {
|
|
371
|
+
readonly none: "none";
|
|
372
|
+
readonly scalar: "scalar";
|
|
373
|
+
readonly ordered: "ordered";
|
|
374
|
+
readonly array: "array";
|
|
375
|
+
readonly string: "string";
|
|
376
|
+
readonly pattern: "pattern";
|
|
377
|
+
readonly range: "range";
|
|
378
|
+
readonly dateValue: "dateValue";
|
|
379
|
+
readonly dateRange: "dateRange";
|
|
380
|
+
readonly dayList: "dayList";
|
|
381
|
+
readonly count: "count";
|
|
382
|
+
readonly predicate: "predicate";
|
|
383
|
+
};
|
|
384
|
+
type ValueShape = (typeof ValueShape)[keyof typeof ValueShape];
|
|
385
|
+
type CatalogEntry = {
|
|
386
|
+
kinds: readonly FieldKind[];
|
|
387
|
+
targets: readonly RuleTarget[];
|
|
388
|
+
valueShape: ValueShape;
|
|
389
|
+
};
|
|
390
|
+
declare const FIELD_OPERATOR_CATALOG: Record<Operator, CatalogEntry>;
|
|
391
|
+
declare const DATE_OPERATOR_CATALOG: Record<DateOperator, CatalogEntry>;
|
|
392
|
+
type ArrayCatalogEntry = {
|
|
393
|
+
targets: readonly RuleTarget[];
|
|
394
|
+
valueShape: ValueShape;
|
|
395
|
+
};
|
|
396
|
+
declare const ARRAY_OPERATOR_CATALOG: Record<ArrayOperator, ArrayCatalogEntry>;
|
|
397
|
+
declare const AGGREGATE_OPERATORS: readonly Operator[];
|
|
398
|
+
declare const isAggregateSingleOperator: (operator: Operator) => boolean;
|
|
399
|
+
declare const isAggregateRangeOperator: (operator: Operator) => boolean;
|
|
400
|
+
declare const getValueShape: (operator: Operator | DateOperator | ArrayOperator) => ValueShape;
|
|
401
|
+
declare const isOperatorSupportedForTarget: (operator: Operator | DateOperator | ArrayOperator, target: RuleTarget) => boolean;
|
|
402
|
+
declare const getOperatorsForKind: (kind: FieldKind, target?: RuleTarget) => {
|
|
403
|
+
field: Operator[];
|
|
404
|
+
date: DateOperator[];
|
|
405
|
+
};
|
|
406
|
+
declare const getArrayOperators: (target?: RuleTarget) => ArrayOperator[];
|
|
407
|
+
|
|
257
408
|
/**
|
|
258
409
|
* Execute a Prisma query plan produced by toPrisma().
|
|
259
410
|
*
|
|
@@ -321,42 +472,8 @@ type SqlBuildOptions = {
|
|
|
321
472
|
alias?: string;
|
|
322
473
|
context?: Record<string, unknown>;
|
|
323
474
|
};
|
|
324
|
-
/**
|
|
325
|
-
* Convert a json-rules Condition to a PostgreSQL WHERE clause.
|
|
326
|
-
*
|
|
327
|
-
* @param condition - The rule condition to convert
|
|
328
|
-
* @param options - Optional map/model/alias for JOIN generation; context for path refs
|
|
329
|
-
* @returns Object with `sql`, `params`, and `joins` (LEFT JOIN clauses)
|
|
330
|
-
*
|
|
331
|
-
* @example
|
|
332
|
-
* ```typescript
|
|
333
|
-
* // Simple field
|
|
334
|
-
* const { sql, params } = toSql({ field: 'status', operator: Operator.equals, value: 'active' });
|
|
335
|
-
* // sql: '"status" = $1'
|
|
336
|
-
*
|
|
337
|
-
* // Relation traversal with JOINs (map required)
|
|
338
|
-
* const { sql, params, joins } = toSql(
|
|
339
|
-
* { field: 'author.email', operator: Operator.equals, value: 'a@b.com' },
|
|
340
|
-
* { map, model: 'Post', alias: 't0' }
|
|
341
|
-
* );
|
|
342
|
-
* // sql: '"t1"."email" = $1'
|
|
343
|
-
* // joins: ['LEFT JOIN "User" AS "t1" ON "t1"."id" = "t0"."authorId"']
|
|
344
|
-
*
|
|
345
|
-
* // Same-record field comparison ($.field)
|
|
346
|
-
* const { sql: sql2 } = toSql({ field: 'endDate', operator: Operator.greaterThan, path: '$.startDate' });
|
|
347
|
-
* // sql2: '"endDate" > "startDate"'
|
|
348
|
-
*
|
|
349
|
-
* // External context ref
|
|
350
|
-
* const { sql: sql3 } = toSql(
|
|
351
|
-
* { field: 'userId', operator: Operator.equals, path: 'currentUser.id' },
|
|
352
|
-
* { context: { currentUser: { id: '123' } } }
|
|
353
|
-
* );
|
|
354
|
-
* // sql3: '"userId" = $1' params: ['123']
|
|
355
|
-
* ```
|
|
356
|
-
*/
|
|
357
475
|
declare const toSql: (condition: Condition, options?: SqlBuildOptions) => SqlResult;
|
|
358
476
|
|
|
359
|
-
type RuleValidationTarget = 'check' | 'toPrisma' | 'toSql';
|
|
360
477
|
type ValidationIssue = {
|
|
361
478
|
path: string;
|
|
362
479
|
message: string;
|
|
@@ -367,10 +484,10 @@ type ValidationResult = {
|
|
|
367
484
|
errors: ValidationIssue[];
|
|
368
485
|
};
|
|
369
486
|
declare const validateRule: (condition: unknown, options?: {
|
|
370
|
-
target?:
|
|
487
|
+
target?: RuleTarget;
|
|
371
488
|
}) => ValidationResult;
|
|
372
489
|
declare const assertValidRule: (condition: unknown, options?: {
|
|
373
|
-
target?:
|
|
490
|
+
target?: RuleTarget;
|
|
374
491
|
}) => asserts condition is Condition;
|
|
375
492
|
|
|
376
|
-
export { type AggregateMode, type AggregateRule, type All, type Any, ArrayOperator, type ArrayRule, type BuildOptions, type Condition, type DateInputValue, DateOperator, type DateRule, type DateRuleValue, type FieldMap, type FieldMapEntry, type GroupByStep, type IfThenElse, Operator, type OrderedRuleValue, type PrismaStep, type PrismaWhere, type Rule, type
|
|
493
|
+
export { AGGREGATE_OPERATORS, ALL_KINDS, ARRAY_OPERATOR_CATALOG, type AggregateMode, type AggregateRule, type All, type Any, type ArrayCatalogEntry, ArrayOperator, type ArrayRule, type Bridge, type BridgeCardinality, type BridgeDictionary, type BridgeEndpoint, type BuildOptions, type CatalogEntry, type CheckOptions, type Condition, type CreateLensInput, DATE_OPERATOR_CATALOG, type DateInputValue, DateOperator, type DateRule, type DateRuleValue, EQUATABLE_KINDS, FIELD_OPERATOR_CATALOG, FieldKind, type FieldMap, type FieldMapEntry, type FieldMapSet, type GroupByStep, type IfThenElse, type Lens, type LensNarrowing, type MapNarrowing, type ModelNarrowing, NUMERIC_KINDS, ORDERABLE_KINDS, Operator, type OrderedRuleValue, type PrismaStep, type PrismaWhere, type Rule, type RuleLensCheck, type RuleLensViolation, type RuleScalar, RuleTarget, type RuleValue, STRINGY_KINDS, type SqlResult, type StepRef, type StrictAggregateRule, type StrictAll, type StrictAny, type StrictArrayCountRule, type StrictArrayPredicateRule, type StrictArrayPresenceRule, type StrictArrayRule, type StrictCondition, type StrictContainsRule, type StrictDateComparisonRule, type StrictDateDayRule, type StrictDateRangeRule, type StrictDateRule, type StrictEqualityRule, type StrictIfThenElse, type StrictMembershipRule, type StrictOrderedComparisonRule, type StrictPatternRule, type StrictPresenceRule, type StrictRangeRule, type StrictRule, type StrictStringBoundaryRule, type ToPrismaResult, type ValidationIssue, type ValidationResult, ValueShape, type WhereStep, applyLens, assertValidRule, buildBridgeDictionary, check, checkRuleAgainstLens, createLens, executePrismaQueryPlan, getArrayOperators, getOperatorsForKind, getValueShape, isAggregateRangeOperator, isAggregateSingleOperator, isOperatorSupportedForTarget, projectNarrowing, stitchFieldMaps, toPrisma, toSql, validateFieldMap, validateFieldMapSet, validateNarrowing, validateRule };
|