@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/sort.ts
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { refusedSort } from './describe.js';
|
|
3
|
+
import { invalid, LatticeError } from './errors.js';
|
|
4
|
+
import { markOf } from './marker.js';
|
|
5
|
+
import {
|
|
6
|
+
type AnyWhere,
|
|
7
|
+
compare,
|
|
8
|
+
type FieldOps,
|
|
9
|
+
type KindOf,
|
|
10
|
+
kindOf,
|
|
11
|
+
} from './where.js';
|
|
12
|
+
|
|
13
|
+
/** The generated find's sort when the caller gives none. */
|
|
14
|
+
export const defaultSort = ['-createdAt'] as const;
|
|
15
|
+
/** The generated find's page size when the caller gives none. */
|
|
16
|
+
export const defaultLimit = 20;
|
|
17
|
+
export const maxLimit = 100;
|
|
18
|
+
|
|
19
|
+
// Booleans have no useful order, and 'other' fields none at all.
|
|
20
|
+
const sortableKinds = ['string', 'number', 'enum'] as const;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Whether a field can be sorted: of an ordered kind. An optional or
|
|
24
|
+
* nullable one sorts too, with its missing values last (0037). Mirrors
|
|
25
|
+
* `sortable`.
|
|
26
|
+
*/
|
|
27
|
+
export type IsSortable<V> =
|
|
28
|
+
KindOf<V> extends (typeof sortableKinds)[number] ? true : false;
|
|
29
|
+
|
|
30
|
+
/** Whether a field can be sorted, from its schema. Mirrors `IsSortable`. */
|
|
31
|
+
export function sortable(schema: z.core.$ZodType): boolean {
|
|
32
|
+
const kind = kindOf(schema);
|
|
33
|
+
return sortableKinds.some((k) => k === kind);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type SortableField<Doc> = {
|
|
37
|
+
[K in keyof Doc & string]: IsSortable<Doc[K]> extends true ? K : never;
|
|
38
|
+
}[keyof Doc & string];
|
|
39
|
+
|
|
40
|
+
/** A field to order by: `field` ascends, `-field` descends. */
|
|
41
|
+
export type SortKey<Doc> = SortableField<Doc> | `-${SortableField<Doc>}`;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The fields to order by, first key first. An array, not an object: Zod
|
|
45
|
+
* rebuilds object keys in shape order, which would reorder the priority.
|
|
46
|
+
*/
|
|
47
|
+
export type Sort<Doc> = readonly SortKey<Doc>[];
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The validator of find's `Sort` on documents of `schema`, which gives
|
|
51
|
+
* `defaultSort` when the caller sends none. The overload states its static
|
|
52
|
+
* type, and that `defaultSort` is a valid `Sort` for every collection: it
|
|
53
|
+
* names a system field, which a generic shape cannot show. The
|
|
54
|
+
* implementation lists the keys it reads from the shape at runtime, which a
|
|
55
|
+
* loop cannot express as a mapped type.
|
|
56
|
+
*
|
|
57
|
+
* Each key is an enum of the sortable fields, ascending and descending, so
|
|
58
|
+
* the JSON Schema shows a client every valid key. The enum's error names
|
|
59
|
+
* why a key is refused: not a field, or a field that cannot be sorted.
|
|
60
|
+
*/
|
|
61
|
+
export function sortSchema<S extends z.ZodObject>(
|
|
62
|
+
schema: S,
|
|
63
|
+
): z.ZodDefault<z.ZodType<Sort<z.output<S>>, Sort<z.output<S>>>>;
|
|
64
|
+
export function sortSchema(schema: z.ZodObject): z.ZodType {
|
|
65
|
+
const fields = Object.entries(schema.shape);
|
|
66
|
+
const keys = fields
|
|
67
|
+
.filter(([, field]) => sortable(field))
|
|
68
|
+
.flatMap(([name]) => [name, `-${name}`]);
|
|
69
|
+
// Only a field that can be sorted at all is refused for being withheld.
|
|
70
|
+
const maskable = fields
|
|
71
|
+
.filter(([, field]) => markOf(field) !== undefined && sortable(field))
|
|
72
|
+
.map(([name]) => name);
|
|
73
|
+
const key = z.enum(keys, {
|
|
74
|
+
// A non-string key gets Zod's own message.
|
|
75
|
+
error: ({ input }) => {
|
|
76
|
+
if (typeof input !== 'string') return undefined;
|
|
77
|
+
const { field } = keyOf(input);
|
|
78
|
+
return fields.some(([name]) => name === field)
|
|
79
|
+
? `"${field}" cannot be sorted: it is not a string, number or enum`
|
|
80
|
+
: `"${field}" is not a field`;
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
const sort = z
|
|
84
|
+
.array(key)
|
|
85
|
+
.superRefine((given, ctx) => {
|
|
86
|
+
const seen = new Set<string>();
|
|
87
|
+
given.forEach((value, index) => {
|
|
88
|
+
const { field } = keyOf(value);
|
|
89
|
+
if (seen.has(field)) {
|
|
90
|
+
ctx.addIssue({
|
|
91
|
+
code: 'custom',
|
|
92
|
+
path: [index],
|
|
93
|
+
message: `"${field}" is sorted twice`,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
seen.add(field);
|
|
97
|
+
});
|
|
98
|
+
})
|
|
99
|
+
.default(() => [...defaultSort]);
|
|
100
|
+
// As on where: the description says a sort on a maskable field may be refused.
|
|
101
|
+
return maskable.length === 0 ? sort : sort.describe(refusedSort(maskable));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Parses a `Sort` on documents of `schema`, or throws `VALIDATION` naming each path. */
|
|
105
|
+
export function parseSort<S extends z.ZodObject>(
|
|
106
|
+
schema: S,
|
|
107
|
+
value: unknown,
|
|
108
|
+
): Sort<z.output<S>> {
|
|
109
|
+
const result = sortSchema(schema).safeParse(value);
|
|
110
|
+
if (result.success) return result.data;
|
|
111
|
+
throw invalid(result.error.issues);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
type OrderKey = {
|
|
115
|
+
readonly field: string;
|
|
116
|
+
readonly direction: 1 | -1;
|
|
117
|
+
/** Whether a document may have no value here: it then sorts last. */
|
|
118
|
+
readonly optional: boolean;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/** A sort as stores apply it: keys in priority order, ending with `id`. */
|
|
122
|
+
export type Order = readonly OrderKey[];
|
|
123
|
+
|
|
124
|
+
/** The field a sort key names, and its direction. */
|
|
125
|
+
export function keyOf(key: string): { field: string; direction: 1 | -1 } {
|
|
126
|
+
return key.startsWith('-')
|
|
127
|
+
? { field: key.slice(1), direction: -1 }
|
|
128
|
+
: { field: key, direction: 1 };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The order a sort means. `id` is the last tiebreak unless the sort has
|
|
133
|
+
* it. `optional` names the sort fields a document may lack.
|
|
134
|
+
*/
|
|
135
|
+
export function orderOf(
|
|
136
|
+
sort: readonly string[],
|
|
137
|
+
optional: readonly string[],
|
|
138
|
+
): Order {
|
|
139
|
+
const keys = sort.map((key) => {
|
|
140
|
+
const { field, direction } = keyOf(key);
|
|
141
|
+
return { field, direction, optional: optional.includes(field) };
|
|
142
|
+
});
|
|
143
|
+
return keys.some((key) => key.field === 'id')
|
|
144
|
+
? keys
|
|
145
|
+
: [...keys, { field: 'id', direction: 1, optional: false }];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function keysOf(order: Order): string[] {
|
|
149
|
+
return order.map((key) =>
|
|
150
|
+
key.direction === 1 ? key.field : `-${key.field}`,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** The value of `field` in `doc`, null when it has none. */
|
|
155
|
+
function sortValue(
|
|
156
|
+
doc: Readonly<Record<string, unknown>>,
|
|
157
|
+
field: string,
|
|
158
|
+
): unknown {
|
|
159
|
+
return doc[field] ?? null;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Orders documents by `order`. Values compare as `Where` comparisons do. On
|
|
164
|
+
* an optional key, a missing value comes after every value, in either
|
|
165
|
+
* direction, and two missing values tie, so the next key decides (0037). A
|
|
166
|
+
* required key compares as before, so an old row that lacks it ties.
|
|
167
|
+
*/
|
|
168
|
+
export function compareDocs(order: Order) {
|
|
169
|
+
return (
|
|
170
|
+
a: Readonly<Record<string, unknown>>,
|
|
171
|
+
b: Readonly<Record<string, unknown>>,
|
|
172
|
+
): number => {
|
|
173
|
+
for (const key of order) {
|
|
174
|
+
const x = sortValue(a, key.field);
|
|
175
|
+
const y = sortValue(b, key.field);
|
|
176
|
+
if (key.optional && (x === null || y === null)) {
|
|
177
|
+
if (x === y) continue;
|
|
178
|
+
return x === null ? 1 : -1;
|
|
179
|
+
}
|
|
180
|
+
const result = compare(x, y) ?? 0;
|
|
181
|
+
if (result !== 0) return result * key.direction;
|
|
182
|
+
}
|
|
183
|
+
return 0;
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// A missing value is null: JSON has no undefined.
|
|
188
|
+
const cursorSchema = z.object({
|
|
189
|
+
sort: z.array(z.string()),
|
|
190
|
+
values: z.array(z.union([z.string(), z.number(), z.null()])),
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
/** An opaque cursor that starts the next page after `doc`. */
|
|
194
|
+
export function encodeCursor(
|
|
195
|
+
order: Order,
|
|
196
|
+
doc: Readonly<Record<string, unknown>>,
|
|
197
|
+
): string {
|
|
198
|
+
const cursor = {
|
|
199
|
+
sort: keysOf(order),
|
|
200
|
+
values: order.map((key) => sortValue(doc, key.field)),
|
|
201
|
+
};
|
|
202
|
+
return Buffer.from(JSON.stringify(cursor)).toString('base64url');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function badCursor(message: string) {
|
|
206
|
+
return LatticeError.VALIDATION({ issues: [{ path: ['after'], message }] });
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function readCursor(cursor: string): unknown {
|
|
210
|
+
try {
|
|
211
|
+
return JSON.parse(Buffer.from(cursor, 'base64url').toString('utf8'));
|
|
212
|
+
} catch {
|
|
213
|
+
throw badCursor('The cursor is malformed');
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The sort values `cursor` holds under `order`, one per key, null where its
|
|
219
|
+
* document had none. Throws VALIDATION at `after` for a malformed cursor,
|
|
220
|
+
* one from another sort, or a null on a required key.
|
|
221
|
+
*/
|
|
222
|
+
export function cursorValues(
|
|
223
|
+
order: Order,
|
|
224
|
+
cursor: string,
|
|
225
|
+
): readonly (string | number | null)[] {
|
|
226
|
+
const parsed = cursorSchema.safeParse(readCursor(cursor));
|
|
227
|
+
if (!parsed.success) throw badCursor('The cursor is malformed');
|
|
228
|
+
const { sort, values } = parsed.data;
|
|
229
|
+
if (sort.join(',') !== keysOf(order).join(',')) {
|
|
230
|
+
throw badCursor('The cursor is for a different sort');
|
|
231
|
+
}
|
|
232
|
+
if (
|
|
233
|
+
values.length !== order.length ||
|
|
234
|
+
order.some((key, index) => values[index] === null && !key.optional)
|
|
235
|
+
) {
|
|
236
|
+
throw badCursor('The cursor is malformed');
|
|
237
|
+
}
|
|
238
|
+
return values;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* The keyset filter for the documents after `cursor` under `order`: the
|
|
243
|
+
* tuple of sort values comes after the cursor's, key by key, in each key's
|
|
244
|
+
* direction. It is a `Where`, so every store applies it as it applies any
|
|
245
|
+
* filter. A missing value comes after every value (0037): after a value, an
|
|
246
|
+
* optional key also takes the rows that have none, and nothing comes after
|
|
247
|
+
* a missing one but the rows that tie on it. A required key's filter is a
|
|
248
|
+
* plain comparison.
|
|
249
|
+
*/
|
|
250
|
+
export function after(order: Order, cursor: string): AnyWhere {
|
|
251
|
+
const values = cursorValues(order, cursor);
|
|
252
|
+
const clauses: AnyWhere[] = [];
|
|
253
|
+
const equal: Record<string, FieldOps> = {};
|
|
254
|
+
order.forEach(({ field, direction, optional }, index) => {
|
|
255
|
+
const value = values[index];
|
|
256
|
+
if (value === undefined) throw badCursor('The cursor is malformed');
|
|
257
|
+
if (value !== null) {
|
|
258
|
+
const beyond = direction === 1 ? { gt: value } : { lt: value };
|
|
259
|
+
clauses.push(
|
|
260
|
+
optional
|
|
261
|
+
? {
|
|
262
|
+
...equal,
|
|
263
|
+
or: [{ [field]: beyond }, { [field]: { exists: false } }],
|
|
264
|
+
}
|
|
265
|
+
: { ...equal, [field]: beyond },
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
equal[field] = value === null ? { exists: false } : { eq: value };
|
|
269
|
+
});
|
|
270
|
+
return { or: clauses };
|
|
271
|
+
}
|