-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.ts
More file actions
389 lines (367 loc) · 13 KB
/
view.ts
File metadata and controls
389 lines (367 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import type {
DbRelationship,
DbSchema,
EntityDefinition,
EntityName,
EntityOutput,
} from "./schema.ts";
/** Brand symbol for {@link DbView} values. Used by the runtime to detect
* view instances and to keep `pickView` / `materializeView` honest about
* nested projections. */
export const dbViewSymbol = Symbol("tanstackstart-db.view");
type EntityRecord<Schema extends DbSchema, Name extends EntityName<Schema>> =
EntityOutput<Schema["entities"][Name]> extends Record<string, unknown>
? EntityOutput<Schema["entities"][Name]>
: never;
/**
* A view-selection literal. Use `true` to include a field, or a nested
* {@link DbView} to project through a relationship.
*
* @typeParam Entity - the row shape this selection projects from.
* @typeParam Relationships - the entity's declared relationships; the
* selection only allows nested views on declared relationship names.
*
* @example
* ```ts
* const userCard = db.view("user", { id: true, name: true });
* const postCard = db.view("post", { id: true, title: true, author: userCard });
* ```
*/
export type ViewSelection<
Entity extends Record<string, unknown>,
Relationships extends Record<string, DbRelationship> = Record<never, never>,
> = {
readonly [Key in keyof Entity]?: true;
} & RelationshipViewSelection<Relationships>;
type RelationshipViewSelection<Relationships extends Record<string, DbRelationship>> =
string extends keyof Relationships
? Record<never, never>
: {
readonly [Key in keyof Relationships]?: DbView<
unknown,
unknown,
Relationships[Key]["entity"]
>;
};
/**
* A schema-validated view. Use `db.view(entityName, selection)` to
* construct one against a {@link DbSchema}, or `defineView(schema, ...)`
* outside of a `StartDb` context. The `Entity`, `Result`, and `Name`
* generics are inferred from the schema and selection literal.
*
* @typeParam Entity - the unprojected row shape; informational.
* @typeParam Result - the projected result of applying the view; this is
* what `q.<entity>...as(view).execute()` returns.
* @typeParam Name - the literal entity name.
*/
export interface DbView<Entity = unknown, Result = unknown, Name extends string = string> {
readonly [dbViewSymbol]: true;
readonly entity: Name;
readonly selection: Readonly<Record<string, true | DbView>>;
readonly __entity?: Entity;
readonly __result?: Result;
}
/**
* The result type of a view selection. For each key in the selection:
* - if the key is a field on the entity, the result has that field's type;
* - if the key is a relationship with a `DbView` selection, the result has
* the view's `Result` (or `Result | undefined` for `one` relationships,
* `ReadonlyArray<Result>` for `many` relationships).
*
* @typeParam Entity - the unprojected row shape.
* @typeParam Selection - the literal selection passed to `db.view(...)`.
* @typeParam Relationships - the entity's declared relationships.
*/
export type SelectionResult<
Entity extends Record<string, unknown>,
Selection extends object,
Relationships extends Record<string, DbRelationship> = Record<never, never>,
> = Readonly<{
[Key in keyof Selection]: Key extends keyof Entity
? Entity[Key]
: Key extends keyof Relationships
? Selection[Key] extends DbView<unknown, infer Result>
? Relationships[Key]["kind"] extends "many"
? ReadonlyArray<Result>
: Result | undefined
: never
: never;
}>;
/** The projected result of a {@link DbView}. */
export type InferView<View extends DbView> =
View extends DbView<unknown, infer Result> ? Result : never;
/** The unprojected row shape that a {@link DbView} applies to. */
export type InferViewEntity<View extends DbView> =
View extends DbView<infer Entity, unknown> ? Entity : never;
/** A `Partial` of {@link InferView}. Useful for action inputs that only
* patch a subset of the view's projected fields. */
export type InferViewInput<View extends DbView> = Partial<InferView<View>>;
/**
* Define a {@link DbView} against a {@link DbSchema}. `db.view(...)` is the
* idiomatic entry point for `StartDb` consumers; this function exists for
* cases where the schema is not yet bound to a `StartDb`.
*
* @typeParam Schema - the schema being projected against.
* @typeParam Name - the entity name; must be a key of `Schema["entities"]`.
* @typeParam Selection - the literal view selection.
*
* @param schema - the schema declaring the target entity.
* @param entityName - the literal entity name.
* @param selection - the literal view selection. `true` includes a field;
* a nested `DbView` projects through a relationship.
* @returns a {@link DbView} whose `Result` type is the projected shape.
* @throws when a relationship key in the selection does not match a
* declared relationship on the target entity.
*
* @example
* ```ts
* const userCard = defineView(schema, "user", { id: true, name: true });
* const postCard = defineView(schema, "post", { id: true, title: true, author: userCard });
* ```
*/
export function defineView<
Schema extends DbSchema,
Name extends EntityName<Schema>,
const Selection extends ViewSelection<
EntityRecord<Schema, Name>,
Schema["entities"][Name]["relationships"]
>,
>(
schema: Schema,
entityName: Name,
selection: Selection,
): DbView<
EntityRecord<Schema, Name>,
SelectionResult<EntityRecord<Schema, Name>, Selection, Schema["entities"][Name]["relationships"]>,
Name
> {
validateNestedViews(
schema.entities[entityName],
selection as Readonly<Record<string, true | DbView>>,
);
return {
[dbViewSymbol]: true,
entity: entityName,
selection: selection as Readonly<Record<string, true | DbView>>,
};
}
function validateNestedViews(
definition: EntityDefinition,
selection: Readonly<Record<string, true | DbView>>,
): void {
for (const [name, nested] of Object.entries(selection)) {
if (nested === true) continue;
const relationship = definition.relationships[name];
if (!relationship) {
throw new Error(`View relationship "${name}" is not defined.`);
}
if (relationship.entity !== nested.entity) {
throw new Error(
`View relationship "${name}" targets "${relationship.entity}", not "${nested.entity}".`,
);
}
}
}
/**
* Define a literal view selection without binding it to a schema. Useful
* for declaring reusable selection fragments that are later composed with
* `defineView(...)` or `db.view(...)`.
*
* @typeParam Selection - the literal selection record. Inferred from the
* argument.
*
* @example
* ```ts
* const idOnly = defineViewFragment({ id: true });
* const postCard = db.view("post", { ...idOnly, title: true });
* ```
*/
export function defineViewFragment<const Selection extends Readonly<Record<string, true | DbView>>>(
selection: Selection,
): Selection {
return selection;
}
/** Alias of {@link defineViewFragment}. Prefer `defineView` for new code. */
export const defineProjection = defineViewFragment;
/**
* Apply a view's selection to a row. The source row's `true`-selected
* fields are copied through; nested `DbView` selections recurse into
* relationship rows. `null` and `undefined` values pass through unchanged.
*
* @typeParam View - the view being applied; the `Entity` generic infers the
* row shape and the `Result` generic carries through to the return.
*
* @param view - the view to apply.
* @param value - the row to project.
* @returns the projected result. The type is `InferView<View>`.
*
* @example
* ```ts
* const card = db.view("user", { id: true, name: true });
* pickView(card, { id: "u1", name: "Alice", email: "alice@example.com" });
* // => { id: "u1", name: "Alice" }
* ```
*/
export function pickView<View extends DbView>(
view: View,
value: InferViewEntity<View>,
): InferView<View> {
const source = value as Record<string, unknown>;
const selection = view.selection;
let hasNested = false;
for (const nested of Object.values(selection)) {
if (nested !== true) {
hasNested = true;
break;
}
}
if (!hasNested) {
const result: Record<string, unknown> = {};
for (const key of Object.keys(selection)) {
result[key] = source[key];
}
return result as InferView<View>;
}
const result: Record<string, unknown> = {};
for (const key of Object.keys(selection)) {
const selected = source[key];
const nestedView = selection[key];
if (nestedView === true) {
result[key] = selected;
} else if (selected == null) {
result[key] = selected;
} else if (Array.isArray(selected)) {
result[key] = selected.map((item) =>
pickView(nestedView as DbView, item as InferViewEntity<typeof nestedView>),
);
} else {
result[key] = pickView(nestedView as DbView, selected as InferViewEntity<typeof nestedView>);
}
}
return result as InferView<View>;
}
/** Alias of {@link pickView}. */
export const maskView = pickView;
/**
* Build a `(row) => projected` function for a view. Equivalent to
* `pickView.bind(null, view)` but more readable at call sites.
*
* @typeParam View - the view type; carried through to the return.
*
* @example
* ```ts
* const card = db.view("user", { id: true, name: true });
* const project = withView(card);
* project({ id: "u1", name: "Alice", email: "x" }); // => { id: "u1", name: "Alice" }
* ```
*/
export function withView<View extends DbView>(
view: View,
): (value: InferViewEntity<View>) => InferView<View> {
return (value) => pickView(view, value);
}
/**
* Type-guard that a value has every key a view expects. Returns `true`
* when `value` is a non-null object and every key in `view.selection` is
* present in `value`. The check is structural; use {@link pickView} for
* actual projection.
*
* @example
* ```ts
* const card = db.view("user", { id: true, name: true });
* satisfiesView(card, { id: "u1", name: "Alice" }); // => true
* satisfiesView(card, { id: "u1" }); // => false
* ```
*/
export function satisfiesView<View extends DbView>(
view: View,
value: unknown,
): value is InferView<View> {
if (typeof value !== "object" || value === null) {
return false;
}
return Object.keys(view.selection).every((key) => key in value);
}
/**
* Deeply project a row through a view and return a runtime-frozen
* value. Useful at component boundaries where you want to pass the
* projected result into `React.memo` children or hand it to a worker
* without risking accidental mutation.
*
* The static type is unchanged from {@link pickView}: the projection is
* already `Readonly<...>` at the type level. `freezeView` adds a
* runtime `Object.freeze` to the top level and to every nested
* relationship projection. The original `value` is not mutated.
*
* @typeParam View - the view being applied; the `Entity` generic
* infers the row shape and the `Result` generic carries through.
*
* @param view - the view to apply.
* @param value - the row to project.
* @returns the projected, deeply-frozen result.
*
* @example
* ```ts
* const card = db.view("user", { id: true, name: true });
* const frozen = freezeView(card, user);
* // frozen.name = "Bob"; // throws in strict mode
* ```
*/
export function freezeView<View extends DbView>(
view: View,
value: InferViewEntity<View>,
): InferView<View> {
const projected = pickView(view, value);
deepFreeze(projected as Record<string, unknown>);
return projected;
}
function deepFreeze(value: unknown): void {
if (value === null || typeof value !== "object") return;
if (Object.isFrozen(value)) return;
Object.freeze(value);
for (const nested of Object.values(value as Record<string, unknown>)) {
deepFreeze(nested);
}
}
/**
* `true` if the view contains at least one nested relationship
* projection. Used by the native-join compiler to decide whether a
* selection can be folded into a TanStack DB `select` projection or
* whether the relationships must be resolved post-execution.
*/
export function hasNestedViews(view: DbView): boolean {
for (const nested of Object.values(view.selection)) {
if (nested !== true) return true;
}
return false;
}
/**
* Compile a view into a native TanStack DB `select` callback. Used by
* `as(View)` for views that do not have foldable nested joins.
*
* @typeParam Refs - the alias map passed to a `select` callback by
* TanStack DB; usually `{ [rowAlias]: row }`.
*
* @param view - the view to compile.
* @param rowAlias - the alias of the source row in the `select` callback.
* @returns a `select` callback that projects the view's `true` keys from
* `refs[rowAlias]`.
*/
export function compileViewSelect<Refs extends Record<string, unknown>>(
view: DbView,
rowAlias: string,
): (refs: Refs) => Record<string, unknown> {
return (refs) => {
const row = refs[rowAlias as keyof Refs] as Record<string, unknown> | undefined;
const result: Record<string, unknown> = {};
for (const [key, nested] of Object.entries(view.selection)) {
if (row) {
result[key] = row[key];
} else {
result[key] = undefined;
void nested;
}
}
return result;
};
}