@trestleinc/replicate 0.1.0 → 1.1.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 +356 -420
- package/dist/client/collection.d.ts +78 -76
- package/dist/client/errors.d.ts +59 -0
- package/dist/client/index.d.ts +22 -18
- package/dist/client/logger.d.ts +0 -1
- package/dist/client/merge.d.ts +77 -0
- package/dist/client/persistence/adapters/index.d.ts +8 -0
- package/dist/client/persistence/adapters/opsqlite.d.ts +46 -0
- package/dist/client/persistence/adapters/sqljs.d.ts +83 -0
- package/dist/client/persistence/index.d.ts +49 -0
- package/dist/client/persistence/indexeddb.d.ts +17 -0
- package/dist/client/persistence/memory.d.ts +16 -0
- package/dist/client/persistence/sqlite-browser.d.ts +51 -0
- package/dist/client/persistence/sqlite-level.d.ts +63 -0
- package/dist/client/persistence/sqlite-rn.d.ts +36 -0
- package/dist/client/persistence/sqlite.d.ts +47 -0
- package/dist/client/persistence/types.d.ts +42 -0
- package/dist/client/prose.d.ts +56 -0
- package/dist/client/replicate.d.ts +40 -0
- package/dist/client/services/checkpoint.d.ts +18 -0
- package/dist/client/services/reconciliation.d.ts +24 -0
- package/dist/component/_generated/api.d.ts +35 -0
- package/dist/component/_generated/api.js +3 -3
- package/dist/component/_generated/component.d.ts +89 -0
- package/dist/component/_generated/component.js +0 -0
- package/dist/component/_generated/dataModel.d.ts +45 -0
- package/dist/component/_generated/dataModel.js +0 -0
- package/{src → dist}/component/_generated/server.d.ts +9 -38
- package/dist/component/convex.config.d.ts +2 -2
- package/dist/component/convex.config.js +2 -1
- package/dist/component/logger.d.ts +8 -0
- package/dist/component/logger.js +30 -0
- package/dist/component/public.d.ts +36 -61
- package/dist/component/public.js +232 -58
- package/dist/component/schema.d.ts +32 -8
- package/dist/component/schema.js +19 -6
- package/dist/index.js +1553 -308
- package/dist/server/builder.d.ts +94 -0
- package/dist/server/index.d.ts +14 -17
- package/dist/server/schema.d.ts +17 -63
- package/dist/server/storage.d.ts +80 -0
- package/dist/server.js +268 -83
- package/dist/shared/index.d.ts +5 -0
- package/dist/shared/index.js +2 -0
- package/dist/shared/types.d.ts +50 -0
- package/dist/shared/types.js +6 -0
- package/dist/shared.js +6 -0
- package/package.json +59 -49
- package/src/client/collection.ts +877 -450
- package/src/client/errors.ts +45 -0
- package/src/client/index.ts +52 -26
- package/src/client/logger.ts +2 -28
- package/src/client/merge.ts +374 -0
- package/src/client/persistence/adapters/index.ts +8 -0
- package/src/client/persistence/adapters/opsqlite.ts +54 -0
- package/src/client/persistence/adapters/sqljs.ts +128 -0
- package/src/client/persistence/index.ts +54 -0
- package/src/client/persistence/indexeddb.ts +110 -0
- package/src/client/persistence/memory.ts +61 -0
- package/src/client/persistence/sqlite-browser.ts +107 -0
- package/src/client/persistence/sqlite-level.ts +407 -0
- package/src/client/persistence/sqlite-rn.ts +44 -0
- package/src/client/persistence/sqlite.ts +161 -0
- package/src/client/persistence/types.ts +49 -0
- package/src/client/prose.ts +369 -0
- package/src/client/replicate.ts +80 -0
- package/src/client/services/checkpoint.ts +86 -0
- package/src/client/services/reconciliation.ts +108 -0
- package/src/component/_generated/api.ts +52 -0
- package/src/component/_generated/component.ts +103 -0
- package/src/component/_generated/{dataModel.d.ts → dataModel.ts} +1 -1
- package/src/component/_generated/server.ts +161 -0
- package/src/component/convex.config.ts +3 -1
- package/src/component/logger.ts +36 -0
- package/src/component/public.ts +364 -111
- package/src/component/schema.ts +18 -5
- package/src/env.d.ts +31 -0
- package/src/server/builder.ts +85 -0
- package/src/server/index.ts +9 -24
- package/src/server/schema.ts +20 -76
- package/src/server/storage.ts +313 -0
- package/src/shared/index.ts +5 -0
- package/src/shared/types.ts +52 -0
- package/LICENSE.package +0 -201
- package/dist/client/storage.d.ts +0 -143
- package/dist/server/replication.d.ts +0 -122
- package/dist/server/ssr.d.ts +0 -79
- package/dist/ssr.js +0 -19
- package/src/client/storage.ts +0 -206
- package/src/component/_generated/api.d.ts +0 -95
- package/src/component/_generated/api.js +0 -23
- package/src/component/_generated/server.js +0 -90
- package/src/server/replication.ts +0 -244
- package/src/server/ssr.ts +0 -106
package/src/client/storage.ts
DELETED
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
Expand,
|
|
3
|
-
FunctionReference,
|
|
4
|
-
GenericDataModel,
|
|
5
|
-
GenericMutationCtx,
|
|
6
|
-
GenericQueryCtx,
|
|
7
|
-
} from 'convex/server';
|
|
8
|
-
import type { GenericId } from 'convex/values';
|
|
9
|
-
import { api } from '../component/_generated/api';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* A client API for interacting with the Convex Replicate storage component.
|
|
13
|
-
*
|
|
14
|
-
* This class provides a type-safe, scoped interface for storing and retrieving
|
|
15
|
-
* CRDT document data from the replicate component. Each instance is scoped to
|
|
16
|
-
* a specific collection name, eliminating the need to pass collectionName to
|
|
17
|
-
* every method call.
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```typescript
|
|
21
|
-
* import { components } from "./_generated/api";
|
|
22
|
-
* import { ReplicateStorage } from "@trestleinc/convex-replicate-component";
|
|
23
|
-
*
|
|
24
|
-
* // Create a storage instance for the "tasks" collection
|
|
25
|
-
* const tasksStorage = new ReplicateStorage(components.replicate, "tasks");
|
|
26
|
-
*
|
|
27
|
-
* export const submitTask = mutation({
|
|
28
|
-
* handler: async (ctx, args) => {
|
|
29
|
-
* return await tasksStorage.submitDocument(
|
|
30
|
-
* ctx,
|
|
31
|
-
* args.id,
|
|
32
|
-
* args.document,
|
|
33
|
-
* args.version
|
|
34
|
-
* );
|
|
35
|
-
* }
|
|
36
|
-
* });
|
|
37
|
-
*
|
|
38
|
-
* export const getTasks = query({
|
|
39
|
-
* handler: async (ctx, args) => {
|
|
40
|
-
* return await tasksStorage.stream(ctx, args.checkpoint, args.limit);
|
|
41
|
-
* }
|
|
42
|
-
* });
|
|
43
|
-
* ```
|
|
44
|
-
*
|
|
45
|
-
* @template TDocument - The document type being stored (must have an id field)
|
|
46
|
-
*/
|
|
47
|
-
export class ReplicateStorage<_TDocument extends { id: string } = { id: string }> {
|
|
48
|
-
/**
|
|
49
|
-
* Create a new ReplicateStorage instance scoped to a specific collection.
|
|
50
|
-
*
|
|
51
|
-
* @param component - The replicate component from your generated API
|
|
52
|
-
* @param collectionName - The name of the collection to interact with
|
|
53
|
-
*
|
|
54
|
-
* @example
|
|
55
|
-
* ```typescript
|
|
56
|
-
* const tasksStorage = new ReplicateStorage(
|
|
57
|
-
* components.replicate,
|
|
58
|
-
* "tasks"
|
|
59
|
-
* );
|
|
60
|
-
* ```
|
|
61
|
-
*/
|
|
62
|
-
constructor(
|
|
63
|
-
private component: UseApi<typeof api>,
|
|
64
|
-
private collectionName: string
|
|
65
|
-
) {}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Insert a new document into the replicate component storage.
|
|
69
|
-
*
|
|
70
|
-
* This stores the CRDT bytes in the component's internal storage table.
|
|
71
|
-
*
|
|
72
|
-
* @param ctx - Convex mutation context
|
|
73
|
-
* @param documentId - Unique identifier for the document
|
|
74
|
-
* @param crdtBytes - The CRDT binary data (from Automerge.save())
|
|
75
|
-
* @param version - Version number for conflict resolution
|
|
76
|
-
* @returns Success indicator
|
|
77
|
-
*/
|
|
78
|
-
async insertDocument(
|
|
79
|
-
ctx: RunMutationCtx,
|
|
80
|
-
documentId: string,
|
|
81
|
-
crdtBytes: ArrayBuffer,
|
|
82
|
-
version: number
|
|
83
|
-
): Promise<{ success: boolean }> {
|
|
84
|
-
return ctx.runMutation(this.component.public.insertDocument, {
|
|
85
|
-
collectionName: this.collectionName,
|
|
86
|
-
documentId,
|
|
87
|
-
crdtBytes,
|
|
88
|
-
version,
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Update an existing document in the replicate component storage.
|
|
94
|
-
*
|
|
95
|
-
* @param ctx - Convex mutation context
|
|
96
|
-
* @param documentId - Unique identifier for the document
|
|
97
|
-
* @param crdtBytes - The CRDT binary data (from Automerge.save())
|
|
98
|
-
* @param version - Version number for conflict resolution
|
|
99
|
-
* @returns Success indicator
|
|
100
|
-
*/
|
|
101
|
-
async updateDocument(
|
|
102
|
-
ctx: RunMutationCtx,
|
|
103
|
-
documentId: string,
|
|
104
|
-
crdtBytes: ArrayBuffer,
|
|
105
|
-
version: number
|
|
106
|
-
): Promise<{ success: boolean }> {
|
|
107
|
-
return ctx.runMutation(this.component.public.updateDocument, {
|
|
108
|
-
collectionName: this.collectionName,
|
|
109
|
-
documentId,
|
|
110
|
-
crdtBytes,
|
|
111
|
-
version,
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Delete a document from the replicate component storage.
|
|
117
|
-
* Appends deletion delta to event log.
|
|
118
|
-
*
|
|
119
|
-
* @param ctx - Convex mutation context
|
|
120
|
-
* @param documentId - Unique identifier for the document
|
|
121
|
-
* @param crdtBytes - Yjs deletion delta
|
|
122
|
-
* @param version - CRDT version number
|
|
123
|
-
* @returns Success indicator
|
|
124
|
-
*/
|
|
125
|
-
async deleteDocument(
|
|
126
|
-
ctx: RunMutationCtx,
|
|
127
|
-
documentId: string,
|
|
128
|
-
crdtBytes: ArrayBuffer,
|
|
129
|
-
version: number
|
|
130
|
-
): Promise<{ success: boolean }> {
|
|
131
|
-
return ctx.runMutation(this.component.public.deleteDocument, {
|
|
132
|
-
collectionName: this.collectionName,
|
|
133
|
-
documentId,
|
|
134
|
-
crdtBytes,
|
|
135
|
-
version,
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Stream CRDT changes for incremental replication.
|
|
141
|
-
*
|
|
142
|
-
* Retrieves CRDT bytes for documents that have been modified since the
|
|
143
|
-
* provided checkpoint, enabling incremental replication.
|
|
144
|
-
* Can be used for both polling (awaitReplication) and subscriptions (live updates).
|
|
145
|
-
*
|
|
146
|
-
* @param ctx - Convex query context
|
|
147
|
-
* @param checkpoint - Last known modification timestamp
|
|
148
|
-
* @param limit - Maximum number of changes to retrieve (default: 100)
|
|
149
|
-
* @returns Array of changes with updated checkpoint
|
|
150
|
-
*/
|
|
151
|
-
async stream(
|
|
152
|
-
ctx: RunQueryCtx,
|
|
153
|
-
checkpoint: { lastModified: number },
|
|
154
|
-
limit?: number
|
|
155
|
-
): Promise<{
|
|
156
|
-
changes: Array<{
|
|
157
|
-
documentId: string;
|
|
158
|
-
crdtBytes: ArrayBuffer;
|
|
159
|
-
version: number;
|
|
160
|
-
timestamp: number;
|
|
161
|
-
}>;
|
|
162
|
-
checkpoint: { lastModified: number };
|
|
163
|
-
hasMore: boolean;
|
|
164
|
-
}> {
|
|
165
|
-
return ctx.runQuery(this.component.public.stream, {
|
|
166
|
-
collectionName: this.collectionName,
|
|
167
|
-
checkpoint,
|
|
168
|
-
limit,
|
|
169
|
-
}) as any;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Re-export the component API for direct access if needed.
|
|
175
|
-
*/
|
|
176
|
-
export { api };
|
|
177
|
-
|
|
178
|
-
/* Type utilities */
|
|
179
|
-
|
|
180
|
-
type RunQueryCtx = {
|
|
181
|
-
runQuery: GenericQueryCtx<GenericDataModel>['runQuery'];
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
type RunMutationCtx = {
|
|
185
|
-
runMutation: GenericMutationCtx<GenericDataModel>['runMutation'];
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
export type OpaqueIds<T> = T extends GenericId<infer _T>
|
|
189
|
-
? string
|
|
190
|
-
: T extends (infer U)[]
|
|
191
|
-
? OpaqueIds<U>[]
|
|
192
|
-
: T extends object
|
|
193
|
-
? { [K in keyof T]: OpaqueIds<T[K]> }
|
|
194
|
-
: T;
|
|
195
|
-
|
|
196
|
-
export type UseApi<API> = Expand<{
|
|
197
|
-
[mod in keyof API]: API[mod] extends FunctionReference<
|
|
198
|
-
infer FType,
|
|
199
|
-
'public',
|
|
200
|
-
infer FArgs,
|
|
201
|
-
infer FReturnType,
|
|
202
|
-
infer FComponentPath
|
|
203
|
-
>
|
|
204
|
-
? FunctionReference<FType, 'internal', OpaqueIds<FArgs>, OpaqueIds<FReturnType>, FComponentPath>
|
|
205
|
-
: UseApi<API[mod]>;
|
|
206
|
-
}>;
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
/**
|
|
3
|
-
* Generated `api` utility.
|
|
4
|
-
*
|
|
5
|
-
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
-
*
|
|
7
|
-
* To regenerate, run `npx convex dev`.
|
|
8
|
-
* @module
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type * as public_ from "../public.js";
|
|
12
|
-
|
|
13
|
-
import type {
|
|
14
|
-
ApiFromModules,
|
|
15
|
-
FilterApi,
|
|
16
|
-
FunctionReference,
|
|
17
|
-
} from "convex/server";
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* A utility for referencing Convex functions in your app's API.
|
|
21
|
-
*
|
|
22
|
-
* Usage:
|
|
23
|
-
* ```js
|
|
24
|
-
* const myFunctionReference = api.myModule.myFunction;
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
declare const fullApi: ApiFromModules<{
|
|
28
|
-
public: typeof public_;
|
|
29
|
-
}>;
|
|
30
|
-
export type Mounts = {
|
|
31
|
-
public: {
|
|
32
|
-
deleteDocument: FunctionReference<
|
|
33
|
-
"mutation",
|
|
34
|
-
"public",
|
|
35
|
-
{ collectionName: string; documentId: string },
|
|
36
|
-
{ success: boolean }
|
|
37
|
-
>;
|
|
38
|
-
insertDocument: FunctionReference<
|
|
39
|
-
"mutation",
|
|
40
|
-
"public",
|
|
41
|
-
{
|
|
42
|
-
collectionName: string;
|
|
43
|
-
crdtBytes: ArrayBuffer;
|
|
44
|
-
documentId: string;
|
|
45
|
-
version: number;
|
|
46
|
-
},
|
|
47
|
-
{ success: boolean }
|
|
48
|
-
>;
|
|
49
|
-
stream: FunctionReference<
|
|
50
|
-
"query",
|
|
51
|
-
"public",
|
|
52
|
-
{
|
|
53
|
-
checkpoint: { lastModified: number };
|
|
54
|
-
collectionName: string;
|
|
55
|
-
limit?: number;
|
|
56
|
-
},
|
|
57
|
-
{
|
|
58
|
-
changes: Array<{
|
|
59
|
-
crdtBytes: ArrayBuffer;
|
|
60
|
-
documentId: string;
|
|
61
|
-
timestamp: number;
|
|
62
|
-
version: number;
|
|
63
|
-
}>;
|
|
64
|
-
checkpoint: { lastModified: number };
|
|
65
|
-
hasMore: boolean;
|
|
66
|
-
}
|
|
67
|
-
>;
|
|
68
|
-
updateDocument: FunctionReference<
|
|
69
|
-
"mutation",
|
|
70
|
-
"public",
|
|
71
|
-
{
|
|
72
|
-
collectionName: string;
|
|
73
|
-
crdtBytes: ArrayBuffer;
|
|
74
|
-
documentId: string;
|
|
75
|
-
version: number;
|
|
76
|
-
},
|
|
77
|
-
{ success: boolean }
|
|
78
|
-
>;
|
|
79
|
-
};
|
|
80
|
-
};
|
|
81
|
-
// For now fullApiWithMounts is only fullApi which provides
|
|
82
|
-
// jump-to-definition in component client code.
|
|
83
|
-
// Use Mounts for the same type without the inference.
|
|
84
|
-
declare const fullApiWithMounts: typeof fullApi;
|
|
85
|
-
|
|
86
|
-
export declare const api: FilterApi<
|
|
87
|
-
typeof fullApiWithMounts,
|
|
88
|
-
FunctionReference<any, "public">
|
|
89
|
-
>;
|
|
90
|
-
export declare const internal: FilterApi<
|
|
91
|
-
typeof fullApiWithMounts,
|
|
92
|
-
FunctionReference<any, "internal">
|
|
93
|
-
>;
|
|
94
|
-
|
|
95
|
-
export declare const components: {};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
/**
|
|
3
|
-
* Generated `api` utility.
|
|
4
|
-
*
|
|
5
|
-
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
-
*
|
|
7
|
-
* To regenerate, run `npx convex dev`.
|
|
8
|
-
* @module
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { anyApi, componentsGeneric } from "convex/server";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* A utility for referencing Convex functions in your app's API.
|
|
15
|
-
*
|
|
16
|
-
* Usage:
|
|
17
|
-
* ```js
|
|
18
|
-
* const myFunctionReference = api.myModule.myFunction;
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
export const api = anyApi;
|
|
22
|
-
export const internal = anyApi;
|
|
23
|
-
export const components = componentsGeneric();
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
/**
|
|
3
|
-
* Generated utilities for implementing server-side Convex query and mutation functions.
|
|
4
|
-
*
|
|
5
|
-
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
-
*
|
|
7
|
-
* To regenerate, run `npx convex dev`.
|
|
8
|
-
* @module
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
actionGeneric,
|
|
13
|
-
httpActionGeneric,
|
|
14
|
-
queryGeneric,
|
|
15
|
-
mutationGeneric,
|
|
16
|
-
internalActionGeneric,
|
|
17
|
-
internalMutationGeneric,
|
|
18
|
-
internalQueryGeneric,
|
|
19
|
-
componentsGeneric,
|
|
20
|
-
} from "convex/server";
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Define a query in this Convex app's public API.
|
|
24
|
-
*
|
|
25
|
-
* This function will be allowed to read your Convex database and will be accessible from the client.
|
|
26
|
-
*
|
|
27
|
-
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
28
|
-
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
29
|
-
*/
|
|
30
|
-
export const query = queryGeneric;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Define a query that is only accessible from other Convex functions (but not from the client).
|
|
34
|
-
*
|
|
35
|
-
* This function will be allowed to read from your Convex database. It will not be accessible from the client.
|
|
36
|
-
*
|
|
37
|
-
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
38
|
-
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
39
|
-
*/
|
|
40
|
-
export const internalQuery = internalQueryGeneric;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Define a mutation in this Convex app's public API.
|
|
44
|
-
*
|
|
45
|
-
* This function will be allowed to modify your Convex database and will be accessible from the client.
|
|
46
|
-
*
|
|
47
|
-
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
48
|
-
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
49
|
-
*/
|
|
50
|
-
export const mutation = mutationGeneric;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Define a mutation that is only accessible from other Convex functions (but not from the client).
|
|
54
|
-
*
|
|
55
|
-
* This function will be allowed to modify your Convex database. It will not be accessible from the client.
|
|
56
|
-
*
|
|
57
|
-
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
58
|
-
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
59
|
-
*/
|
|
60
|
-
export const internalMutation = internalMutationGeneric;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Define an action in this Convex app's public API.
|
|
64
|
-
*
|
|
65
|
-
* An action is a function which can execute any JavaScript code, including non-deterministic
|
|
66
|
-
* code and code with side-effects, like calling third-party services.
|
|
67
|
-
* They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
|
|
68
|
-
* They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
|
|
69
|
-
*
|
|
70
|
-
* @param func - The action. It receives an {@link ActionCtx} as its first argument.
|
|
71
|
-
* @returns The wrapped action. Include this as an `export` to name it and make it accessible.
|
|
72
|
-
*/
|
|
73
|
-
export const action = actionGeneric;
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Define an action that is only accessible from other Convex functions (but not from the client).
|
|
77
|
-
*
|
|
78
|
-
* @param func - The function. It receives an {@link ActionCtx} as its first argument.
|
|
79
|
-
* @returns The wrapped function. Include this as an `export` to name it and make it accessible.
|
|
80
|
-
*/
|
|
81
|
-
export const internalAction = internalActionGeneric;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Define a Convex HTTP action.
|
|
85
|
-
*
|
|
86
|
-
* @param func - The function. It receives an {@link ActionCtx} as its first argument, and a `Request` object
|
|
87
|
-
* as its second.
|
|
88
|
-
* @returns The wrapped endpoint function. Route a URL path to this function in `convex/http.js`.
|
|
89
|
-
*/
|
|
90
|
-
export const httpAction = httpActionGeneric;
|
|
@@ -1,244 +0,0 @@
|
|
|
1
|
-
import type { GenericDataModel } from 'convex/server';
|
|
2
|
-
|
|
3
|
-
function cleanDocument(doc: unknown): unknown {
|
|
4
|
-
return Object.fromEntries(
|
|
5
|
-
Object.entries(doc as Record<string, unknown>).filter(
|
|
6
|
-
([_, value]) => value !== undefined && value !== null
|
|
7
|
-
)
|
|
8
|
-
);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Insert a document into both the CRDT component and the main application table.
|
|
13
|
-
*
|
|
14
|
-
* DUAL-STORAGE ARCHITECTURE:
|
|
15
|
-
* This helper implements a dual-storage pattern where documents are stored in two places:
|
|
16
|
-
*
|
|
17
|
-
* 1. Component Storage (CRDT Layer):
|
|
18
|
-
* - Stores CRDT bytes (from Yjs) for offline-first conflict resolution
|
|
19
|
-
* - Handles concurrent updates with automatic merging
|
|
20
|
-
* - Provides the source of truth for offline changes
|
|
21
|
-
*
|
|
22
|
-
* 2. Main Application Table:
|
|
23
|
-
* - Stores materialized documents for efficient querying
|
|
24
|
-
* - Used by server-side Convex functions that need to query/join data
|
|
25
|
-
* - Optimized for reactive subscriptions and complex queries
|
|
26
|
-
*
|
|
27
|
-
* WHY BOTH?
|
|
28
|
-
* - Component: Handles conflict resolution and offline replication (CRDT bytes)
|
|
29
|
-
* - Main table: Enables efficient server-side queries (materialized docs)
|
|
30
|
-
* - Similar to event sourcing: component = event log, main table = read model
|
|
31
|
-
*
|
|
32
|
-
* @param ctx - Convex mutation context
|
|
33
|
-
* @param components - Generated components from Convex
|
|
34
|
-
* @param tableName - Name of the main application table
|
|
35
|
-
* @param args - Document data with id, crdtBytes, materializedDoc, and version
|
|
36
|
-
* @returns Success indicator
|
|
37
|
-
*/
|
|
38
|
-
export async function insertDocumentHelper<_DataModel extends GenericDataModel>(
|
|
39
|
-
ctx: unknown,
|
|
40
|
-
components: unknown,
|
|
41
|
-
tableName: string,
|
|
42
|
-
args: { id: string; crdtBytes: ArrayBuffer; materializedDoc: unknown; version: number }
|
|
43
|
-
): Promise<{
|
|
44
|
-
success: boolean;
|
|
45
|
-
metadata: {
|
|
46
|
-
documentId: string;
|
|
47
|
-
timestamp: number;
|
|
48
|
-
version: number;
|
|
49
|
-
collectionName: string;
|
|
50
|
-
};
|
|
51
|
-
}> {
|
|
52
|
-
// Use consistent timestamp for both writes to enable sync matching
|
|
53
|
-
const timestamp = Date.now();
|
|
54
|
-
|
|
55
|
-
// Write CRDT bytes to component
|
|
56
|
-
await (ctx as any).runMutation((components as any).replicate.public.insertDocument, {
|
|
57
|
-
collectionName: tableName,
|
|
58
|
-
documentId: args.id,
|
|
59
|
-
crdtBytes: args.crdtBytes,
|
|
60
|
-
version: args.version,
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
// Write materialized doc to main table
|
|
64
|
-
const db = (ctx as any).db;
|
|
65
|
-
const cleanDoc = cleanDocument(args.materializedDoc) as Record<string, unknown>;
|
|
66
|
-
|
|
67
|
-
await db.insert(tableName, {
|
|
68
|
-
id: args.id,
|
|
69
|
-
...cleanDoc,
|
|
70
|
-
version: args.version,
|
|
71
|
-
timestamp,
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
// Return metadata for replication matching
|
|
75
|
-
return {
|
|
76
|
-
success: true,
|
|
77
|
-
metadata: {
|
|
78
|
-
documentId: args.id,
|
|
79
|
-
timestamp,
|
|
80
|
-
version: args.version,
|
|
81
|
-
collectionName: tableName,
|
|
82
|
-
},
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Update a document in both the CRDT component and the main application table.
|
|
88
|
-
*
|
|
89
|
-
* @param ctx - Convex mutation context
|
|
90
|
-
* @param components - Generated components from Convex
|
|
91
|
-
* @param tableName - Name of the main application table
|
|
92
|
-
* @param args - Document data with id, crdtBytes, materializedDoc, and version
|
|
93
|
-
* @returns Success indicator
|
|
94
|
-
*/
|
|
95
|
-
export async function updateDocumentHelper<_DataModel extends GenericDataModel>(
|
|
96
|
-
ctx: unknown,
|
|
97
|
-
components: unknown,
|
|
98
|
-
tableName: string,
|
|
99
|
-
args: { id: string; crdtBytes: ArrayBuffer; materializedDoc: unknown; version: number }
|
|
100
|
-
): Promise<{
|
|
101
|
-
success: boolean;
|
|
102
|
-
metadata: {
|
|
103
|
-
documentId: string;
|
|
104
|
-
timestamp: number;
|
|
105
|
-
version: number;
|
|
106
|
-
collectionName: string;
|
|
107
|
-
};
|
|
108
|
-
}> {
|
|
109
|
-
// Use consistent timestamp for both writes to enable sync matching
|
|
110
|
-
const timestamp = Date.now();
|
|
111
|
-
|
|
112
|
-
// Write CRDT bytes to component
|
|
113
|
-
await (ctx as any).runMutation((components as any).replicate.public.updateDocument, {
|
|
114
|
-
collectionName: tableName,
|
|
115
|
-
documentId: args.id,
|
|
116
|
-
crdtBytes: args.crdtBytes,
|
|
117
|
-
version: args.version,
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
// Update materialized doc in main table
|
|
121
|
-
const db = (ctx as any).db;
|
|
122
|
-
const existing = await db
|
|
123
|
-
.query(tableName)
|
|
124
|
-
.withIndex('by_user_id', (q: unknown) => (q as any).eq('id', args.id))
|
|
125
|
-
.first();
|
|
126
|
-
|
|
127
|
-
if (!existing) {
|
|
128
|
-
throw new Error(`Document ${args.id} not found in table ${tableName}`);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const cleanDoc = cleanDocument(args.materializedDoc) as Record<string, unknown>;
|
|
132
|
-
|
|
133
|
-
await db.patch(existing._id, {
|
|
134
|
-
...cleanDoc,
|
|
135
|
-
version: args.version,
|
|
136
|
-
timestamp,
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
// Return metadata for replication matching
|
|
140
|
-
return {
|
|
141
|
-
success: true,
|
|
142
|
-
metadata: {
|
|
143
|
-
documentId: args.id,
|
|
144
|
-
timestamp,
|
|
145
|
-
version: args.version,
|
|
146
|
-
collectionName: tableName,
|
|
147
|
-
},
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* HARD delete a document from main table, APPEND deletion delta to component.
|
|
153
|
-
*
|
|
154
|
-
* NEW BEHAVIOR (v0.3.0):
|
|
155
|
-
* - Appends deletion delta to component event log (preserves history)
|
|
156
|
-
* - Physically removes document from main table (hard delete)
|
|
157
|
-
* - CRDT history preserved for future recovery features
|
|
158
|
-
*
|
|
159
|
-
* @param ctx - Convex mutation context
|
|
160
|
-
* @param components - Generated components from Convex
|
|
161
|
-
* @param tableName - Name of the main application table
|
|
162
|
-
* @param args - Document data with id, crdtBytes (deletion delta), and version
|
|
163
|
-
* @returns Success indicator with metadata
|
|
164
|
-
*/
|
|
165
|
-
export async function deleteDocumentHelper<_DataModel extends GenericDataModel>(
|
|
166
|
-
ctx: unknown,
|
|
167
|
-
components: unknown,
|
|
168
|
-
tableName: string,
|
|
169
|
-
args: { id: string; crdtBytes: ArrayBuffer; version: number }
|
|
170
|
-
): Promise<{
|
|
171
|
-
success: boolean;
|
|
172
|
-
metadata: {
|
|
173
|
-
documentId: string;
|
|
174
|
-
timestamp: number;
|
|
175
|
-
version: number;
|
|
176
|
-
collectionName: string;
|
|
177
|
-
};
|
|
178
|
-
}> {
|
|
179
|
-
const timestamp = Date.now();
|
|
180
|
-
|
|
181
|
-
// 1. Append deletion delta to component (event log)
|
|
182
|
-
await (ctx as any).runMutation((components as any).replicate.public.deleteDocument, {
|
|
183
|
-
collectionName: tableName,
|
|
184
|
-
documentId: args.id,
|
|
185
|
-
crdtBytes: args.crdtBytes,
|
|
186
|
-
version: args.version,
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
// 2. HARD DELETE from main table (physical removal)
|
|
190
|
-
const db = (ctx as any).db;
|
|
191
|
-
const existing = await db
|
|
192
|
-
.query(tableName)
|
|
193
|
-
.withIndex('by_user_id', (q: unknown) => (q as any).eq('id', args.id))
|
|
194
|
-
.first();
|
|
195
|
-
|
|
196
|
-
if (existing) {
|
|
197
|
-
await db.delete(existing._id);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
return {
|
|
201
|
-
success: true,
|
|
202
|
-
metadata: {
|
|
203
|
-
documentId: args.id,
|
|
204
|
-
timestamp,
|
|
205
|
-
version: args.version,
|
|
206
|
-
collectionName: tableName,
|
|
207
|
-
},
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Stream document changes from the CRDT component storage.
|
|
213
|
-
*
|
|
214
|
-
* This reads CRDT bytes from the component (not the main table) to enable
|
|
215
|
-
* true Y.applyUpdate() conflict resolution on the client.
|
|
216
|
-
* Can be used for both polling (awaitReplication) and subscriptions (live updates).
|
|
217
|
-
*
|
|
218
|
-
* @param ctx - Convex query context
|
|
219
|
-
* @param components - Generated components from Convex
|
|
220
|
-
* @param tableName - Name of the collection
|
|
221
|
-
* @param args - Checkpoint and limit for pagination
|
|
222
|
-
* @returns Array of changes with CRDT bytes
|
|
223
|
-
*/
|
|
224
|
-
export async function streamHelper<_DataModel extends GenericDataModel>(
|
|
225
|
-
ctx: unknown,
|
|
226
|
-
components: unknown,
|
|
227
|
-
tableName: string,
|
|
228
|
-
args: { checkpoint: { lastModified: number }; limit?: number }
|
|
229
|
-
): Promise<{
|
|
230
|
-
changes: Array<{
|
|
231
|
-
documentId: string;
|
|
232
|
-
crdtBytes: ArrayBuffer;
|
|
233
|
-
version: number;
|
|
234
|
-
timestamp: number;
|
|
235
|
-
}>;
|
|
236
|
-
checkpoint: { lastModified: number };
|
|
237
|
-
hasMore: boolean;
|
|
238
|
-
}> {
|
|
239
|
-
return (ctx as any).runQuery((components as any).replicate.public.stream, {
|
|
240
|
-
collectionName: tableName,
|
|
241
|
-
checkpoint: args.checkpoint,
|
|
242
|
-
limit: args.limit,
|
|
243
|
-
});
|
|
244
|
-
}
|