@hyper-fetch/db 0.0.0-stage → 1.0.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 +180 -2
- package/__tests__/e2e/db.e2e.spec.ts +269 -0
- package/__tests__/features/connection/connection.spec.ts +23 -0
- package/__tests__/features/model/live-query.model.spec.ts +320 -0
- package/__tests__/features/operators/operators.spec.ts +113 -0
- package/__tests__/features/runtime/runtime.spec.ts +185 -0
- package/__tests__/features/serializer/serializer.spec.ts +73 -0
- package/__tests__/features/store/store.spec.ts +147 -0
- package/__tests__/features/types/db.types.spec.ts +337 -0
- package/__tests__/integration/audit.spec.ts +639 -0
- package/__tests__/integration/competitors.spec.ts +393 -0
- package/__tests__/integration/convergence.model.spec.ts +162 -0
- package/__tests__/integration/edge-cases.spec.ts +465 -0
- package/__tests__/integration/errors.spec.ts +178 -0
- package/__tests__/integration/infinite.model.spec.ts +142 -0
- package/__tests__/integration/infinite.spec.ts +290 -0
- package/__tests__/integration/inspect.spec.ts +103 -0
- package/__tests__/integration/joins-many.spec.ts +177 -0
- package/__tests__/integration/joins.spec.ts +179 -0
- package/__tests__/integration/key-loaders.spec.ts +196 -0
- package/__tests__/integration/links.spec.ts +105 -0
- package/__tests__/integration/mutations.spec.ts +337 -0
- package/__tests__/integration/normalize.spec.ts +80 -0
- package/__tests__/integration/offline.spec.ts +479 -0
- package/__tests__/integration/queries.spec.ts +513 -0
- package/__tests__/integration/races.spec.ts +127 -0
- package/__tests__/integration/server-operators.spec.ts +123 -0
- package/__tests__/integration/ssr.spec.ts +111 -0
- package/__tests__/integration/syncs.spec.ts +564 -0
- package/__tests__/integration/transactions.spec.ts +344 -0
- package/__tests__/integration/windows.spec.ts +192 -0
- package/__tests__/tsconfig.json +11 -0
- package/__tests__/type-budget/budget.fixture.ts +2476 -0
- package/__tests__/type-budget/tsconfig.json +4 -0
- package/__tests__/utils/fixtures.ts +225 -0
- package/__tests__/utils/random.ts +19 -0
- package/__tests__/vitest.setup.ts +11 -0
- package/dist/collection/collection.d.ts +111 -0
- package/dist/collection/collection.d.ts.map +1 -0
- package/dist/collection/collection.types.d.ts +316 -0
- package/dist/collection/collection.types.d.ts.map +1 -0
- package/dist/collection/resource.types.d.ts +77 -0
- package/dist/collection/resource.types.d.ts.map +1 -0
- package/dist/db/collection.runtime.d.ts +98 -0
- package/dist/db/collection.runtime.d.ts.map +1 -0
- package/dist/db/db.d.ts +154 -0
- package/dist/db/db.d.ts.map +1 -0
- package/dist/db/db.runtime.d.ts +114 -0
- package/dist/db/db.runtime.d.ts.map +1 -0
- package/dist/db/db.types.d.ts +162 -0
- package/dist/db/db.types.d.ts.map +1 -0
- package/dist/graphql/connection.d.ts +25 -0
- package/dist/graphql/connection.d.ts.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +4220 -0
- package/dist/index.mjs.map +1 -0
- package/dist/mutation/mutation.d.ts +136 -0
- package/dist/mutation/mutation.d.ts.map +1 -0
- package/dist/mutation/mutation.errors.d.ts +6 -0
- package/dist/mutation/mutation.errors.d.ts.map +1 -0
- package/dist/mutation/mutation.utils.d.ts +35 -0
- package/dist/mutation/mutation.utils.d.ts.map +1 -0
- package/dist/operators/operators.d.ts +33 -0
- package/dist/operators/operators.d.ts.map +1 -0
- package/dist/operators/operators.types.d.ts +68 -0
- package/dist/operators/operators.types.d.ts.map +1 -0
- package/dist/operators/operators.utils.d.ts +19 -0
- package/dist/operators/operators.utils.d.ts.map +1 -0
- package/dist/persistence/persistence.d.ts +42 -0
- package/dist/persistence/persistence.d.ts.map +1 -0
- package/dist/persistence/serializer.d.ts +23 -0
- package/dist/persistence/serializer.d.ts.map +1 -0
- package/dist/persistence/snapshot.d.ts +29 -0
- package/dist/persistence/snapshot.d.ts.map +1 -0
- package/dist/persistence/tabs.d.ts +53 -0
- package/dist/persistence/tabs.d.ts.map +1 -0
- package/dist/query/infinite-query.d.ts +59 -0
- package/dist/query/infinite-query.d.ts.map +1 -0
- package/dist/query/live-query.d.ts +85 -0
- package/dist/query/live-query.d.ts.map +1 -0
- package/dist/query/planner.d.ts +57 -0
- package/dist/query/planner.d.ts.map +1 -0
- package/dist/query/query.d.ts +155 -0
- package/dist/query/query.d.ts.map +1 -0
- package/dist/query/query.types.d.ts +71 -0
- package/dist/query/query.types.d.ts.map +1 -0
- package/dist/schema/schema.types.d.ts +33 -0
- package/dist/schema/schema.types.d.ts.map +1 -0
- package/dist/store/store.d.ts +76 -0
- package/dist/store/store.d.ts.map +1 -0
- package/dist/store/store.types.d.ts +27 -0
- package/dist/store/store.types.d.ts.map +1 -0
- package/dist/subset/base-subset.d.ts +66 -0
- package/dist/subset/base-subset.d.ts.map +1 -0
- package/dist/subset/key-subset.d.ts +23 -0
- package/dist/subset/key-subset.d.ts.map +1 -0
- package/dist/subset/list-subset.d.ts +63 -0
- package/dist/subset/list-subset.d.ts.map +1 -0
- package/dist/sync/sync.d.ts +42 -0
- package/dist/sync/sync.d.ts.map +1 -0
- package/dist/utils/request.utils.d.ts +11 -0
- package/dist/utils/request.utils.d.ts.map +1 -0
- package/package.json +71 -4
- package/project.json +4 -0
- package/scripts/docs-typescript.mjs +24 -0
- package/scripts/type-budget.mjs +37 -0
- package/src/collection/collection.ts +330 -0
- package/src/collection/collection.types.ts +383 -0
- package/src/collection/resource.types.ts +114 -0
- package/src/db/collection.runtime.ts +572 -0
- package/src/db/db.runtime.ts +537 -0
- package/src/db/db.ts +451 -0
- package/src/db/db.types.ts +157 -0
- package/src/graphql/connection.ts +23 -0
- package/src/index.ts +16 -0
- package/src/mutation/mutation.errors.ts +7 -0
- package/src/mutation/mutation.ts +776 -0
- package/src/mutation/mutation.utils.ts +58 -0
- package/src/operators/operators.ts +90 -0
- package/src/operators/operators.types.ts +67 -0
- package/src/operators/operators.utils.ts +169 -0
- package/src/persistence/persistence.ts +168 -0
- package/src/persistence/serializer.ts +123 -0
- package/src/persistence/snapshot.ts +76 -0
- package/src/persistence/tabs.ts +86 -0
- package/src/query/infinite-query.ts +206 -0
- package/src/query/live-query.ts +552 -0
- package/src/query/planner.ts +199 -0
- package/src/query/query.ts +319 -0
- package/src/query/query.types.ts +90 -0
- package/src/schema/schema.types.ts +29 -0
- package/src/store/store.ts +300 -0
- package/src/store/store.types.ts +17 -0
- package/src/subset/base-subset.ts +169 -0
- package/src/subset/key-subset.ts +44 -0
- package/src/subset/list-subset.ts +328 -0
- package/src/sync/sync.ts +99 -0
- package/src/utils/request.utils.ts +67 -0
- package/tsconfig.json +12 -0
- package/vite.config.ts +71 -0
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExtractParamsType,
|
|
3
|
+
ExtractPayloadType,
|
|
4
|
+
ExtractQueryParamsType,
|
|
5
|
+
ExtractResponseType,
|
|
6
|
+
RequestInstance,
|
|
7
|
+
} from "@hyper-fetch/core";
|
|
8
|
+
import type { ExtractListenerDeliveredType, ListenerInstance } from "@hyper-fetch/sockets";
|
|
9
|
+
|
|
10
|
+
import type { ComparisonNode, ComparisonOperator } from "../operators/operators.types";
|
|
11
|
+
import type { StandardSchemaV1 } from "../schema/schema.types";
|
|
12
|
+
|
|
13
|
+
export type SortDirection = "asc" | "desc";
|
|
14
|
+
|
|
15
|
+
/** Fields whose values can be a key: strings and numbers */
|
|
16
|
+
export type KeyFieldOf<Row> = {
|
|
17
|
+
[Field in keyof Row]-?: NonNullable<Row[Field]> extends string | number ? Field : never;
|
|
18
|
+
}[keyof Row] &
|
|
19
|
+
string;
|
|
20
|
+
|
|
21
|
+
export type RowKey = string | number;
|
|
22
|
+
|
|
23
|
+
type PartialIfObject<T> = T extends Record<string, unknown> ? Partial<T> : T;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* What a loader, mapping or mutation adds to a request: path params, query params and payload, each checked against
|
|
27
|
+
* the request's own types. Objects are merged into what the request already has.
|
|
28
|
+
*/
|
|
29
|
+
export type RequestPatch<Req> = {
|
|
30
|
+
params?: PartialIfObject<NonNullable<ExtractParamsType<Req>>>;
|
|
31
|
+
queryParams?: PartialIfObject<NonNullable<ExtractQueryParamsType<Req>>>;
|
|
32
|
+
payload?: PartialIfObject<NonNullable<ExtractPayloadType<Req>>>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** A patch without request types, used by the db-wide default notation */
|
|
36
|
+
export type AnyRequestPatch = {
|
|
37
|
+
params?: Record<string, unknown>;
|
|
38
|
+
queryParams?: Record<string, unknown> | string | readonly unknown[];
|
|
39
|
+
payload?: unknown;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/** What a sync adds to a listener: topic params, a subscription payload or listener query params */
|
|
43
|
+
export type ListenerPatch = {
|
|
44
|
+
params?: Record<string, unknown>;
|
|
45
|
+
payload?: unknown;
|
|
46
|
+
queryParams?: unknown;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** How one field's operators reach the backend. `true` uses the db-wide default notation. */
|
|
50
|
+
export type FieldWhereDeclaration<Value, Req> = {
|
|
51
|
+
eq?: ((value: Value) => RequestPatch<Req>) | true;
|
|
52
|
+
gt?: ((value: NonNullable<Value>) => RequestPatch<Req>) | true;
|
|
53
|
+
gte?: ((value: NonNullable<Value>) => RequestPatch<Req>) | true;
|
|
54
|
+
lt?: ((value: NonNullable<Value>) => RequestPatch<Req>) | true;
|
|
55
|
+
lte?: ((value: NonNullable<Value>) => RequestPatch<Req>) | true;
|
|
56
|
+
inArray?: ((values: Value[]) => RequestPatch<Req>) | true;
|
|
57
|
+
/** The endpoint cannot work without this filter, such as `teamId` in `/teams/:teamId/posts` */
|
|
58
|
+
required?: true;
|
|
59
|
+
} & {
|
|
60
|
+
/** Server-only operators by name, such as `search`, which queries use through `op("search", field, value)` */
|
|
61
|
+
[operator: string]: ((value: never) => RequestPatch<Req>) | true | undefined;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export type WhereDeclaration<Row, Req> = { [Field in keyof Row & string]?: FieldWhereDeclaration<Row[Field], Req> };
|
|
65
|
+
|
|
66
|
+
export type OrderDeclaration<Row, Req> = {
|
|
67
|
+
[Field in keyof Row & string]?: ((direction: SortDirection) => RequestPatch<Req>) | true;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
type ResponseItem<Response> = Response extends readonly (infer Item)[] ? Item : Response;
|
|
71
|
+
|
|
72
|
+
/** A type argument that was not inferred is `unknown`: then use the fallback */
|
|
73
|
+
export type Inferred<T, Fallback> = unknown extends T ? Fallback : T;
|
|
74
|
+
|
|
75
|
+
/** Whether an optional option was given, judged by whether its type argument was inferred */
|
|
76
|
+
type IsGiven<T> = unknown extends T ? false : true;
|
|
77
|
+
|
|
78
|
+
/** The rows a loader writes when it has no `rows` mapping: the elements of a list, or the response itself */
|
|
79
|
+
export type DefaultRows<Req> = ResponseItem<ExtractResponseType<Req>>;
|
|
80
|
+
|
|
81
|
+
/** Whether mapped rows are exactly the collection's rows: nothing missing, nothing extra */
|
|
82
|
+
export type IsExactRow<Mapped, Row> = [Mapped] extends [Row]
|
|
83
|
+
? [Exclude<keyof Mapped, keyof Row>] extends [never]
|
|
84
|
+
? true
|
|
85
|
+
: false
|
|
86
|
+
: false;
|
|
87
|
+
|
|
88
|
+
/** Whether a partial write carries the key and only row fields of the same types */
|
|
89
|
+
export type IsPartialRow<Mapped, Row, KeyField extends keyof Row> = [Mapped] extends [
|
|
90
|
+
Partial<Row> & Required<Pick<Row, KeyField>>,
|
|
91
|
+
]
|
|
92
|
+
? [Exclude<keyof Mapped, keyof Row>] extends [never]
|
|
93
|
+
? true
|
|
94
|
+
: false
|
|
95
|
+
: false;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* What a collection method returns when its rows do not match the collection's row type. It has no collection
|
|
99
|
+
* methods, so chaining on it and passing it to `createDb` do not compile.
|
|
100
|
+
*/
|
|
101
|
+
export type RowMismatch<Received, Expected> = {
|
|
102
|
+
readonly rowMismatch: { readonly received: Received; readonly expected: Expected };
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* How a backend writes every operator and order, shipped by an adapter preset such as `firestoreCollection`. A loader
|
|
107
|
+
* with a notation can send any condition and order on any field.
|
|
108
|
+
*/
|
|
109
|
+
export type Notation = {
|
|
110
|
+
where?: Partial<Record<ComparisonOperator, (field: string, value: unknown) => AnyRequestPatch>>;
|
|
111
|
+
orderBy?: (field: string, direction: SortDirection) => AnyRequestPatch;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export type LoaderOptions<Row, Key, Req, Mapped, Where, Order, Cursor, Limit, ByKey, ByKeys, LoaderNotation> = {
|
|
115
|
+
/** How the backend writes every operator and order, so the loader sends conditions on any field */
|
|
116
|
+
notation?: LoaderNotation & Notation;
|
|
117
|
+
/**
|
|
118
|
+
* Reads complete rows from a response, for example from a wrapped list such as `{ data: { items }, meta }` or by
|
|
119
|
+
* reshaping each item. Without it, the response is the list of rows, or the row.
|
|
120
|
+
*/
|
|
121
|
+
rows?: (response: ExtractResponseType<Req>) => readonly Mapped[];
|
|
122
|
+
/** The operators the endpoint can serve for each field */
|
|
123
|
+
where?: Where & WhereDeclaration<Row, Req>;
|
|
124
|
+
/** The fields the endpoint can sort by */
|
|
125
|
+
orderBy?: Order & OrderDeclaration<Row, Req>;
|
|
126
|
+
/** Reads the next page's cursor from a response. Declaring it makes the loader paginated. */
|
|
127
|
+
cursor?: (response: ExtractResponseType<Req>) => Cursor | null | undefined;
|
|
128
|
+
/** Sends a cursor to load the page after it */
|
|
129
|
+
page?: (cursor: Cursor) => RequestPatch<Req>;
|
|
130
|
+
/** Sends a query's limit. Declaring it makes the loader paginated. */
|
|
131
|
+
limit?: Limit & ((limit: number) => RequestPatch<Req>);
|
|
132
|
+
/** Conditions the endpoint applies on its own, such as `{ archived: false }` */
|
|
133
|
+
fixed?: Partial<Row>;
|
|
134
|
+
/** Fields the backend compares differently from the client, so queries on them are never answered locally */
|
|
135
|
+
serverOnly?: readonly (keyof Row & string)[];
|
|
136
|
+
/** Reads one row by key. A loader with it serves `findOne`. */
|
|
137
|
+
byKey?: ByKey & ((key: Key) => RequestPatch<Req>);
|
|
138
|
+
/** Reads several rows by key in one request. A loader with it serves `findOne`, batched. */
|
|
139
|
+
byKeys?: ByKeys & ((keys: Key[]) => RequestPatch<Req>);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/** What the types know about a loader that lists rows */
|
|
143
|
+
export type ListLoaderDescriptor<
|
|
144
|
+
Required extends string = string,
|
|
145
|
+
Tokens extends string = string,
|
|
146
|
+
Order extends string = string,
|
|
147
|
+
Paginated extends boolean = boolean,
|
|
148
|
+
> = {
|
|
149
|
+
kind: "list";
|
|
150
|
+
required: Required;
|
|
151
|
+
tokens: Tokens;
|
|
152
|
+
order: Order;
|
|
153
|
+
paginated: Paginated;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/** What the types know about a loader that reads rows by key */
|
|
157
|
+
export type KeyLoaderDescriptor = { kind: "key" };
|
|
158
|
+
|
|
159
|
+
export type LoaderDescriptor = ListLoaderDescriptor | KeyLoaderDescriptor;
|
|
160
|
+
|
|
161
|
+
type RequiredFields<Where> = {
|
|
162
|
+
[Field in keyof Where & string]: Where[Field] extends { required: true } ? Field : never;
|
|
163
|
+
}[keyof Where & string];
|
|
164
|
+
|
|
165
|
+
type WhereTokens<Where> = {
|
|
166
|
+
[Field in keyof Where & string]: `${Field}:${Exclude<keyof Where[Field], "required"> & string}`;
|
|
167
|
+
}[keyof Where & string];
|
|
168
|
+
|
|
169
|
+
export type DescribeLoader<Row, Where, Order, Cursor, Limit, ByKey, ByKeys, LoaderNotation> =
|
|
170
|
+
IsGiven<ByKey> extends true
|
|
171
|
+
? KeyLoaderDescriptor
|
|
172
|
+
: IsGiven<ByKeys> extends true
|
|
173
|
+
? KeyLoaderDescriptor
|
|
174
|
+
: ListLoaderDescriptor<
|
|
175
|
+
RequiredFields<Inferred<Where, {}>>,
|
|
176
|
+
| WhereTokens<Inferred<Where, {}>>
|
|
177
|
+
| (IsGiven<LoaderNotation> extends true ? `${keyof Row & string}:${ComparisonOperator}` : never),
|
|
178
|
+
(keyof Inferred<Order, {}> & string) | (IsGiven<LoaderNotation> extends true ? keyof Row & string : never),
|
|
179
|
+
IsGiven<Cursor> extends true ? true : IsGiven<Limit>
|
|
180
|
+
>;
|
|
181
|
+
|
|
182
|
+
export type SyncType = "insert" | "update" | "delete";
|
|
183
|
+
|
|
184
|
+
export type SyncMessage<Listener> = ExtractListenerDeliveredType<Listener>;
|
|
185
|
+
|
|
186
|
+
export type SyncOptions<Row, KeyField extends keyof Row, Listener, Type extends SyncType, Mapped> = {
|
|
187
|
+
type: Type;
|
|
188
|
+
/** Maps a query's filters to listener params or a subscription payload */
|
|
189
|
+
where?: { [Field in keyof Row & string]?: { eq: (value: Row[Field]) => ListenerPatch } };
|
|
190
|
+
/** Listener params that keep one row read by key live, such as a channel per record */
|
|
191
|
+
byKey?: (key: NonNullable<Row[KeyField]>) => ListenerPatch;
|
|
192
|
+
/** Reshapes a message into a row, or into the key and changed fields for `update` */
|
|
193
|
+
row?: (message: SyncMessage<Listener>) => Mapped;
|
|
194
|
+
/** Reads the key of a deleted row from a message */
|
|
195
|
+
key?: (message: SyncMessage<Listener>) => NonNullable<Row[KeyField]>;
|
|
196
|
+
/** Loads the row by key when an `update` arrives for a row the collection does not hold */
|
|
197
|
+
incomplete?: "refetch";
|
|
198
|
+
/** The adapter resumes the subscription from its last event id after a reconnect, so nothing is missed */
|
|
199
|
+
resumes?: boolean;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
export type IsSyncRow<Row, KeyField extends keyof Row, Type extends SyncType, Mapped> = Type extends "insert"
|
|
203
|
+
? IsExactRow<Mapped, Row>
|
|
204
|
+
: Type extends "update"
|
|
205
|
+
? IsPartialRow<Mapped, Row, KeyField>
|
|
206
|
+
: true;
|
|
207
|
+
|
|
208
|
+
/** The event a listener callback receives: `data` plus adapter details such as Firestore's grouped changes */
|
|
209
|
+
export type SyncEvent<Listener> = Listener extends { listen: (callback: infer Callback, ...rest: any[]) => any }
|
|
210
|
+
? Callback extends (event: infer Event) => any
|
|
211
|
+
? Event
|
|
212
|
+
: never
|
|
213
|
+
: never;
|
|
214
|
+
|
|
215
|
+
export type SyncChangesOptions<Row, KeyField extends keyof Row, Listener, Mapped> = {
|
|
216
|
+
/** Reads the complete rows to write and the keys to delete from one event */
|
|
217
|
+
changes: (event: SyncEvent<Listener>) => {
|
|
218
|
+
upsert: readonly Mapped[];
|
|
219
|
+
delete: readonly NonNullable<Row[KeyField]>[];
|
|
220
|
+
/** Rows to load again by key, such as documents that left a filtered query and may have changed or gone */
|
|
221
|
+
refetch?: readonly NonNullable<Row[KeyField]>[];
|
|
222
|
+
};
|
|
223
|
+
/** Turns a subset's sent conditions into listener params, so the sync covers every subset */
|
|
224
|
+
notation?: (conditions: readonly ComparisonNode[]) => ListenerPatch;
|
|
225
|
+
/** Listener params that keep one row read by key live, such as a listener on one document */
|
|
226
|
+
byKey?: (key: NonNullable<Row[KeyField]>) => ListenerPatch;
|
|
227
|
+
/** The adapter resumes the subscription from its last event id after a reconnect, so nothing is missed */
|
|
228
|
+
resumes?: boolean;
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
export type IngestType = "insert" | "update";
|
|
232
|
+
|
|
233
|
+
export type IngestOptions<Req, Mapped, Type> = {
|
|
234
|
+
/** Reads rows from the response, as a loader's `rows` does */
|
|
235
|
+
rows?: (response: ExtractResponseType<Req>) => readonly Mapped[];
|
|
236
|
+
/** `insert` writes complete rows, `update` changes only rows the collection holds */
|
|
237
|
+
type?: Type & IngestType;
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
export type IsIngestRow<Row, KeyField extends keyof Row, Type extends IngestType, Mapped> = Type extends "update"
|
|
241
|
+
? IsPartialRow<Mapped, Row, KeyField>
|
|
242
|
+
: IsExactRow<Mapped, Row>;
|
|
243
|
+
|
|
244
|
+
/** What happens after a mutation request succeeds with a response that is not the row */
|
|
245
|
+
export type MutationSuccessMode = "keep" | "refetch" | "sync";
|
|
246
|
+
|
|
247
|
+
export type MutationContext = {
|
|
248
|
+
/** The transaction id. Send it as an idempotency key, so a request sent twice is applied once. */
|
|
249
|
+
id: string;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
export type InsertMutation<Row, Req, Mapped, Success> = {
|
|
253
|
+
/** `false` when the backend applies writes locally on its own, such as Firestore, so the db adds no optimistic layer */
|
|
254
|
+
optimistic?: boolean;
|
|
255
|
+
/** `false` when the backend queues writes offline on its own, such as Firestore, so they skip the outbox */
|
|
256
|
+
outbox?: boolean;
|
|
257
|
+
request: Req;
|
|
258
|
+
map: (row: Row, context: MutationContext) => RequestPatch<Req>;
|
|
259
|
+
/** Reads the inserted row from the response */
|
|
260
|
+
row?: (response: ExtractResponseType<Req>) => Mapped;
|
|
261
|
+
/** Needed when the response is not the row */
|
|
262
|
+
onSuccess?: Success & MutationSuccessMode;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
export type UpdateMutation<Row, KeyField extends keyof Row, Req, Mapped, Success> = {
|
|
266
|
+
/** `false` when the backend applies writes locally on its own, such as Firestore, so the db adds no optimistic layer */
|
|
267
|
+
optimistic?: boolean;
|
|
268
|
+
/** `false` when the backend queues writes offline on its own, such as Firestore, so they skip the outbox */
|
|
269
|
+
outbox?: boolean;
|
|
270
|
+
request: Req;
|
|
271
|
+
map: (key: NonNullable<Row[KeyField]>, changes: Partial<Row>, context: MutationContext) => RequestPatch<Req>;
|
|
272
|
+
/** Reads the key and changed fields from the response */
|
|
273
|
+
row?: (response: ExtractResponseType<Req>) => Mapped;
|
|
274
|
+
/** Needed when the response is not the row or its changed fields with the key */
|
|
275
|
+
onSuccess?: Success & MutationSuccessMode;
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
export type DeleteMutation<Row, KeyField extends keyof Row, Req> = {
|
|
279
|
+
/** `false` when the backend applies writes locally on its own, such as Firestore, so the db adds no optimistic layer */
|
|
280
|
+
optimistic?: boolean;
|
|
281
|
+
/** `false` when the backend queues writes offline on its own, such as Firestore, so they skip the outbox */
|
|
282
|
+
outbox?: boolean;
|
|
283
|
+
request: Req;
|
|
284
|
+
map: (key: NonNullable<Row[KeyField]>, context: MutationContext) => RequestPatch<Req>;
|
|
285
|
+
onSuccess?: MutationSuccessMode;
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
/** Whether a write's success is handled: it is not declared, its response fits, or `onSuccess` says what to do */
|
|
289
|
+
export type IsWriteHandled<Req, Fits, Success> =
|
|
290
|
+
IsGiven<Req> extends true ? (IsGiven<Success> extends true ? true : Fits) : true;
|
|
291
|
+
|
|
292
|
+
/** The writes a collection can make, and the changes `update` accepts */
|
|
293
|
+
export type MutationDescriptor<Input = unknown, Changes = unknown> = {
|
|
294
|
+
insert?: Input;
|
|
295
|
+
update?: Changes;
|
|
296
|
+
delete?: true;
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
type PayloadChanges<Row, Req> =
|
|
300
|
+
NonNullable<ExtractPayloadType<Req>> extends Record<string, unknown>
|
|
301
|
+
? Partial<Pick<Row, Extract<keyof Row, keyof NonNullable<ExtractPayloadType<Req>>>>>
|
|
302
|
+
: Partial<Row>;
|
|
303
|
+
|
|
304
|
+
export type DescribeMutations<Input, Row, InsertReq, UpdateReq, DeleteReq> = (IsGiven<InsertReq> extends true
|
|
305
|
+
? { insert: Input }
|
|
306
|
+
: {}) &
|
|
307
|
+
(IsGiven<UpdateReq> extends true ? { update: PayloadChanges<Row, UpdateReq> } : {}) &
|
|
308
|
+
(IsGiven<DeleteReq> extends true ? { delete: true } : {});
|
|
309
|
+
|
|
310
|
+
/* Erased runtime configuration */
|
|
311
|
+
|
|
312
|
+
export type LoaderConfig = {
|
|
313
|
+
kind: "list" | "byKey" | "byKeys";
|
|
314
|
+
request: RequestInstance;
|
|
315
|
+
rows?: (response: unknown) => readonly unknown[];
|
|
316
|
+
where?: Record<string, Partial<Record<ComparisonOperator, unknown>> & { required?: true }>;
|
|
317
|
+
notation?: Notation;
|
|
318
|
+
orderBy?: Record<string, unknown>;
|
|
319
|
+
cursor?: (response: unknown) => unknown;
|
|
320
|
+
page?: (cursor: unknown) => AnyRequestPatch;
|
|
321
|
+
limit?: (limit: number) => AnyRequestPatch;
|
|
322
|
+
fixed?: Record<string, unknown>;
|
|
323
|
+
serverOnly?: readonly string[];
|
|
324
|
+
byKey?: (key: RowKey) => AnyRequestPatch;
|
|
325
|
+
byKeys?: (keys: RowKey[]) => AnyRequestPatch;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
export type SyncConfig = {
|
|
329
|
+
listener: ListenerInstance;
|
|
330
|
+
/** `changes` syncs read inserts and deletes from one event, such as a Firestore snapshot */
|
|
331
|
+
type: SyncType | "changes";
|
|
332
|
+
changes?: (event: unknown) => { upsert: readonly unknown[]; delete: readonly RowKey[]; refetch?: readonly RowKey[] };
|
|
333
|
+
/** Covers every subset by turning its sent conditions into listener params, as adapter presets do */
|
|
334
|
+
notation?: (conditions: readonly ComparisonNode[]) => ListenerPatch;
|
|
335
|
+
where?: Record<string, { eq: (value: unknown) => ListenerPatch }>;
|
|
336
|
+
byKey?: (key: RowKey) => ListenerPatch;
|
|
337
|
+
row?: (message: unknown) => unknown;
|
|
338
|
+
key?: (message: unknown) => RowKey;
|
|
339
|
+
incomplete?: "refetch";
|
|
340
|
+
resumes?: boolean;
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
export type IngestConfig = {
|
|
344
|
+
request: RequestInstance;
|
|
345
|
+
rows?: (response: unknown) => readonly unknown[];
|
|
346
|
+
type: IngestType;
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
export type NormalizeConfig = {
|
|
350
|
+
target: CollectionConfig;
|
|
351
|
+
extract: (row: unknown) => readonly unknown[];
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
export type MutationConfig = {
|
|
355
|
+
request: RequestInstance;
|
|
356
|
+
map: (...args: any[]) => AnyRequestPatch;
|
|
357
|
+
row?: (response: unknown) => unknown;
|
|
358
|
+
onSuccess?: MutationSuccessMode;
|
|
359
|
+
optimistic?: boolean;
|
|
360
|
+
outbox?: boolean;
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
export type CollectionConfig = {
|
|
364
|
+
schema?: StandardSchemaV1;
|
|
365
|
+
key: string;
|
|
366
|
+
version?: string;
|
|
367
|
+
validate: "client" | "all";
|
|
368
|
+
loaders: readonly LoaderConfig[];
|
|
369
|
+
syncs: readonly SyncConfig[];
|
|
370
|
+
ingests: readonly IngestConfig[];
|
|
371
|
+
mutations: { insert?: MutationConfig; update?: MutationConfig; delete?: MutationConfig };
|
|
372
|
+
/** Nested rows that complete writes carry into other collections */
|
|
373
|
+
normalize?: readonly NormalizeConfig[];
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
export type CollectionOptions<Row, KeyField extends string> = {
|
|
377
|
+
/** The field that identifies a row */
|
|
378
|
+
key: KeyField;
|
|
379
|
+
/** A field that grows with every change on the server. Writes never replace fields with a newer version. */
|
|
380
|
+
version?: Exclude<keyof Row & string, KeyField>;
|
|
381
|
+
/** `client` validates client writes only, `all` validates data from the server too */
|
|
382
|
+
validate?: "client" | "all";
|
|
383
|
+
};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { ExtractQueryParamsType, ExtractResponseType, ExtractUrlParams, RequestInstance } from "@hyper-fetch/core";
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
DefaultRows,
|
|
5
|
+
Inferred,
|
|
6
|
+
IsExactRow,
|
|
7
|
+
IsPartialRow,
|
|
8
|
+
KeyLoaderDescriptor,
|
|
9
|
+
ListLoaderDescriptor,
|
|
10
|
+
MutationSuccessMode,
|
|
11
|
+
RequestPatch,
|
|
12
|
+
} from "./collection.types";
|
|
13
|
+
|
|
14
|
+
type MethodKey = "$get" | "$post" | "$put" | "$patch" | "$delete";
|
|
15
|
+
|
|
16
|
+
/** The `$param` segment of an SDK resource that reads one row, such as `$postId` */
|
|
17
|
+
export type SdkItemKey<Node> = {
|
|
18
|
+
[Key in keyof Node & `$${string}`]: Key extends MethodKey
|
|
19
|
+
? never
|
|
20
|
+
: Node[Key] extends { $get: RequestInstance }
|
|
21
|
+
? Key
|
|
22
|
+
: never;
|
|
23
|
+
}[keyof Node & `$${string}`];
|
|
24
|
+
|
|
25
|
+
type ListRequest<Node> = Node extends { $get: infer Req } ? Req : never;
|
|
26
|
+
|
|
27
|
+
type EndpointOf<Req> = Req extends { endpoint: infer Endpoint extends string } ? Endpoint : string;
|
|
28
|
+
|
|
29
|
+
/** Path params of the list endpoint that are row fields. They become required `eq` filters. */
|
|
30
|
+
export type ResourcePathFilters<Row, Node> = Extract<
|
|
31
|
+
keyof NonNullable<ExtractUrlParams<EndpointOf<ListRequest<Node>>>>,
|
|
32
|
+
keyof Row
|
|
33
|
+
> &
|
|
34
|
+
string;
|
|
35
|
+
|
|
36
|
+
/** Query params of the list endpoint that are row fields, which `.resource()` can send as `eq` filters */
|
|
37
|
+
export type ResourceQueryFilters<Row, Node> = Extract<
|
|
38
|
+
keyof NonNullable<ExtractQueryParamsType<ListRequest<Node>>>,
|
|
39
|
+
keyof Row
|
|
40
|
+
> &
|
|
41
|
+
string;
|
|
42
|
+
|
|
43
|
+
export type ResourceListOptions<Row, Node, Mapped, Filters, Cursor> = {
|
|
44
|
+
/** Reads complete rows from the list response */
|
|
45
|
+
rows?: (response: ExtractResponseType<ListRequest<Node>>) => readonly Mapped[];
|
|
46
|
+
/** Query params sent as `eq` filters. Only query params that are row fields of the same name are offered. */
|
|
47
|
+
where?: readonly (Filters & ResourceQueryFilters<Row, Node>)[];
|
|
48
|
+
/** Reads the next page's cursor, which makes the list paginated */
|
|
49
|
+
cursor?: (response: ExtractResponseType<ListRequest<Node>>) => Cursor | null | undefined;
|
|
50
|
+
/** Sends a cursor to load the page after it */
|
|
51
|
+
page?: (cursor: Cursor) => RequestPatch<ListRequest<Node>>;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export type ResourceOptions<Row, Node, Item, Mapped, Filters, Cursor, InsertSuccess, UpdateSuccess> = {
|
|
55
|
+
/** The segment that reads one row, such as `"$postId"` for `/posts/:postId` */
|
|
56
|
+
item: Item & SdkItemKey<Node>;
|
|
57
|
+
/** The list loader, or `false` when the list returns another shape and belongs to its own collection */
|
|
58
|
+
list?: false | ResourceListOptions<Row, Node, Mapped, Filters, Cursor>;
|
|
59
|
+
insert?: { onSuccess?: InsertSuccess & MutationSuccessMode };
|
|
60
|
+
update?: { method?: "$patch" | "$put"; onSuccess?: UpdateSuccess & MutationSuccessMode };
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
type ItemNode<Node, Item> = Item extends keyof Node ? Node[Item] : never;
|
|
64
|
+
|
|
65
|
+
type UpdateRequestOf<Node, Item> =
|
|
66
|
+
ItemNode<Node, Item> extends { $patch: infer Req }
|
|
67
|
+
? Req
|
|
68
|
+
: ItemNode<Node, Item> extends { $put: infer Req }
|
|
69
|
+
? Req
|
|
70
|
+
: never;
|
|
71
|
+
|
|
72
|
+
export type ResourceLoaders<Row, Node, Item, Filters, Cursor, ListGiven> =
|
|
73
|
+
| (ListGiven extends true
|
|
74
|
+
? ListLoaderDescriptor<
|
|
75
|
+
ResourcePathFilters<Row, Node>,
|
|
76
|
+
`${ResourcePathFilters<Row, Node> | (Filters & string)}:eq`,
|
|
77
|
+
never,
|
|
78
|
+
unknown extends Cursor ? false : true
|
|
79
|
+
>
|
|
80
|
+
: never)
|
|
81
|
+
| (ItemNode<Node, Item> extends { $get: RequestInstance } ? KeyLoaderDescriptor : never);
|
|
82
|
+
|
|
83
|
+
/** Whether every derived loader and mutation reads rows that fit the collection */
|
|
84
|
+
export type IsResourceValid<
|
|
85
|
+
Row,
|
|
86
|
+
KeyField extends keyof Row,
|
|
87
|
+
Node,
|
|
88
|
+
Item,
|
|
89
|
+
Mapped,
|
|
90
|
+
ListGiven,
|
|
91
|
+
InsertSuccess,
|
|
92
|
+
UpdateSuccess,
|
|
93
|
+
> = [
|
|
94
|
+
ListGiven extends true ? IsExactRow<Inferred<Mapped, DefaultRows<ListRequest<Node>>>, Row> : true,
|
|
95
|
+
ItemNode<Node, Item> extends { $get: infer Req } ? IsExactRow<DefaultRows<Req>, Row> : true,
|
|
96
|
+
Node extends { $post: infer Req }
|
|
97
|
+
? unknown extends InsertSuccess
|
|
98
|
+
? IsExactRow<ExtractResponseType<Req>, Row>
|
|
99
|
+
: true
|
|
100
|
+
: true,
|
|
101
|
+
[UpdateRequestOf<Node, Item>] extends [never]
|
|
102
|
+
? true
|
|
103
|
+
: unknown extends UpdateSuccess
|
|
104
|
+
? IsPartialRow<ExtractResponseType<UpdateRequestOf<Node, Item>>, Row, KeyField>
|
|
105
|
+
: true,
|
|
106
|
+
] extends [true, true, true, true]
|
|
107
|
+
? true
|
|
108
|
+
: false;
|
|
109
|
+
|
|
110
|
+
export type ResourceMutations<Input, Row, Node, Item> = (Node extends { $post: RequestInstance }
|
|
111
|
+
? { insert: Input }
|
|
112
|
+
: {}) &
|
|
113
|
+
([UpdateRequestOf<Node, Item>] extends [never] ? {} : { update: Partial<Row> }) &
|
|
114
|
+
(ItemNode<Node, Item> extends { $delete: RequestInstance } ? { delete: true } : {});
|