@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.
Files changed (94) hide show
  1. package/README.md +356 -420
  2. package/dist/client/collection.d.ts +78 -76
  3. package/dist/client/errors.d.ts +59 -0
  4. package/dist/client/index.d.ts +22 -18
  5. package/dist/client/logger.d.ts +0 -1
  6. package/dist/client/merge.d.ts +77 -0
  7. package/dist/client/persistence/adapters/index.d.ts +8 -0
  8. package/dist/client/persistence/adapters/opsqlite.d.ts +46 -0
  9. package/dist/client/persistence/adapters/sqljs.d.ts +83 -0
  10. package/dist/client/persistence/index.d.ts +49 -0
  11. package/dist/client/persistence/indexeddb.d.ts +17 -0
  12. package/dist/client/persistence/memory.d.ts +16 -0
  13. package/dist/client/persistence/sqlite-browser.d.ts +51 -0
  14. package/dist/client/persistence/sqlite-level.d.ts +63 -0
  15. package/dist/client/persistence/sqlite-rn.d.ts +36 -0
  16. package/dist/client/persistence/sqlite.d.ts +47 -0
  17. package/dist/client/persistence/types.d.ts +42 -0
  18. package/dist/client/prose.d.ts +56 -0
  19. package/dist/client/replicate.d.ts +40 -0
  20. package/dist/client/services/checkpoint.d.ts +18 -0
  21. package/dist/client/services/reconciliation.d.ts +24 -0
  22. package/dist/component/_generated/api.d.ts +35 -0
  23. package/dist/component/_generated/api.js +3 -3
  24. package/dist/component/_generated/component.d.ts +89 -0
  25. package/dist/component/_generated/component.js +0 -0
  26. package/dist/component/_generated/dataModel.d.ts +45 -0
  27. package/dist/component/_generated/dataModel.js +0 -0
  28. package/{src → dist}/component/_generated/server.d.ts +9 -38
  29. package/dist/component/convex.config.d.ts +2 -2
  30. package/dist/component/convex.config.js +2 -1
  31. package/dist/component/logger.d.ts +8 -0
  32. package/dist/component/logger.js +30 -0
  33. package/dist/component/public.d.ts +36 -61
  34. package/dist/component/public.js +232 -58
  35. package/dist/component/schema.d.ts +32 -8
  36. package/dist/component/schema.js +19 -6
  37. package/dist/index.js +1553 -308
  38. package/dist/server/builder.d.ts +94 -0
  39. package/dist/server/index.d.ts +14 -17
  40. package/dist/server/schema.d.ts +17 -63
  41. package/dist/server/storage.d.ts +80 -0
  42. package/dist/server.js +268 -83
  43. package/dist/shared/index.d.ts +5 -0
  44. package/dist/shared/index.js +2 -0
  45. package/dist/shared/types.d.ts +50 -0
  46. package/dist/shared/types.js +6 -0
  47. package/dist/shared.js +6 -0
  48. package/package.json +59 -49
  49. package/src/client/collection.ts +877 -450
  50. package/src/client/errors.ts +45 -0
  51. package/src/client/index.ts +52 -26
  52. package/src/client/logger.ts +2 -28
  53. package/src/client/merge.ts +374 -0
  54. package/src/client/persistence/adapters/index.ts +8 -0
  55. package/src/client/persistence/adapters/opsqlite.ts +54 -0
  56. package/src/client/persistence/adapters/sqljs.ts +128 -0
  57. package/src/client/persistence/index.ts +54 -0
  58. package/src/client/persistence/indexeddb.ts +110 -0
  59. package/src/client/persistence/memory.ts +61 -0
  60. package/src/client/persistence/sqlite-browser.ts +107 -0
  61. package/src/client/persistence/sqlite-level.ts +407 -0
  62. package/src/client/persistence/sqlite-rn.ts +44 -0
  63. package/src/client/persistence/sqlite.ts +161 -0
  64. package/src/client/persistence/types.ts +49 -0
  65. package/src/client/prose.ts +369 -0
  66. package/src/client/replicate.ts +80 -0
  67. package/src/client/services/checkpoint.ts +86 -0
  68. package/src/client/services/reconciliation.ts +108 -0
  69. package/src/component/_generated/api.ts +52 -0
  70. package/src/component/_generated/component.ts +103 -0
  71. package/src/component/_generated/{dataModel.d.ts → dataModel.ts} +1 -1
  72. package/src/component/_generated/server.ts +161 -0
  73. package/src/component/convex.config.ts +3 -1
  74. package/src/component/logger.ts +36 -0
  75. package/src/component/public.ts +364 -111
  76. package/src/component/schema.ts +18 -5
  77. package/src/env.d.ts +31 -0
  78. package/src/server/builder.ts +85 -0
  79. package/src/server/index.ts +9 -24
  80. package/src/server/schema.ts +20 -76
  81. package/src/server/storage.ts +313 -0
  82. package/src/shared/index.ts +5 -0
  83. package/src/shared/types.ts +52 -0
  84. package/LICENSE.package +0 -201
  85. package/dist/client/storage.d.ts +0 -143
  86. package/dist/server/replication.d.ts +0 -122
  87. package/dist/server/ssr.d.ts +0 -79
  88. package/dist/ssr.js +0 -19
  89. package/src/client/storage.ts +0 -206
  90. package/src/component/_generated/api.d.ts +0 -95
  91. package/src/component/_generated/api.js +0 -23
  92. package/src/component/_generated/server.js +0 -90
  93. package/src/server/replication.ts +0 -244
  94. package/src/server/ssr.ts +0 -106
package/LICENSE.package DELETED
@@ -1,201 +0,0 @@
1
- Apache License
2
- Version 2.0, January 2004
3
- http://www.apache.org/licenses/
4
-
5
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
-
7
- 1. Definitions.
8
-
9
- "License" shall mean the terms and conditions for use, reproduction,
10
- and distribution as defined by Sections 1 through 9 of this document.
11
-
12
- "Licensor" shall mean the copyright owner or entity authorized by
13
- the copyright owner that is granting the License.
14
-
15
- "Legal Entity" shall mean the union of the acting entity and all
16
- other entities that control, are controlled by, or are under common
17
- control with that entity. For the purposes of this definition,
18
- "control" means (i) the power, direct or indirect, to cause the
19
- direction or management of such entity, whether by contract or
20
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
- outstanding shares, or (iii) beneficial ownership of such entity.
22
-
23
- "You" (or "Your") shall mean an individual or Legal Entity
24
- exercising permissions granted by this License.
25
-
26
- "Source" form shall mean the preferred form for making modifications,
27
- including but not limited to software source code, documentation
28
- source, and configuration files.
29
-
30
- "Object" form shall mean any form resulting from mechanical
31
- transformation or translation of a Source form, including but
32
- not limited to compiled object code, generated documentation,
33
- and conversions to other media types.
34
-
35
- "Work" shall mean the work of authorship, whether in Source or
36
- Object form, made available under the License, as indicated by a
37
- copyright notice that is included in or attached to the work
38
- (an example is provided in the Appendix below).
39
-
40
- "Derivative Works" shall mean any work, whether in Source or Object
41
- form, that is based on (or derived from) the Work and for which the
42
- editorial revisions, annotations, elaborations, or other modifications
43
- represent, as a whole, an original work of authorship. For the purposes
44
- of this License, Derivative Works shall not include works that remain
45
- separable from, or merely link (or bind by name) to the interfaces of,
46
- the Work and Derivative Works thereof.
47
-
48
- "Contribution" shall mean any work of authorship, including
49
- the original version of the Work and any modifications or additions
50
- to that Work or Derivative Works thereof, that is intentionally
51
- submitted to Licensor for inclusion in the Work by the copyright owner
52
- or by an individual or Legal Entity authorized to submit on behalf of
53
- the copyright owner. For the purposes of this definition, "submitted"
54
- means any form of electronic, verbal, or written communication sent
55
- to the Licensor or its representatives, including but not limited to
56
- communication on electronic mailing lists, source code control systems,
57
- and issue tracking systems that are managed by, or on behalf of, the
58
- Licensor for the purpose of discussing and improving the Work, but
59
- excluding communication that is conspicuously marked or otherwise
60
- designated in writing by the copyright owner as "Not a Contribution."
61
-
62
- "Contributor" shall mean Licensor and any individual or Legal Entity
63
- on behalf of whom a Contribution has been received by Licensor and
64
- subsequently incorporated within the Work.
65
-
66
- 2. Grant of Copyright License. Subject to the terms and conditions of
67
- this License, each Contributor hereby grants to You a perpetual,
68
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
- copyright license to reproduce, prepare Derivative Works of,
70
- publicly display, publicly perform, sublicense, and distribute the
71
- Work and such Derivative Works in Source or Object form.
72
-
73
- 3. Grant of Patent License. Subject to the terms and conditions of
74
- this License, each Contributor hereby grants to You a perpetual,
75
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
- (except as stated in this section) patent license to make, have made,
77
- use, offer to sell, sell, import, and otherwise transfer the Work,
78
- where such license applies only to those patent claims licensable
79
- by such Contributor that are necessarily infringed by their
80
- Contribution(s) alone or by combination of their Contribution(s)
81
- with the Work to which such Contribution(s) was submitted. If You
82
- institute patent litigation against any entity (including a
83
- cross-claim or counterclaim in a lawsuit) alleging that the Work
84
- or a Contribution incorporated within the Work constitutes direct
85
- or contributory patent infringement, then any patent licenses
86
- granted to You under this License for that Work shall terminate
87
- as of the date such litigation is filed.
88
-
89
- 4. Redistribution. You may reproduce and distribute copies of the
90
- Work or Derivative Works thereof in any medium, with or without
91
- modifications, and in Source or Object form, provided that You
92
- meet the following conditions:
93
-
94
- (a) You must give any other recipients of the Work or
95
- Derivative Works a copy of this License; and
96
-
97
- (b) You must cause any modified files to carry prominent notices
98
- stating that You changed the files; and
99
-
100
- (c) You must retain, in the Source form of any Derivative Works
101
- that You distribute, all copyright, patent, trademark, and
102
- attribution notices from the Source form of the Work,
103
- excluding those notices that do not pertain to any part of
104
- the Derivative Works; and
105
-
106
- (d) If the Work includes a "NOTICE" text file as part of its
107
- distribution, then any Derivative Works that You distribute must
108
- include a readable copy of the attribution notices contained
109
- within such NOTICE file, excluding those notices that do not
110
- pertain to any part of the Derivative Works, in at least one
111
- of the following places: within a NOTICE text file distributed
112
- as part of the Derivative Works; within the Source form or
113
- documentation, if provided along with the Derivative Works; or,
114
- within a display generated by the Derivative Works, if and
115
- wherever such third-party notices normally appear. The contents
116
- of the NOTICE file are for informational purposes only and
117
- do not modify the License. You may add Your own attribution
118
- notices within Derivative Works that You distribute, alongside
119
- or as an addendum to the NOTICE text from the Work, provided
120
- that such additional attribution notices cannot be construed
121
- as modifying the License.
122
-
123
- You may add Your own copyright statement to Your modifications and
124
- may provide additional or different license terms and conditions
125
- for use, reproduction, or distribution of Your modifications, or
126
- for any such Derivative Works as a whole, provided Your use,
127
- reproduction, and distribution of the Work otherwise complies with
128
- the conditions stated in this License.
129
-
130
- 5. Submission of Contributions. Unless You explicitly state otherwise,
131
- any Contribution intentionally submitted for inclusion in the Work
132
- by You to the Licensor shall be under the terms and conditions of
133
- this License, without any additional terms or conditions.
134
- Notwithstanding the above, nothing herein shall supersede or modify
135
- the terms of any separate license agreement you may have executed
136
- with Licensor regarding such Contributions.
137
-
138
- 6. Trademarks. This License does not grant permission to use the trade
139
- names, trademarks, service marks, or product names of the Licensor,
140
- except as required for reasonable and customary use in describing the
141
- origin of the Work and reproducing the content of the NOTICE file.
142
-
143
- 7. Disclaimer of Warranty. Unless required by applicable law or
144
- agreed to in writing, Licensor provides the Work (and each
145
- Contributor provides its Contributions) on an "AS IS" BASIS,
146
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
- implied, including, without limitation, any warranties or conditions
148
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
- PARTICULAR PURPOSE. You are solely responsible for determining the
150
- appropriateness of using or redistributing the Work and assume any
151
- risks associated with Your exercise of permissions under this License.
152
-
153
- 8. Limitation of Liability. In no event and under no legal theory,
154
- whether in tort (including negligence), contract, or otherwise,
155
- unless required by applicable law (such as deliberate and grossly
156
- negligent acts) or agreed to in writing, shall any Contributor be
157
- liable to You for damages, including any direct, indirect, special,
158
- incidental, or consequential damages of any character arising as a
159
- result of this License or out of the use or inability to use the
160
- Work (including but not limited to damages for loss of goodwill,
161
- work stoppage, computer failure or malfunction, or any and all
162
- other commercial damages or losses), even if such Contributor
163
- has been advised of the possibility of such damages.
164
-
165
- 9. Accepting Warranty or Additional Liability. While redistributing
166
- the Work or Derivative Works thereof, You may choose to offer,
167
- and charge a fee for, acceptance of support, warranty, indemnity,
168
- or other liability obligations and/or rights consistent with this
169
- License. However, in accepting such obligations, You may act only
170
- on Your own behalf and on Your sole responsibility, not on behalf
171
- of any other Contributor, and only if You agree to indemnify,
172
- defend, and hold each Contributor harmless for any liability
173
- incurred by, or claims asserted against, such Contributor by reason
174
- of your accepting any such warranty or additional liability.
175
-
176
- END OF TERMS AND CONDITIONS
177
-
178
- APPENDIX: How to apply the Apache License to your work.
179
-
180
- To apply the Apache License to your work, attach the following
181
- boilerplate notice, with the fields enclosed by brackets "[]"
182
- replaced with your own identifying information. (Don't include
183
- the brackets!) The text should be enclosed in the appropriate
184
- comment syntax for the file format. We also recommend that a
185
- file or class name and description of purpose be included on the
186
- same "printed page" as the copyright notice for easier
187
- identification within third-party archives.
188
-
189
- Copyright 2025 Trestle Inc
190
-
191
- Licensed under the Apache License, Version 2.0 (the "License");
192
- you may not use this file except in compliance with the License.
193
- You may obtain a copy of the License at
194
-
195
- http://www.apache.org/licenses/LICENSE-2.0
196
-
197
- Unless required by applicable law or agreed to in writing, software
198
- distributed under the License is distributed on an "AS IS" BASIS,
199
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
- See the License for the specific language governing permissions and
201
- limitations under the License.
@@ -1,143 +0,0 @@
1
- import type { Expand, FunctionReference, GenericDataModel, GenericMutationCtx, GenericQueryCtx } from 'convex/server';
2
- import type { GenericId } from 'convex/values';
3
- import { api } from '../component/_generated/api';
4
- /**
5
- * A client API for interacting with the Convex Replicate storage component.
6
- *
7
- * This class provides a type-safe, scoped interface for storing and retrieving
8
- * CRDT document data from the replicate component. Each instance is scoped to
9
- * a specific collection name, eliminating the need to pass collectionName to
10
- * every method call.
11
- *
12
- * @example
13
- * ```typescript
14
- * import { components } from "./_generated/api";
15
- * import { ReplicateStorage } from "@trestleinc/convex-replicate-component";
16
- *
17
- * // Create a storage instance for the "tasks" collection
18
- * const tasksStorage = new ReplicateStorage(components.replicate, "tasks");
19
- *
20
- * export const submitTask = mutation({
21
- * handler: async (ctx, args) => {
22
- * return await tasksStorage.submitDocument(
23
- * ctx,
24
- * args.id,
25
- * args.document,
26
- * args.version
27
- * );
28
- * }
29
- * });
30
- *
31
- * export const getTasks = query({
32
- * handler: async (ctx, args) => {
33
- * return await tasksStorage.stream(ctx, args.checkpoint, args.limit);
34
- * }
35
- * });
36
- * ```
37
- *
38
- * @template TDocument - The document type being stored (must have an id field)
39
- */
40
- export declare class ReplicateStorage<_TDocument extends {
41
- id: string;
42
- } = {
43
- id: string;
44
- }> {
45
- private component;
46
- private collectionName;
47
- /**
48
- * Create a new ReplicateStorage instance scoped to a specific collection.
49
- *
50
- * @param component - The replicate component from your generated API
51
- * @param collectionName - The name of the collection to interact with
52
- *
53
- * @example
54
- * ```typescript
55
- * const tasksStorage = new ReplicateStorage(
56
- * components.replicate,
57
- * "tasks"
58
- * );
59
- * ```
60
- */
61
- constructor(component: UseApi<typeof api>, collectionName: string);
62
- /**
63
- * Insert a new document into the replicate component storage.
64
- *
65
- * This stores the CRDT bytes in the component's internal storage table.
66
- *
67
- * @param ctx - Convex mutation context
68
- * @param documentId - Unique identifier for the document
69
- * @param crdtBytes - The CRDT binary data (from Automerge.save())
70
- * @param version - Version number for conflict resolution
71
- * @returns Success indicator
72
- */
73
- insertDocument(ctx: RunMutationCtx, documentId: string, crdtBytes: ArrayBuffer, version: number): Promise<{
74
- success: boolean;
75
- }>;
76
- /**
77
- * Update an existing document in the replicate component storage.
78
- *
79
- * @param ctx - Convex mutation context
80
- * @param documentId - Unique identifier for the document
81
- * @param crdtBytes - The CRDT binary data (from Automerge.save())
82
- * @param version - Version number for conflict resolution
83
- * @returns Success indicator
84
- */
85
- updateDocument(ctx: RunMutationCtx, documentId: string, crdtBytes: ArrayBuffer, version: number): Promise<{
86
- success: boolean;
87
- }>;
88
- /**
89
- * Delete a document from the replicate component storage.
90
- * Appends deletion delta to event log.
91
- *
92
- * @param ctx - Convex mutation context
93
- * @param documentId - Unique identifier for the document
94
- * @param crdtBytes - Yjs deletion delta
95
- * @param version - CRDT version number
96
- * @returns Success indicator
97
- */
98
- deleteDocument(ctx: RunMutationCtx, documentId: string, crdtBytes: ArrayBuffer, version: number): Promise<{
99
- success: boolean;
100
- }>;
101
- /**
102
- * Stream CRDT changes for incremental replication.
103
- *
104
- * Retrieves CRDT bytes for documents that have been modified since the
105
- * provided checkpoint, enabling incremental replication.
106
- * Can be used for both polling (awaitReplication) and subscriptions (live updates).
107
- *
108
- * @param ctx - Convex query context
109
- * @param checkpoint - Last known modification timestamp
110
- * @param limit - Maximum number of changes to retrieve (default: 100)
111
- * @returns Array of changes with updated checkpoint
112
- */
113
- stream(ctx: RunQueryCtx, checkpoint: {
114
- lastModified: number;
115
- }, limit?: number): Promise<{
116
- changes: Array<{
117
- documentId: string;
118
- crdtBytes: ArrayBuffer;
119
- version: number;
120
- timestamp: number;
121
- }>;
122
- checkpoint: {
123
- lastModified: number;
124
- };
125
- hasMore: boolean;
126
- }>;
127
- }
128
- /**
129
- * Re-export the component API for direct access if needed.
130
- */
131
- export { api };
132
- type RunQueryCtx = {
133
- runQuery: GenericQueryCtx<GenericDataModel>['runQuery'];
134
- };
135
- type RunMutationCtx = {
136
- runMutation: GenericMutationCtx<GenericDataModel>['runMutation'];
137
- };
138
- export type OpaqueIds<T> = T extends GenericId<infer _T> ? string : T extends (infer U)[] ? OpaqueIds<U>[] : T extends object ? {
139
- [K in keyof T]: OpaqueIds<T[K]>;
140
- } : T;
141
- export type UseApi<API> = Expand<{
142
- [mod in keyof API]: API[mod] extends FunctionReference<infer FType, 'public', infer FArgs, infer FReturnType, infer FComponentPath> ? FunctionReference<FType, 'internal', OpaqueIds<FArgs>, OpaqueIds<FReturnType>, FComponentPath> : UseApi<API[mod]>;
143
- }>;
@@ -1,122 +0,0 @@
1
- import type { GenericDataModel } from 'convex/server';
2
- /**
3
- * Insert a document into both the CRDT component and the main application table.
4
- *
5
- * DUAL-STORAGE ARCHITECTURE:
6
- * This helper implements a dual-storage pattern where documents are stored in two places:
7
- *
8
- * 1. Component Storage (CRDT Layer):
9
- * - Stores CRDT bytes (from Yjs) for offline-first conflict resolution
10
- * - Handles concurrent updates with automatic merging
11
- * - Provides the source of truth for offline changes
12
- *
13
- * 2. Main Application Table:
14
- * - Stores materialized documents for efficient querying
15
- * - Used by server-side Convex functions that need to query/join data
16
- * - Optimized for reactive subscriptions and complex queries
17
- *
18
- * WHY BOTH?
19
- * - Component: Handles conflict resolution and offline replication (CRDT bytes)
20
- * - Main table: Enables efficient server-side queries (materialized docs)
21
- * - Similar to event sourcing: component = event log, main table = read model
22
- *
23
- * @param ctx - Convex mutation context
24
- * @param components - Generated components from Convex
25
- * @param tableName - Name of the main application table
26
- * @param args - Document data with id, crdtBytes, materializedDoc, and version
27
- * @returns Success indicator
28
- */
29
- export declare function insertDocumentHelper<_DataModel extends GenericDataModel>(ctx: unknown, components: unknown, tableName: string, args: {
30
- id: string;
31
- crdtBytes: ArrayBuffer;
32
- materializedDoc: unknown;
33
- version: number;
34
- }): Promise<{
35
- success: boolean;
36
- metadata: {
37
- documentId: string;
38
- timestamp: number;
39
- version: number;
40
- collectionName: string;
41
- };
42
- }>;
43
- /**
44
- * Update a document in both the CRDT component and the main application table.
45
- *
46
- * @param ctx - Convex mutation context
47
- * @param components - Generated components from Convex
48
- * @param tableName - Name of the main application table
49
- * @param args - Document data with id, crdtBytes, materializedDoc, and version
50
- * @returns Success indicator
51
- */
52
- export declare function updateDocumentHelper<_DataModel extends GenericDataModel>(ctx: unknown, components: unknown, tableName: string, args: {
53
- id: string;
54
- crdtBytes: ArrayBuffer;
55
- materializedDoc: unknown;
56
- version: number;
57
- }): Promise<{
58
- success: boolean;
59
- metadata: {
60
- documentId: string;
61
- timestamp: number;
62
- version: number;
63
- collectionName: string;
64
- };
65
- }>;
66
- /**
67
- * HARD delete a document from main table, APPEND deletion delta to component.
68
- *
69
- * NEW BEHAVIOR (v0.3.0):
70
- * - Appends deletion delta to component event log (preserves history)
71
- * - Physically removes document from main table (hard delete)
72
- * - CRDT history preserved for future recovery features
73
- *
74
- * @param ctx - Convex mutation context
75
- * @param components - Generated components from Convex
76
- * @param tableName - Name of the main application table
77
- * @param args - Document data with id, crdtBytes (deletion delta), and version
78
- * @returns Success indicator with metadata
79
- */
80
- export declare function deleteDocumentHelper<_DataModel extends GenericDataModel>(ctx: unknown, components: unknown, tableName: string, args: {
81
- id: string;
82
- crdtBytes: ArrayBuffer;
83
- version: number;
84
- }): Promise<{
85
- success: boolean;
86
- metadata: {
87
- documentId: string;
88
- timestamp: number;
89
- version: number;
90
- collectionName: string;
91
- };
92
- }>;
93
- /**
94
- * Stream document changes from the CRDT component storage.
95
- *
96
- * This reads CRDT bytes from the component (not the main table) to enable
97
- * true Y.applyUpdate() conflict resolution on the client.
98
- * Can be used for both polling (awaitReplication) and subscriptions (live updates).
99
- *
100
- * @param ctx - Convex query context
101
- * @param components - Generated components from Convex
102
- * @param tableName - Name of the collection
103
- * @param args - Checkpoint and limit for pagination
104
- * @returns Array of changes with CRDT bytes
105
- */
106
- export declare function streamHelper<_DataModel extends GenericDataModel>(ctx: unknown, components: unknown, tableName: string, args: {
107
- checkpoint: {
108
- lastModified: number;
109
- };
110
- limit?: number;
111
- }): Promise<{
112
- changes: Array<{
113
- documentId: string;
114
- crdtBytes: ArrayBuffer;
115
- version: number;
116
- timestamp: number;
117
- }>;
118
- checkpoint: {
119
- lastModified: number;
120
- };
121
- hasMore: boolean;
122
- }>;
@@ -1,79 +0,0 @@
1
- /**
2
- * Server-Side Rendering (SSR) Utilities
3
- *
4
- * This module provides utilities for loading collection data during
5
- * server-side rendering. Use `loadCollection` with an explicit config
6
- * object for clarity and type safety.
7
- *
8
- * @module ssr
9
- * @example
10
- * ```typescript
11
- * import { loadCollection } from '@convex-replicate/core/ssr';
12
- * import { api } from '../convex/_generated/api';
13
- *
14
- * const tasks = await loadCollection<Task>(httpClient, {
15
- * api: api.tasks,
16
- * collection: 'tasks',
17
- * limit: 100,
18
- * });
19
- * ```
20
- */
21
- import type { ConvexHttpClient } from 'convex/browser';
22
- import type { FunctionReference } from 'convex/server';
23
- /**
24
- * API module shape expected by loadCollection.
25
- *
26
- * This should match the generated API module for your collection
27
- * (e.g., api.tasks, api.users, etc.)
28
- */
29
- export type CollectionAPI = {
30
- stream: FunctionReference<'query', 'public' | 'internal'>;
31
- };
32
- /**
33
- * Configuration for loading collection data during SSR.
34
- */
35
- export interface LoadCollectionConfig {
36
- /** The API module for the collection (e.g., api.tasks) */
37
- api: CollectionAPI;
38
- /** The collection name (should match the API module name) */
39
- collection: string;
40
- /** Maximum number of items to load (default: 100) */
41
- limit?: number;
42
- }
43
- /**
44
- * Load collection data for server-side rendering.
45
- *
46
- * **IMPORTANT**: This function is currently limited because `stream` only returns
47
- * CRDT bytes, not materialized documents. For most SSR use cases, it's recommended to
48
- * create a separate query that reads from your main table instead.
49
- *
50
- * @deprecated Consider creating a dedicated SSR query instead. See example below.
51
- *
52
- * @param httpClient - Convex HTTP client for server-side queries
53
- * @param config - Configuration object with api, collection, and options
54
- * @returns Promise resolving to array of items from the collection
55
- *
56
- * @example
57
- * **Recommended SSR Pattern:**
58
- * ```typescript
59
- * // convex/tasks.ts
60
- * export const list = query({
61
- * handler: async (ctx) => {
62
- * return await ctx.db
63
- * .query('tasks')
64
- * .filter((q) => q.neq(q.field('deleted'), true))
65
- * .collect();
66
- * },
67
- * });
68
- *
69
- * // In your route loader
70
- * import { ConvexHttpClient } from 'convex/browser';
71
- * import { api } from '../convex/_generated/api';
72
- *
73
- * const httpClient = new ConvexHttpClient(import.meta.env.VITE_CONVEX_URL);
74
- * const tasks = await httpClient.query(api.tasks.list);
75
- * ```
76
- */
77
- export declare function loadCollection<TItem extends {
78
- id: string;
79
- }>(httpClient: ConvexHttpClient, config: LoadCollectionConfig): Promise<ReadonlyArray<TItem>>;
package/dist/ssr.js DELETED
@@ -1,19 +0,0 @@
1
- async function loadCollection(httpClient, config) {
2
- const result = await httpClient.query(config.api.stream, {
3
- collectionName: config.collection,
4
- checkpoint: {
5
- lastModified: 0
6
- },
7
- limit: config.limit ?? 100
8
- });
9
- const items = [];
10
- for (const change of result.changes){
11
- const item = {
12
- id: change.documentId,
13
- ...change.document
14
- };
15
- items.push(item);
16
- }
17
- return items;
18
- }
19
- export { loadCollection };