@latticecms/core 0.0.0 → 0.1.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 +1174 -2
- package/dist/access.d.ts +210 -0
- package/dist/access.d.ts.map +1 -0
- package/dist/access.js +225 -0
- package/dist/access.js.map +1 -0
- package/dist/build.d.ts +507 -0
- package/dist/build.d.ts.map +1 -0
- package/dist/build.js +598 -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 +344 -0
- package/dist/collection.js.map +1 -0
- package/dist/crud.d.ts +30 -0
- package/dist/crud.d.ts.map +1 -0
- package/dist/crud.js +539 -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 +179 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +146 -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 +231 -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 +255 -0
- package/dist/mask.js.map +1 -0
- package/dist/populate.d.ts +32 -0
- package/dist/populate.d.ts.map +1 -0
- package/dist/populate.js +73 -0
- package/dist/populate.js.map +1 -0
- package/dist/relations.d.ts +143 -0
- package/dist/relations.d.ts.map +1 -0
- package/dist/relations.js +128 -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 +83 -0
- package/dist/repo.js.map +1 -0
- package/dist/sort.d.ts +61 -0
- package/dist/sort.d.ts.map +1 -0
- package/dist/sort.js +145 -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 +683 -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 +113 -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 +79 -2
- package/skills/lattice/SKILL.md +106 -0
- package/skills/lattice/references/access.md +76 -0
- package/skills/lattice/references/cli.md +87 -0
- package/skills/lattice/references/collection.md +91 -0
- package/skills/lattice/references/event-handler.md +88 -0
- package/skills/lattice/references/masking.md +72 -0
- package/skills/lattice/references/method.md +85 -0
- package/skills/lattice/references/override.md +77 -0
- package/skills/lattice/references/relations.md +90 -0
- package/skills/lattice/references/tenancy.md +82 -0
- package/skills/lattice/references/testing.md +75 -0
- package/src/access.ts +429 -0
- package/src/build.ts +1722 -0
- package/src/collection.ts +1018 -0
- package/src/crud.ts +795 -0
- package/src/describe.ts +232 -0
- package/src/errors.ts +170 -0
- package/src/family.ts +334 -0
- package/src/index.ts +42 -0
- package/src/marker.ts +54 -0
- package/src/mask.ts +307 -0
- package/src/populate.ts +96 -0
- package/src/relations.ts +353 -0
- package/src/repo.ts +209 -0
- package/src/sort.ts +214 -0
- package/src/store/conformance.ts +1007 -0
- package/src/store/index.ts +16 -0
- package/src/store.ts +309 -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,214 @@
|
|
|
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 KindOf,
|
|
9
|
+
kindOf,
|
|
10
|
+
nullish,
|
|
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: never null or missing, so a cursor
|
|
24
|
+
* predicate never meets null, and of an ordered kind. Mirrors `sortable`.
|
|
25
|
+
*/
|
|
26
|
+
export type IsSortable<V> = null extends V
|
|
27
|
+
? false
|
|
28
|
+
: undefined extends V
|
|
29
|
+
? false
|
|
30
|
+
: KindOf<V> extends (typeof sortableKinds)[number]
|
|
31
|
+
? true
|
|
32
|
+
: false;
|
|
33
|
+
|
|
34
|
+
/** Whether a field can be sorted, from its schema. Mirrors `IsSortable`. */
|
|
35
|
+
export function sortable(schema: z.core.$ZodType): boolean {
|
|
36
|
+
const kind = kindOf(schema);
|
|
37
|
+
return !nullish(schema) && sortableKinds.some((k) => k === kind);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
type SortableField<Doc> = {
|
|
41
|
+
[K in keyof Doc & string]: IsSortable<Doc[K]> extends true ? K : never;
|
|
42
|
+
}[keyof Doc & string];
|
|
43
|
+
|
|
44
|
+
/** A field to order by: `field` ascends, `-field` descends. */
|
|
45
|
+
export type SortKey<Doc> = SortableField<Doc> | `-${SortableField<Doc>}`;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The fields to order by, first key first. An array, not an object: Zod
|
|
49
|
+
* rebuilds object keys in shape order, which would reorder the priority.
|
|
50
|
+
*/
|
|
51
|
+
export type Sort<Doc> = readonly SortKey<Doc>[];
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The validator of find's `Sort` on documents of `schema`, which gives
|
|
55
|
+
* `defaultSort` when the caller sends none. The overload states its static
|
|
56
|
+
* type, and that `defaultSort` is a valid `Sort` for every collection: it
|
|
57
|
+
* names a system field, which a generic shape cannot show. The
|
|
58
|
+
* implementation lists the keys it reads from the shape at runtime, which a
|
|
59
|
+
* loop cannot express as a mapped type.
|
|
60
|
+
*
|
|
61
|
+
* Each key is an enum of the sortable fields, ascending and descending, so
|
|
62
|
+
* the JSON Schema shows a client every valid key. The enum's error names
|
|
63
|
+
* why a key is refused: not a field, or a field that cannot be sorted.
|
|
64
|
+
*/
|
|
65
|
+
export function sortSchema<S extends z.ZodObject>(
|
|
66
|
+
schema: S,
|
|
67
|
+
): z.ZodDefault<z.ZodType<Sort<z.output<S>>, Sort<z.output<S>>>>;
|
|
68
|
+
export function sortSchema(schema: z.ZodObject): z.ZodType {
|
|
69
|
+
const fields = Object.entries(schema.shape);
|
|
70
|
+
const keys = fields
|
|
71
|
+
.filter(([, field]) => sortable(field))
|
|
72
|
+
.flatMap(([name]) => [name, `-${name}`]);
|
|
73
|
+
// Only a field that can be sorted at all is refused for being withheld.
|
|
74
|
+
const maskable = fields
|
|
75
|
+
.filter(([, field]) => markOf(field) !== undefined && sortable(field))
|
|
76
|
+
.map(([name]) => name);
|
|
77
|
+
const key = z.enum(keys, {
|
|
78
|
+
// A non-string key gets Zod's own message.
|
|
79
|
+
error: ({ input }) => {
|
|
80
|
+
if (typeof input !== 'string') return undefined;
|
|
81
|
+
const { field } = keyOf(input);
|
|
82
|
+
return fields.some(([name]) => name === field)
|
|
83
|
+
? `"${field}" cannot be sorted: it is optional, nullable, or not a string, number or enum`
|
|
84
|
+
: `"${field}" is not a field`;
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
const sort = z
|
|
88
|
+
.array(key)
|
|
89
|
+
.superRefine((given, ctx) => {
|
|
90
|
+
const seen = new Set<string>();
|
|
91
|
+
given.forEach((value, index) => {
|
|
92
|
+
const { field } = keyOf(value);
|
|
93
|
+
if (seen.has(field)) {
|
|
94
|
+
ctx.addIssue({
|
|
95
|
+
code: 'custom',
|
|
96
|
+
path: [index],
|
|
97
|
+
message: `"${field}" is sorted twice`,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
seen.add(field);
|
|
101
|
+
});
|
|
102
|
+
})
|
|
103
|
+
.default(() => [...defaultSort]);
|
|
104
|
+
// As on where: the description says a sort on a maskable field may be refused.
|
|
105
|
+
return maskable.length === 0 ? sort : sort.describe(refusedSort(maskable));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Parses a `Sort` on documents of `schema`, or throws `VALIDATION` naming each path. */
|
|
109
|
+
export function parseSort<S extends z.ZodObject>(
|
|
110
|
+
schema: S,
|
|
111
|
+
value: unknown,
|
|
112
|
+
): Sort<z.output<S>> {
|
|
113
|
+
const result = sortSchema(schema).safeParse(value);
|
|
114
|
+
if (result.success) return result.data;
|
|
115
|
+
throw invalid(result.error.issues);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
type OrderKey = { readonly field: string; readonly direction: 1 | -1 };
|
|
119
|
+
|
|
120
|
+
/** A sort as stores apply it: keys in priority order, ending with `id`. */
|
|
121
|
+
export type Order = readonly OrderKey[];
|
|
122
|
+
|
|
123
|
+
function keyOf(key: string): OrderKey {
|
|
124
|
+
return key.startsWith('-')
|
|
125
|
+
? { field: key.slice(1), direction: -1 }
|
|
126
|
+
: { field: key, direction: 1 };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** The order a sort means. `id` is the last tiebreak unless the sort has it. */
|
|
130
|
+
export function orderOf(sort: readonly string[]): Order {
|
|
131
|
+
const keys = sort.map(keyOf);
|
|
132
|
+
return keys.some((key) => key.field === 'id')
|
|
133
|
+
? keys
|
|
134
|
+
: [...keys, { field: 'id', direction: 1 }];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function keysOf(order: Order): string[] {
|
|
138
|
+
return order.map((key) =>
|
|
139
|
+
key.direction === 1 ? key.field : `-${key.field}`,
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Orders documents by `order`. Values compare as `Where` comparisons do. */
|
|
144
|
+
export function compareDocs(order: Order) {
|
|
145
|
+
return (
|
|
146
|
+
a: Readonly<Record<string, unknown>>,
|
|
147
|
+
b: Readonly<Record<string, unknown>>,
|
|
148
|
+
): number => {
|
|
149
|
+
for (const key of order) {
|
|
150
|
+
const result = compare(a[key.field], b[key.field]) ?? 0;
|
|
151
|
+
if (result !== 0) return result * key.direction;
|
|
152
|
+
}
|
|
153
|
+
return 0;
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const cursorSchema = z.object({
|
|
158
|
+
sort: z.array(z.string()),
|
|
159
|
+
values: z.array(z.union([z.string(), z.number()])),
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
/** An opaque cursor that starts the next page after `doc`. */
|
|
163
|
+
export function encodeCursor(
|
|
164
|
+
order: Order,
|
|
165
|
+
doc: Readonly<Record<string, unknown>>,
|
|
166
|
+
): string {
|
|
167
|
+
const cursor = {
|
|
168
|
+
sort: keysOf(order),
|
|
169
|
+
values: order.map((key) => doc[key.field]),
|
|
170
|
+
};
|
|
171
|
+
return Buffer.from(JSON.stringify(cursor)).toString('base64url');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function badCursor(message: string) {
|
|
175
|
+
return LatticeError.VALIDATION({ issues: [{ path: ['after'], message }] });
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function readCursor(cursor: string): unknown {
|
|
179
|
+
try {
|
|
180
|
+
return JSON.parse(Buffer.from(cursor, 'base64url').toString('utf8'));
|
|
181
|
+
} catch {
|
|
182
|
+
throw badCursor('The cursor is malformed');
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* The keyset filter for the documents after `cursor` under `order`: the
|
|
188
|
+
* tuple of sort values comes after the cursor's, key by key, in each key's
|
|
189
|
+
* direction. It is a `Where`, so every store applies it as it applies any
|
|
190
|
+
* filter, and sort fields are never null, so no key meets a missing value.
|
|
191
|
+
*/
|
|
192
|
+
export function after(order: Order, cursor: string): AnyWhere {
|
|
193
|
+
const parsed = cursorSchema.safeParse(readCursor(cursor));
|
|
194
|
+
if (!parsed.success) throw badCursor('The cursor is malformed');
|
|
195
|
+
const { sort, values } = parsed.data;
|
|
196
|
+
if (sort.join(',') !== keysOf(order).join(',')) {
|
|
197
|
+
throw badCursor('The cursor is for a different sort');
|
|
198
|
+
}
|
|
199
|
+
if (values.length !== order.length) {
|
|
200
|
+
throw badCursor('The cursor is malformed');
|
|
201
|
+
}
|
|
202
|
+
const clauses: AnyWhere[] = [];
|
|
203
|
+
const equal: Record<string, { readonly eq: string | number }> = {};
|
|
204
|
+
order.forEach(({ field, direction }, index) => {
|
|
205
|
+
const value = values[index];
|
|
206
|
+
if (value === undefined) throw badCursor('The cursor is malformed');
|
|
207
|
+
clauses.push({
|
|
208
|
+
...equal,
|
|
209
|
+
[field]: direction === 1 ? { gt: value } : { lt: value },
|
|
210
|
+
});
|
|
211
|
+
equal[field] = { eq: value };
|
|
212
|
+
});
|
|
213
|
+
return { or: clauses };
|
|
214
|
+
}
|