@latticecms/core 0.0.0 → 0.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 +1297 -2
- package/dist/access.d.ts +209 -0
- package/dist/access.d.ts.map +1 -0
- package/dist/access.js +241 -0
- package/dist/access.js.map +1 -0
- package/dist/build.d.ts +555 -0
- package/dist/build.d.ts.map +1 -0
- package/dist/build.js +655 -0
- package/dist/build.js.map +1 -0
- package/dist/collection.d.ts +480 -0
- package/dist/collection.d.ts.map +1 -0
- package/dist/collection.js +351 -0
- package/dist/collection.js.map +1 -0
- package/dist/crud.d.ts +46 -0
- package/dist/crud.d.ts.map +1 -0
- package/dist/crud.js +527 -0
- package/dist/crud.js.map +1 -0
- package/dist/describe.d.ts +89 -0
- package/dist/describe.d.ts.map +1 -0
- package/dist/describe.js +119 -0
- package/dist/describe.js.map +1 -0
- package/dist/errors.d.ts +183 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +150 -0
- package/dist/errors.js.map +1 -0
- package/dist/family.d.ts +80 -0
- package/dist/family.d.ts.map +1 -0
- package/dist/family.js +227 -0
- package/dist/family.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/marker.d.ts +15 -0
- package/dist/marker.d.ts.map +1 -0
- package/dist/marker.js +28 -0
- package/dist/marker.js.map +1 -0
- package/dist/mask.d.ts +69 -0
- package/dist/mask.d.ts.map +1 -0
- package/dist/mask.js +277 -0
- package/dist/mask.js.map +1 -0
- package/dist/populate.d.ts +39 -0
- package/dist/populate.d.ts.map +1 -0
- package/dist/populate.js +72 -0
- package/dist/populate.js.map +1 -0
- package/dist/relations.d.ts +144 -0
- package/dist/relations.d.ts.map +1 -0
- package/dist/relations.js +144 -0
- package/dist/relations.js.map +1 -0
- package/dist/repo.d.ts +99 -0
- package/dist/repo.d.ts.map +1 -0
- package/dist/repo.js +92 -0
- package/dist/repo.js.map +1 -0
- package/dist/sort.d.ts +86 -0
- package/dist/sort.d.ts.map +1 -0
- package/dist/sort.js +187 -0
- package/dist/sort.js.map +1 -0
- package/dist/store/conformance.d.ts +15 -0
- package/dist/store/conformance.d.ts.map +1 -0
- package/dist/store/conformance.js +811 -0
- package/dist/store/conformance.js.map +1 -0
- package/dist/store/index.d.ts +9 -0
- package/dist/store/index.d.ts.map +1 -0
- package/dist/store/index.js +8 -0
- package/dist/store/index.js.map +1 -0
- package/dist/store.d.ts +119 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +176 -0
- package/dist/store.js.map +1 -0
- package/dist/tenancy.d.ts +66 -0
- package/dist/tenancy.d.ts.map +1 -0
- package/dist/tenancy.js +69 -0
- package/dist/tenancy.js.map +1 -0
- package/dist/tracing.d.ts +12 -0
- package/dist/tracing.d.ts.map +1 -0
- package/dist/tracing.js +27 -0
- package/dist/tracing.js.map +1 -0
- package/dist/where.d.ts +95 -0
- package/dist/where.d.ts.map +1 -0
- package/dist/where.js +308 -0
- package/dist/where.js.map +1 -0
- package/package.json +80 -2
- package/skills/lattice/SKILL.md +106 -0
- package/skills/lattice/references/access.md +90 -0
- package/skills/lattice/references/cli.md +87 -0
- package/skills/lattice/references/collection.md +94 -0
- package/skills/lattice/references/event-handler.md +88 -0
- package/skills/lattice/references/masking.md +72 -0
- package/skills/lattice/references/method.md +88 -0
- package/skills/lattice/references/override.md +160 -0
- package/skills/lattice/references/relations.md +91 -0
- package/skills/lattice/references/tenancy.md +82 -0
- package/skills/lattice/references/testing.md +75 -0
- package/src/access.ts +449 -0
- package/src/build.ts +1888 -0
- package/src/collection.ts +1025 -0
- package/src/crud.ts +797 -0
- package/src/describe.ts +232 -0
- package/src/errors.ts +174 -0
- package/src/family.ts +334 -0
- package/src/index.ts +44 -0
- package/src/marker.ts +54 -0
- package/src/mask.ts +336 -0
- package/src/populate.ts +103 -0
- package/src/relations.ts +370 -0
- package/src/repo.ts +218 -0
- package/src/sort.ts +271 -0
- package/src/store/conformance.ts +1179 -0
- package/src/store/index.ts +22 -0
- package/src/store.ts +315 -0
- package/src/tenancy.ts +124 -0
- package/src/tracing.ts +29 -0
- package/src/where.ts +463 -0
package/src/where.ts
ADDED
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { refusedWhere } from './describe.js';
|
|
3
|
+
import { invalid } from './errors.js';
|
|
4
|
+
import { markOf } from './marker.js';
|
|
5
|
+
|
|
6
|
+
export type Kind = 'string' | 'number' | 'boolean' | 'enum' | 'array' | 'other';
|
|
7
|
+
|
|
8
|
+
const equality = ['string', 'number', 'boolean', 'enum'] as const;
|
|
9
|
+
const ordered = ['string', 'number'] as const;
|
|
10
|
+
|
|
11
|
+
/** The field kinds each operator applies to. `Where` and the validator both read it. */
|
|
12
|
+
const operators = {
|
|
13
|
+
eq: equality,
|
|
14
|
+
ne: equality,
|
|
15
|
+
in: equality,
|
|
16
|
+
nin: equality,
|
|
17
|
+
gt: ordered,
|
|
18
|
+
gte: ordered,
|
|
19
|
+
lt: ordered,
|
|
20
|
+
lte: ordered,
|
|
21
|
+
contains: ['string'],
|
|
22
|
+
// An array of strings, numbers or enum values: has one equal element.
|
|
23
|
+
has: ['array'],
|
|
24
|
+
exists: ['string', 'number', 'boolean', 'enum', 'array', 'other'],
|
|
25
|
+
} as const satisfies Record<string, readonly Kind[]>;
|
|
26
|
+
|
|
27
|
+
type Operator = keyof typeof operators;
|
|
28
|
+
|
|
29
|
+
/** A brand is type-only: a branded string is a string at runtime. */
|
|
30
|
+
type Unbrand<T> =
|
|
31
|
+
T extends z.core.$brand<infer Brand>
|
|
32
|
+
? T extends infer U & z.core.$brand<Brand>
|
|
33
|
+
? U
|
|
34
|
+
: T
|
|
35
|
+
: T;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A field's kind, from its value type. `kindOf` below makes the same
|
|
39
|
+
* decision from the schema: all strings, with an open `string` among them,
|
|
40
|
+
* is 'string'; only string or only number literals is 'enum'; and so on.
|
|
41
|
+
* The table test in where.test.ts checks both sides on every field form.
|
|
42
|
+
*/
|
|
43
|
+
export type KindOf<V> = [Unbrand<Exclude<V, null | undefined>>] extends [
|
|
44
|
+
infer B,
|
|
45
|
+
]
|
|
46
|
+
? [B] extends [never]
|
|
47
|
+
? 'other'
|
|
48
|
+
: [B] extends [boolean]
|
|
49
|
+
? 'boolean'
|
|
50
|
+
: [B] extends [string]
|
|
51
|
+
? string extends B
|
|
52
|
+
? 'string'
|
|
53
|
+
: 'enum'
|
|
54
|
+
: [B] extends [number]
|
|
55
|
+
? number extends B
|
|
56
|
+
? 'number'
|
|
57
|
+
: 'enum'
|
|
58
|
+
: [B] extends [readonly (infer E)[]]
|
|
59
|
+
? KindOf<E> extends 'string' | 'number' | 'enum'
|
|
60
|
+
? 'array'
|
|
61
|
+
: 'other'
|
|
62
|
+
: 'other'
|
|
63
|
+
: never;
|
|
64
|
+
|
|
65
|
+
type Operand<Op extends Operator, B> = Op extends 'eq' | 'ne'
|
|
66
|
+
? B | null
|
|
67
|
+
: Op extends 'in' | 'nin'
|
|
68
|
+
? readonly (B | null)[]
|
|
69
|
+
: Op extends 'contains'
|
|
70
|
+
? string
|
|
71
|
+
: Op extends 'exists'
|
|
72
|
+
? boolean
|
|
73
|
+
: Op extends 'has'
|
|
74
|
+
? B extends readonly (infer E)[]
|
|
75
|
+
? Exclude<E, null | undefined>
|
|
76
|
+
: never
|
|
77
|
+
: B;
|
|
78
|
+
|
|
79
|
+
/** The operators one field allows, typed from its value. */
|
|
80
|
+
export type FieldWhere<V> = {
|
|
81
|
+
readonly [Op in Operator as KindOf<V> extends (typeof operators)[Op][number]
|
|
82
|
+
? Op
|
|
83
|
+
: never]?: Operand<Op, Exclude<V, null | undefined>>;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* A filter on a collection's documents. Several fields, or several operators
|
|
88
|
+
* on one field, all have to match. `and` and `or` take one or more filters.
|
|
89
|
+
*/
|
|
90
|
+
export type Where<Doc> = {
|
|
91
|
+
readonly [K in keyof Doc]?: FieldWhere<Doc[K]>;
|
|
92
|
+
} & {
|
|
93
|
+
readonly and?: readonly Where<Doc>[];
|
|
94
|
+
readonly or?: readonly Where<Doc>[];
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* A wrapper that keeps its inner schema's values (optional, nullable,
|
|
99
|
+
* default, prefault, nonoptional, readonly, catch): its inner schema, and the
|
|
100
|
+
* same wrapper around another one. Undefined for any other schema.
|
|
101
|
+
*/
|
|
102
|
+
export function wrapperOf(schema: z.core.$ZodType):
|
|
103
|
+
| {
|
|
104
|
+
readonly inner: z.core.$ZodType;
|
|
105
|
+
readonly around: (inner: z.core.$ZodType) => z.core.$ZodType;
|
|
106
|
+
}
|
|
107
|
+
| undefined {
|
|
108
|
+
if (
|
|
109
|
+
schema instanceof z.core.$ZodOptional ||
|
|
110
|
+
schema instanceof z.core.$ZodNullable ||
|
|
111
|
+
schema instanceof z.core.$ZodDefault ||
|
|
112
|
+
schema instanceof z.core.$ZodPrefault ||
|
|
113
|
+
schema instanceof z.core.$ZodNonOptional ||
|
|
114
|
+
schema instanceof z.core.$ZodReadonly ||
|
|
115
|
+
schema instanceof z.core.$ZodCatch
|
|
116
|
+
) {
|
|
117
|
+
const { def } = schema._zod;
|
|
118
|
+
return {
|
|
119
|
+
inner: def.innerType,
|
|
120
|
+
around: (inner) =>
|
|
121
|
+
z.core.util.clone(schema, { ...def, innerType: inner }),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The schema a wrapper keeps the values of, or undefined for any other schema. */
|
|
128
|
+
export function innerOf(schema: z.core.$ZodType): z.core.$ZodType | undefined {
|
|
129
|
+
return wrapperOf(schema)?.inner;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function unwrap(schema: z.core.$ZodType): z.core.$ZodType {
|
|
133
|
+
const inner = innerOf(schema);
|
|
134
|
+
return inner === undefined ? schema : unwrap(inner);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
type Atom =
|
|
138
|
+
| 'string'
|
|
139
|
+
| 'stringLiteral'
|
|
140
|
+
| 'number'
|
|
141
|
+
| 'numberLiteral'
|
|
142
|
+
| 'boolean'
|
|
143
|
+
| 'null'
|
|
144
|
+
| 'undefined'
|
|
145
|
+
| 'other';
|
|
146
|
+
|
|
147
|
+
function valueAtoms(value: unknown): Atom[] {
|
|
148
|
+
switch (typeof value) {
|
|
149
|
+
case 'string':
|
|
150
|
+
return ['stringLiteral'];
|
|
151
|
+
case 'number':
|
|
152
|
+
return ['numberLiteral'];
|
|
153
|
+
case 'boolean':
|
|
154
|
+
return ['boolean'];
|
|
155
|
+
case 'undefined':
|
|
156
|
+
return ['undefined'];
|
|
157
|
+
default:
|
|
158
|
+
return value === null ? ['null'] : ['other'];
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const notUndefined = (atom: Atom) => atom !== 'undefined';
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The kinds of value a schema's output can hold. Each wrapper changes them
|
|
166
|
+
* the way it changes the output type: optional adds undefined, nullable adds
|
|
167
|
+
* null, default, prefault and nonoptional remove undefined.
|
|
168
|
+
*/
|
|
169
|
+
function atoms(schema: z.core.$ZodType): Atom[] {
|
|
170
|
+
if (schema instanceof z.core.$ZodOptional) {
|
|
171
|
+
return ['undefined', ...atoms(schema._zod.def.innerType)];
|
|
172
|
+
}
|
|
173
|
+
if (schema instanceof z.core.$ZodNullable) {
|
|
174
|
+
return ['null', ...atoms(schema._zod.def.innerType)];
|
|
175
|
+
}
|
|
176
|
+
if (
|
|
177
|
+
schema instanceof z.core.$ZodDefault ||
|
|
178
|
+
schema instanceof z.core.$ZodPrefault ||
|
|
179
|
+
schema instanceof z.core.$ZodNonOptional
|
|
180
|
+
) {
|
|
181
|
+
return atoms(schema._zod.def.innerType).filter(notUndefined);
|
|
182
|
+
}
|
|
183
|
+
const inner = innerOf(schema);
|
|
184
|
+
if (inner !== undefined) return atoms(inner);
|
|
185
|
+
if (schema instanceof z.core.$ZodUnion) {
|
|
186
|
+
return schema._zod.def.options.flatMap(atoms);
|
|
187
|
+
}
|
|
188
|
+
switch (schema._zod.def.type) {
|
|
189
|
+
case 'string':
|
|
190
|
+
return ['string'];
|
|
191
|
+
case 'number':
|
|
192
|
+
return ['number'];
|
|
193
|
+
case 'boolean':
|
|
194
|
+
return ['boolean'];
|
|
195
|
+
case 'template_literal':
|
|
196
|
+
return ['stringLiteral'];
|
|
197
|
+
case 'enum':
|
|
198
|
+
case 'literal':
|
|
199
|
+
return [...(schema._zod.values ?? [])].flatMap(valueAtoms);
|
|
200
|
+
case 'null':
|
|
201
|
+
return ['null'];
|
|
202
|
+
case 'undefined':
|
|
203
|
+
case 'void':
|
|
204
|
+
return ['undefined'];
|
|
205
|
+
default:
|
|
206
|
+
return ['other'];
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Whether a schema's output can be null or undefined. Mirrors `null extends V`. */
|
|
211
|
+
export function nullish(schema: z.core.$ZodType): boolean {
|
|
212
|
+
return atoms(schema).some((atom) => atom === 'null' || atom === 'undefined');
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** The element schema of an array field, through its wrappers. */
|
|
216
|
+
function elementOf(schema: z.core.$ZodType): z.core.$ZodType | undefined {
|
|
217
|
+
const inner = innerOf(schema);
|
|
218
|
+
if (inner !== undefined) return elementOf(inner);
|
|
219
|
+
return schema instanceof z.core.$ZodArray
|
|
220
|
+
? schema._zod.def.element
|
|
221
|
+
: undefined;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** A field's kind, from its schema. Mirrors `KindOf` above. */
|
|
225
|
+
export function kindOf(schema: z.core.$ZodType): Kind {
|
|
226
|
+
const element = elementOf(schema);
|
|
227
|
+
if (element !== undefined) {
|
|
228
|
+
const kind = kindOf(element);
|
|
229
|
+
return kind === 'string' || kind === 'number' || kind === 'enum'
|
|
230
|
+
? 'array'
|
|
231
|
+
: 'other';
|
|
232
|
+
}
|
|
233
|
+
const found = atoms(schema).filter(
|
|
234
|
+
(atom) => atom !== 'null' && atom !== 'undefined',
|
|
235
|
+
);
|
|
236
|
+
const only = (...allowed: Atom[]) =>
|
|
237
|
+
found.every((atom) => allowed.includes(atom));
|
|
238
|
+
if (found.length === 0) return 'other';
|
|
239
|
+
if (only('boolean')) return 'boolean';
|
|
240
|
+
if (only('string', 'stringLiteral')) {
|
|
241
|
+
return found.includes('string') ? 'string' : 'enum';
|
|
242
|
+
}
|
|
243
|
+
if (only('number', 'numberLiteral')) {
|
|
244
|
+
return found.includes('number') ? 'number' : 'enum';
|
|
245
|
+
}
|
|
246
|
+
return 'other';
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** The schema of a single value an operator compares against. */
|
|
250
|
+
function valueSchema(kind: Kind, leaf: z.core.$ZodType): z.core.$ZodType {
|
|
251
|
+
switch (kind) {
|
|
252
|
+
case 'string':
|
|
253
|
+
return z.string();
|
|
254
|
+
case 'number':
|
|
255
|
+
return z.number();
|
|
256
|
+
case 'boolean':
|
|
257
|
+
return z.boolean();
|
|
258
|
+
case 'enum':
|
|
259
|
+
return leaf;
|
|
260
|
+
default:
|
|
261
|
+
// 'other' fields only take `exists`, which reads no value.
|
|
262
|
+
return z.never();
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function operandSchema(op: string, value: z.core.$ZodType): z.core.$ZodType {
|
|
267
|
+
switch (op) {
|
|
268
|
+
case 'eq':
|
|
269
|
+
case 'ne':
|
|
270
|
+
return z.nullable(value);
|
|
271
|
+
case 'in':
|
|
272
|
+
case 'nin':
|
|
273
|
+
return z.array(z.nullable(value));
|
|
274
|
+
case 'contains':
|
|
275
|
+
return z.string();
|
|
276
|
+
case 'exists':
|
|
277
|
+
return z.boolean();
|
|
278
|
+
default:
|
|
279
|
+
return value;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function fieldSchema(field: z.core.$ZodType): z.core.$ZodType {
|
|
284
|
+
const kind = kindOf(field);
|
|
285
|
+
// An enum's values are checked by its own schema, without wrappers such as
|
|
286
|
+
// catch, which would accept anything. An array's operand is one element.
|
|
287
|
+
const element = elementOf(field);
|
|
288
|
+
const value =
|
|
289
|
+
element === undefined
|
|
290
|
+
? valueSchema(kind, unwrap(field))
|
|
291
|
+
: valueSchema(kindOf(element), unwrap(element));
|
|
292
|
+
return z.strictObject(
|
|
293
|
+
Object.fromEntries(
|
|
294
|
+
Object.entries(operators)
|
|
295
|
+
.filter(([, kinds]) => kinds.some((k) => k === kind))
|
|
296
|
+
.map(([op]) => [op, z.optional(operandSchema(op, value))]),
|
|
297
|
+
),
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* The validator of a `Where` on documents of `schema`. The overload states
|
|
303
|
+
* its static type, as input and output alike: a `Where` has no defaults or
|
|
304
|
+
* transforms. The implementation builds the schema from the shape at
|
|
305
|
+
* runtime, which a loop cannot express as a mapped type.
|
|
306
|
+
*/
|
|
307
|
+
export function whereSchema<S extends z.ZodObject>(
|
|
308
|
+
schema: S,
|
|
309
|
+
): z.ZodType<Where<z.output<S>>, Where<z.output<S>>>;
|
|
310
|
+
export function whereSchema(schema: z.ZodObject): z.ZodType {
|
|
311
|
+
const fields = Object.fromEntries(
|
|
312
|
+
Object.entries(schema.shape).map(([name, field]) => {
|
|
313
|
+
const entry = z.optional(fieldSchema(field));
|
|
314
|
+
// The family guard refuses it for a caller who may not read it; the
|
|
315
|
+
// description reaches JSON Schema, so OpenAPI and MCP say so.
|
|
316
|
+
return [
|
|
317
|
+
name,
|
|
318
|
+
markOf(field) === undefined
|
|
319
|
+
? entry
|
|
320
|
+
: entry.describe(refusedWhere(name)),
|
|
321
|
+
];
|
|
322
|
+
}),
|
|
323
|
+
);
|
|
324
|
+
const clauses = z.optional(z.array(z.lazy(() => where)).min(1));
|
|
325
|
+
const where: z.ZodType = z.strictObject({
|
|
326
|
+
...fields,
|
|
327
|
+
and: clauses,
|
|
328
|
+
or: clauses,
|
|
329
|
+
});
|
|
330
|
+
return where;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** Parses a `Where` on documents of `schema`, or throws `VALIDATION` naming each path. */
|
|
334
|
+
export function parseWhere<S extends z.ZodObject>(
|
|
335
|
+
schema: S,
|
|
336
|
+
value: unknown,
|
|
337
|
+
): Where<z.output<S>> {
|
|
338
|
+
const result = whereSchema(schema).safeParse(value);
|
|
339
|
+
if (result.success) return result.data;
|
|
340
|
+
throw invalid(result.error.issues);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** The operators of one field in a parsed `Where`. */
|
|
344
|
+
export type FieldOps = {
|
|
345
|
+
readonly eq?: unknown;
|
|
346
|
+
readonly ne?: unknown;
|
|
347
|
+
readonly in?: readonly unknown[];
|
|
348
|
+
readonly nin?: readonly unknown[];
|
|
349
|
+
readonly gt?: unknown;
|
|
350
|
+
readonly gte?: unknown;
|
|
351
|
+
readonly lt?: unknown;
|
|
352
|
+
readonly lte?: unknown;
|
|
353
|
+
readonly contains?: string;
|
|
354
|
+
readonly has?: unknown;
|
|
355
|
+
readonly exists?: boolean;
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
/** Both filters: undefined when neither is set. */
|
|
359
|
+
export function both(
|
|
360
|
+
a: AnyWhere | undefined,
|
|
361
|
+
b: AnyWhere | undefined,
|
|
362
|
+
): AnyWhere | undefined {
|
|
363
|
+
if (a === undefined) return b;
|
|
364
|
+
if (b === undefined) return a;
|
|
365
|
+
return { and: [a, b] };
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** Any parsed `Where`, as `matches` reads it. */
|
|
369
|
+
export type AnyWhere = {
|
|
370
|
+
readonly and?: readonly AnyWhere[];
|
|
371
|
+
readonly or?: readonly AnyWhere[];
|
|
372
|
+
readonly [field: string]: FieldOps | readonly AnyWhere[] | undefined;
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
// Null and missing follow MongoDB's query semantics, so MongoStore can pass a
|
|
376
|
+
// Where through unchanged. `eq: null` matches null or missing; `ne` and `nin`
|
|
377
|
+
// match a missing field; a comparison with null or missing is false. The one
|
|
378
|
+
// difference is `exists`: it asks whether the field has a value, so null does
|
|
379
|
+
// not exist (Mongo `{ $ne: null }` and `{ $eq: null }`).
|
|
380
|
+
function present(value: unknown): boolean {
|
|
381
|
+
return value !== undefined && value !== null;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function equals(value: unknown, target: unknown): boolean {
|
|
385
|
+
return target === null ? !present(value) : value === target;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** Strings compare by code unit, so ISO timestamps order correctly. */
|
|
389
|
+
export function compare(value: unknown, target: unknown): number | undefined {
|
|
390
|
+
if (typeof value === 'number' && typeof target === 'number') {
|
|
391
|
+
return value - target;
|
|
392
|
+
}
|
|
393
|
+
if (typeof value === 'string' && typeof target === 'string') {
|
|
394
|
+
return value < target ? -1 : value > target ? 1 : 0;
|
|
395
|
+
}
|
|
396
|
+
return undefined;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function compares(
|
|
400
|
+
value: unknown,
|
|
401
|
+
target: unknown,
|
|
402
|
+
test: (order: number) => boolean,
|
|
403
|
+
): boolean {
|
|
404
|
+
if (target === undefined) return true;
|
|
405
|
+
const order = compare(value, target);
|
|
406
|
+
return order !== undefined && test(order);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
function matchesField(value: unknown, ops: FieldOps): boolean {
|
|
410
|
+
if (ops.eq !== undefined && !equals(value, ops.eq)) return false;
|
|
411
|
+
if (ops.ne !== undefined && equals(value, ops.ne)) return false;
|
|
412
|
+
if (ops.in !== undefined && !ops.in.some((t) => equals(value, t))) {
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
415
|
+
if (ops.nin?.some((t) => equals(value, t))) return false;
|
|
416
|
+
if (!compares(value, ops.gt, (order) => order > 0)) return false;
|
|
417
|
+
if (!compares(value, ops.gte, (order) => order >= 0)) return false;
|
|
418
|
+
if (!compares(value, ops.lt, (order) => order < 0)) return false;
|
|
419
|
+
if (!compares(value, ops.lte, (order) => order <= 0)) return false;
|
|
420
|
+
if (ops.contains !== undefined) {
|
|
421
|
+
// A literal, case-insensitive substring. Never a regex.
|
|
422
|
+
const needle = ops.contains.toLowerCase();
|
|
423
|
+
if (typeof value !== 'string' || !value.toLowerCase().includes(needle)) {
|
|
424
|
+
return false;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
// An element equal to the operand. A missing or null array has none.
|
|
428
|
+
if (
|
|
429
|
+
ops.has !== undefined &&
|
|
430
|
+
!(Array.isArray(value) && value.some((element) => equals(element, ops.has)))
|
|
431
|
+
) {
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
434
|
+
if (ops.exists !== undefined && present(value) !== ops.exists) return false;
|
|
435
|
+
return true;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function isClauses(
|
|
439
|
+
condition: FieldOps | readonly AnyWhere[],
|
|
440
|
+
): condition is readonly AnyWhere[] {
|
|
441
|
+
return Array.isArray(condition);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/** Whether `doc` matches a parsed `where`. The in-memory store's filter. */
|
|
445
|
+
export function matches(
|
|
446
|
+
doc: Readonly<Record<string, unknown>>,
|
|
447
|
+
where: AnyWhere,
|
|
448
|
+
): boolean {
|
|
449
|
+
for (const [field, condition] of Object.entries(where)) {
|
|
450
|
+
if (condition === undefined) continue;
|
|
451
|
+
if (isClauses(condition)) {
|
|
452
|
+
// collection() rejects fields named `and` and `or`, so only they hold arrays.
|
|
453
|
+
const holds =
|
|
454
|
+
field === 'and'
|
|
455
|
+
? condition.every((clause) => matches(doc, clause))
|
|
456
|
+
: condition.some((clause) => matches(doc, clause));
|
|
457
|
+
if (!holds) return false;
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
if (!matchesField(doc[field], condition)) return false;
|
|
461
|
+
}
|
|
462
|
+
return true;
|
|
463
|
+
}
|