@lunora/db 1.0.0-alpha.3 → 1.0.0-alpha.31

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.
@@ -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 };