@byearlybird/starling 0.10.0 → 0.11.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/dist/core-DI0FfUjX.js +423 -0
- package/dist/core.d.ts +2 -0
- package/dist/core.js +3 -0
- package/dist/db-qQgPYE41.d.ts +199 -0
- package/dist/index-D7bXWDg6.d.ts +270 -0
- package/dist/index.d.ts +3 -270
- package/dist/index.js +390 -351
- package/dist/plugin-http.d.ts +139 -0
- package/dist/plugin-http.js +191 -0
- package/dist/plugin-idb.d.ts +59 -0
- package/dist/plugin-idb.js +169 -0
- package/package.json +22 -2
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
//#region src/core/clock/clock.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A Hybrid Logical Clock that generates monotonically increasing eventstamps.
|
|
4
|
+
* Combines wall-clock time with a counter for handling clock stalls and a
|
|
5
|
+
* random nonce for tie-breaking.
|
|
6
|
+
*
|
|
7
|
+
* The clock automatically increments the counter when the wall clock doesn't
|
|
8
|
+
* advance, ensuring eventstamps are always unique and monotonic.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const clock = createClock();
|
|
13
|
+
* const stamp1 = clock.now();
|
|
14
|
+
* const stamp2 = clock.now();
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
type Clock = ReturnType<typeof createClock>;
|
|
18
|
+
/**
|
|
19
|
+
* Create a new Clock instance.
|
|
20
|
+
* @param initialState - Optional initial state for the clock
|
|
21
|
+
*/
|
|
22
|
+
declare function createClock(initialState?: {
|
|
23
|
+
counter: number;
|
|
24
|
+
lastMs: number;
|
|
25
|
+
lastNonce: string;
|
|
26
|
+
}): {
|
|
27
|
+
now: () => string;
|
|
28
|
+
latest: () => string;
|
|
29
|
+
forward: (eventstamp: string) => void;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Create a Clock from an eventstamp string.
|
|
33
|
+
* @param eventstamp - Eventstamp string to decode and initialize clock from
|
|
34
|
+
* @throws Error if eventstamp is invalid
|
|
35
|
+
*/
|
|
36
|
+
declare function createClockFromEventstamp(eventstamp: string): Clock;
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/core/clock/errors.d.ts
|
|
39
|
+
declare class InvalidEventstampError extends Error {
|
|
40
|
+
constructor(eventstamp: string);
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/core/clock/eventstamp.d.ts
|
|
44
|
+
/**
|
|
45
|
+
* Validates whether a string is a properly formatted eventstamp.
|
|
46
|
+
* Expected format: YYYY-MM-DDTHH:mm:ss.SSSZ|HHHH+|HHHH
|
|
47
|
+
* where HHHH+ represents 4 or more hex characters for the counter,
|
|
48
|
+
* and HHHH represents exactly 4 hex characters for the nonce.
|
|
49
|
+
*/
|
|
50
|
+
declare function isValidEventstamp(stamp: string): boolean;
|
|
51
|
+
declare const MIN_EVENTSTAMP: string;
|
|
52
|
+
/**
|
|
53
|
+
* Find the maximum eventstamp from an array of eventstamps.
|
|
54
|
+
* Returns MIN_EVENTSTAMP if the array is empty.
|
|
55
|
+
* @param eventstamps - Array of eventstamp strings
|
|
56
|
+
* @returns The maximum eventstamp
|
|
57
|
+
*/
|
|
58
|
+
declare function maxEventstamp(eventstamps: string[]): string;
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/core/document/resource.d.ts
|
|
61
|
+
/**
|
|
62
|
+
* Resource object structure representing a single stored entity.
|
|
63
|
+
* Resources are the primary unit of storage and synchronization in Starling.
|
|
64
|
+
*
|
|
65
|
+
* Each resource has a type, unique identifier, attributes containing the data,
|
|
66
|
+
* and metadata for tracking deletion state and eventstamps.
|
|
67
|
+
*/
|
|
68
|
+
type ResourceObject<T extends AnyObject> = {
|
|
69
|
+
/** Resource type identifier */
|
|
70
|
+
type: string;
|
|
71
|
+
/** Unique identifier for this resource */
|
|
72
|
+
id: string;
|
|
73
|
+
/** The resource's data as a nested object structure */
|
|
74
|
+
attributes: T;
|
|
75
|
+
/** Metadata for tracking deletion and eventstamps */
|
|
76
|
+
meta: {
|
|
77
|
+
/** Flat map of dot-separated paths to eventstamps (e.g., "user.address.street": "2025-11-18...") */
|
|
78
|
+
eventstamps: Record<string, string>;
|
|
79
|
+
/** The greatest eventstamp in this resource (including deletedAt if applicable) */
|
|
80
|
+
latest: string;
|
|
81
|
+
/** Eventstamp when this resource was soft-deleted, or null if not deleted */
|
|
82
|
+
deletedAt: string | null;
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
declare function makeResource<T extends AnyObject>(type: string, id: string, obj: T, eventstamp: string, deletedAt?: string | null): ResourceObject<T>;
|
|
86
|
+
declare function mergeResources<T extends AnyObject>(into: ResourceObject<T>, from: ResourceObject<T>): ResourceObject<T>;
|
|
87
|
+
declare function deleteResource<T extends AnyObject>(resource: ResourceObject<T>, eventstamp: string): ResourceObject<T>;
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/core/document/document.d.ts
|
|
90
|
+
/**
|
|
91
|
+
* Base constraint for all document data in Starling.
|
|
92
|
+
* Documents must be plain JavaScript objects with string keys.
|
|
93
|
+
*/
|
|
94
|
+
type AnyObject = Record<string, unknown>;
|
|
95
|
+
/**
|
|
96
|
+
* A JSON:API document represents the complete state of a store:
|
|
97
|
+
* - API version information
|
|
98
|
+
* - Metadata including the highest eventstamp observed across all operations
|
|
99
|
+
* - A set of resource objects (including soft-deleted ones)
|
|
100
|
+
*
|
|
101
|
+
* Documents are the unit of synchronization between store replicas.
|
|
102
|
+
*/
|
|
103
|
+
type JsonDocument<T extends AnyObject> = {
|
|
104
|
+
/** API version information */
|
|
105
|
+
jsonapi: {
|
|
106
|
+
version: "1.1";
|
|
107
|
+
};
|
|
108
|
+
/** Document-level metadata */
|
|
109
|
+
meta: {
|
|
110
|
+
/** Latest eventstamp observed by this document for clock synchronization */
|
|
111
|
+
latest: string;
|
|
112
|
+
};
|
|
113
|
+
/** Array of resource objects with eventstamps and metadata */
|
|
114
|
+
data: ResourceObject<T>[];
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Change tracking information returned by mergeDocuments.
|
|
118
|
+
* Categorizes resources by mutation type for hook notifications.
|
|
119
|
+
*/
|
|
120
|
+
type DocumentChanges<T extends AnyObject> = {
|
|
121
|
+
/** Resources that were newly added (didn't exist before or were previously deleted) */
|
|
122
|
+
added: Map<string, ResourceObject<T>>;
|
|
123
|
+
/** Resources that were modified (existed before and changed) */
|
|
124
|
+
updated: Map<string, ResourceObject<T>>;
|
|
125
|
+
/** Resources that were deleted (newly marked with deletedAt) */
|
|
126
|
+
deleted: Set<string>;
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Result of merging two JSON:API documents.
|
|
130
|
+
*/
|
|
131
|
+
type MergeDocumentsResult<T extends AnyObject> = {
|
|
132
|
+
/** The merged document with updated resources and forwarded clock */
|
|
133
|
+
document: JsonDocument<T>;
|
|
134
|
+
/** Change tracking for plugin hook notifications */
|
|
135
|
+
changes: DocumentChanges<T>;
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Merges two JSON:API documents using field-level Last-Write-Wins semantics.
|
|
139
|
+
*
|
|
140
|
+
* The merge operation:
|
|
141
|
+
* 1. Forwards the clock to the newest eventstamp from either document
|
|
142
|
+
* 2. Merges each resource pair using field-level LWW (via mergeResources)
|
|
143
|
+
* 3. Tracks what changed for hook notifications (added/updated/deleted)
|
|
144
|
+
*
|
|
145
|
+
* Deletion is final: once a resource is deleted, updates to it are merged into
|
|
146
|
+
* the resource's attributes but don't restore visibility. Only new resources or
|
|
147
|
+
* transitions into the deleted state are tracked.
|
|
148
|
+
*
|
|
149
|
+
* @param into - The base document to merge into
|
|
150
|
+
* @param from - The source document to merge from
|
|
151
|
+
* @returns Merged document and categorized changes
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* ```typescript
|
|
155
|
+
* const into = {
|
|
156
|
+
* jsonapi: { version: "1.1" },
|
|
157
|
+
* meta: { latest: "2025-01-01T00:00:00.000Z|0001|a1b2" },
|
|
158
|
+
* data: [{ type: "items", id: "doc1", attributes: {...}, meta: { deletedAt: null, latest: "..." } }]
|
|
159
|
+
* };
|
|
160
|
+
*
|
|
161
|
+
* const from = {
|
|
162
|
+
* jsonapi: { version: "1.1" },
|
|
163
|
+
* meta: { latest: "2025-01-01T00:05:00.000Z|0001|c3d4" },
|
|
164
|
+
* data: [
|
|
165
|
+
* { type: "items", id: "doc1", attributes: {...}, meta: { deletedAt: null, latest: "..." } }, // updated
|
|
166
|
+
* { type: "items", id: "doc2", attributes: {...}, meta: { deletedAt: null, latest: "..." } } // new
|
|
167
|
+
* ]
|
|
168
|
+
* };
|
|
169
|
+
*
|
|
170
|
+
* const result = mergeDocuments(into, from);
|
|
171
|
+
* // result.document.meta.latest === "2025-01-01T00:05:00.000Z|0001|c3d4"
|
|
172
|
+
* // result.changes.added has "doc2"
|
|
173
|
+
* // result.changes.updated has "doc1"
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
declare function mergeDocuments<T extends AnyObject>(into: JsonDocument<T>, from: JsonDocument<T>): MergeDocumentsResult<T>;
|
|
177
|
+
/**
|
|
178
|
+
* Creates an empty JSON:API document with the given eventstamp.
|
|
179
|
+
* Useful for initializing new stores or testing.
|
|
180
|
+
*
|
|
181
|
+
* @param eventstamp - Initial clock value for this document
|
|
182
|
+
* @returns Empty document
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```typescript
|
|
186
|
+
* const empty = makeDocument("2025-01-01T00:00:00.000Z|0000|0000");
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
declare function makeDocument<T extends AnyObject>(eventstamp: string): JsonDocument<T>;
|
|
190
|
+
//#endregion
|
|
191
|
+
//#region src/core/document/utils.d.ts
|
|
192
|
+
/**
|
|
193
|
+
* Convert a JsonDocument's data array into a Map keyed by resource ID.
|
|
194
|
+
* @param document - JsonDocument containing resource data
|
|
195
|
+
* @returns Map of resource ID to ResourceObject
|
|
196
|
+
*/
|
|
197
|
+
declare function documentToMap<T extends AnyObject>(document: JsonDocument<T>): Map<string, ResourceObject<T>>;
|
|
198
|
+
/**
|
|
199
|
+
* Convert a Map of resources into a JsonDocument.
|
|
200
|
+
* @param resources - Map of resource ID to ResourceObject
|
|
201
|
+
* @param fallbackEventstamp - Eventstamp to include when computing the max (optional)
|
|
202
|
+
* @returns JsonDocument representation of the resources
|
|
203
|
+
*/
|
|
204
|
+
declare function mapToDocument<T extends AnyObject>(resources: Map<string, ResourceObject<T>>, fallbackEventstamp?: string): JsonDocument<T>;
|
|
205
|
+
//#endregion
|
|
206
|
+
//#region src/core/resource-map/resource-map.d.ts
|
|
207
|
+
/**
|
|
208
|
+
* A ResourceMap container for storing and managing ResourceObjects.
|
|
209
|
+
*
|
|
210
|
+
* This factory function creates a ResourceMap with state-based replication
|
|
211
|
+
* and automatic convergence via Last-Write-Wins conflict resolution.
|
|
212
|
+
* It stores complete resource snapshots with encoded metadata, including deletion markers.
|
|
213
|
+
*
|
|
214
|
+
* ResourceMap does NOT filter based on deletion status—it stores and returns
|
|
215
|
+
* all ResourceObjects including deleted ones. The Store class is responsible
|
|
216
|
+
* for filtering what's visible to users.
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```typescript
|
|
220
|
+
* const resourceMap = createMap("todos");
|
|
221
|
+
* resourceMap.set("id1", { name: "Alice" });
|
|
222
|
+
* const resource = resourceMap.get("id1"); // ResourceObject with metadata
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
declare function createMap<T extends AnyObject>(resourceType: string, initialMap?: Map<string, ResourceObject<T>>, eventstamp?: string): {
|
|
226
|
+
/**
|
|
227
|
+
* Check if a resource exists by ID (regardless of deletion status).
|
|
228
|
+
* @param id - Resource ID
|
|
229
|
+
*/
|
|
230
|
+
has(id: string): boolean;
|
|
231
|
+
/**
|
|
232
|
+
* Get a resource by ID (regardless of deletion status).
|
|
233
|
+
* @returns The raw resource with metadata (including deletedAt flag), or undefined if not found
|
|
234
|
+
*/
|
|
235
|
+
get(id: string): ResourceObject<T> | undefined;
|
|
236
|
+
/**
|
|
237
|
+
* Iterate over all resources (including deleted) as [id, resource] tuples.
|
|
238
|
+
*/
|
|
239
|
+
entries(): IterableIterator<readonly [string, ResourceObject<T>]>;
|
|
240
|
+
/**
|
|
241
|
+
* Set a resource using field-level Last-Write-Wins merge.
|
|
242
|
+
* Creates a new resource if it doesn't exist, or merges with existing resource.
|
|
243
|
+
* @param id - Resource ID (provided by caller, not generated)
|
|
244
|
+
* @param object - Data to set (partial fields are merged, full objects replace)
|
|
245
|
+
*/
|
|
246
|
+
set(id: string, object: Partial<T>): void;
|
|
247
|
+
delete(id: string): void;
|
|
248
|
+
/**
|
|
249
|
+
* Clone the internal map of encoded resources.
|
|
250
|
+
*/
|
|
251
|
+
cloneMap(): Map<string, ResourceObject<T>>;
|
|
252
|
+
/**
|
|
253
|
+
* Export the current state as a JsonDocument snapshot.
|
|
254
|
+
*/
|
|
255
|
+
toDocument(): JsonDocument<T>;
|
|
256
|
+
/**
|
|
257
|
+
* Merge another document into this ResourceMap using field-level Last-Write-Wins.
|
|
258
|
+
* @returns The merge result containing the merged document and tracked changes
|
|
259
|
+
* @param document - JsonDocument from another replica or storage
|
|
260
|
+
*/
|
|
261
|
+
merge(document: JsonDocument<T>): MergeDocumentsResult<T>;
|
|
262
|
+
};
|
|
263
|
+
/**
|
|
264
|
+
* Create a ResourceMap from a JsonDocument snapshot.
|
|
265
|
+
* @param type - Resource type identifier (defaults to "default")
|
|
266
|
+
* @param document - JsonDocument containing resource data
|
|
267
|
+
*/
|
|
268
|
+
declare function createMapFromDocument<U extends AnyObject>(type: string, document: JsonDocument<U>): ReturnType<typeof createMap<U>>;
|
|
269
|
+
//#endregion
|
|
270
|
+
export { maxEventstamp as _, AnyObject as a, createClock as b, MergeDocumentsResult as c, ResourceObject as d, deleteResource as f, isValidEventstamp as g, MIN_EVENTSTAMP as h, mapToDocument as i, makeDocument as l, mergeResources as m, createMapFromDocument as n, DocumentChanges as o, makeResource as p, documentToMap as r, JsonDocument as s, createMap as t, mergeDocuments as u, InvalidEventstampError as v, createClockFromEventstamp as x, Clock as y };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,270 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* Combines wall-clock time with a counter for handling clock stalls and a
|
|
5
|
-
* random nonce for tie-breaking.
|
|
6
|
-
*
|
|
7
|
-
* The clock automatically increments the counter when the wall clock doesn't
|
|
8
|
-
* advance, ensuring eventstamps are always unique and monotonic.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```typescript
|
|
12
|
-
* const clock = createClock();
|
|
13
|
-
* const stamp1 = clock.now();
|
|
14
|
-
* const stamp2 = clock.now();
|
|
15
|
-
* ```
|
|
16
|
-
*/
|
|
17
|
-
type Clock = ReturnType<typeof createClock>;
|
|
18
|
-
/**
|
|
19
|
-
* Create a new Clock instance.
|
|
20
|
-
* @param initialState - Optional initial state for the clock
|
|
21
|
-
*/
|
|
22
|
-
declare function createClock(initialState?: {
|
|
23
|
-
counter: number;
|
|
24
|
-
lastMs: number;
|
|
25
|
-
lastNonce: string;
|
|
26
|
-
}): {
|
|
27
|
-
now: () => string;
|
|
28
|
-
latest: () => string;
|
|
29
|
-
forward: (eventstamp: string) => void;
|
|
30
|
-
};
|
|
31
|
-
/**
|
|
32
|
-
* Create a Clock from an eventstamp string.
|
|
33
|
-
* @param eventstamp - Eventstamp string to decode and initialize clock from
|
|
34
|
-
* @throws Error if eventstamp is invalid
|
|
35
|
-
*/
|
|
36
|
-
declare function createClockFromEventstamp(eventstamp: string): Clock;
|
|
37
|
-
//#endregion
|
|
38
|
-
//#region src/clock/errors.d.ts
|
|
39
|
-
declare class InvalidEventstampError extends Error {
|
|
40
|
-
constructor(eventstamp: string);
|
|
41
|
-
}
|
|
42
|
-
//#endregion
|
|
43
|
-
//#region src/clock/eventstamp.d.ts
|
|
44
|
-
/**
|
|
45
|
-
* Validates whether a string is a properly formatted eventstamp.
|
|
46
|
-
* Expected format: YYYY-MM-DDTHH:mm:ss.SSSZ|HHHH+|HHHH
|
|
47
|
-
* where HHHH+ represents 4 or more hex characters for the counter,
|
|
48
|
-
* and HHHH represents exactly 4 hex characters for the nonce.
|
|
49
|
-
*/
|
|
50
|
-
declare function isValidEventstamp(stamp: string): boolean;
|
|
51
|
-
declare const MIN_EVENTSTAMP: string;
|
|
52
|
-
/**
|
|
53
|
-
* Find the maximum eventstamp from an array of eventstamps.
|
|
54
|
-
* Returns MIN_EVENTSTAMP if the array is empty.
|
|
55
|
-
* @param eventstamps - Array of eventstamp strings
|
|
56
|
-
* @returns The maximum eventstamp
|
|
57
|
-
*/
|
|
58
|
-
declare function maxEventstamp(eventstamps: string[]): string;
|
|
59
|
-
//#endregion
|
|
60
|
-
//#region src/document/resource.d.ts
|
|
61
|
-
/**
|
|
62
|
-
* Resource object structure representing a single stored entity.
|
|
63
|
-
* Resources are the primary unit of storage and synchronization in Starling.
|
|
64
|
-
*
|
|
65
|
-
* Each resource has a type, unique identifier, attributes containing the data,
|
|
66
|
-
* and metadata for tracking deletion state and eventstamps.
|
|
67
|
-
*/
|
|
68
|
-
type ResourceObject<T extends AnyObject> = {
|
|
69
|
-
/** Resource type identifier */
|
|
70
|
-
type: string;
|
|
71
|
-
/** Unique identifier for this resource */
|
|
72
|
-
id: string;
|
|
73
|
-
/** The resource's data as a nested object structure */
|
|
74
|
-
attributes: T;
|
|
75
|
-
/** Metadata for tracking deletion and eventstamps */
|
|
76
|
-
meta: {
|
|
77
|
-
/** Flat map of dot-separated paths to eventstamps (e.g., "user.address.street": "2025-11-18...") */
|
|
78
|
-
eventstamps: Record<string, string>;
|
|
79
|
-
/** The greatest eventstamp in this resource (including deletedAt if applicable) */
|
|
80
|
-
latest: string;
|
|
81
|
-
/** Eventstamp when this resource was soft-deleted, or null if not deleted */
|
|
82
|
-
deletedAt: string | null;
|
|
83
|
-
};
|
|
84
|
-
};
|
|
85
|
-
declare function makeResource<T extends AnyObject>(type: string, id: string, obj: T, eventstamp: string, deletedAt?: string | null): ResourceObject<T>;
|
|
86
|
-
declare function mergeResources<T extends AnyObject>(into: ResourceObject<T>, from: ResourceObject<T>): ResourceObject<T>;
|
|
87
|
-
declare function deleteResource<T extends AnyObject>(resource: ResourceObject<T>, eventstamp: string): ResourceObject<T>;
|
|
88
|
-
//#endregion
|
|
89
|
-
//#region src/document/document.d.ts
|
|
90
|
-
/**
|
|
91
|
-
* Base constraint for all document data in Starling.
|
|
92
|
-
* Documents must be plain JavaScript objects with string keys.
|
|
93
|
-
*/
|
|
94
|
-
type AnyObject = Record<string, unknown>;
|
|
95
|
-
/**
|
|
96
|
-
* A JSON:API document represents the complete state of a store:
|
|
97
|
-
* - API version information
|
|
98
|
-
* - Metadata including the highest eventstamp observed across all operations
|
|
99
|
-
* - A set of resource objects (including soft-deleted ones)
|
|
100
|
-
*
|
|
101
|
-
* Documents are the unit of synchronization between store replicas.
|
|
102
|
-
*/
|
|
103
|
-
type JsonDocument<T extends AnyObject> = {
|
|
104
|
-
/** API version information */
|
|
105
|
-
jsonapi: {
|
|
106
|
-
version: "1.1";
|
|
107
|
-
};
|
|
108
|
-
/** Document-level metadata */
|
|
109
|
-
meta: {
|
|
110
|
-
/** Latest eventstamp observed by this document for clock synchronization */
|
|
111
|
-
latest: string;
|
|
112
|
-
};
|
|
113
|
-
/** Array of resource objects with eventstamps and metadata */
|
|
114
|
-
data: ResourceObject<T>[];
|
|
115
|
-
};
|
|
116
|
-
/**
|
|
117
|
-
* Change tracking information returned by mergeDocuments.
|
|
118
|
-
* Categorizes resources by mutation type for hook notifications.
|
|
119
|
-
*/
|
|
120
|
-
type DocumentChanges<T extends AnyObject> = {
|
|
121
|
-
/** Resources that were newly added (didn't exist before or were previously deleted) */
|
|
122
|
-
added: Map<string, ResourceObject<T>>;
|
|
123
|
-
/** Resources that were modified (existed before and changed) */
|
|
124
|
-
updated: Map<string, ResourceObject<T>>;
|
|
125
|
-
/** Resources that were deleted (newly marked with deletedAt) */
|
|
126
|
-
deleted: Set<string>;
|
|
127
|
-
};
|
|
128
|
-
/**
|
|
129
|
-
* Result of merging two JSON:API documents.
|
|
130
|
-
*/
|
|
131
|
-
type MergeDocumentsResult<T extends AnyObject> = {
|
|
132
|
-
/** The merged document with updated resources and forwarded clock */
|
|
133
|
-
document: JsonDocument<T>;
|
|
134
|
-
/** Change tracking for plugin hook notifications */
|
|
135
|
-
changes: DocumentChanges<T>;
|
|
136
|
-
};
|
|
137
|
-
/**
|
|
138
|
-
* Merges two JSON:API documents using field-level Last-Write-Wins semantics.
|
|
139
|
-
*
|
|
140
|
-
* The merge operation:
|
|
141
|
-
* 1. Forwards the clock to the newest eventstamp from either document
|
|
142
|
-
* 2. Merges each resource pair using field-level LWW (via mergeResources)
|
|
143
|
-
* 3. Tracks what changed for hook notifications (added/updated/deleted)
|
|
144
|
-
*
|
|
145
|
-
* Deletion is final: once a resource is deleted, updates to it are merged into
|
|
146
|
-
* the resource's attributes but don't restore visibility. Only new resources or
|
|
147
|
-
* transitions into the deleted state are tracked.
|
|
148
|
-
*
|
|
149
|
-
* @param into - The base document to merge into
|
|
150
|
-
* @param from - The source document to merge from
|
|
151
|
-
* @returns Merged document and categorized changes
|
|
152
|
-
*
|
|
153
|
-
* @example
|
|
154
|
-
* ```typescript
|
|
155
|
-
* const into = {
|
|
156
|
-
* jsonapi: { version: "1.1" },
|
|
157
|
-
* meta: { latest: "2025-01-01T00:00:00.000Z|0001|a1b2" },
|
|
158
|
-
* data: [{ type: "items", id: "doc1", attributes: {...}, meta: { deletedAt: null, latest: "..." } }]
|
|
159
|
-
* };
|
|
160
|
-
*
|
|
161
|
-
* const from = {
|
|
162
|
-
* jsonapi: { version: "1.1" },
|
|
163
|
-
* meta: { latest: "2025-01-01T00:05:00.000Z|0001|c3d4" },
|
|
164
|
-
* data: [
|
|
165
|
-
* { type: "items", id: "doc1", attributes: {...}, meta: { deletedAt: null, latest: "..." } }, // updated
|
|
166
|
-
* { type: "items", id: "doc2", attributes: {...}, meta: { deletedAt: null, latest: "..." } } // new
|
|
167
|
-
* ]
|
|
168
|
-
* };
|
|
169
|
-
*
|
|
170
|
-
* const result = mergeDocuments(into, from);
|
|
171
|
-
* // result.document.meta.latest === "2025-01-01T00:05:00.000Z|0001|c3d4"
|
|
172
|
-
* // result.changes.added has "doc2"
|
|
173
|
-
* // result.changes.updated has "doc1"
|
|
174
|
-
* ```
|
|
175
|
-
*/
|
|
176
|
-
declare function mergeDocuments<T extends AnyObject>(into: JsonDocument<T>, from: JsonDocument<T>): MergeDocumentsResult<T>;
|
|
177
|
-
/**
|
|
178
|
-
* Creates an empty JSON:API document with the given eventstamp.
|
|
179
|
-
* Useful for initializing new stores or testing.
|
|
180
|
-
*
|
|
181
|
-
* @param eventstamp - Initial clock value for this document
|
|
182
|
-
* @returns Empty document
|
|
183
|
-
*
|
|
184
|
-
* @example
|
|
185
|
-
* ```typescript
|
|
186
|
-
* const empty = makeDocument("2025-01-01T00:00:00.000Z|0000|0000");
|
|
187
|
-
* ```
|
|
188
|
-
*/
|
|
189
|
-
declare function makeDocument<T extends AnyObject>(eventstamp: string): JsonDocument<T>;
|
|
190
|
-
//#endregion
|
|
191
|
-
//#region src/document/utils.d.ts
|
|
192
|
-
/**
|
|
193
|
-
* Convert a JsonDocument's data array into a Map keyed by resource ID.
|
|
194
|
-
* @param document - JsonDocument containing resource data
|
|
195
|
-
* @returns Map of resource ID to ResourceObject
|
|
196
|
-
*/
|
|
197
|
-
declare function documentToMap<T extends AnyObject>(document: JsonDocument<T>): Map<string, ResourceObject<T>>;
|
|
198
|
-
/**
|
|
199
|
-
* Convert a Map of resources into a JsonDocument.
|
|
200
|
-
* @param resources - Map of resource ID to ResourceObject
|
|
201
|
-
* @param fallbackEventstamp - Eventstamp to include when computing the max (optional)
|
|
202
|
-
* @returns JsonDocument representation of the resources
|
|
203
|
-
*/
|
|
204
|
-
declare function mapToDocument<T extends AnyObject>(resources: Map<string, ResourceObject<T>>, fallbackEventstamp?: string): JsonDocument<T>;
|
|
205
|
-
//#endregion
|
|
206
|
-
//#region src/resource-map/resource-map.d.ts
|
|
207
|
-
/**
|
|
208
|
-
* A ResourceMap container for storing and managing ResourceObjects.
|
|
209
|
-
*
|
|
210
|
-
* This factory function creates a ResourceMap with state-based replication
|
|
211
|
-
* and automatic convergence via Last-Write-Wins conflict resolution.
|
|
212
|
-
* It stores complete resource snapshots with encoded metadata, including deletion markers.
|
|
213
|
-
*
|
|
214
|
-
* ResourceMap does NOT filter based on deletion status—it stores and returns
|
|
215
|
-
* all ResourceObjects including deleted ones. The Store class is responsible
|
|
216
|
-
* for filtering what's visible to users.
|
|
217
|
-
*
|
|
218
|
-
* @example
|
|
219
|
-
* ```typescript
|
|
220
|
-
* const resourceMap = createMap("todos");
|
|
221
|
-
* resourceMap.set("id1", { name: "Alice" });
|
|
222
|
-
* const resource = resourceMap.get("id1"); // ResourceObject with metadata
|
|
223
|
-
* ```
|
|
224
|
-
*/
|
|
225
|
-
declare function createMap<T extends AnyObject>(resourceType: string, initialMap?: Map<string, ResourceObject<T>>, eventstamp?: string): {
|
|
226
|
-
/**
|
|
227
|
-
* Check if a resource exists by ID (regardless of deletion status).
|
|
228
|
-
* @param id - Resource ID
|
|
229
|
-
*/
|
|
230
|
-
has(id: string): boolean;
|
|
231
|
-
/**
|
|
232
|
-
* Get a resource by ID (regardless of deletion status).
|
|
233
|
-
* @returns The raw resource with metadata (including deletedAt flag), or undefined if not found
|
|
234
|
-
*/
|
|
235
|
-
get(id: string): ResourceObject<T> | undefined;
|
|
236
|
-
/**
|
|
237
|
-
* Iterate over all resources (including deleted) as [id, resource] tuples.
|
|
238
|
-
*/
|
|
239
|
-
entries(): IterableIterator<readonly [string, ResourceObject<T>]>;
|
|
240
|
-
/**
|
|
241
|
-
* Set a resource using field-level Last-Write-Wins merge.
|
|
242
|
-
* Creates a new resource if it doesn't exist, or merges with existing resource.
|
|
243
|
-
* @param id - Resource ID (provided by caller, not generated)
|
|
244
|
-
* @param object - Data to set (partial fields are merged, full objects replace)
|
|
245
|
-
*/
|
|
246
|
-
set(id: string, object: Partial<T>): void;
|
|
247
|
-
delete(id: string): void;
|
|
248
|
-
/**
|
|
249
|
-
* Clone the internal map of encoded resources.
|
|
250
|
-
*/
|
|
251
|
-
cloneMap(): Map<string, ResourceObject<T>>;
|
|
252
|
-
/**
|
|
253
|
-
* Export the current state as a JsonDocument snapshot.
|
|
254
|
-
*/
|
|
255
|
-
toDocument(): JsonDocument<T>;
|
|
256
|
-
/**
|
|
257
|
-
* Merge another document into this ResourceMap using field-level Last-Write-Wins.
|
|
258
|
-
* @returns The merge result containing the merged document and tracked changes
|
|
259
|
-
* @param document - JsonDocument from another replica or storage
|
|
260
|
-
*/
|
|
261
|
-
merge(document: JsonDocument<T>): MergeDocumentsResult<T>;
|
|
262
|
-
};
|
|
263
|
-
/**
|
|
264
|
-
* Create a ResourceMap from a JsonDocument snapshot.
|
|
265
|
-
* @param type - Resource type identifier (defaults to "default")
|
|
266
|
-
* @param document - JsonDocument containing resource data
|
|
267
|
-
*/
|
|
268
|
-
declare function createMapFromDocument<U extends AnyObject>(type: string, document: JsonDocument<U>): ReturnType<typeof createMap<U>>;
|
|
269
|
-
//#endregion
|
|
270
|
-
export { AnyObject, Clock, DocumentChanges, InvalidEventstampError, JsonDocument, MIN_EVENTSTAMP, MergeDocumentsResult, ResourceObject, createClock, createClockFromEventstamp, createMap, createMapFromDocument, deleteResource, documentToMap, isValidEventstamp, makeDocument, makeResource, mapToDocument, maxEventstamp, mergeDocuments, mergeResources };
|
|
1
|
+
import { a as AnyObject, s as JsonDocument } from "./index-D7bXWDg6.js";
|
|
2
|
+
import { a as createDatabase, c as QueryCollectionHandle, d as Collection, f as CollectionInternals, g as StandardSchemaV1, i as DbConfig, l as QueryContext, m as IdNotFoundError, n as Database, o as TransactionCollectionHandle, p as DuplicateIdError, r as DatabasePlugin, s as TransactionContext, t as CollectionConfig, u as QueryHandle } from "./db-qQgPYE41.js";
|
|
3
|
+
export { type AnyObject, type Collection, type CollectionConfig, CollectionInternals, type Database, type DatabasePlugin, type DbConfig, DuplicateIdError, IdNotFoundError, type JsonDocument, type QueryCollectionHandle, type QueryContext, type QueryHandle, type StandardSchemaV1, type TransactionCollectionHandle, type TransactionContext, createDatabase };
|