@lunora/db 1.0.0-alpha.9 → 1.0.0-alpha.91
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 +7 -1
- package/dist/collections/index.d.mts +2 -2
- package/dist/collections/index.d.ts +2 -2
- package/dist/collections/index.mjs +1 -2
- package/dist/index.d.mts +144 -5
- package/dist/index.d.ts +144 -5
- package/dist/index.mjs +1 -8
- package/dist/mutators/index.d.mts +2 -2
- package/dist/mutators/index.d.ts +2 -2
- package/dist/mutators/index.mjs +1 -1
- package/dist/packem_shared/CHECKPOINT_FALLBACK_MS-DVkxHO2j.mjs +1 -0
- package/dist/packem_shared/DIRECT_TRANSACTION_METADATA_KEY-O-hmqO42.mjs +1 -0
- package/dist/packem_shared/OUTBOX_MUTATION_FN_NAME-BfFCughR.mjs +1 -0
- package/dist/packem_shared/applyPlanToCollections-C_eyqNF9.mjs +1 -0
- package/dist/packem_shared/collection-options.d-bmH3BTGq.d.mts +364 -0
- package/dist/packem_shared/collection-options.d-bmH3BTGq.d.ts +364 -0
- package/dist/packem_shared/defineCollections-Cz4-m1Mp.mjs +1 -0
- package/dist/packem_shared/index.d-BzXEOiP5.d.mts +174 -0
- package/dist/packem_shared/index.d-DQsjGcjP.d.ts +235 -0
- package/dist/packem_shared/index.d-DlfHbDas.d.ts +174 -0
- package/dist/packem_shared/index.d-DrpzHIoj.d.mts +235 -0
- package/package.json +3 -2
- package/dist/packem_shared/OUTBOX_MUTATION_FN_NAME-Cf8iP6Wa.mjs +0 -102
- package/dist/packem_shared/bindMutators-DNhICkoy.mjs +0 -66
- package/dist/packem_shared/collection-options.d-lJBOVJgq.d.mts +0 -216
- package/dist/packem_shared/collection-options.d-lJBOVJgq.d.ts +0 -216
- package/dist/packem_shared/createCheckpointRegistry-SvwBczzv.mjs +0 -127
- package/dist/packem_shared/define-collections.d-CdORnl3S.d.mts +0 -150
- package/dist/packem_shared/define-collections.d-X2MfU5Es.d.ts +0 -150
- package/dist/packem_shared/define-mutators.d-DhO8mWz0.d.ts +0 -80
- package/dist/packem_shared/define-mutators.d-HWj94_nL.d.mts +0 -80
- package/dist/packem_shared/defineCollections-BJNtgnWB.mjs +0 -105
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { createCollection } from '@tanstack/db';
|
|
2
|
-
import { startOfflineExecutor, NonRetriableError } from '@tanstack/offline-transactions';
|
|
3
|
-
import { lunoraCollectionOptions } from './createCheckpointRegistry-SvwBczzv.mjs';
|
|
4
|
-
import { createOptimisticOnlineDetector, runOutboxMutation, OUTBOX_MUTATION_FN_NAME } from './OUTBOX_MUTATION_FN_NAME-Cf8iP6Wa.mjs';
|
|
5
|
-
|
|
6
|
-
const defineCollections = (client, defs, options = {}) => {
|
|
7
|
-
const collections = {};
|
|
8
|
-
const scope = {};
|
|
9
|
-
const mutationFns = {};
|
|
10
|
-
const entries = Object.entries(defs);
|
|
11
|
-
for (const [name, definition] of entries) {
|
|
12
|
-
const insert = definition.insert;
|
|
13
|
-
const { config, scope: scopeFunction } = lunoraCollectionOptions({
|
|
14
|
-
client,
|
|
15
|
-
getKey: definition.getKey,
|
|
16
|
-
id: name,
|
|
17
|
-
// `AnyDef` erases `list` to `any` (`TList = any`); it's a `FunctionReference` here.
|
|
18
|
-
list: definition.list,
|
|
19
|
-
...definition.load === void 0 ? {} : { load: definition.load },
|
|
20
|
-
onError: definition.onError,
|
|
21
|
-
scopeBy: definition.scopeBy
|
|
22
|
-
});
|
|
23
|
-
collections[name] = createCollection(config);
|
|
24
|
-
if (definition.scopeBy !== void 0) {
|
|
25
|
-
scope[name] = scopeFunction;
|
|
26
|
-
}
|
|
27
|
-
if (insert) {
|
|
28
|
-
mutationFns[name] = async ({ transaction }) => {
|
|
29
|
-
for (const mutation of transaction.mutations) {
|
|
30
|
-
const row = mutation.modified;
|
|
31
|
-
try {
|
|
32
|
-
await runOutboxMutation(() => client.mutation(insert.mutation, insert.toArgs(row)));
|
|
33
|
-
} catch (error) {
|
|
34
|
-
if (error instanceof NonRetriableError && options.onWriteRejected) {
|
|
35
|
-
try {
|
|
36
|
-
options.onWriteRejected({ code: error.code, collection: name, error, row });
|
|
37
|
-
} catch {
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
throw error;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
mutationFns[OUTBOX_MUTATION_FN_NAME] = async ({ transaction }) => {
|
|
47
|
-
const meta = transaction.metadata;
|
|
48
|
-
if (!meta) {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
if (meta.identity !== client.currentIdentity()) {
|
|
52
|
-
throw new NonRetriableError("outbox write dropped: identity changed since it was queued");
|
|
53
|
-
}
|
|
54
|
-
await runOutboxMutation(
|
|
55
|
-
() => client.mutation({ __lunoraRef: meta.functionPath }, meta.args, { mutationId: meta.idempotencyKey, shardKey: meta.shardKey })
|
|
56
|
-
);
|
|
57
|
-
};
|
|
58
|
-
const executor = startOfflineExecutor({
|
|
59
|
-
collections,
|
|
60
|
-
mutationFns,
|
|
61
|
-
onlineDetector: createOptimisticOnlineDetector(),
|
|
62
|
-
...options.onLeadershipChange ? { onLeadershipChange: options.onLeadershipChange } : {},
|
|
63
|
-
...options.onStorageFailure ? { onStorageFailure: options.onStorageFailure } : {},
|
|
64
|
-
// A persisted write whose target collection was removed/renamed in a deploy
|
|
65
|
-
// hits an unregistered mutationFn. The executor drops it as a
|
|
66
|
-
// NonRetriableError *before* our per-collection `mutationFns` catch runs, so
|
|
67
|
-
// this hook is the only place to surface it on `onWriteRejected`.
|
|
68
|
-
onUnknownMutationFn: (name, tx) => {
|
|
69
|
-
try {
|
|
70
|
-
options.onWriteRejected?.({
|
|
71
|
-
code: "UNKNOWN_MUTATION_FN",
|
|
72
|
-
collection: name,
|
|
73
|
-
error: new Error(`offline write dropped: mutation "${name}" no longer exists (removed or renamed in a deploy?)`),
|
|
74
|
-
// Best-effort recovered row: the persisted `modified` shape isn't a
|
|
75
|
-
// validated `Row`, and a batched transaction surfaces only its first
|
|
76
|
-
// mutation's row. Enough to describe the dropped write to the user.
|
|
77
|
-
row: tx.mutations[0]?.modified
|
|
78
|
-
});
|
|
79
|
-
} catch {
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
const actions = {};
|
|
84
|
-
for (const [name, definition] of entries) {
|
|
85
|
-
const insert = definition.insert;
|
|
86
|
-
const collection = collections[name];
|
|
87
|
-
if (!insert || !collection) {
|
|
88
|
-
continue;
|
|
89
|
-
}
|
|
90
|
-
const action = executor.createOfflineAction({
|
|
91
|
-
mutationFnName: name,
|
|
92
|
-
onMutate: ({ id, input }) => {
|
|
93
|
-
collection.insert(insert.optimistic(input, id));
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
actions[name] = (input) => {
|
|
97
|
-
const id = crypto.randomUUID();
|
|
98
|
-
const transaction = action({ id, input });
|
|
99
|
-
return { id, transaction };
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
return { actions, collections, executor, scope };
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
export { defineCollections };
|