@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,147 @@
|
|
|
1
|
+
import { Store } from "../../../src/store/store";
|
|
2
|
+
import type { Stamp } from "../../../src/store/store.types";
|
|
3
|
+
|
|
4
|
+
const sent = (seq: number): Stamp => ({ kind: "sent", seq, wall: seq });
|
|
5
|
+
const now = (seq: number): Stamp => ({ kind: "now", seq, wall: seq });
|
|
6
|
+
|
|
7
|
+
const createStore = (versionField?: string) => {
|
|
8
|
+
const changes: (string | number)[] = [];
|
|
9
|
+
const store = new Store({ keyField: "id", versionField, onChange: (key) => changes.push(key) });
|
|
10
|
+
return { store, changes };
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
describe("Store", () => {
|
|
14
|
+
it("creates rows with complete writes and ignores partial writes to rows it does not hold", () => {
|
|
15
|
+
const { store, changes } = createStore();
|
|
16
|
+
expect(store.write(1, { id: 1, title: "a" }, { complete: false, stamp: sent(1) })).toBeFalse();
|
|
17
|
+
expect(store.write(1, { id: 1, title: "a" }, { complete: true, stamp: sent(2) })).toBeTrue();
|
|
18
|
+
expect(store.write(1, { id: 1, title: "a" }, { complete: true, stamp: sent(3) })).toBeFalse();
|
|
19
|
+
expect(store.getServerKeys()).toStrictEqual([1]);
|
|
20
|
+
expect(changes).toStrictEqual([1]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("keeps fields written after a request was sent", () => {
|
|
24
|
+
const { store } = createStore();
|
|
25
|
+
store.write(1, { id: 1, title: "first", likes: 1 }, { complete: true, stamp: sent(1) });
|
|
26
|
+
store.write(1, { id: 1, title: "event" }, { complete: false, stamp: now(5) });
|
|
27
|
+
store.write(1, { id: 1, title: "stale", likes: 2 }, { complete: true, stamp: sent(4) });
|
|
28
|
+
expect(store.get(1)).toStrictEqual({ id: 1, title: "event", likes: 2 });
|
|
29
|
+
store.write(1, { id: 1, title: "newer" }, { complete: true, stamp: sent(6) });
|
|
30
|
+
expect(store.get(1)?.title).toBe("newer");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("never lets an event applied late, after a buffer, beat a newer write or bring back a deleted row", () => {
|
|
34
|
+
const { store } = createStore();
|
|
35
|
+
store.write(1, { id: 1, title: "response" }, { complete: true, stamp: sent(5) });
|
|
36
|
+
store.write(1, { id: 1, title: "older event" }, { complete: true, stamp: now(3) });
|
|
37
|
+
expect(store.get(1)?.title).toBe("response");
|
|
38
|
+
store.remove(2, sent(7));
|
|
39
|
+
expect(store.write(2, { id: 2 }, { complete: true, stamp: now(6) })).toBeFalse();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("orders writes by version when they carry it", () => {
|
|
43
|
+
const { store } = createStore("version");
|
|
44
|
+
store.write(1, { id: 1, title: "v5", version: 5 }, { complete: true, stamp: sent(1) });
|
|
45
|
+
store.write(1, { id: 1, title: "v3", version: 3 }, { complete: true, stamp: now(2) });
|
|
46
|
+
expect(store.get(1)?.title).toBe("v5");
|
|
47
|
+
store.write(1, { id: 1, title: "v5 again", version: 5 }, { complete: true, stamp: sent(3) });
|
|
48
|
+
expect(store.get(1)?.title).toBe("v5 again");
|
|
49
|
+
store.write(1, { id: 1, likes: 1 }, { complete: false, stamp: now(4) });
|
|
50
|
+
store.write(1, { id: 1, likes: 0 }, { complete: false, stamp: sent(3) });
|
|
51
|
+
expect(store.get(1)?.likes).toBe(1);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("drops the fields a complete write leaves out, unless a newer write set them", () => {
|
|
55
|
+
const { store } = createStore();
|
|
56
|
+
store.write(1, { id: 1, title: "a", note: "x", draft: true }, { complete: true, stamp: sent(1) });
|
|
57
|
+
store.write(1, { id: 1, draft: false }, { complete: false, stamp: now(5) });
|
|
58
|
+
store.write(1, { id: 1, title: "b" }, { complete: true, stamp: sent(3) });
|
|
59
|
+
// `note` was last written before the response, so the response's row has no `note`; `draft` changed after it
|
|
60
|
+
expect(store.get(1)).toStrictEqual({ id: 1, title: "b", draft: false });
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("does not let an older write bring back a deleted row", () => {
|
|
64
|
+
const { store } = createStore();
|
|
65
|
+
store.write(1, { id: 1 }, { complete: true, stamp: sent(1) });
|
|
66
|
+
store.remove(1, now(5));
|
|
67
|
+
expect(store.write(1, { id: 1 }, { complete: true, stamp: sent(4) })).toBeFalse();
|
|
68
|
+
expect(store.write(1, { id: 1 }, { complete: true, stamp: sent(6) })).toBeTrue();
|
|
69
|
+
store.remove(1, now(10));
|
|
70
|
+
expect(store.write(1, { id: 1 }, { complete: true, stamp: now(11) })).toBeTrue();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("keeps the newest delete and never drops a row written after an older delete", () => {
|
|
74
|
+
const { store } = createStore();
|
|
75
|
+
store.remove(1, now(15));
|
|
76
|
+
store.remove(1, sent(10));
|
|
77
|
+
expect(store.write(1, { id: 1 }, { complete: true, stamp: sent(12) })).toBeFalse();
|
|
78
|
+
store.write(2, { id: 2 }, { complete: true, stamp: now(12) });
|
|
79
|
+
store.remove(2, sent(10));
|
|
80
|
+
expect(store.get(2)).toStrictEqual({ id: 2 });
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("drops a field a newer complete version leaves out", () => {
|
|
84
|
+
const { store } = createStore("version");
|
|
85
|
+
store.write(1, { id: 1, note: "x", version: 1 }, { complete: true, stamp: sent(1) });
|
|
86
|
+
store.write(1, { id: 1, version: 2 }, { complete: true, stamp: sent(2) });
|
|
87
|
+
expect(store.get(1)).toStrictEqual({ id: 1, version: 2 });
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("orders writes of an equal version by when they were made", () => {
|
|
91
|
+
const { store } = createStore("version");
|
|
92
|
+
store.write(1, { id: 1, title: "A", version: 3 }, { complete: true, stamp: sent(1) });
|
|
93
|
+
store.write(1, { id: 1, title: "B" }, { complete: false, stamp: now(5) });
|
|
94
|
+
store.write(1, { id: 1, title: "A", version: 3 }, { complete: true, stamp: sent(2) });
|
|
95
|
+
expect(store.get(1)?.title).toBe("B");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("forgets deletes once every earlier request settled", () => {
|
|
99
|
+
const { store } = createStore();
|
|
100
|
+
store.remove(1, now(5));
|
|
101
|
+
store.remove(2, now(9));
|
|
102
|
+
store.forgetTombstones(6);
|
|
103
|
+
expect(store.write(1, { id: 1 }, { complete: true, stamp: sent(1) })).toBeTrue();
|
|
104
|
+
expect(store.write(2, { id: 2 }, { complete: true, stamp: sent(1) })).toBeFalse();
|
|
105
|
+
expect(store.drop(3)).toBeFalse();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("applies optimistic layers over server rows and reverts them", () => {
|
|
109
|
+
const { store } = createStore();
|
|
110
|
+
store.write(1, { id: 1, title: "server" }, { complete: true, stamp: sent(1) });
|
|
111
|
+
store.addLayer({ id: "a", key: 1, type: "update", values: { title: "edit" } });
|
|
112
|
+
store.addLayer({ id: "b", key: 2, type: "update", values: { title: "missing row" } });
|
|
113
|
+
store.addLayer({ id: "c", key: 3, type: "insert", values: { id: 3 } });
|
|
114
|
+
store.addLayer({ id: "d", key: 3, type: "delete", values: {} });
|
|
115
|
+
expect(store.get(1)?.title).toBe("edit");
|
|
116
|
+
expect(store.get(2)).toBeUndefined();
|
|
117
|
+
expect(store.get(3)).toBeUndefined();
|
|
118
|
+
expect(store.keys()).toStrictEqual([1]);
|
|
119
|
+
expect(store.hasLayer(1)).toBeTrue();
|
|
120
|
+
store.removeLayer("a");
|
|
121
|
+
store.removeLayer("unknown");
|
|
122
|
+
expect(store.get(1)?.title).toBe("server");
|
|
123
|
+
expect(store.hasLayer(1)).toBeFalse();
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("moves layers of a temporary key to the server's key", () => {
|
|
127
|
+
const { store } = createStore();
|
|
128
|
+
store.addLayer({ id: "a", key: "tmp", type: "insert", values: { id: "tmp", title: "new" } });
|
|
129
|
+
store.addLayer({ id: "b", key: 5, type: "update", values: { title: "other" } });
|
|
130
|
+
store.rekeyLayers("tmp", 9);
|
|
131
|
+
expect(store.get(9)).toStrictEqual({ id: 9, title: "new" });
|
|
132
|
+
expect(store.get("tmp")).toBeUndefined();
|
|
133
|
+
expect(store.getArrival(9)).toBeNumber();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("clears rows, layers and deletes", () => {
|
|
137
|
+
const { store, changes } = createStore();
|
|
138
|
+
store.write(1, { id: 1 }, { complete: true, stamp: sent(1) });
|
|
139
|
+
store.addLayer({ id: "a", key: 2, type: "insert", values: { id: 2 } });
|
|
140
|
+
store.remove(3, now(2));
|
|
141
|
+
changes.length = 0;
|
|
142
|
+
store.clear();
|
|
143
|
+
expect(store.keys()).toStrictEqual([]);
|
|
144
|
+
expect(changes).toIncludeSameMembers([1, 2]);
|
|
145
|
+
expect(store.write(3, { id: 3 }, { complete: true, stamp: sent(1) })).toBeTrue();
|
|
146
|
+
});
|
|
147
|
+
});
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import { and, collection, createDb, eq, gt, inArray, or, rowType } from "@hyper-fetch/db";
|
|
2
|
+
import type {
|
|
3
|
+
ExtractQueryData,
|
|
4
|
+
ExtractQueryResult,
|
|
5
|
+
InfiniteQueryCheck,
|
|
6
|
+
QueryState,
|
|
7
|
+
ServableQuery,
|
|
8
|
+
} from "@hyper-fetch/db";
|
|
9
|
+
import { expectTypeOf } from "vitest";
|
|
10
|
+
|
|
11
|
+
import type { Post, PostSummary } from "../../utils/fixtures";
|
|
12
|
+
import {
|
|
13
|
+
PostSchema,
|
|
14
|
+
PostSummarySchema,
|
|
15
|
+
createHttpClient,
|
|
16
|
+
createListeners,
|
|
17
|
+
createRequests,
|
|
18
|
+
createSocket,
|
|
19
|
+
} from "../../utils/fixtures";
|
|
20
|
+
|
|
21
|
+
const client = createHttpClient();
|
|
22
|
+
const requests = createRequests(client);
|
|
23
|
+
const listeners = createListeners(createSocket());
|
|
24
|
+
|
|
25
|
+
const byTeam = { teamId: { eq: (teamId: number) => ({ params: { teamId } }), required: true as const } };
|
|
26
|
+
|
|
27
|
+
describe("db [ Types ]", () => {
|
|
28
|
+
describe("collections", () => {
|
|
29
|
+
it("takes the row type from the schema or a type argument", () => {
|
|
30
|
+
const fromSchema = collection(PostSchema, { key: "id" });
|
|
31
|
+
const fromType = collection(rowType<Post>(), { key: "id" });
|
|
32
|
+
expectTypeOf(fromSchema.__types!.row).toEqualTypeOf<Post>();
|
|
33
|
+
expectTypeOf(fromType.__types!.row).toEqualTypeOf<Post>();
|
|
34
|
+
// @ts-expect-error a key must be a string or number field
|
|
35
|
+
collection(PostSchema, { key: "missing" });
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("accepts a loader that returns complete rows", () => {
|
|
39
|
+
const summaries = collection(PostSummarySchema, { key: "id" }).load(requests.getTeamPosts, { where: byTeam });
|
|
40
|
+
const posts = collection(PostSchema, { key: "id" }).load(requests.getPost, {
|
|
41
|
+
byKey: (id) => ({ params: { postId: id } }),
|
|
42
|
+
});
|
|
43
|
+
createDb({ summaries, posts });
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("rejects a loader whose rows lack fields", () => {
|
|
47
|
+
const posts = collection(PostSchema, { key: "id" }).load(requests.getTeamPosts, { where: byTeam });
|
|
48
|
+
expectTypeOf(posts.rowMismatch.received).toEqualTypeOf<PostSummary>();
|
|
49
|
+
// @ts-expect-error the list returns summaries without `body`
|
|
50
|
+
createDb({ posts });
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("rejects a rows mapping with a missing or extra field", () => {
|
|
54
|
+
const exact = collection(PostSummarySchema, { key: "id" }).load(requests.getAllPosts, {
|
|
55
|
+
rows: (response) => response.items.map(({ body: _body, ...summary }) => summary),
|
|
56
|
+
});
|
|
57
|
+
const extra = collection(PostSummarySchema, { key: "id" }).load(requests.getAllPosts, {
|
|
58
|
+
rows: (response) => response.items.map(({ body: _body, ...summary }) => ({ ...summary, extra: true })),
|
|
59
|
+
});
|
|
60
|
+
const missing = collection(PostSummarySchema, { key: "id" }).load(requests.getAllPosts, {
|
|
61
|
+
rows: (response) => response.items.map(({ body: _body, title: _title, ...summary }) => summary),
|
|
62
|
+
});
|
|
63
|
+
createDb({ exact });
|
|
64
|
+
// @ts-expect-error `extra` is not a row field
|
|
65
|
+
extra.load(requests.getTeamPosts);
|
|
66
|
+
// @ts-expect-error `title` is missing
|
|
67
|
+
createDb({ missing });
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("types the declared filters by their field", () => {
|
|
71
|
+
collection(PostSummarySchema, { key: "id" }).load(requests.getTeamPosts, {
|
|
72
|
+
where: {
|
|
73
|
+
// @ts-expect-error `teamId` is a number
|
|
74
|
+
teamId: { eq: (teamId: string) => ({ params: { teamId } }) },
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe("queries", () => {
|
|
81
|
+
const Summary = collection(PostSummarySchema, { key: "id" })
|
|
82
|
+
.load(requests.getTeamPosts, {
|
|
83
|
+
where: { ...byTeam, likes: { gt: (likes) => ({ queryParams: { likes_gt: likes } }) } },
|
|
84
|
+
})
|
|
85
|
+
.load(requests.getAllPosts, {
|
|
86
|
+
rows: (response) => response.items.map(({ body: _body, ...summary }) => summary),
|
|
87
|
+
cursor: (response) => response.next,
|
|
88
|
+
page: (cursor) => ({ queryParams: { cursor } }),
|
|
89
|
+
orderBy: { createdAt: (direction) => ({ queryParams: { sort: `createdAt:${direction}` } }) },
|
|
90
|
+
});
|
|
91
|
+
const Detail = collection(PostSchema, { key: "id" }).load(requests.getPost, {
|
|
92
|
+
byKey: (id) => ({ params: { postId: id } }),
|
|
93
|
+
});
|
|
94
|
+
const db = createDb({ postSummary: Summary, post: Detail });
|
|
95
|
+
|
|
96
|
+
it("types the result by the selection", () => {
|
|
97
|
+
const query = db.postSummary
|
|
98
|
+
.where((post) => eq(post.teamId, 1))
|
|
99
|
+
.select((post) => ({ id: post.id, name: post.title }));
|
|
100
|
+
expectTypeOf(query.getSnapshot()).toEqualTypeOf<QueryState<{ id: number; name: string }[]>>();
|
|
101
|
+
expectTypeOf(db.post.findOne(1).getSnapshot().data).toEqualTypeOf<Post | undefined>();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("rejects unknown fields and values of the wrong type", () => {
|
|
105
|
+
// @ts-expect-error `body` is not a summary field
|
|
106
|
+
db.postSummary.where((post) => eq(post.body, ""));
|
|
107
|
+
// @ts-expect-error likes is a number
|
|
108
|
+
db.postSummary.where((post) => gt(post.likes, "10"));
|
|
109
|
+
// @ts-expect-error title is a string
|
|
110
|
+
db.postSummary.where((post) => gt(post.title, 10));
|
|
111
|
+
db.postSummary.where((post) => inArray(post.id, [1, 2]));
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("requires a query to set the required filters of some loader", () => {
|
|
115
|
+
db.postSummary.where((post) => and(eq(post.teamId, 1), gt(post.likes, 3))).subscribe(() => {});
|
|
116
|
+
// @ts-expect-error the team list needs `teamId`, and the full list is paginated and cannot send `likes`
|
|
117
|
+
db.postSummary.where((post) => gt(post.likes, 3)).subscribe(() => {});
|
|
118
|
+
db.postSummary
|
|
119
|
+
.local()
|
|
120
|
+
.where((post) => gt(post.likes, 3))
|
|
121
|
+
.subscribe(() => {});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("requires a paginated loader to serve every condition", () => {
|
|
125
|
+
db.postSummary.orderBy((post) => post.createdAt).subscribe(() => {});
|
|
126
|
+
// @ts-expect-error `or` runs on the client, and the only loader without required filters is paginated
|
|
127
|
+
db.postSummary.where((post) => or(eq(post.id, 1), eq(post.id, 2))).subscribe(() => {});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("requires a key loader for findOne", () => {
|
|
131
|
+
db.post.findOne(1).subscribe(() => {});
|
|
132
|
+
// @ts-expect-error summaries have no key loader
|
|
133
|
+
db.postSummary.findOne(1).subscribe(() => {});
|
|
134
|
+
// @ts-expect-error a paginated loader cannot sort by `likes` on the client
|
|
135
|
+
db.postSummary.orderBy((post) => post.likes).subscribe(() => {});
|
|
136
|
+
db.postSummary
|
|
137
|
+
.findOne(1)
|
|
138
|
+
.local()
|
|
139
|
+
.subscribe(() => {});
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
describe("syncs", () => {
|
|
144
|
+
it("requires complete rows for inserts and the key for updates", () => {
|
|
145
|
+
const Detail = collection(PostSchema, { key: "id" })
|
|
146
|
+
.sync(listeners.onPostCreated, { type: "insert" })
|
|
147
|
+
.sync(listeners.onPostChanged, { type: "update" })
|
|
148
|
+
.sync(listeners.onPostDeleted, { type: "delete" });
|
|
149
|
+
const partialInsert = Detail.sync(listeners.onPostChanged, { type: "insert" });
|
|
150
|
+
const Summary = collection(PostSummarySchema, { key: "id" }).sync(listeners.onPostCreated, {
|
|
151
|
+
type: "insert",
|
|
152
|
+
row: ({ body: _body, ...summary }) => summary,
|
|
153
|
+
});
|
|
154
|
+
createDb({ Detail, Summary });
|
|
155
|
+
// @ts-expect-error a changed post is not a complete row
|
|
156
|
+
createDb({ partialInsert });
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe("mutations", () => {
|
|
161
|
+
it("types writes by the mutations a collection declares", () => {
|
|
162
|
+
const Detail = collection(PostSchema, { key: "id" }).mutate({
|
|
163
|
+
insert: { request: requests.createPostRequest, map: (row) => ({ payload: row }) },
|
|
164
|
+
update: {
|
|
165
|
+
request: requests.updatePostRequest,
|
|
166
|
+
map: (key, changes) => ({ params: { postId: key }, payload: changes }),
|
|
167
|
+
},
|
|
168
|
+
delete: {
|
|
169
|
+
request: requests.deletePostRequest,
|
|
170
|
+
map: (key) => ({ params: { postId: key } }),
|
|
171
|
+
onSuccess: "keep",
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
const db = createDb({ post: Detail, summary: collection(PostSummarySchema, { key: "id" }) });
|
|
175
|
+
db.post.update(1, { title: "New" });
|
|
176
|
+
// @ts-expect-error `title` is a string
|
|
177
|
+
db.post.update(1, { title: 1 });
|
|
178
|
+
db.post.delete(1);
|
|
179
|
+
// @ts-expect-error summaries declare no mutations
|
|
180
|
+
db.summary.update(1, { title: "New" });
|
|
181
|
+
expectTypeOf(db.post.insert).parameter(0).toEqualTypeOf<Post>();
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("requires onSuccess when the response is not the row", () => {
|
|
185
|
+
const unhandled = collection(PostSchema, { key: "id" }).mutate({
|
|
186
|
+
update: { request: requests.deletePostRequest, map: (key) => ({ params: { postId: key } }) },
|
|
187
|
+
});
|
|
188
|
+
const handled = collection(PostSchema, { key: "id" }).mutate({
|
|
189
|
+
update: {
|
|
190
|
+
request: requests.deletePostRequest,
|
|
191
|
+
map: (key) => ({ params: { postId: key } }),
|
|
192
|
+
onSuccess: "refetch",
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
createDb({ handled });
|
|
196
|
+
// @ts-expect-error a null response does not fit an update
|
|
197
|
+
createDb({ unhandled });
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
describe("links and normalizing", () => {
|
|
202
|
+
it("checks nested rows against the target collection and links by collection name", () => {
|
|
203
|
+
const Summary = collection(PostSummarySchema, { key: "id" });
|
|
204
|
+
const Detail = collection(PostSchema, { key: "id" });
|
|
205
|
+
const normalized = Detail.normalize(Summary, ({ body: _body, ...summary }) => [summary]);
|
|
206
|
+
const mismatched = Detail.normalize(Summary, (post) => [post]);
|
|
207
|
+
createDb({ summary: Summary, post: normalized }, { links: [["post", "summary"]] });
|
|
208
|
+
// @ts-expect-error a post has a body, which summaries do not
|
|
209
|
+
createDb({ summary: Summary, post: mismatched });
|
|
210
|
+
// @ts-expect-error links name collections of the db
|
|
211
|
+
createDb({ summary: Summary, post: Detail }, { links: [["post", "missing"]] });
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
describe("joins", () => {
|
|
216
|
+
it("adds the joined row, which may be missing, and checks the foreign key's type", () => {
|
|
217
|
+
type Team = { id: number; name: string };
|
|
218
|
+
type Tag = { slug: string };
|
|
219
|
+
const db = createDb({
|
|
220
|
+
post: collection(PostSchema, { key: "id" }).load(requests.getPost, {
|
|
221
|
+
byKey: (id) => ({ params: { postId: id } }),
|
|
222
|
+
}),
|
|
223
|
+
team: collection(rowType<Team>(), { key: "id" }),
|
|
224
|
+
tag: collection(rowType<Tag>(), { key: "slug" }),
|
|
225
|
+
});
|
|
226
|
+
const joined = db.post
|
|
227
|
+
.findOne(1)
|
|
228
|
+
.select((post) => ({ title: post.title, teamId: post.teamId }))
|
|
229
|
+
.join("team", db.team, (post) => post.teamId);
|
|
230
|
+
expectTypeOf(joined.getSnapshot().data).toEqualTypeOf<
|
|
231
|
+
{ title: string; teamId: number; team: Team | undefined } | undefined
|
|
232
|
+
>();
|
|
233
|
+
const list = db.post.join("team", db.team, (post) => post.teamId).join("owner", db.team, (post) => post.likes);
|
|
234
|
+
expectTypeOf(list.getSnapshot().data).toEqualTypeOf<
|
|
235
|
+
| { [Field in keyof Post | "team" | "owner"]: Field extends keyof Post ? Post[Field] : Team | undefined }[]
|
|
236
|
+
| undefined
|
|
237
|
+
>();
|
|
238
|
+
// @ts-expect-error a number field cannot point at a string key
|
|
239
|
+
db.post.join("tag", db.tag, (post) => post.teamId);
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
describe("infinite queries", () => {
|
|
244
|
+
it("needs an ordered list query and types its pages", () => {
|
|
245
|
+
const db = createDb({
|
|
246
|
+
post: collection(PostSchema, { key: "id" }).load(requests.getAllPosts, {
|
|
247
|
+
rows: (response) => response.items,
|
|
248
|
+
cursor: (response) => response.next,
|
|
249
|
+
page: (cursor) => ({ queryParams: { cursor } }),
|
|
250
|
+
orderBy: { createdAt: (direction) => ({ queryParams: { sort: direction } }) },
|
|
251
|
+
}),
|
|
252
|
+
});
|
|
253
|
+
const feed = db.post
|
|
254
|
+
.orderBy((post) => post.createdAt)
|
|
255
|
+
.select((post) => ({ id: post.id }))
|
|
256
|
+
.infinite({ pageSize: 20 });
|
|
257
|
+
expectTypeOf(feed.getSnapshot().pages).toEqualTypeOf<{ id: number }[][]>();
|
|
258
|
+
expectTypeOf(feed.getSnapshot().data).toEqualTypeOf<{ id: number }[] | undefined>();
|
|
259
|
+
// @ts-expect-error pages need an order
|
|
260
|
+
db.post.where((post) => eq(post.likes, 1)).infinite({ pageSize: 20 });
|
|
261
|
+
// @ts-expect-error one row has no pages
|
|
262
|
+
db.post
|
|
263
|
+
.findOne(1)
|
|
264
|
+
.orderBy((post) => post.createdAt)
|
|
265
|
+
.infinite({ pageSize: 20 });
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
describe("query extractors used by hooks", () => {
|
|
270
|
+
it("reads the data, result and checks of any query", () => {
|
|
271
|
+
const db = createDb({
|
|
272
|
+
post: collection(PostSchema, { key: "id" }).load(requests.getAllPosts, {
|
|
273
|
+
rows: (response) => response.items,
|
|
274
|
+
cursor: (response) => response.next,
|
|
275
|
+
page: (cursor) => ({ queryParams: { cursor } }),
|
|
276
|
+
orderBy: { createdAt: (direction) => ({ queryParams: { sort: direction } }) },
|
|
277
|
+
}),
|
|
278
|
+
});
|
|
279
|
+
const ordered = db.post.orderBy((post) => post.createdAt).select((post) => ({ id: post.id }));
|
|
280
|
+
const one = db.post.findOne(1).local();
|
|
281
|
+
expectTypeOf<ExtractQueryData<typeof ordered>>().toEqualTypeOf<{ id: number }[]>();
|
|
282
|
+
expectTypeOf<ExtractQueryData<typeof one>>().toEqualTypeOf<Post>();
|
|
283
|
+
expectTypeOf<ExtractQueryResult<typeof ordered>>().toEqualTypeOf<{ id: number }>();
|
|
284
|
+
expectTypeOf<InfiniteQueryCheck<typeof ordered>>().toEqualTypeOf<unknown>();
|
|
285
|
+
expectTypeOf<InfiniteQueryCheck<typeof one>>().not.toEqualTypeOf<unknown>();
|
|
286
|
+
expectTypeOf<InfiniteQueryCheck<ReturnType<typeof db.post.local>>>().toEqualTypeOf<{ orderBy: never }>();
|
|
287
|
+
expectTypeOf<ServableQuery<typeof ordered>>().toEqualTypeOf<unknown>();
|
|
288
|
+
// The list loader is paginated, so a condition it cannot send does not compile
|
|
289
|
+
expectTypeOf<
|
|
290
|
+
ServableQuery<ReturnType<typeof db.post.where<ReturnType<typeof gt<"likes", number>>>>>
|
|
291
|
+
>().toEqualTypeOf<{
|
|
292
|
+
servable: never;
|
|
293
|
+
}>();
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
describe("holes found by the audit", () => {
|
|
298
|
+
it("does not let a read by key take list-only parts it would ignore", () => {
|
|
299
|
+
const db = createDb({
|
|
300
|
+
post: collection(PostSchema, { key: "id" }).load(requests.getPost, {
|
|
301
|
+
byKey: (id) => ({ params: { postId: id } }),
|
|
302
|
+
}),
|
|
303
|
+
});
|
|
304
|
+
// @ts-expect-error one row has no filter
|
|
305
|
+
db.post.findOne(1).where((post) => eq(post.teamId, 1));
|
|
306
|
+
// @ts-expect-error one row has no order
|
|
307
|
+
db.post.findOne(1).orderBy((post) => post.createdAt);
|
|
308
|
+
// @ts-expect-error one row has no limit
|
|
309
|
+
db.post.findOne(1).limit(1);
|
|
310
|
+
db.post
|
|
311
|
+
.findOne(1)
|
|
312
|
+
.select((post) => ({ id: post.id }))
|
|
313
|
+
.local();
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("needs the key of an insert made in a transaction", () => {
|
|
317
|
+
const db = createDb({
|
|
318
|
+
post: collection(PostSchema, { key: "id" }).mutate({
|
|
319
|
+
insert: { request: requests.createPostRequest, map: (row) => ({ payload: row }) },
|
|
320
|
+
}),
|
|
321
|
+
});
|
|
322
|
+
const check = () =>
|
|
323
|
+
db.transaction((tx) => {
|
|
324
|
+
const { id: _id, ...row } = {} as Post;
|
|
325
|
+
// @ts-expect-error the transaction cannot learn a key the server assigns
|
|
326
|
+
tx.post.insert(row);
|
|
327
|
+
tx.post.insert({} as Post);
|
|
328
|
+
});
|
|
329
|
+
expect(check).toBeFunction();
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it("rejects collection names the db uses itself", () => {
|
|
334
|
+
// @ts-expect-error `clear` is a db method
|
|
335
|
+
createDb({ clear: collection(rowType<PostSummary>(), { key: "id" }) });
|
|
336
|
+
});
|
|
337
|
+
});
|