@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/family.ts
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import type { Logger } from '@hexok/logger';
|
|
2
|
+
import { type Attributes, type Span, SpanKind } from '@opentelemetry/api';
|
|
3
|
+
import type { McpEffect } from '@ts-pf/mcp/meta';
|
|
4
|
+
import { type StandardSchemaV1, UseCase } from 'hexok';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import { CallAccess } from './access.js';
|
|
7
|
+
import type { AnyCollection } from './collection.js';
|
|
8
|
+
import { invalid, LatticeError, refusal } from './errors.js';
|
|
9
|
+
import { withheldPaths, withheldWrites } from './mask.js';
|
|
10
|
+
import { type Tenant, tenancy } from './tenancy.js';
|
|
11
|
+
import { failed, tracer } from './tracing.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The statics every use case in a Lattice family declares. A method also
|
|
15
|
+
* declares `summary` (one sentence on what it does) and `effect`, which
|
|
16
|
+
* become its procedure's docs and MCP meta. An override declares neither:
|
|
17
|
+
* it keeps the operation's own.
|
|
18
|
+
*/
|
|
19
|
+
export type FamilySpec = {
|
|
20
|
+
readonly collection: AnyCollection;
|
|
21
|
+
readonly input?: z.core.$ZodType;
|
|
22
|
+
readonly output?: z.core.$ZodType;
|
|
23
|
+
readonly summary?: string;
|
|
24
|
+
readonly effect?: McpEffect;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* What a caller gives each call: the edge's context function, a
|
|
29
|
+
* `createLocalClient` context, or `build().context()`. The edge passes
|
|
30
|
+
* `rootLogger.child({ requestId })`; workers and tests pass their own.
|
|
31
|
+
*/
|
|
32
|
+
export type CallContext<Actor> = {
|
|
33
|
+
readonly actor: Actor;
|
|
34
|
+
readonly logger: Logger;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The per-call context of a Lattice family: the caller's, plus the call's
|
|
39
|
+
* access, which Lattice adds (0023).
|
|
40
|
+
*/
|
|
41
|
+
export type FamilyContext<Actor> = CallContext<Actor> & {
|
|
42
|
+
readonly access: CallAccess;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A Lattice family: a hexok `UseCase.context` factory, its actor schema, the
|
|
47
|
+
* id of an actor as log lines and the generated events carry it, and the
|
|
48
|
+
* actor's tenant. `Tenanted` is true when the family has `tenantOf`, so a
|
|
49
|
+
* tenanted collection in a family without it is a compile error.
|
|
50
|
+
*/
|
|
51
|
+
export type Family<
|
|
52
|
+
Actor,
|
|
53
|
+
Tenanted extends boolean = boolean,
|
|
54
|
+
> = UseCase.ContextFactory<FamilyContext<Actor>, FamilySpec> & {
|
|
55
|
+
readonly actor: StandardSchemaV1<unknown, Actor>;
|
|
56
|
+
readonly actorId: (actor: Actor) => string;
|
|
57
|
+
readonly tenantOf: ((actor: Actor) => Tenant) | undefined;
|
|
58
|
+
/** Type-only. */
|
|
59
|
+
readonly '~tenanted'?: Tenanted;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The fields that say who made a call, for its log lines: the actor's id,
|
|
64
|
+
* and its tenant id or `tenancy: 'bypass'`.
|
|
65
|
+
*/
|
|
66
|
+
export function callerFields<Actor>(
|
|
67
|
+
family: Pick<Family<Actor>, 'actorId' | 'tenantOf'>,
|
|
68
|
+
actor: Actor,
|
|
69
|
+
): Readonly<Record<string, string>> {
|
|
70
|
+
const actorId = family.actorId(actor);
|
|
71
|
+
const tenant = family.tenantOf?.(actor);
|
|
72
|
+
if (tenant === undefined) return { actorId };
|
|
73
|
+
if (tenant === tenancy.bypass) return { actorId, tenancy: 'bypass' };
|
|
74
|
+
return { actorId, tenantId: tenant };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** How a family logged an error: at info as a refusal, or at error. */
|
|
78
|
+
export type Logged = 'refused' | 'failed';
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The errors a family has logged, its guards' rejections and what `onCatch`
|
|
82
|
+
* saw, with how. The procedure middleware skips both outcomes; the edge's
|
|
83
|
+
* `onError` skips only failures, so a refusal the client sees as INTERNAL
|
|
84
|
+
* still reaches it. A WeakMap keeps the error object as it is and holds it
|
|
85
|
+
* no longer than the caller.
|
|
86
|
+
*/
|
|
87
|
+
const logged = new WeakMap<object, Logged>();
|
|
88
|
+
|
|
89
|
+
/** How a family logged `error`, or undefined when no family did. */
|
|
90
|
+
export function loggedByFamily(error: unknown): Logged | undefined {
|
|
91
|
+
return typeof error === 'object' && error !== null
|
|
92
|
+
? logged.get(error)
|
|
93
|
+
: undefined;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The action of `token`: a generated operation's or a method's name. */
|
|
97
|
+
function actionOf(spec: FamilySpec, token: string): string {
|
|
98
|
+
const { slug } = spec.collection;
|
|
99
|
+
return token.startsWith(`${slug}.`) ? token.slice(slug.length + 1) : token;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The logger of one call: `ctx.logger` with the call's collection, action
|
|
104
|
+
* and, once the actor has parsed, its id and tenant. Every family log line
|
|
105
|
+
* goes through it, so the fields cannot drift.
|
|
106
|
+
*/
|
|
107
|
+
function callLogger(
|
|
108
|
+
logger: Logger,
|
|
109
|
+
spec: FamilySpec,
|
|
110
|
+
token: string,
|
|
111
|
+
caller: Readonly<Record<string, string>> | undefined,
|
|
112
|
+
): Logger {
|
|
113
|
+
return logger.child({
|
|
114
|
+
collection: spec.collection.slug,
|
|
115
|
+
action: actionOf(spec, token),
|
|
116
|
+
...caller,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Logs a thrown value once and records it: info for a refusal, error for
|
|
122
|
+
* anything else. Returns it, so a guard can throw it.
|
|
123
|
+
*/
|
|
124
|
+
function logThrown(
|
|
125
|
+
log: Logger,
|
|
126
|
+
token: string,
|
|
127
|
+
error: unknown,
|
|
128
|
+
durationMs?: number,
|
|
129
|
+
): unknown {
|
|
130
|
+
const timing = durationMs === undefined ? {} : { durationMs };
|
|
131
|
+
const refused = refusal(error);
|
|
132
|
+
if (refused !== undefined) {
|
|
133
|
+
log.info(token, { outcome: 'refused', ...refused, ...timing });
|
|
134
|
+
} else if (error instanceof Error) {
|
|
135
|
+
log.error(error, { outcome: 'failed', ...timing });
|
|
136
|
+
} else {
|
|
137
|
+
log.error(token, { outcome: 'failed', thrown: error, ...timing });
|
|
138
|
+
}
|
|
139
|
+
if (typeof error === 'object' && error !== null) {
|
|
140
|
+
logged.set(error, refused === undefined ? 'failed' : 'refused');
|
|
141
|
+
}
|
|
142
|
+
return error;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Builds the app's one use case family on hexok `UseCase.context`. Before
|
|
147
|
+
* any member's `execute` runs, the actor must parse against `actor`, the
|
|
148
|
+
* input against the member's `input` static, and `ctx.access` must allow
|
|
149
|
+
* the member's action. Each call writes one line
|
|
150
|
+
* through a child of `ctx.logger`: info when it returns or a guard or
|
|
151
|
+
* `execute` refuses it, error when it throws anything else.
|
|
152
|
+
*/
|
|
153
|
+
export function family<
|
|
154
|
+
A extends StandardSchemaV1,
|
|
155
|
+
T extends Tenant = never,
|
|
156
|
+
>(options: {
|
|
157
|
+
actor: A;
|
|
158
|
+
/** The id the call's log lines and the generated events carry as `actorId`. */
|
|
159
|
+
actorId: (actor: StandardSchemaV1.InferOutput<A>) => string;
|
|
160
|
+
/**
|
|
161
|
+
* The actor's tenant id, or `tenancy.bypass` for a platform actor that
|
|
162
|
+
* reads across tenants. Required by a framework with a tenanted
|
|
163
|
+
* collection.
|
|
164
|
+
*/
|
|
165
|
+
tenantOf?: (actor: StandardSchemaV1.InferOutput<A>) => T;
|
|
166
|
+
}): Family<
|
|
167
|
+
StandardSchemaV1.InferOutput<A>,
|
|
168
|
+
[T] extends [never] ? false : true
|
|
169
|
+
> {
|
|
170
|
+
const caller = (actor: StandardSchemaV1.InferOutput<A>) =>
|
|
171
|
+
callerFields(
|
|
172
|
+
{ actorId: options.actorId, tenantOf: options.tenantOf },
|
|
173
|
+
actor,
|
|
174
|
+
);
|
|
175
|
+
const factory = UseCase.context<
|
|
176
|
+
FamilyContext<StandardSchemaV1.InferOutput<A>>,
|
|
177
|
+
FamilySpec
|
|
178
|
+
>()
|
|
179
|
+
// Guards run outside hexok's hooks, so each logs its own rejection.
|
|
180
|
+
.guard(({ ctx, spec, token }) => {
|
|
181
|
+
// The call's context parsed the actor once (familyContext), so this
|
|
182
|
+
// guard reads that parse rather than repeating it. The issues describe
|
|
183
|
+
// the caller's credentials, so they stay out of the error and the log.
|
|
184
|
+
if (!CallAccess.parsed(ctx.access, ctx.actor)) {
|
|
185
|
+
const log = callLogger(ctx.logger, spec, token, undefined);
|
|
186
|
+
throw logThrown(log, token, LatticeError.UNAUTHORIZED());
|
|
187
|
+
}
|
|
188
|
+
})
|
|
189
|
+
.guard(({ ctx, spec, token, input }) => {
|
|
190
|
+
// hexok hands execute the caller's input, not the parsed value. So a
|
|
191
|
+
// method's input schema uses no defaults or transforms; a generated use
|
|
192
|
+
// case parses its own input inside execute to strip unknown keys and
|
|
193
|
+
// apply defaults.
|
|
194
|
+
if (spec.input === undefined) return;
|
|
195
|
+
const result = z.safeParse(spec.input, input);
|
|
196
|
+
if (!result.success) {
|
|
197
|
+
const log = callLogger(ctx.logger, spec, token, caller(ctx.actor));
|
|
198
|
+
throw logThrown(log, token, invalid(result.error.issues));
|
|
199
|
+
}
|
|
200
|
+
})
|
|
201
|
+
// Access, after the actor and the input are known good. Generated
|
|
202
|
+
// operations, overrides and methods all pass here, so none can skip the
|
|
203
|
+
// FORBIDDEN check. The answer stays on ctx.access for execute's scope.
|
|
204
|
+
.guard(async ({ ctx, spec, token, input }) => {
|
|
205
|
+
const action = actionOf(spec, token);
|
|
206
|
+
// Create's function receives the parsed input, as execute will see it.
|
|
207
|
+
const data =
|
|
208
|
+
action !== 'create'
|
|
209
|
+
? undefined
|
|
210
|
+
: spec.input === undefined
|
|
211
|
+
? input
|
|
212
|
+
: z.parse(spec.input, input);
|
|
213
|
+
try {
|
|
214
|
+
await ctx.access.scope(spec.collection, action, data);
|
|
215
|
+
} catch (error) {
|
|
216
|
+
const log = callLogger(ctx.logger, spec, token, caller(ctx.actor));
|
|
217
|
+
throw logThrown(log, token, error);
|
|
218
|
+
}
|
|
219
|
+
})
|
|
220
|
+
// A where or a sort on a field the caller's rules withhold would reveal
|
|
221
|
+
// it through which rows match or their order, and an update would change
|
|
222
|
+
// a value the caller cannot see (0035), so each is refused, on every
|
|
223
|
+
// path. Output masking happens where a client receives the result.
|
|
224
|
+
.guard(async ({ ctx, spec, token, input }) => {
|
|
225
|
+
const action = actionOf(spec, token);
|
|
226
|
+
if (action !== 'find' && action !== 'count' && action !== 'update') {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
const withheld = await CallAccess.withheld(ctx.access, spec.collection);
|
|
230
|
+
if (withheld.size === 0) return;
|
|
231
|
+
const issues =
|
|
232
|
+
action === 'update'
|
|
233
|
+
? withheldWrites(input, withheld)
|
|
234
|
+
: withheldPaths(input, withheld);
|
|
235
|
+
if (issues.length > 0) {
|
|
236
|
+
const log = callLogger(ctx.logger, spec, token, caller(ctx.actor));
|
|
237
|
+
throw logThrown(log, token, LatticeError.VALIDATION({ issues }));
|
|
238
|
+
}
|
|
239
|
+
})
|
|
240
|
+
.hooks({
|
|
241
|
+
preExecute: ({ ctx, spec, token }) => {
|
|
242
|
+
const fields = caller(ctx.actor);
|
|
243
|
+
return {
|
|
244
|
+
log: callLogger(ctx.logger, spec, token, fields),
|
|
245
|
+
started: performance.now(),
|
|
246
|
+
span: useCaseSpan(spec, token, fields),
|
|
247
|
+
};
|
|
248
|
+
},
|
|
249
|
+
// The span ends in postExecute and onCatch, beside the log line,
|
|
250
|
+
// rather than in its own onFinally: each hook is one more await on
|
|
251
|
+
// every call, traced or not.
|
|
252
|
+
postExecute: ({ token }, { log, started, span }) => {
|
|
253
|
+
log.info(token, { outcome: 'ok', durationMs: since(started) });
|
|
254
|
+
if (span.isRecording()) span.setAttribute('lattice.outcome', 'ok');
|
|
255
|
+
span.end();
|
|
256
|
+
},
|
|
257
|
+
onCatch: ({ ctx, spec, token, error }, state) => {
|
|
258
|
+
// State is undefined only when preExecute itself threw.
|
|
259
|
+
if (state === undefined) {
|
|
260
|
+
logThrown(
|
|
261
|
+
callLogger(ctx.logger, spec, token, undefined),
|
|
262
|
+
token,
|
|
263
|
+
error,
|
|
264
|
+
);
|
|
265
|
+
} else {
|
|
266
|
+
logThrown(state.log, token, error, since(state.started));
|
|
267
|
+
const refused = refusal(error);
|
|
268
|
+
if (refused === undefined) {
|
|
269
|
+
failed(state.span, error);
|
|
270
|
+
} else if (state.span.isRecording()) {
|
|
271
|
+
// A refusal is not a failure: the status stays UNSET.
|
|
272
|
+
state.span.setAttributes({
|
|
273
|
+
'lattice.outcome': 'refused',
|
|
274
|
+
'lattice.code': String(refused.code),
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
state.span.end();
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
});
|
|
281
|
+
// The generated use cases and the procedure middleware read these, so the
|
|
282
|
+
// app states them once.
|
|
283
|
+
return Object.assign(factory, {
|
|
284
|
+
actor: options.actor,
|
|
285
|
+
actorId: options.actorId,
|
|
286
|
+
tenantOf: options.tenantOf,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* The use case's span: a child of the active span, the procedure's or a
|
|
292
|
+
* direct caller's. hexok has no around hook, so this span cannot be made
|
|
293
|
+
* active for execute: the store calls execute makes are its siblings, not
|
|
294
|
+
* its children. Its attributes are set only when a span records, so
|
|
295
|
+
* without an SDK they cost nothing.
|
|
296
|
+
*/
|
|
297
|
+
function useCaseSpan(
|
|
298
|
+
spec: FamilySpec,
|
|
299
|
+
token: string,
|
|
300
|
+
caller: Readonly<Record<string, string>>,
|
|
301
|
+
): Span {
|
|
302
|
+
const span = tracer.startSpan(token, { kind: SpanKind.INTERNAL });
|
|
303
|
+
if (span.isRecording()) span.setAttributes(spanFields(spec, token, caller));
|
|
304
|
+
return span;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* A use case span's attributes: what ran and for whom, never its input or
|
|
309
|
+
* a document. The caller's fields are its log line's.
|
|
310
|
+
*/
|
|
311
|
+
function spanFields(
|
|
312
|
+
spec: FamilySpec,
|
|
313
|
+
token: string,
|
|
314
|
+
caller: Readonly<Record<string, string>>,
|
|
315
|
+
): Attributes {
|
|
316
|
+
return {
|
|
317
|
+
'lattice.use_case': token,
|
|
318
|
+
'lattice.collection': spec.collection.slug,
|
|
319
|
+
'lattice.action': actionOf(spec, token),
|
|
320
|
+
...(caller.actorId === undefined
|
|
321
|
+
? {}
|
|
322
|
+
: { 'lattice.actor_id': caller.actorId }),
|
|
323
|
+
...(caller.tenantId === undefined
|
|
324
|
+
? {}
|
|
325
|
+
: { 'lattice.tenant_id': caller.tenantId }),
|
|
326
|
+
...(caller.tenancy === undefined
|
|
327
|
+
? {}
|
|
328
|
+
: { 'lattice.tenancy': caller.tenancy }),
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function since(started: number): number {
|
|
333
|
+
return Math.round(performance.now() - started);
|
|
334
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export {
|
|
2
|
+
Access,
|
|
3
|
+
type AccessRequest,
|
|
4
|
+
type CallAccess,
|
|
5
|
+
unscoped,
|
|
6
|
+
} from './access.js';
|
|
7
|
+
export {
|
|
8
|
+
type Contract,
|
|
9
|
+
type Framework,
|
|
10
|
+
framework,
|
|
11
|
+
type ReadUseCase,
|
|
12
|
+
reservedNames,
|
|
13
|
+
} from './build.js';
|
|
14
|
+
export {
|
|
15
|
+
type Collection,
|
|
16
|
+
collection,
|
|
17
|
+
type RelationsOf,
|
|
18
|
+
reservedFields,
|
|
19
|
+
} from './collection.js';
|
|
20
|
+
export { type LatticeMeta, labelOf } from './describe.js';
|
|
21
|
+
export { LatticeError } from './errors.js';
|
|
22
|
+
export {
|
|
23
|
+
type CallContext,
|
|
24
|
+
type Family,
|
|
25
|
+
type FamilyContext,
|
|
26
|
+
family,
|
|
27
|
+
} from './family.js';
|
|
28
|
+
export type { Withheld } from './populate.js';
|
|
29
|
+
export {
|
|
30
|
+
type JoinShape,
|
|
31
|
+
type Many,
|
|
32
|
+
type ManyOnDelete,
|
|
33
|
+
many,
|
|
34
|
+
type OnDelete,
|
|
35
|
+
type Ref,
|
|
36
|
+
ref,
|
|
37
|
+
} from './relations.js';
|
|
38
|
+
export type { ClientDoc, Doc, Repo } from './repo.js';
|
|
39
|
+
export type { Sort } from './sort.js';
|
|
40
|
+
export { CollectionStore, MemoryStore } from './store.js';
|
|
41
|
+
export { type Scope, tenancy } from './tenancy.js';
|
|
42
|
+
export type { Where } from './where.js';
|
package/src/marker.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/** A maskable field as the marker records it: its collection and name. */
|
|
4
|
+
export type Masked = { readonly collection: string; readonly field: string };
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The check that marks a maskable field. It accepts every value, and a
|
|
8
|
+
* clone keeps its checks, so `.pick()`, `.extend()` and `shape.amount`
|
|
9
|
+
* all carry it (as a ref's marker does).
|
|
10
|
+
*/
|
|
11
|
+
type MaskMarker = z.core.$ZodCheck<unknown> & {
|
|
12
|
+
readonly '~lattice-mask': Masked;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
function isMaskMarker(check: unknown): check is MaskMarker {
|
|
16
|
+
return (
|
|
17
|
+
typeof check === 'object' && check !== null && '~lattice-mask' in check
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The field a schema node is marked as, or undefined. */
|
|
22
|
+
export function markOf(schema: z.core.$ZodType): Masked | undefined {
|
|
23
|
+
return (schema._zod.def.checks ?? []).find(isMaskMarker)?.['~lattice-mask'];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* `schema` with the mask marker added: a clone, never the app's schema. The
|
|
28
|
+
* clone keeps the app's meta, such as a `title`, and adds
|
|
29
|
+
* `x-lattice-maskable`, so the field's JSON Schema says it may be withheld.
|
|
30
|
+
*/
|
|
31
|
+
export function marked<T extends z.core.$ZodType>(
|
|
32
|
+
schema: T,
|
|
33
|
+
masked: Masked,
|
|
34
|
+
): T {
|
|
35
|
+
const marker: MaskMarker = Object.assign(
|
|
36
|
+
z.check<unknown>(() => {}),
|
|
37
|
+
{
|
|
38
|
+
'~lattice-mask': masked,
|
|
39
|
+
},
|
|
40
|
+
);
|
|
41
|
+
const { def } = schema._zod;
|
|
42
|
+
// `parent` makes the clone inherit the schema's meta, as Zod's own
|
|
43
|
+
// methods do.
|
|
44
|
+
const clone = z.core.util.clone(
|
|
45
|
+
schema,
|
|
46
|
+
{ ...def, checks: [...(def.checks ?? []), marker] },
|
|
47
|
+
{ parent: true },
|
|
48
|
+
);
|
|
49
|
+
z.globalRegistry.add(clone, {
|
|
50
|
+
...z.globalRegistry.get(clone),
|
|
51
|
+
'x-lattice-maskable': true,
|
|
52
|
+
});
|
|
53
|
+
return clone;
|
|
54
|
+
}
|