@lunora/db 1.0.0-alpha.2 → 1.0.0-alpha.21
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/LICENSE.md +6 -0
- package/README.md +12 -0
- package/__assets__/package-og.svg +1 -1
- package/dist/collections/index.d.mts +5 -0
- package/dist/collections/index.d.ts +5 -0
- package/dist/collections/index.mjs +2 -0
- package/dist/index.d.mts +7 -141
- package/dist/index.d.ts +7 -141
- package/dist/index.mjs +4 -2
- package/dist/mutators/index.d.mts +5 -0
- package/dist/mutators/index.d.ts +5 -0
- package/dist/mutators/index.mjs +1 -0
- package/dist/packem_shared/OUTBOX_MUTATION_FN_NAME-Cf8iP6Wa.mjs +102 -0
- package/dist/packem_shared/bindMutators-BseARPYg.mjs +70 -0
- package/dist/packem_shared/collection-options.d-DgAuvqvn.d.mts +226 -0
- package/dist/packem_shared/collection-options.d-DgAuvqvn.d.ts +226 -0
- package/dist/packem_shared/createCheckpointRegistry-Bf4JtkVH.mjs +136 -0
- package/dist/packem_shared/define-collections.d-1z8QtQW8.d.mts +166 -0
- package/dist/packem_shared/define-collections.d-DPgIHAtd.d.ts +166 -0
- package/dist/packem_shared/define-mutators.d-10NiOBqm.d.ts +80 -0
- package/dist/packem_shared/define-mutators.d-Cq_qr5Z1.d.mts +80 -0
- package/dist/packem_shared/defineCollections-yLxJvEsu.mjs +107 -0
- package/package.json +12 -3
- package/dist/packem_shared/createOptimisticOnlineDetector-CRulWqZ7.mjs +0 -65
- package/dist/packem_shared/defineCollections-BAmslSrF.mjs +0 -109
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { NonRetriableError } from '@tanstack/offline-transactions';
|
|
2
|
-
|
|
3
|
-
const OUTBOX_DRAIN_INTERVAL_MS = 1e3;
|
|
4
|
-
const toMap = (rows, getKey) => {
|
|
5
|
-
const map = /* @__PURE__ */ new Map();
|
|
6
|
-
for (const row of rows) {
|
|
7
|
-
map.set(getKey(row), row);
|
|
8
|
-
}
|
|
9
|
-
return map;
|
|
10
|
-
};
|
|
11
|
-
const makeDiffEmit = (synced, writer) => (next) => {
|
|
12
|
-
writer.begin();
|
|
13
|
-
for (const [key, value] of next) {
|
|
14
|
-
const previous = synced.get(key);
|
|
15
|
-
if (previous === void 0) {
|
|
16
|
-
writer.write({ type: "insert", value });
|
|
17
|
-
} else if (JSON.stringify(previous) !== JSON.stringify(value)) {
|
|
18
|
-
writer.write({ type: "update", value });
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
for (const key of synced.keys()) {
|
|
22
|
-
if (!next.has(key)) {
|
|
23
|
-
writer.write({ key, type: "delete" });
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
writer.commit();
|
|
27
|
-
synced.clear();
|
|
28
|
-
for (const [key, value] of next) {
|
|
29
|
-
synced.set(key, value);
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
const runOutboxMutation = async (mutate) => {
|
|
33
|
-
try {
|
|
34
|
-
await mutate();
|
|
35
|
-
} catch (error) {
|
|
36
|
-
if (typeof error.code === "string") {
|
|
37
|
-
throw new NonRetriableError(error instanceof Error ? error.message : String(error));
|
|
38
|
-
}
|
|
39
|
-
throw error;
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
const createOptimisticOnlineDetector = () => {
|
|
43
|
-
const intervals = /* @__PURE__ */ new Set();
|
|
44
|
-
return {
|
|
45
|
-
dispose: () => {
|
|
46
|
-
for (const handle of intervals) {
|
|
47
|
-
clearInterval(handle);
|
|
48
|
-
}
|
|
49
|
-
intervals.clear();
|
|
50
|
-
},
|
|
51
|
-
isOnline: () => true,
|
|
52
|
-
notifyOnline: () => {
|
|
53
|
-
},
|
|
54
|
-
subscribe: (callback) => {
|
|
55
|
-
const handle = setInterval(callback, OUTBOX_DRAIN_INTERVAL_MS);
|
|
56
|
-
intervals.add(handle);
|
|
57
|
-
return () => {
|
|
58
|
-
clearInterval(handle);
|
|
59
|
-
intervals.delete(handle);
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
export { createOptimisticOnlineDetector, makeDiffEmit, runOutboxMutation, toMap };
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { createCollection, BTreeIndex } from '@tanstack/db';
|
|
2
|
-
import { startOfflineExecutor } from '@tanstack/offline-transactions';
|
|
3
|
-
import { toMap, createOptimisticOnlineDetector, makeDiffEmit, runOutboxMutation } from './createOptimisticOnlineDetector-CRulWqZ7.mjs';
|
|
4
|
-
|
|
5
|
-
const defineCollections = (client, defs) => {
|
|
6
|
-
const collections = {};
|
|
7
|
-
const scope = {};
|
|
8
|
-
const subscriptions = {};
|
|
9
|
-
const emitters = {};
|
|
10
|
-
const errorHandlers = {};
|
|
11
|
-
const mutationFns = {};
|
|
12
|
-
const entries = Object.entries(defs);
|
|
13
|
-
for (const [name, definition] of entries) {
|
|
14
|
-
const getKey = definition.getKey ?? ((row) => row._id);
|
|
15
|
-
const insert = definition.insert;
|
|
16
|
-
const synced = /* @__PURE__ */ new Map();
|
|
17
|
-
collections[name] = createCollection({
|
|
18
|
-
// Auto-build ordered (B-tree) indexes for whatever the app's live
|
|
19
|
-
// queries join / filter / sort on, so they stay fast as data grows.
|
|
20
|
-
autoIndex: "eager",
|
|
21
|
-
defaultIndexType: BTreeIndex,
|
|
22
|
-
getKey,
|
|
23
|
-
id: name,
|
|
24
|
-
sync: {
|
|
25
|
-
sync: (writer) => {
|
|
26
|
-
const emit = makeDiffEmit(synced, writer);
|
|
27
|
-
emitters[name] = emit;
|
|
28
|
-
const onError = (error) => {
|
|
29
|
-
writer.markReady();
|
|
30
|
-
definition.onError?.(error);
|
|
31
|
-
};
|
|
32
|
-
errorHandlers[name] = onError;
|
|
33
|
-
if (definition.scopeBy === void 0) {
|
|
34
|
-
subscriptions[name] = client.subscribe(
|
|
35
|
-
definition.list,
|
|
36
|
-
{},
|
|
37
|
-
(rows) => {
|
|
38
|
-
emit(toMap(rows, getKey));
|
|
39
|
-
writer.markReady();
|
|
40
|
-
},
|
|
41
|
-
{ onError }
|
|
42
|
-
);
|
|
43
|
-
} else {
|
|
44
|
-
writer.markReady();
|
|
45
|
-
}
|
|
46
|
-
return () => {
|
|
47
|
-
emitters[name] = void 0;
|
|
48
|
-
errorHandlers[name] = void 0;
|
|
49
|
-
subscriptions[name]?.();
|
|
50
|
-
subscriptions[name] = void 0;
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
if (definition.scopeBy !== void 0) {
|
|
56
|
-
scope[name] = (args) => {
|
|
57
|
-
subscriptions[name]?.();
|
|
58
|
-
subscriptions[name] = void 0;
|
|
59
|
-
emitters[name]?.(/* @__PURE__ */ new Map());
|
|
60
|
-
if (args === void 0) {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
subscriptions[name] = client.subscribe(
|
|
64
|
-
definition.list,
|
|
65
|
-
args,
|
|
66
|
-
(rows) => {
|
|
67
|
-
emitters[name]?.(toMap(rows, getKey));
|
|
68
|
-
},
|
|
69
|
-
{ onError: (error) => errorHandlers[name]?.(error) }
|
|
70
|
-
);
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
if (insert) {
|
|
74
|
-
mutationFns[name] = async ({ transaction }) => {
|
|
75
|
-
for (const mutation of transaction.mutations) {
|
|
76
|
-
const row = mutation.modified;
|
|
77
|
-
await runOutboxMutation(() => client.mutation(insert.mutation, insert.toArgs(row)));
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
const executor = startOfflineExecutor({
|
|
83
|
-
collections,
|
|
84
|
-
mutationFns,
|
|
85
|
-
onlineDetector: createOptimisticOnlineDetector()
|
|
86
|
-
});
|
|
87
|
-
const actions = {};
|
|
88
|
-
for (const [name, definition] of entries) {
|
|
89
|
-
const insert = definition.insert;
|
|
90
|
-
const collection = collections[name];
|
|
91
|
-
if (!insert || !collection) {
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
const action = executor.createOfflineAction({
|
|
95
|
-
mutationFnName: name,
|
|
96
|
-
onMutate: ({ id, input }) => {
|
|
97
|
-
collection.insert(insert.optimistic(input, id));
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
actions[name] = (input) => {
|
|
101
|
-
const id = crypto.randomUUID();
|
|
102
|
-
const transaction = action({ id, input });
|
|
103
|
-
return { id, transaction };
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
return { actions, collections, executor, scope };
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
export { defineCollections };
|