@inixiative/json-rules 3.1.0 → 3.2.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/README.md +92 -23
- package/dist/index.cjs +3 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +250 -153
- package/dist/index.d.ts +250 -153
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.cts
CHANGED
|
@@ -140,6 +140,107 @@ type FuzzyConfig = {
|
|
|
140
140
|
maxRatio?: number;
|
|
141
141
|
};
|
|
142
142
|
|
|
143
|
+
type Lens = FieldMapSet & {
|
|
144
|
+
mapName: string;
|
|
145
|
+
model: string;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Narrowing applied wherever a model appears (intrinsic to the model).
|
|
149
|
+
* Has no `relations` because relations are path-specific by definition.
|
|
150
|
+
*
|
|
151
|
+
* Two kinds of narrowing live here:
|
|
152
|
+
* - SCHEMA narrowing (picks/omits/enumPicks/enumOmits): controls what's visible
|
|
153
|
+
* in the type surface. AI/SDK consumers can't see narrowed-away fields.
|
|
154
|
+
* - DATA narrowing (where): controls which ROWS are in scope. Filter-first
|
|
155
|
+
* semantic, anchored to the model. Under arrayOperator: 'all', it becomes the window filter
|
|
156
|
+
* (filter-first) — see narrowRule.
|
|
157
|
+
*/
|
|
158
|
+
type ModelDefaultNarrowing = {
|
|
159
|
+
picks?: string[];
|
|
160
|
+
omits?: string[];
|
|
161
|
+
enumPicks?: Record<string, readonly string[]>;
|
|
162
|
+
enumOmits?: Record<string, readonly string[]>;
|
|
163
|
+
/**
|
|
164
|
+
* Row-level filter anchored to this model — "from what you can see, this is true."
|
|
165
|
+
* Composes via filter-first semantic at every visit of this model.
|
|
166
|
+
*/
|
|
167
|
+
where?: Condition;
|
|
168
|
+
/**
|
|
169
|
+
* Per-field eligibility over THIS model — decorates a field's option picker.
|
|
170
|
+
* A bare `Condition` is the eligibility `where`: the field's selectable values =
|
|
171
|
+
* DISTINCT(field) over this model filtered by `where` (plus the model's own
|
|
172
|
+
* narrowing). A `SourceSpec` adds an optional `label` — a sibling column, or a
|
|
173
|
+
* dotted to-one path ending on a scalar (like `groupBy`), co-selected as each
|
|
174
|
+
* value's display label. Referenced-model option sets need no special form: declare
|
|
175
|
+
* the source at a relation-traversed narrowing node and it compiles over whatever
|
|
176
|
+
* model that path resolves to. The `where` composes AND-only across layers (general
|
|
177
|
+
* via `mapDefaults`, path-specific via `root`/`relations`); a later layer's `label` wins.
|
|
178
|
+
*/
|
|
179
|
+
sources?: Record<string, SourceEntry>;
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* A sourced field's eligibility `where` plus an optional display-label column — a
|
|
183
|
+
* sibling, or a dotted to-one path ending on a scalar, resolved exactly like a
|
|
184
|
+
* `groupBy` axis — and an optional `groupBy`: a dotted path (to-one hops only, ending
|
|
185
|
+
* on a scalar) whose value partitions the option set. Grouped options carry `group`;
|
|
186
|
+
* the classic flat set is the ungrouped case. At least one key is required — `{}` is not a
|
|
187
|
+
* Condition; the unconstrained spelling is `true`.
|
|
188
|
+
*/
|
|
189
|
+
type SourceSpec = {
|
|
190
|
+
where: Condition;
|
|
191
|
+
label?: string;
|
|
192
|
+
groupBy?: string | string[];
|
|
193
|
+
from?: never;
|
|
194
|
+
} | {
|
|
195
|
+
where?: Condition;
|
|
196
|
+
label: string;
|
|
197
|
+
groupBy?: string | string[];
|
|
198
|
+
from?: never;
|
|
199
|
+
} | {
|
|
200
|
+
where?: Condition;
|
|
201
|
+
label?: string;
|
|
202
|
+
groupBy: string | string[];
|
|
203
|
+
from?: never;
|
|
204
|
+
}
|
|
205
|
+
/** A path source that offers its model's own source — `mapDefaults[map].models[model]
|
|
206
|
+
* .sources[field]` for the map and model this path reaches — instead of the rows reachable
|
|
207
|
+
* down the path. Its label and axes are the model source's; its `where` can only narrow. */
|
|
208
|
+
| {
|
|
209
|
+
from: 'mapDefaults';
|
|
210
|
+
where?: Condition;
|
|
211
|
+
label?: never;
|
|
212
|
+
groupBy?: never;
|
|
213
|
+
};
|
|
214
|
+
/** A `sources` entry: a bare eligibility `Condition`, or a richer `SourceSpec`. */
|
|
215
|
+
type SourceEntry = Condition | SourceSpec;
|
|
216
|
+
/** Narrowing for a model at a specific traversal path. Adds relations to the default shape. */
|
|
217
|
+
type ModelNarrowing = ModelDefaultNarrowing & {
|
|
218
|
+
relations?: Record<string, ModelNarrowing>;
|
|
219
|
+
};
|
|
220
|
+
/** Narrowing for an enum type (applies anywhere the enum is referenced). */
|
|
221
|
+
type EnumNarrowing = {
|
|
222
|
+
picks?: readonly string[];
|
|
223
|
+
omits?: readonly string[];
|
|
224
|
+
};
|
|
225
|
+
/** Applies-everywhere narrowings for one map — per-model (no relations) + per-enum-type. */
|
|
226
|
+
type NarrowingDefaults = {
|
|
227
|
+
models?: Record<string, ModelDefaultNarrowing>;
|
|
228
|
+
enums?: Record<string, EnumNarrowing>;
|
|
229
|
+
};
|
|
230
|
+
type LensNarrowing = {
|
|
231
|
+
parent: Lens | LensNarrowing;
|
|
232
|
+
/**
|
|
233
|
+
* Path-specific narrowing anchored at (lens.mapName, lens.model). Descends via
|
|
234
|
+
* `.relations` and may cross maps through bridge relations.
|
|
235
|
+
*/
|
|
236
|
+
root?: ModelNarrowing;
|
|
237
|
+
/**
|
|
238
|
+
* Per-map applies-everywhere narrowings, keyed by map name. Apply wherever the
|
|
239
|
+
* named model/enum appears in the visit being resolved.
|
|
240
|
+
*/
|
|
241
|
+
mapDefaults?: Record<string, NarrowingDefaults>;
|
|
242
|
+
};
|
|
243
|
+
|
|
143
244
|
declare const FieldKind: {
|
|
144
245
|
readonly String: "String";
|
|
145
246
|
readonly Boolean: "Boolean";
|
|
@@ -179,12 +280,17 @@ declare const ValueShape: {
|
|
|
179
280
|
type ValueShape = (typeof ValueShape)[keyof typeof ValueShape];
|
|
180
281
|
/** Which catalog an operator belongs to — `between` is both a field and a date operator. */
|
|
181
282
|
type OperatorFamily = 'field' | 'date' | 'array';
|
|
283
|
+
/** The operand an operator takes in its family (`between` is a field and a date operator).
|
|
284
|
+
* Throws on an operator the family doesn't have. */
|
|
182
285
|
declare const getValueShape: (operator: string, family: OperatorFamily) => ValueShape;
|
|
286
|
+
/** The field and date operators a field kind takes, narrowed to one target when given. */
|
|
183
287
|
declare const getOperatorsForKind: (kind: FieldKind, target?: RuleTarget) => {
|
|
184
288
|
field: Operator[];
|
|
185
289
|
date: DateOperator[];
|
|
186
290
|
};
|
|
291
|
+
/** The array operators, narrowed to one target when given. */
|
|
187
292
|
declare const getArrayOperators: (target?: RuleTarget) => ArrayOperator[];
|
|
293
|
+
/** The comparisons an aggregate rule takes; every target compiles all of them. */
|
|
188
294
|
declare const getAggregateOperators: () => readonly Operator[];
|
|
189
295
|
|
|
190
296
|
type OperatorValues = typeof Operator;
|
|
@@ -403,11 +509,14 @@ type StrictCondition<TRuleValue = RuleValue, TDateValue = DateRuleValue> = Stric
|
|
|
403
509
|
/** A row as a rule reads it: a record of fields. */
|
|
404
510
|
type Row = Record<string, unknown>;
|
|
405
511
|
/** What both compilers take: the schema (a FieldMap, or a FieldMapSet with `mapName`), the
|
|
406
|
-
* model the rule reads, the context
|
|
512
|
+
* model the rule reads, the context bare `path` refs read, and the clock. `lens` compiles the
|
|
513
|
+
* rule under a lens instead — narrowed (`narrowRule`), against the base lens's maps, map and
|
|
514
|
+
* model — and can't be passed with `map` / `mapName` / `model`. */
|
|
407
515
|
type CompileOptions = {
|
|
408
516
|
map?: FieldMap | FieldMapSet;
|
|
409
517
|
mapName?: string;
|
|
410
518
|
model?: string;
|
|
519
|
+
lens?: Lens | LensNarrowing;
|
|
411
520
|
context?: Row;
|
|
412
521
|
} & DateConfig;
|
|
413
522
|
/** What check() evaluates: one row, or a root array of them. */
|
|
@@ -434,6 +543,9 @@ type CheckOptions = {
|
|
|
434
543
|
context?: CheckData;
|
|
435
544
|
bindings?: Record<string, RuleValue>;
|
|
436
545
|
} & DateConfig;
|
|
546
|
+
/** Evaluate a rule against one row, or a root array of rows (fieldless array rules under
|
|
547
|
+
* `all` / `any` only): `true` when it holds, else the failing rule's error text. Throws on a
|
|
548
|
+
* structurally invalid rule or malformed data. */
|
|
437
549
|
declare const check: <TData extends CheckData>(conditions: Condition, data: TData, options?: CheckOptions) => boolean | string;
|
|
438
550
|
|
|
439
551
|
type PrismaProvider = 'postgresql' | 'mysql' | 'sqlite' | 'sqlserver' | 'cockroachdb' | 'mongodb';
|
|
@@ -454,6 +566,8 @@ type EngineGlobalsState = {
|
|
|
454
566
|
type DeepPartial<T> = {
|
|
455
567
|
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K];
|
|
456
568
|
};
|
|
569
|
+
/** Process-wide defaults, read and written by dotted path (`string.caseInsensitive`,
|
|
570
|
+
* `string.fuzzy`, `prismaOptions.datasource.provider`, `prismaOptions.anyNull`). */
|
|
457
571
|
declare const engineGlobals: {
|
|
458
572
|
set: (path: string, value: unknown) => void;
|
|
459
573
|
get: (path: string) => unknown;
|
|
@@ -464,8 +578,14 @@ declare const engineGlobals: {
|
|
|
464
578
|
type BridgeDictionary = Record<string, // map name
|
|
465
579
|
Record<string, // model name
|
|
466
580
|
Record<string, Record<string, Row | Row[]>>>>;
|
|
581
|
+
/** Raw rows (keyed by endpoint, `<fieldMap>:<Model>`) indexed for embedding under bridge keys:
|
|
582
|
+
* map → model → `on` field → its value → the row, or the rows on a oneToMany's "many" side.
|
|
583
|
+
* Throws on a duplicate value where a side must be unique. */
|
|
467
584
|
declare const indexBridges: (set: FieldMapSet, rawData: Record<string, Row[]>) => BridgeDictionary;
|
|
468
585
|
|
|
586
|
+
/** A copy of the set where each bridge's endpoint models gain a `kind: 'bridge'` field named
|
|
587
|
+
* after the other endpoint (`<fieldMap>:<Model>`). Throws on a missing endpoint or `on` field, a
|
|
588
|
+
* self-bridge, or a bridge stitched twice. */
|
|
469
589
|
declare const stitchFieldMaps: (set: FieldMapSet) => FieldMapSet;
|
|
470
590
|
|
|
471
591
|
type ValidationIssue = {
|
|
@@ -481,114 +601,17 @@ type ValidationResult = {
|
|
|
481
601
|
type ValidateRuleOptions = {
|
|
482
602
|
target?: RuleTarget;
|
|
483
603
|
};
|
|
604
|
+
/** A rule's shape checked without data: every node well formed, and runnable on `target`
|
|
605
|
+
* (operators, windows, scope refs, patterns). */
|
|
484
606
|
declare const validateRule: (condition: unknown, options?: ValidateRuleOptions) => ValidationResult;
|
|
607
|
+
/** `validateRule`, throwing its issues; narrows the input to a `Condition`. */
|
|
485
608
|
declare const assertValidRule: (condition: unknown, options?: ValidateRuleOptions) => asserts condition is Condition;
|
|
486
609
|
|
|
487
610
|
/** Field names are plain identifiers: `.` walks a path and `:` names a bridge target. */
|
|
488
611
|
declare const validateFieldMaps: (set: FieldMapSet) => ValidationResult;
|
|
612
|
+
/** `validateFieldMaps`, throwing its issues. */
|
|
489
613
|
declare const assertValidFieldMaps: (set: FieldMapSet) => void;
|
|
490
614
|
|
|
491
|
-
type Lens = FieldMapSet & {
|
|
492
|
-
mapName: string;
|
|
493
|
-
model: string;
|
|
494
|
-
};
|
|
495
|
-
/**
|
|
496
|
-
* Narrowing applied wherever a model appears (intrinsic to the model).
|
|
497
|
-
* Has no `relations` because relations are path-specific by definition.
|
|
498
|
-
*
|
|
499
|
-
* Two kinds of narrowing live here:
|
|
500
|
-
* - SCHEMA narrowing (picks/omits/enumPicks/enumOmits): controls what's visible
|
|
501
|
-
* in the type surface. AI/SDK consumers can't see narrowed-away fields.
|
|
502
|
-
* - DATA narrowing (where): controls which ROWS are in scope. Filter-first
|
|
503
|
-
* semantic, anchored to the model. Under arrayOperator: 'all', it becomes the window filter
|
|
504
|
-
* (filter-first) — see narrowRule.
|
|
505
|
-
*/
|
|
506
|
-
type ModelDefaultNarrowing = {
|
|
507
|
-
picks?: string[];
|
|
508
|
-
omits?: string[];
|
|
509
|
-
enumPicks?: Record<string, readonly string[]>;
|
|
510
|
-
enumOmits?: Record<string, readonly string[]>;
|
|
511
|
-
/**
|
|
512
|
-
* Row-level filter anchored to this model — "from what you can see, this is true."
|
|
513
|
-
* Composes via filter-first semantic at every visit of this model.
|
|
514
|
-
*/
|
|
515
|
-
where?: Condition;
|
|
516
|
-
/**
|
|
517
|
-
* Per-field eligibility over THIS model — decorates a field's option picker.
|
|
518
|
-
* A bare `Condition` is the eligibility `where`: the field's selectable values =
|
|
519
|
-
* DISTINCT(field) over this model filtered by `where` (plus the model's own
|
|
520
|
-
* narrowing). A `SourceSpec` adds an optional `label` — a sibling column, or a
|
|
521
|
-
* dotted to-one path ending on a scalar (like `groupBy`), co-selected as each
|
|
522
|
-
* value's display label. Referenced-model option sets need no special form: declare
|
|
523
|
-
* the source at a relation-traversed narrowing node and it compiles over whatever
|
|
524
|
-
* model that path resolves to. The `where` composes AND-only across layers (general
|
|
525
|
-
* via `mapDefaults`, path-specific via `root`/`relations`); a later layer's `label` wins.
|
|
526
|
-
*/
|
|
527
|
-
sources?: Record<string, SourceEntry>;
|
|
528
|
-
};
|
|
529
|
-
/**
|
|
530
|
-
* A sourced field's eligibility `where` plus an optional display-label column — a
|
|
531
|
-
* sibling, or a dotted to-one path ending on a scalar, resolved exactly like a
|
|
532
|
-
* `groupBy` axis — and an optional `groupBy`: a dotted path (to-one hops only, ending
|
|
533
|
-
* on a scalar) whose value partitions the option set. Grouped options carry `group`;
|
|
534
|
-
* the classic flat set is the ungrouped case. At least one key is required — `{}` is not a
|
|
535
|
-
* Condition; the unconstrained spelling is `true`.
|
|
536
|
-
*/
|
|
537
|
-
type SourceSpec = {
|
|
538
|
-
where: Condition;
|
|
539
|
-
label?: string;
|
|
540
|
-
groupBy?: string | string[];
|
|
541
|
-
from?: never;
|
|
542
|
-
} | {
|
|
543
|
-
where?: Condition;
|
|
544
|
-
label: string;
|
|
545
|
-
groupBy?: string | string[];
|
|
546
|
-
from?: never;
|
|
547
|
-
} | {
|
|
548
|
-
where?: Condition;
|
|
549
|
-
label?: string;
|
|
550
|
-
groupBy: string | string[];
|
|
551
|
-
from?: never;
|
|
552
|
-
}
|
|
553
|
-
/** A path source that offers its model's own source — `mapDefaults[map].models[model]
|
|
554
|
-
* .sources[field]` for the map and model this path reaches — instead of the rows reachable
|
|
555
|
-
* down the path. Its label and axes are the model source's; its `where` can only narrow. */
|
|
556
|
-
| {
|
|
557
|
-
from: 'mapDefaults';
|
|
558
|
-
where?: Condition;
|
|
559
|
-
label?: never;
|
|
560
|
-
groupBy?: never;
|
|
561
|
-
};
|
|
562
|
-
/** A `sources` entry: a bare eligibility `Condition`, or a richer `SourceSpec`. */
|
|
563
|
-
type SourceEntry = Condition | SourceSpec;
|
|
564
|
-
/** Narrowing for a model at a specific traversal path. Adds relations to the default shape. */
|
|
565
|
-
type ModelNarrowing = ModelDefaultNarrowing & {
|
|
566
|
-
relations?: Record<string, ModelNarrowing>;
|
|
567
|
-
};
|
|
568
|
-
/** Narrowing for an enum type (applies anywhere the enum is referenced). */
|
|
569
|
-
type EnumNarrowing = {
|
|
570
|
-
picks?: readonly string[];
|
|
571
|
-
omits?: readonly string[];
|
|
572
|
-
};
|
|
573
|
-
/** Applies-everywhere narrowings for one map — per-model (no relations) + per-enum-type. */
|
|
574
|
-
type NarrowingDefaults = {
|
|
575
|
-
models?: Record<string, ModelDefaultNarrowing>;
|
|
576
|
-
enums?: Record<string, EnumNarrowing>;
|
|
577
|
-
};
|
|
578
|
-
type LensNarrowing = {
|
|
579
|
-
parent: Lens | LensNarrowing;
|
|
580
|
-
/**
|
|
581
|
-
* Path-specific narrowing anchored at (lens.mapName, lens.model). Descends via
|
|
582
|
-
* `.relations` and may cross maps through bridge relations.
|
|
583
|
-
*/
|
|
584
|
-
root?: ModelNarrowing;
|
|
585
|
-
/**
|
|
586
|
-
* Per-map applies-everywhere narrowings, keyed by map name. Apply wherever the
|
|
587
|
-
* named model/enum appears in the visit being resolved.
|
|
588
|
-
*/
|
|
589
|
-
mapDefaults?: Record<string, NarrowingDefaults>;
|
|
590
|
-
};
|
|
591
|
-
|
|
592
615
|
/**
|
|
593
616
|
* Every bind name a lens (its whole narrowing chain) needs supplied to execute —
|
|
594
617
|
* `bindOptional` tokens are not required (unsupplied, they resolve to null).
|
|
@@ -600,14 +623,21 @@ type LensNarrowing = {
|
|
|
600
623
|
declare const listLensBindings: (lensOrNarrowing: Lens | LensNarrowing) => string[];
|
|
601
624
|
/**
|
|
602
625
|
* Preprocess a lens: resolve every `{ bind }` token the map covers in the chain's
|
|
603
|
-
* `where`/`sources`, returning a structurally-new lens with concrete
|
|
604
|
-
* Partial — uncovered tokens stay, so stages bind progressively. Once resolved,
|
|
605
|
-
*
|
|
606
|
-
*
|
|
607
|
-
* value as the ancestor's `name`. Does not mutate the input.
|
|
626
|
+
* `where`/`sources`, returning a structurally-new lens of the same form with concrete
|
|
627
|
+
* conditions. Partial — uncovered tokens stay, so stages bind progressively. Once resolved, `narrowRule` / `toPrisma` / `toSql` / `toSourceQueries` / `projectLens` consume the
|
|
628
|
+
* lens unchanged. `parent:name` draws the same value as the ancestor's `name`. Does not mutate
|
|
629
|
+
* the input.
|
|
608
630
|
*/
|
|
609
|
-
declare const bindLens:
|
|
631
|
+
declare const bindLens: <T extends Lens | LensNarrowing>(lensOrNarrowing: T, bindings: Record<string, RuleValue>) => T;
|
|
632
|
+
|
|
633
|
+
/** The base lens a narrowing chain is rooted at; a lens is its own. Throws on a cyclic chain. */
|
|
634
|
+
declare const getLensRoot: (x: Lens | LensNarrowing) => Lens;
|
|
610
635
|
|
|
636
|
+
/** Stamp `coerceType` onto every field rule from the lens's field map: the rule carries its
|
|
637
|
+
* coercion; `check()` never infers types from values. A relation node's condition / filter stamp
|
|
638
|
+
* against the relation's model; below a Json boundary the kind is undeclared, so nothing is
|
|
639
|
+
* stamped. A date rule, an aggregate comparison (numeric by contract) and a rule that already
|
|
640
|
+
* names its coercion are left as they are. */
|
|
611
641
|
declare const coerceRule: (condition: Condition, lensOrNarrowing: Lens | LensNarrowing) => Condition;
|
|
612
642
|
|
|
613
643
|
/** A lens over field maps, its bridges stitched into the maps as fields. */
|
|
@@ -620,6 +650,9 @@ type RuleDescription = {
|
|
|
620
650
|
/** What the lens refuses in the rule — validateRuleInLens's issues. */
|
|
621
651
|
errors: ValidationIssue[];
|
|
622
652
|
};
|
|
653
|
+
/** A rule under a lens, for routing and UX: the maps it reads, whether it crosses a bridge, the
|
|
654
|
+
* targets `validateRule` passes it for (a bridge leaves `check` alone), and the lens gate's
|
|
655
|
+
* issues. `validateRuleInLens` stays the gate. */
|
|
623
656
|
declare const describeRule: (rule: Condition, lensOrNarrowing: Lens | LensNarrowing) => RuleDescription;
|
|
624
657
|
|
|
625
658
|
/**
|
|
@@ -660,6 +693,79 @@ type RuleSourceDescription = {
|
|
|
660
693
|
*/
|
|
661
694
|
declare const describeRuleSources: (rule: Condition, lensOrNarrowing: Lens | LensNarrowing) => RuleSourceDescription[];
|
|
662
695
|
|
|
696
|
+
type PrismaWhere = Record<string, unknown>;
|
|
697
|
+
type StepRef = {
|
|
698
|
+
__step: number;
|
|
699
|
+
};
|
|
700
|
+
type GroupByStep = {
|
|
701
|
+
operation: 'groupBy';
|
|
702
|
+
model: string;
|
|
703
|
+
args: {
|
|
704
|
+
by: string[];
|
|
705
|
+
where: Record<string, unknown>;
|
|
706
|
+
having: Record<string, unknown>;
|
|
707
|
+
};
|
|
708
|
+
extract: string;
|
|
709
|
+
};
|
|
710
|
+
type WhereStep = {
|
|
711
|
+
operation: 'where';
|
|
712
|
+
where: Record<string, unknown>;
|
|
713
|
+
};
|
|
714
|
+
type PrismaStep = GroupByStep | WhereStep;
|
|
715
|
+
type ToPrismaResult = {
|
|
716
|
+
steps: PrismaStep[];
|
|
717
|
+
};
|
|
718
|
+
type ToPrismaOptions = CompileOptions & {
|
|
719
|
+
datasource?: {
|
|
720
|
+
provider?: PrismaProvider;
|
|
721
|
+
};
|
|
722
|
+
};
|
|
723
|
+
|
|
724
|
+
/** A Prisma `select` tree: a column, or a relation with its own `select` and, to-many, `where`. */
|
|
725
|
+
type LensSelect = {
|
|
726
|
+
[field: string]: true | LensRelationSelect;
|
|
727
|
+
};
|
|
728
|
+
type LensRelationSelect = {
|
|
729
|
+
select?: LensSelect;
|
|
730
|
+
where?: PrismaWhere;
|
|
731
|
+
};
|
|
732
|
+
/** `rules`: the rules the rows will be re-checked with — each relation they read past the declared
|
|
733
|
+
* paths opens as a declared one. The rest is what a grant's compile reads: the clock and context,
|
|
734
|
+
* never a schema (the lens is it). */
|
|
735
|
+
type LensSelectOptions = Omit<ToPrismaOptions, 'map' | 'mapName' | 'model' | 'lens'> & {
|
|
736
|
+
rules?: readonly Condition[];
|
|
737
|
+
};
|
|
738
|
+
/** `keepGrantColumns`: keep the columns the lens's `where`s read, though it hides them. `rules`: as
|
|
739
|
+
* `toLensSelect`'s. The rest is what each `where` is checked with. */
|
|
740
|
+
type ProjectRowsOptions = CheckOptions & {
|
|
741
|
+
keepGrantColumns?: boolean;
|
|
742
|
+
rules?: readonly Condition[];
|
|
743
|
+
};
|
|
744
|
+
/**
|
|
745
|
+
* Prisma `findMany` args for the rows a lens shows, at its base model: each projected path's
|
|
746
|
+
* visible columns, the relations its declared paths open (a visible relation off them, its columns
|
|
747
|
+
* only), the relations `rules` read past them (opened as declared), and the columns every `where`
|
|
748
|
+
* on the way reads. A to-many relation carries its visit's grants compiled as its `where`, so
|
|
749
|
+
* related rows come pre-narrowed, unless a grant reads that list (it reads it whole); a to-one relation can't (Prisma
|
|
750
|
+
* takes no `where` there), so `projectRows` drops one its grant hides. The root's own grants are the
|
|
751
|
+
* query's `where`: `toPrisma(rule, { lens })`. Bridges are not selected. A relation grant that needs
|
|
752
|
+
* a counting step throws.
|
|
753
|
+
*/
|
|
754
|
+
declare const toLensSelect: (lensOrNarrowing: Lens | LensNarrowing, { rules, ...options }?: LensSelectOptions) => {
|
|
755
|
+
select: LensSelect;
|
|
756
|
+
};
|
|
757
|
+
/**
|
|
758
|
+
* Rows cut to what a lens shows, recursively from its base model: hidden columns and relations
|
|
759
|
+
* removed, and every row a visit's `where` hides gone — a root or list row dropped, a to-one row
|
|
760
|
+
* null. `keepGrantColumns` also keeps the columns those `where`s read (hidden or not), and a hidden to-one row, or a hidden row of a list a grant
|
|
761
|
+
* reads, as those columns alone, so a later `check(narrowRule(rule, lens), row)` re-tests the
|
|
762
|
+
* grants as the database does — for the rules passed in `rules`, or ones reading only the declared
|
|
763
|
+
* paths. Its output carries hidden values: it's for that re-check, never for
|
|
764
|
+
* a viewer. The rest of `options` is what each
|
|
765
|
+
* `where` is checked with (`now`, `bindings`). Plain JSON in and out; the input is not mutated.
|
|
766
|
+
*/
|
|
767
|
+
declare const projectRows: (lensOrNarrowing: Lens | LensNarrowing, rows: readonly Row[], { keepGrantColumns, rules, ...options }?: ProjectRowsOptions) => Row[];
|
|
768
|
+
|
|
663
769
|
type LensPathHop = {
|
|
664
770
|
field: string;
|
|
665
771
|
entry: FieldMapEntry;
|
|
@@ -720,34 +826,6 @@ type ProjectLensOptions = {
|
|
|
720
826
|
sourceValues?: readonly SourceValues[];
|
|
721
827
|
};
|
|
722
828
|
|
|
723
|
-
type PrismaWhere = Record<string, unknown>;
|
|
724
|
-
type StepRef = {
|
|
725
|
-
__step: number;
|
|
726
|
-
};
|
|
727
|
-
type GroupByStep = {
|
|
728
|
-
operation: 'groupBy';
|
|
729
|
-
model: string;
|
|
730
|
-
args: {
|
|
731
|
-
by: string[];
|
|
732
|
-
where: Record<string, unknown>;
|
|
733
|
-
having: Record<string, unknown>;
|
|
734
|
-
};
|
|
735
|
-
extract: string;
|
|
736
|
-
};
|
|
737
|
-
type WhereStep = {
|
|
738
|
-
operation: 'where';
|
|
739
|
-
where: Record<string, unknown>;
|
|
740
|
-
};
|
|
741
|
-
type PrismaStep = GroupByStep | WhereStep;
|
|
742
|
-
type ToPrismaResult = {
|
|
743
|
-
steps: PrismaStep[];
|
|
744
|
-
};
|
|
745
|
-
type ToPrismaOptions = CompileOptions & {
|
|
746
|
-
datasource?: {
|
|
747
|
-
provider?: PrismaProvider;
|
|
748
|
-
};
|
|
749
|
-
};
|
|
750
|
-
|
|
751
829
|
/** Prisma `select` shape — nested for a grouped source's relation path. */
|
|
752
830
|
type SourceSelect = {
|
|
753
831
|
[field: string]: true | {
|
|
@@ -791,14 +869,20 @@ type SourceQuery = {
|
|
|
791
869
|
/**
|
|
792
870
|
* Compile a DISTINCT(value) query — Prisma and SQL — per sourced field across
|
|
793
871
|
* the projected lens. The WHERE is the field's composed eligibility: the model's
|
|
794
|
-
* own narrowing at that path
|
|
795
|
-
*
|
|
796
|
-
*
|
|
872
|
+
* own narrowing at that path, the grants above it carried down the path, its source
|
|
873
|
+
* where(s), the guards of the relations they cross and any allowed values. A
|
|
874
|
+
* `from: 'mapDefaults'` source reads the model's own source and carries no grant from
|
|
875
|
+
* its layer on. The app runs these (with its own client) to materialize each field's
|
|
876
|
+
* option set — feed the fetched rows to `materializeSourceQuery`.
|
|
797
877
|
*/
|
|
798
878
|
declare const toSourceQueries: (lensOrNarrowing: Lens | LensNarrowing) => SourceQuery[];
|
|
799
879
|
|
|
800
880
|
/** Which executor produced the rows — the caller always knows; never guessed. */
|
|
801
881
|
type SourceRowShape = 'prisma' | 'sql';
|
|
882
|
+
/** `rowShape`: how the rows came back — nested Prisma rows (the default) or flat SQL rows. */
|
|
883
|
+
type MaterializeSourceQueryOptions = {
|
|
884
|
+
rowShape?: SourceRowShape;
|
|
885
|
+
};
|
|
802
886
|
/**
|
|
803
887
|
* Materialize one compiled `SourceQuery`'s fetched rows into its `SourceValues` —
|
|
804
888
|
* the executor-side counterpart of `toSourceQueries`, so apps never hand-map rows.
|
|
@@ -807,10 +891,6 @@ type SourceRowShape = 'prisma' | 'sql';
|
|
|
807
891
|
* statement's `__group_i` / `__label` aliases. Grouped queries fetch without
|
|
808
892
|
* DISTINCT, so dedup per (groups, value) happens here.
|
|
809
893
|
*/
|
|
810
|
-
/** `rowShape`: how the rows came back — nested Prisma rows (the default) or flat SQL rows. */
|
|
811
|
-
type MaterializeSourceQueryOptions = {
|
|
812
|
-
rowShape?: SourceRowShape;
|
|
813
|
-
};
|
|
814
894
|
declare const materializeSourceQuery: (query: SourceQuery, rows: readonly Row[], opts?: MaterializeSourceQueryOptions) => SourceValues;
|
|
815
895
|
|
|
816
896
|
/**
|
|
@@ -818,18 +898,26 @@ declare const materializeSourceQuery: (query: SourceQuery, rows: readonly Row[],
|
|
|
818
898
|
* the in-memory executor of `sources` declarations, alongside `toSourceQueries`
|
|
819
899
|
* (which compiles the same declarations to DISTINCT queries for a DB). Rows are
|
|
820
900
|
* the collection fetched under the lens (relations inline). Each row must meet the field's
|
|
821
|
-
* eligibility
|
|
822
|
-
*
|
|
823
|
-
*
|
|
824
|
-
* option per element, labels take the first non-null value of the label column
|
|
901
|
+
* eligibility — its source `where`, the grants above it, the guards of the relations it crosses
|
|
902
|
+
* and any allowed values — evaluated with `check()` (`options` feeds `{bind}` clauses); its own
|
|
903
|
+
* visit's `where` is not re-applied, since the rows were fetched under it. Scalar-list fields
|
|
904
|
+
* contribute one option per element, labels take the first non-null value of the label column
|
|
825
905
|
* (a sibling, or a dotted to-one path read through the nested rows), and sorting is
|
|
826
|
-
* numeric-aware in a fixed locale. Feed the result to `projectLens` as `{ sourceValues }`.
|
|
906
|
+
* numeric-aware in a fixed locale. Feed the result to `projectLens` as `{ sourceValues }`. A
|
|
907
|
+
* `from: 'mapDefaults'` source throws: a fetched collection can't hold unlinked rows.
|
|
827
908
|
*/
|
|
828
909
|
declare const materializeSources: (lensOrNarrowing: Lens | LensNarrowing, rows: readonly Row[], options?: CheckOptions) => SourceValues[];
|
|
829
910
|
|
|
911
|
+
/** A narrowing layer checked against the layers above it: it names only what they still show and
|
|
912
|
+
* only narrows. Each problem is an issue with a code (`not_in_lens`, `not_visible`,
|
|
913
|
+
* `conflicting_selection`, `wrong_kind`, `value_not_allowed`, `invalid_source`,
|
|
914
|
+
* `invalid_binding`, or the lens gate's for a `where`). */
|
|
830
915
|
declare const validateNarrowing: (narrowing: LensNarrowing) => ValidationResult;
|
|
916
|
+
/** `validateNarrowing`, throwing its issues. */
|
|
831
917
|
declare const assertValidNarrowing: (narrowing: LensNarrowing) => void;
|
|
832
918
|
|
|
919
|
+
/** A rule with the lens's grants (`where`s) injected at their anchors: the root's around it, each
|
|
920
|
+
* relation's where the rule descends into it — under an `all`, into its window `filter`. */
|
|
833
921
|
declare const narrowRule: (rule: Condition, lensOrNarrowing: Lens | LensNarrowing) => Condition;
|
|
834
922
|
|
|
835
923
|
/**
|
|
@@ -870,7 +958,7 @@ declare const validateRuleInLens: (rule: Condition, lensOrNarrowing: Lens | Lens
|
|
|
870
958
|
|
|
871
959
|
/**
|
|
872
960
|
* Resolve one dotted path through a lens, hop by hop, verifying as it walks: every hop is checked
|
|
873
|
-
* against the narrowing at that visit, so a
|
|
961
|
+
* against the narrowing at that visit, so a field the narrowing hides is `hidden`, a field the
|
|
874
962
|
* model lacks is `missing`, and a segment past a scalar is `pastScalar`. This is the walk
|
|
875
963
|
* `validateRuleInLens` gates a rule's field with, exposed for consumers that resolve paths of their
|
|
876
964
|
* own (template tokens, loop bindings, presence guards).
|
|
@@ -881,6 +969,7 @@ type ScopeRef = {
|
|
|
881
969
|
depth: number;
|
|
882
970
|
path: string;
|
|
883
971
|
};
|
|
972
|
+
/** A `$`-prefixed ref's depth (one per `$`) and the path after it; `null` for a bare ref. */
|
|
884
973
|
declare const parseScopeRef: (ref: string) => ScopeRef | null;
|
|
885
974
|
type ScopedRef<S> = {
|
|
886
975
|
scope: S;
|
|
@@ -889,6 +978,9 @@ type ScopedRef<S> = {
|
|
|
889
978
|
type ScopeOutOfBounds = {
|
|
890
979
|
outOfBounds: string;
|
|
891
980
|
};
|
|
981
|
+
/** The scope a ref names in a stack (innermost last) and the path left to read in it: a bare
|
|
982
|
+
* ref and `$.` read the innermost, `$$.` the one above it, … A ref deeper than the stack comes
|
|
983
|
+
* back as `{ outOfBounds }` with its message; it never throws. */
|
|
892
984
|
declare const readScopeRef: <S>(ref: string, scopes: readonly S[]) => ScopedRef<S> | ScopeOutOfBounds;
|
|
893
985
|
|
|
894
986
|
/**
|
|
@@ -912,7 +1004,8 @@ declare const executePrismaPlan: (plan: ToPrismaResult, prismaDelegate: Record<s
|
|
|
912
1004
|
|
|
913
1005
|
/**
|
|
914
1006
|
* Compile a condition to a Prisma query plan: `steps`, any groupBy steps (counts and relation
|
|
915
|
-
* aggregates, which need `{ map, model }`) and then the final `where`.
|
|
1007
|
+
* aggregates, which need `{ map, model }` or `{ lens }`) and then the final `where`. With `lens`,
|
|
1008
|
+
* the rule compiles narrowed by it, against its base lens. Run a plan with
|
|
916
1009
|
* `executePrismaPlan(plan, client)` to resolve step refs; a single-step plan's `where` is its
|
|
917
1010
|
* last step's.
|
|
918
1011
|
*
|
|
@@ -924,9 +1017,11 @@ declare const executePrismaPlan: (plan: ToPrismaResult, prismaDelegate: Record<s
|
|
|
924
1017
|
* const plan = toPrisma({ field: 'posts', arrayOperator: 'atLeast', count: 3, condition }, { map, model: 'User' });
|
|
925
1018
|
* const where = await executePrismaPlan(plan, prisma);
|
|
926
1019
|
* await prisma.user.findMany({ where });
|
|
1020
|
+
*
|
|
1021
|
+
* toPrisma(rule, { lens: narrowing, now }); // toPrisma(narrowRule(rule, narrowing), { map: base, mapName, model, now })
|
|
927
1022
|
* ```
|
|
928
1023
|
*/
|
|
929
|
-
declare const toPrisma: (
|
|
1024
|
+
declare const toPrisma: (rule: Condition, compileOptions?: ToPrismaOptions) => ToPrismaResult;
|
|
930
1025
|
|
|
931
1026
|
type ToSqlResult = {
|
|
932
1027
|
sql: string;
|
|
@@ -938,6 +1033,8 @@ type ToSqlOptions = CompileOptions & {
|
|
|
938
1033
|
alias?: string;
|
|
939
1034
|
};
|
|
940
1035
|
|
|
941
|
-
|
|
1036
|
+
/** Compile a condition to a SQL WHERE. With `lens`, the rule compiles narrowed by it, against its
|
|
1037
|
+
* base lens. */
|
|
1038
|
+
declare const toSql: (rule: Condition, compileOptions?: ToSqlOptions) => ToSqlResult;
|
|
942
1039
|
|
|
943
|
-
export { ALL_KINDS, type AggregateMode, type AggregateRule, type All, type Any, ArrayOperator, type ArrayRule, type Bridge, type BridgeCardinality, type BridgeDictionary, type BridgeEndpoint, type CheckData, type CheckOptions, type CompileOptions, type Condition, type DateConfig, type DateExpr, type DateInputOrExpr, type DateInputValue, type DateOffset, DateOperator, type DateRule, type DateRuleValue, type EdgeExpr, type EngineGlobalsState, type EnumNarrowing, FieldKind, type FieldMap, type FieldMapEntry, type FieldMapSet, type FuzzyConfig, type GroupByStep, type IfThenElse, type Lens, type LensNarrowing, type LensPathHop, type LensPathResolution, type ListBindingsOptions, type Magnitude, type MaterializeSourceQueryOptions, type ModelDefaultNarrowing, type ModelEntry, type ModelNarrowing, NUMERIC_KINDS, type NarrowingDefaults, type NumberOffset, Operator, type OperatorFamily, type OrderBy, type OrderedRuleValue, type PathProjection, type PeriodExpr, type PeriodUnit, type PrismaProvider, type PrismaStep, type PrismaWhere, type ProjectLensOptions, type ProjectedVisit, type RelativeUnits, type RollingExpr, type Row, type Rule, type RuleDescription, type RuleScalar, type RuleSourceDescription, RuleTarget, type RuleValue, type ScopeOutOfBounds, type ScopeRef, type ScopedRef, type SortDir, type SourceEntry, type SourceOption, type SourcePrismaQuery, type SourceQuery, type SourceRowShape, type SourceSelect, type SourceSpec, type SourceSqlQuery, type SourceValues, type StepRef, type StoredLens, 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 TimeZoneConfig, type ToPrismaOptions, type ToPrismaResult, type ToSqlOptions, type ToSqlResult, type ValidateRuleOptions, type ValidationIssue, type ValidationResult, ValueShape, type ValueSourceFields, type ValueSourceOf, type WeekStart, type WhereStep, type WindowFields, assertValidFieldMaps, assertValidNarrowing, assertValidRule, bindLens, bindRule, check, coerceRule, composeLens, createLens, describeRule, describeRuleSources, engineGlobals, executePrismaPlan, getAggregateOperators, getArrayOperators, getOperatorsForKind, getValueShape, indexBridges, listBindings, listLensBindings, materializeSourceQuery, materializeSources, narrowRule, parseScopeRef, projectLens, readScopeRef, stitchFieldMaps, storeLens, toPrisma, toSourceQueries, toSql, validateFieldMaps, validateNarrowing, validateRule, validateRuleInLens, walkLensPath };
|
|
1040
|
+
export { ALL_KINDS, type AggregateMode, type AggregateRule, type All, type Any, ArrayOperator, type ArrayRule, type Bridge, type BridgeCardinality, type BridgeDictionary, type BridgeEndpoint, type CheckData, type CheckOptions, type CompileOptions, type Condition, type DateConfig, type DateExpr, type DateInputOrExpr, type DateInputValue, type DateOffset, DateOperator, type DateRule, type DateRuleValue, type EdgeExpr, type EngineGlobalsState, type EnumNarrowing, FieldKind, type FieldMap, type FieldMapEntry, type FieldMapSet, type FuzzyConfig, type GroupByStep, type IfThenElse, type Lens, type LensNarrowing, type LensPathHop, type LensPathResolution, type LensRelationSelect, type LensSelect, type LensSelectOptions, type ListBindingsOptions, type Magnitude, type MaterializeSourceQueryOptions, type ModelDefaultNarrowing, type ModelEntry, type ModelNarrowing, NUMERIC_KINDS, type NarrowingDefaults, type NumberOffset, Operator, type OperatorFamily, type OrderBy, type OrderedRuleValue, type PathProjection, type PeriodExpr, type PeriodUnit, type PrismaProvider, type PrismaStep, type PrismaWhere, type ProjectLensOptions, type ProjectRowsOptions, type ProjectedVisit, type RelativeUnits, type RollingExpr, type Row, type Rule, type RuleDescription, type RuleScalar, type RuleSourceDescription, RuleTarget, type RuleValue, type ScopeOutOfBounds, type ScopeRef, type ScopedRef, type SortDir, type SourceEntry, type SourceOption, type SourcePrismaQuery, type SourceQuery, type SourceRowShape, type SourceSelect, type SourceSpec, type SourceSqlQuery, type SourceValues, type StepRef, type StoredLens, 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 TimeZoneConfig, type ToPrismaOptions, type ToPrismaResult, type ToSqlOptions, type ToSqlResult, type ValidateRuleOptions, type ValidationIssue, type ValidationResult, ValueShape, type ValueSourceFields, type ValueSourceOf, type WeekStart, type WhereStep, type WindowFields, assertValidFieldMaps, assertValidNarrowing, assertValidRule, bindLens, bindRule, check, coerceRule, composeLens, createLens, describeRule, describeRuleSources, engineGlobals, executePrismaPlan, getAggregateOperators, getArrayOperators, getLensRoot, getOperatorsForKind, getValueShape, indexBridges, listBindings, listLensBindings, materializeSourceQuery, materializeSources, narrowRule, parseScopeRef, projectLens, projectRows, readScopeRef, stitchFieldMaps, storeLens, toLensSelect, toPrisma, toSourceQueries, toSql, validateFieldMaps, validateNarrowing, validateRule, validateRuleInLens, walkLensPath };
|