@spooky-sync/core 0.0.0-canary.1
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 +21 -0
- package/dist/index.d.ts +590 -0
- package/dist/index.js +3082 -0
- package/package.json +46 -0
- package/src/events/events.test.ts +242 -0
- package/src/events/index.ts +261 -0
- package/src/index.ts +3 -0
- package/src/modules/auth/events/index.ts +18 -0
- package/src/modules/auth/index.ts +267 -0
- package/src/modules/cache/index.ts +241 -0
- package/src/modules/cache/types.ts +19 -0
- package/src/modules/data/data.test.ts +58 -0
- package/src/modules/data/index.ts +777 -0
- package/src/modules/devtools/index.ts +364 -0
- package/src/modules/sync/engine.ts +163 -0
- package/src/modules/sync/events/index.ts +77 -0
- package/src/modules/sync/index.ts +3 -0
- package/src/modules/sync/queue/index.ts +2 -0
- package/src/modules/sync/queue/queue-down.ts +89 -0
- package/src/modules/sync/queue/queue-up.ts +223 -0
- package/src/modules/sync/scheduler.ts +84 -0
- package/src/modules/sync/sync.ts +407 -0
- package/src/modules/sync/utils.test.ts +311 -0
- package/src/modules/sync/utils.ts +171 -0
- package/src/services/database/database.ts +108 -0
- package/src/services/database/events/index.ts +32 -0
- package/src/services/database/index.ts +5 -0
- package/src/services/database/local-migrator.ts +203 -0
- package/src/services/database/local.ts +99 -0
- package/src/services/database/remote.ts +110 -0
- package/src/services/logger/index.ts +118 -0
- package/src/services/persistence/localstorage.ts +26 -0
- package/src/services/persistence/surrealdb.ts +62 -0
- package/src/services/stream-processor/index.ts +364 -0
- package/src/services/stream-processor/stream-processor.test.ts +140 -0
- package/src/services/stream-processor/wasm-types.ts +31 -0
- package/src/spooky.ts +346 -0
- package/src/types.ts +237 -0
- package/src/utils/error-classification.ts +28 -0
- package/src/utils/index.ts +172 -0
- package/src/utils/parser.test.ts +125 -0
- package/src/utils/parser.ts +46 -0
- package/src/utils/surql.ts +182 -0
- package/src/utils/utils.test.ts +152 -0
- package/src/utils/withRetry.test.ts +153 -0
- package/tsconfig.json +14 -0
- package/tsdown.config.ts +9 -0
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
## Register Query Flow
|
|
2
|
+
|
|
3
|
+
1. Query registering (local) - Data Module
|
|
4
|
+
2. Query get local data - Data Module
|
|
5
|
+
3. (Query registing (remote) - Sync Module)
|
|
6
|
+
|
|
7
|
+
## Query Local Updates
|
|
8
|
+
|
|
9
|
+
1. Local Mutation is executed - Data Module
|
|
10
|
+
2. Mutation is enqueued for remote - Sync Moduke
|
|
11
|
+
3. New Record is ingested to DBSP - Data Module
|
|
12
|
+
4. Related Queries are updated - Data Module
|
|
13
|
+
5. (Sync Module syncs the mutation to remote - Sync Module)
|
|
14
|
+
|
|
15
|
+
## Query Live Updates
|
|
16
|
+
|
|
17
|
+
1. Live Update is received - Sync Module
|
|
18
|
+
2. Version is compared with local version - Sync Module
|
|
19
|
+
3. If remote version is higher, the record is updated - Sync Module
|
|
20
|
+
4. Record is ingested to DBSP Module - Data Module
|
|
21
|
+
5. Related Queries are updated - Data Module
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
import * as surrealdb0 from "surrealdb";
|
|
2
|
+
import { Duration, RecordId, Surreal, SurrealTransaction } from "surrealdb";
|
|
3
|
+
import { AccessDefinition, BackendNames, BackendRoutes, ColumnSchema, GetTable, QueryBuilder, QueryOptions, RecordId as RecordId$1, RoutePayload, SchemaStructure, TableModel, TableNames, TypeNameToTypeMap } from "@spooky-sync/query-builder";
|
|
4
|
+
import { Level, Level as Level$1, Logger } from "pino";
|
|
5
|
+
|
|
6
|
+
//#region src/events/index.d.ts
|
|
7
|
+
/**
|
|
8
|
+
* Utility type to define the payload structure of an event.
|
|
9
|
+
* If the payload type P is never, it defines payload as undefined.
|
|
10
|
+
*/
|
|
11
|
+
type EventPayloadDefinition<P> = [P] extends [never] ? {
|
|
12
|
+
payload: undefined;
|
|
13
|
+
} : {
|
|
14
|
+
payload: P;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Defines the structure of an event with a specific type and payload.
|
|
18
|
+
* @template T The string literal type of the event.
|
|
19
|
+
* @template P The type of the event payload.
|
|
20
|
+
*/
|
|
21
|
+
type EventDefinition<T extends string, P> = {
|
|
22
|
+
type: T;
|
|
23
|
+
} & EventPayloadDefinition<P>;
|
|
24
|
+
/**
|
|
25
|
+
* A map of event types to their definitions.
|
|
26
|
+
* Keys are event names, values are EventDefinitions.
|
|
27
|
+
*/
|
|
28
|
+
type EventTypeMap = Record<string, EventDefinition<any, unknown> | EventDefinition<any, never>>;
|
|
29
|
+
/**
|
|
30
|
+
* Options for pushing/emitting events.
|
|
31
|
+
*/
|
|
32
|
+
interface PushEventOptions {
|
|
33
|
+
/** Configuration for debouncing the event. */
|
|
34
|
+
debounced?: {
|
|
35
|
+
key: string;
|
|
36
|
+
delay: number;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Extracts the full Event object type from the map for a given key.
|
|
41
|
+
*/
|
|
42
|
+
type Event<E extends EventTypeMap, T extends EventType<E>> = E[T];
|
|
43
|
+
/**
|
|
44
|
+
* Extracts the payload type from the map for a given key.
|
|
45
|
+
*/
|
|
46
|
+
type EventPayload<E extends EventTypeMap, T extends EventType<E>> = E[T]['payload'];
|
|
47
|
+
/**
|
|
48
|
+
* Array of available event type keys.
|
|
49
|
+
*/
|
|
50
|
+
type EventTypes<E extends EventTypeMap> = (keyof E)[];
|
|
51
|
+
/**
|
|
52
|
+
* Represents a valid key (event name) from the EventTypeMap.
|
|
53
|
+
*/
|
|
54
|
+
type EventType<E extends EventTypeMap> = keyof E;
|
|
55
|
+
/**
|
|
56
|
+
* Function signature for an event handler.
|
|
57
|
+
*/
|
|
58
|
+
type EventHandler<E extends EventTypeMap, T extends EventType<E>> = (event: Event<E, T>) => void;
|
|
59
|
+
/**
|
|
60
|
+
* Options when subscribing to an event.
|
|
61
|
+
*/
|
|
62
|
+
type EventSubscriptionOptions$1 = {
|
|
63
|
+
/** If true, the handler will be called immediately with the last emitted event of this type (if any). */
|
|
64
|
+
immediately?: boolean;
|
|
65
|
+
/** If true, the subscription will be automatically removed after the first event is handled. */
|
|
66
|
+
once?: boolean;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* A type-safe event system that handles subscription, emission (including debouncing), and buffering of events.
|
|
70
|
+
* @template E The EventTypeMap defining all supported events.
|
|
71
|
+
*/
|
|
72
|
+
declare class EventSystem<E extends EventTypeMap> {
|
|
73
|
+
private _eventTypes;
|
|
74
|
+
private subscriberId;
|
|
75
|
+
private isProcessing;
|
|
76
|
+
private buffer;
|
|
77
|
+
private subscribers;
|
|
78
|
+
private subscribersTypeMap;
|
|
79
|
+
private lastEvents;
|
|
80
|
+
private debouncedEvents;
|
|
81
|
+
constructor(_eventTypes: EventTypes<E>);
|
|
82
|
+
get eventTypes(): EventTypes<E>;
|
|
83
|
+
/**
|
|
84
|
+
* Subscribes a handler to a specific event type.
|
|
85
|
+
* @param type The event type to subscribe to.
|
|
86
|
+
* @param handler The function to call when the event occurs.
|
|
87
|
+
* @param options Subscription options (once, immediately).
|
|
88
|
+
* @returns A subscription ID that can be used to unsubscribe.
|
|
89
|
+
*/
|
|
90
|
+
subscribe<T extends EventType<E>>(type: T, handler: EventHandler<E, T>, options?: EventSubscriptionOptions$1): number;
|
|
91
|
+
/**
|
|
92
|
+
* Subscribes a handler to multiple event types.
|
|
93
|
+
* @param types An array of event types to subscribe to.
|
|
94
|
+
* @param handler The function to call when any of the events occur.
|
|
95
|
+
* @param options Subscription options.
|
|
96
|
+
* @returns An array of subscription IDs.
|
|
97
|
+
*/
|
|
98
|
+
subscribeMany<T extends EventType<E>>(types: T[], handler: EventHandler<E, T>, options?: EventSubscriptionOptions$1): number[];
|
|
99
|
+
/**
|
|
100
|
+
* Unsubscribes a specific subscription by ID.
|
|
101
|
+
* @param id The subscription ID returned by subscribe().
|
|
102
|
+
* @returns True if the subscription was found and removed, false otherwise.
|
|
103
|
+
*/
|
|
104
|
+
unsubscribe(id: number): boolean;
|
|
105
|
+
/**
|
|
106
|
+
* Emits an event with the given type and payload.
|
|
107
|
+
* @param type The type of event to emit.
|
|
108
|
+
* @param payload The data associated with the event.
|
|
109
|
+
*/
|
|
110
|
+
emit<T extends EventType<E>, P extends EventPayload<E, T>>(type: T, payload: P): void;
|
|
111
|
+
/**
|
|
112
|
+
* Adds a fully constructed event object to the system.
|
|
113
|
+
* Similar to emit, but takes the full event object directly.
|
|
114
|
+
* Supports debouncing if options are provided.
|
|
115
|
+
* @param event The event object.
|
|
116
|
+
* @param options Options for the event push (e.g., debouncing).
|
|
117
|
+
*/
|
|
118
|
+
addEvent<T extends EventType<E>>(event: Event<E, T>, options?: PushEventOptions): void;
|
|
119
|
+
private handleDebouncedEvent;
|
|
120
|
+
private scheduleProcessing;
|
|
121
|
+
private processEvents;
|
|
122
|
+
private dequeue;
|
|
123
|
+
private setLastEvent;
|
|
124
|
+
private broadcastEvent;
|
|
125
|
+
}
|
|
126
|
+
//#endregion
|
|
127
|
+
//#region src/services/logger/index.d.ts
|
|
128
|
+
type Logger$1 = Logger;
|
|
129
|
+
//#endregion
|
|
130
|
+
//#region src/services/database/events/index.d.ts
|
|
131
|
+
declare const DatabaseEventTypes: {
|
|
132
|
+
readonly LocalQuery: "DATABASE_LOCAL_QUERY";
|
|
133
|
+
readonly RemoteQuery: "DATABASE_REMOTE_QUERY";
|
|
134
|
+
};
|
|
135
|
+
interface DatabaseQueryEventPayload {
|
|
136
|
+
query: string;
|
|
137
|
+
vars?: Record<string, unknown>;
|
|
138
|
+
duration: number;
|
|
139
|
+
success: boolean;
|
|
140
|
+
error?: string;
|
|
141
|
+
timestamp: number;
|
|
142
|
+
}
|
|
143
|
+
type DatabaseEventTypeMap = {
|
|
144
|
+
[DatabaseEventTypes.LocalQuery]: EventDefinition<typeof DatabaseEventTypes.LocalQuery, DatabaseQueryEventPayload>;
|
|
145
|
+
[DatabaseEventTypes.RemoteQuery]: EventDefinition<typeof DatabaseEventTypes.RemoteQuery, DatabaseQueryEventPayload>;
|
|
146
|
+
};
|
|
147
|
+
type DatabaseEventSystem = EventSystem<DatabaseEventTypeMap>;
|
|
148
|
+
//#endregion
|
|
149
|
+
//#region src/utils/surql.d.ts
|
|
150
|
+
interface SealedQuery<T = void> {
|
|
151
|
+
readonly sql: string;
|
|
152
|
+
readonly extract: (results: unknown[]) => T;
|
|
153
|
+
}
|
|
154
|
+
//#endregion
|
|
155
|
+
//#region src/services/database/database.d.ts
|
|
156
|
+
declare abstract class AbstractDatabaseService {
|
|
157
|
+
protected client: Surreal;
|
|
158
|
+
protected logger: Logger$1;
|
|
159
|
+
protected events: DatabaseEventSystem;
|
|
160
|
+
protected abstract eventType: typeof DatabaseEventTypes.LocalQuery | typeof DatabaseEventTypes.RemoteQuery;
|
|
161
|
+
constructor(client: Surreal, logger: Logger$1, events: DatabaseEventSystem);
|
|
162
|
+
abstract connect(): Promise<void>;
|
|
163
|
+
getClient(): Surreal;
|
|
164
|
+
getEvents(): DatabaseEventSystem;
|
|
165
|
+
tx(): Promise<SurrealTransaction>;
|
|
166
|
+
private queryQueue;
|
|
167
|
+
/**
|
|
168
|
+
* Execute a query with serialized execution to prevent WASM transaction issues.
|
|
169
|
+
*/
|
|
170
|
+
query<T extends unknown[]>(query: string, vars?: Record<string, unknown>): Promise<T>;
|
|
171
|
+
execute<T>(query: SealedQuery<T>, vars?: Record<string, unknown>): Promise<T>;
|
|
172
|
+
close(): Promise<void>;
|
|
173
|
+
}
|
|
174
|
+
//#endregion
|
|
175
|
+
//#region src/services/database/local.d.ts
|
|
176
|
+
declare class LocalDatabaseService extends AbstractDatabaseService {
|
|
177
|
+
private config;
|
|
178
|
+
protected eventType: "DATABASE_LOCAL_QUERY";
|
|
179
|
+
constructor(config: SpookyConfig<any>['database'], logger: Logger$1);
|
|
180
|
+
getConfig(): SpookyConfig<any>['database'];
|
|
181
|
+
connect(): Promise<void>;
|
|
182
|
+
}
|
|
183
|
+
//#endregion
|
|
184
|
+
//#region src/services/database/remote.d.ts
|
|
185
|
+
declare class RemoteDatabaseService extends AbstractDatabaseService {
|
|
186
|
+
private config;
|
|
187
|
+
protected eventType: "DATABASE_REMOTE_QUERY";
|
|
188
|
+
constructor(config: SpookyConfig<any>['database'], logger: Logger$1);
|
|
189
|
+
getConfig(): SpookyConfig<any>['database'];
|
|
190
|
+
connect(): Promise<void>;
|
|
191
|
+
signin(params: any): Promise<any>;
|
|
192
|
+
signup(params: any): Promise<any>;
|
|
193
|
+
authenticate(token: string): Promise<any>;
|
|
194
|
+
invalidate(): Promise<void>;
|
|
195
|
+
}
|
|
196
|
+
//#endregion
|
|
197
|
+
//#region src/modules/sync/queue/queue-up.d.ts
|
|
198
|
+
type CreateEvent = {
|
|
199
|
+
type: 'create';
|
|
200
|
+
mutation_id: RecordId;
|
|
201
|
+
record_id: RecordId;
|
|
202
|
+
data: Record<string, unknown>;
|
|
203
|
+
record?: Record<string, unknown>;
|
|
204
|
+
tableName?: string;
|
|
205
|
+
options?: PushEventOptions;
|
|
206
|
+
};
|
|
207
|
+
type UpdateEvent = {
|
|
208
|
+
type: 'update';
|
|
209
|
+
mutation_id: RecordId;
|
|
210
|
+
record_id: RecordId;
|
|
211
|
+
data: Record<string, unknown>;
|
|
212
|
+
record?: Record<string, unknown>;
|
|
213
|
+
beforeRecord?: Record<string, unknown>;
|
|
214
|
+
options?: PushEventOptions;
|
|
215
|
+
};
|
|
216
|
+
type DeleteEvent = {
|
|
217
|
+
type: 'delete';
|
|
218
|
+
mutation_id: RecordId;
|
|
219
|
+
record_id: RecordId;
|
|
220
|
+
options?: PushEventOptions;
|
|
221
|
+
};
|
|
222
|
+
type UpEvent = CreateEvent | UpdateEvent | DeleteEvent;
|
|
223
|
+
//#endregion
|
|
224
|
+
//#region src/services/stream-processor/wasm-types.d.ts
|
|
225
|
+
interface WasmStreamUpdate {
|
|
226
|
+
query_id: string;
|
|
227
|
+
result_hash: string;
|
|
228
|
+
result_data: RecordVersionArray;
|
|
229
|
+
}
|
|
230
|
+
//#endregion
|
|
231
|
+
//#region src/services/stream-processor/index.d.ts
|
|
232
|
+
interface QueryPlanConfig {
|
|
233
|
+
queryHash: string;
|
|
234
|
+
surql: string;
|
|
235
|
+
params: Record<string, any>;
|
|
236
|
+
ttl: QueryTimeToLive | Duration;
|
|
237
|
+
lastActiveAt: Date;
|
|
238
|
+
localArray: RecordVersionArray;
|
|
239
|
+
remoteArray: RecordVersionArray;
|
|
240
|
+
meta: {
|
|
241
|
+
tableName: string;
|
|
242
|
+
involvedTables?: string[];
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
interface StreamUpdate {
|
|
246
|
+
queryHash: string;
|
|
247
|
+
localArray: RecordVersionArray;
|
|
248
|
+
op?: 'CREATE' | 'UPDATE' | 'DELETE';
|
|
249
|
+
}
|
|
250
|
+
type StreamProcessorEvents = {
|
|
251
|
+
stream_update: EventDefinition<'stream_update', StreamUpdate[]>;
|
|
252
|
+
};
|
|
253
|
+
/**
|
|
254
|
+
* Interface for receiving stream updates directly.
|
|
255
|
+
* Implemented by DataManager and DevToolsService for direct coupling.
|
|
256
|
+
*/
|
|
257
|
+
interface StreamUpdateReceiver {
|
|
258
|
+
onStreamUpdate(update: StreamUpdate): void;
|
|
259
|
+
}
|
|
260
|
+
declare class StreamProcessorService {
|
|
261
|
+
events: EventSystem<StreamProcessorEvents>;
|
|
262
|
+
private db;
|
|
263
|
+
private persistenceClient;
|
|
264
|
+
private logger;
|
|
265
|
+
private processor;
|
|
266
|
+
private isInitialized;
|
|
267
|
+
private receivers;
|
|
268
|
+
constructor(events: EventSystem<StreamProcessorEvents>, db: LocalDatabaseService, persistenceClient: PersistenceClient, logger: Logger);
|
|
269
|
+
/**
|
|
270
|
+
* Add a receiver for stream updates.
|
|
271
|
+
* Multiple receivers can be registered (DataManager, DevTools, etc.)
|
|
272
|
+
*/
|
|
273
|
+
addReceiver(receiver: StreamUpdateReceiver): void;
|
|
274
|
+
private notifyUpdates;
|
|
275
|
+
/**
|
|
276
|
+
* Initialize the WASM module and processor.
|
|
277
|
+
* This must be called before using other methods.
|
|
278
|
+
*/
|
|
279
|
+
init(): Promise<void>;
|
|
280
|
+
loadState(): Promise<void>;
|
|
281
|
+
saveState(): Promise<void>;
|
|
282
|
+
/**
|
|
283
|
+
* Ingest a record change into the processor.
|
|
284
|
+
* Emits 'stream_update' event if materialized views are affected.
|
|
285
|
+
* @param isOptimistic true = local mutation (increment versions), false = remote sync (keep versions)
|
|
286
|
+
*/
|
|
287
|
+
ingest(table: string, op: 'CREATE' | 'UPDATE' | 'DELETE', id: string, record: any): WasmStreamUpdate[];
|
|
288
|
+
/**
|
|
289
|
+
* Register a new query plan.
|
|
290
|
+
* Emits 'stream_update' with the initial result.
|
|
291
|
+
*/
|
|
292
|
+
registerQueryPlan(queryPlan: QueryPlanConfig): StreamUpdate | undefined;
|
|
293
|
+
/**
|
|
294
|
+
* Unregister a query plan by ID.
|
|
295
|
+
*/
|
|
296
|
+
unregisterQueryPlan(queryHash: string): void;
|
|
297
|
+
private normalizeValue;
|
|
298
|
+
}
|
|
299
|
+
//#endregion
|
|
300
|
+
//#region src/types.d.ts
|
|
301
|
+
/**
|
|
302
|
+
* The type of storage backend to use for the local database.
|
|
303
|
+
* - 'memory': In-memory storage (transient).
|
|
304
|
+
* - 'indexeddb': IndexedDB storage (persistent).
|
|
305
|
+
*/
|
|
306
|
+
type StoreType = 'memory' | 'indexeddb';
|
|
307
|
+
/**
|
|
308
|
+
* Interface for a custom persistence client.
|
|
309
|
+
* Allows providing a custom storage mechanism for the local database.
|
|
310
|
+
*/
|
|
311
|
+
interface PersistenceClient {
|
|
312
|
+
/**
|
|
313
|
+
* Sets a value in the storage.
|
|
314
|
+
* @param key The key to set.
|
|
315
|
+
* @param value The value to store.
|
|
316
|
+
*/
|
|
317
|
+
set<T>(key: string, value: T): Promise<void>;
|
|
318
|
+
/**
|
|
319
|
+
* Gets a value from the storage.
|
|
320
|
+
* @param key The key to retrieve.
|
|
321
|
+
* @returns The stored value or null if not found.
|
|
322
|
+
*/
|
|
323
|
+
get<T>(key: string): Promise<T | null>;
|
|
324
|
+
/**
|
|
325
|
+
* Removes a value from the storage.
|
|
326
|
+
* @param key The key to remove.
|
|
327
|
+
*/
|
|
328
|
+
remove(key: string): Promise<void>;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Supported Time-To-Live (TTL) values for cached queries.
|
|
332
|
+
* Format: number + unit (m=minutes, h=hours, d=days).
|
|
333
|
+
*/
|
|
334
|
+
type QueryTimeToLive = '1m' | '5m' | '10m' | '15m' | '20m' | '25m' | '30m' | '1h' | '2h' | '3h' | '4h' | '5h' | '6h' | '7h' | '8h' | '9h' | '10h' | '11h' | '12h' | '1d';
|
|
335
|
+
/**
|
|
336
|
+
* Result object returned when a query is registered or executed.
|
|
337
|
+
*/
|
|
338
|
+
interface SpookyQueryResult {
|
|
339
|
+
/** The unique hash identifier for the query. */
|
|
340
|
+
hash: string;
|
|
341
|
+
}
|
|
342
|
+
type SpookyQueryResultPromise = Promise<SpookyQueryResult>;
|
|
343
|
+
interface EventSubscriptionOptions {
|
|
344
|
+
priority?: number;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Configuration options for the Spooky client.
|
|
348
|
+
* @template S The schema structure type.
|
|
349
|
+
*/
|
|
350
|
+
interface SpookyConfig<S extends SchemaStructure> {
|
|
351
|
+
/** Database connection configuration. */
|
|
352
|
+
database: {
|
|
353
|
+
/** The SurrealDB endpoint URL. */
|
|
354
|
+
endpoint?: string;
|
|
355
|
+
/** The namespace to use. */
|
|
356
|
+
namespace: string;
|
|
357
|
+
/** The database name. */
|
|
358
|
+
database: string;
|
|
359
|
+
/** The local store type implementation. */
|
|
360
|
+
store?: StoreType;
|
|
361
|
+
/** Authentication token. */
|
|
362
|
+
token?: string;
|
|
363
|
+
};
|
|
364
|
+
/** Unique client identifier. If not provided, one will be generated. */
|
|
365
|
+
clientId?: string;
|
|
366
|
+
/** The schema definition. */
|
|
367
|
+
schema: S;
|
|
368
|
+
/** The compiled SURQL schema string. */
|
|
369
|
+
schemaSurql: string;
|
|
370
|
+
/** Logging level. */
|
|
371
|
+
logLevel: Level$1;
|
|
372
|
+
/**
|
|
373
|
+
* Persistence client to use.
|
|
374
|
+
* Can be a custom implementation, 'surrealdb' (default), or 'localstorage'.
|
|
375
|
+
*/
|
|
376
|
+
persistenceClient?: PersistenceClient | 'surrealdb' | 'localstorage';
|
|
377
|
+
/** OpenTelemetry collector endpoint for telemetry data. */
|
|
378
|
+
otelEndpoint?: string;
|
|
379
|
+
/**
|
|
380
|
+
* Debounce time in milliseconds for stream updates.
|
|
381
|
+
* Defaults to 100ms.
|
|
382
|
+
*/
|
|
383
|
+
streamDebounceTime?: number;
|
|
384
|
+
}
|
|
385
|
+
type QueryHash = string;
|
|
386
|
+
type RecordVersionArray = Array<[string, number]>;
|
|
387
|
+
/**
|
|
388
|
+
* Represents the difference between two record version sets.
|
|
389
|
+
* Used for synchronizing local and remote states.
|
|
390
|
+
*/
|
|
391
|
+
interface RecordVersionDiff {
|
|
392
|
+
/** List of records added. */
|
|
393
|
+
added: Array<{
|
|
394
|
+
id: RecordId$1<string>;
|
|
395
|
+
version: number;
|
|
396
|
+
}>;
|
|
397
|
+
/** List of records updated. */
|
|
398
|
+
updated: Array<{
|
|
399
|
+
id: RecordId$1<string>;
|
|
400
|
+
version: number;
|
|
401
|
+
}>;
|
|
402
|
+
/** List of record IDs removed. */
|
|
403
|
+
removed: RecordId$1<string>[];
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Configuration for a specific query instance.
|
|
407
|
+
* Stores metadata about the query's state, parameters, and versioning.
|
|
408
|
+
*/
|
|
409
|
+
interface QueryConfig {
|
|
410
|
+
/** The unique ID of the query config record. */
|
|
411
|
+
id: RecordId$1<string>;
|
|
412
|
+
/** The SURQL query string. */
|
|
413
|
+
surql: string;
|
|
414
|
+
/** Parameters used in the query. */
|
|
415
|
+
params: Record<string, any>;
|
|
416
|
+
/** The version array representing the local state of results. */
|
|
417
|
+
localArray: RecordVersionArray;
|
|
418
|
+
/** The version array representing the remote (server) state of results. */
|
|
419
|
+
remoteArray: RecordVersionArray;
|
|
420
|
+
/** Time-To-Live for this query. */
|
|
421
|
+
ttl: QueryTimeToLive;
|
|
422
|
+
/** Timestamp when the query was last accessed/active. */
|
|
423
|
+
lastActiveAt: Date;
|
|
424
|
+
/** The name of the table this query targets (if applicable). */
|
|
425
|
+
tableName: string;
|
|
426
|
+
}
|
|
427
|
+
type QueryConfigRecord = QueryConfig & {
|
|
428
|
+
id: string;
|
|
429
|
+
};
|
|
430
|
+
/**
|
|
431
|
+
* Internal state of a live query.
|
|
432
|
+
*/
|
|
433
|
+
interface QueryState {
|
|
434
|
+
/** The configuration for this query. */
|
|
435
|
+
config: QueryConfig;
|
|
436
|
+
/** The current cached records for this query. */
|
|
437
|
+
records: Record<string, any>[];
|
|
438
|
+
/** Timer for TTL expiration. */
|
|
439
|
+
ttlTimer: NodeJS.Timeout | null;
|
|
440
|
+
/** TTL duration in milliseconds. */
|
|
441
|
+
ttlDurationMs: number;
|
|
442
|
+
/** Number of times the query has been updated. */
|
|
443
|
+
updateCount: number;
|
|
444
|
+
}
|
|
445
|
+
type QueryUpdateCallback = (records: Record<string, any>[]) => void;
|
|
446
|
+
type MutationCallback = (mutations: UpEvent[]) => void;
|
|
447
|
+
type MutationEventType = 'create' | 'update' | 'delete';
|
|
448
|
+
/**
|
|
449
|
+
* Represents a mutation event (create, update, delete) to be synchronized.
|
|
450
|
+
*/
|
|
451
|
+
interface MutationEvent {
|
|
452
|
+
/** Example: 'create', 'update', or 'delete'. */
|
|
453
|
+
type: MutationEventType;
|
|
454
|
+
/** unique id of the mutation */
|
|
455
|
+
mutation_id: RecordId$1<string>;
|
|
456
|
+
/** The ID of the record being mutated. */
|
|
457
|
+
record_id: RecordId$1<string>;
|
|
458
|
+
/** The data payload for create/update operations. */
|
|
459
|
+
data?: any;
|
|
460
|
+
/** The full record data (optional context). */
|
|
461
|
+
record?: any;
|
|
462
|
+
/** Options for the mutation event (e.g., debounce settings). */
|
|
463
|
+
options?: PushEventOptions;
|
|
464
|
+
/** Timestamp when the event was created. */
|
|
465
|
+
createdAt: Date;
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Options for run operations.
|
|
469
|
+
*/
|
|
470
|
+
interface RunOptions {
|
|
471
|
+
assignedTo?: string;
|
|
472
|
+
max_retries?: number;
|
|
473
|
+
retry_strategy?: 'linear' | 'exponential';
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Options for update operations.
|
|
477
|
+
*/
|
|
478
|
+
interface UpdateOptions {
|
|
479
|
+
/**
|
|
480
|
+
* Debounce configuration for the update.
|
|
481
|
+
* If boolean, enables default debounce behavior.
|
|
482
|
+
*/
|
|
483
|
+
debounced?: boolean | DebounceOptions;
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Configuration options for debouncing updates.
|
|
487
|
+
*/
|
|
488
|
+
interface DebounceOptions {
|
|
489
|
+
/**
|
|
490
|
+
* The key to use for debouncing.
|
|
491
|
+
* - 'recordId': Debounce based on the specific record ID. WARNING: IT WILL ONLY ACCEPT THE LATEST CHANGE AND DOES *NOT* MERGE THE PREVIOUS ONCES. IF YOU ARE UNSURE JUST USE 'recordId_x_fields'.
|
|
492
|
+
* - 'recordId_x_fields': Debounce based on record ID and specific fields.
|
|
493
|
+
*/
|
|
494
|
+
key?: 'recordId' | 'recordId_x_fields';
|
|
495
|
+
/** The debounce delay in milliseconds. */
|
|
496
|
+
delay?: number;
|
|
497
|
+
}
|
|
498
|
+
//#endregion
|
|
499
|
+
//#region src/modules/auth/events/index.d.ts
|
|
500
|
+
declare const AuthEventTypes: {
|
|
501
|
+
readonly AuthStateChanged: "AUTH_STATE_CHANGED";
|
|
502
|
+
};
|
|
503
|
+
type AuthEventTypeMap = {
|
|
504
|
+
[AuthEventTypes.AuthStateChanged]: EventDefinition<typeof AuthEventTypes.AuthStateChanged, string | null>;
|
|
505
|
+
};
|
|
506
|
+
type AuthEventSystem = EventSystem<AuthEventTypeMap>;
|
|
507
|
+
declare function createAuthEventSystem(): AuthEventSystem;
|
|
508
|
+
//#endregion
|
|
509
|
+
//#region src/modules/auth/index.d.ts
|
|
510
|
+
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
511
|
+
type MapColumnType<T extends ColumnSchema> = T['optional'] extends true ? TypeNameToTypeMap[T['type']] | undefined : TypeNameToTypeMap[T['type']];
|
|
512
|
+
type ExtractAccessParams<S extends SchemaStructure, Name extends keyof S['access'], Method extends 'signIn' | 'signup'> = S['access'] extends undefined ? never : S['access'][Name] extends AccessDefinition ? Prettify<{ [K in keyof S['access'][Name][Method]['params']]: MapColumnType<S['access'][Name][Method]['params'][K]> }> : never;
|
|
513
|
+
declare class AuthService<S extends SchemaStructure> {
|
|
514
|
+
private schema;
|
|
515
|
+
private remote;
|
|
516
|
+
private persistenceClient;
|
|
517
|
+
private logger;
|
|
518
|
+
token: string | null;
|
|
519
|
+
currentUser: any | null;
|
|
520
|
+
isAuthenticated: boolean;
|
|
521
|
+
isLoading: boolean;
|
|
522
|
+
private events;
|
|
523
|
+
get eventSystem(): AuthEventSystem;
|
|
524
|
+
constructor(schema: S, remote: RemoteDatabaseService, persistenceClient: PersistenceClient, logger: Logger$1);
|
|
525
|
+
init(): Promise<void>;
|
|
526
|
+
getAccessDefinition<Name extends keyof S['access']>(name: Name): AccessDefinition | undefined;
|
|
527
|
+
/**
|
|
528
|
+
* Subscribe to auth state changes.
|
|
529
|
+
* callback is called immediately with current value and whenever validation status changes.
|
|
530
|
+
*/
|
|
531
|
+
subscribe(cb: (userId: string | null) => void): () => void;
|
|
532
|
+
private notifyListeners;
|
|
533
|
+
/**
|
|
534
|
+
* Check for existing session and validate
|
|
535
|
+
*/
|
|
536
|
+
check(accessToken?: string): Promise<void>;
|
|
537
|
+
/**
|
|
538
|
+
* Sign out and clear session
|
|
539
|
+
*/
|
|
540
|
+
signOut(): Promise<void>;
|
|
541
|
+
private setSession;
|
|
542
|
+
signUp<Name extends keyof S['access'] & string>(accessName: Name, params: ExtractAccessParams<S, Name, 'signup'>): Promise<void>;
|
|
543
|
+
signIn<Name extends keyof S['access'] & string>(accessName: Name, params: ExtractAccessParams<S, Name, 'signIn'>): Promise<void>;
|
|
544
|
+
}
|
|
545
|
+
//#endregion
|
|
546
|
+
//#region src/spooky.d.ts
|
|
547
|
+
declare class SpookyClient<S extends SchemaStructure> {
|
|
548
|
+
private config;
|
|
549
|
+
private local;
|
|
550
|
+
private remote;
|
|
551
|
+
private persistenceClient;
|
|
552
|
+
private migrator;
|
|
553
|
+
private cache;
|
|
554
|
+
private dataModule;
|
|
555
|
+
private sync;
|
|
556
|
+
private devTools;
|
|
557
|
+
private logger;
|
|
558
|
+
auth: AuthService<S>;
|
|
559
|
+
streamProcessor: StreamProcessorService;
|
|
560
|
+
get remoteClient(): Surreal;
|
|
561
|
+
get localClient(): Surreal;
|
|
562
|
+
get pendingMutationCount(): number;
|
|
563
|
+
subscribeToPendingMutations(cb: (count: number) => void): () => void;
|
|
564
|
+
constructor(config: SpookyConfig<S>);
|
|
565
|
+
/**
|
|
566
|
+
* Setup direct callbacks instead of event subscriptions
|
|
567
|
+
*/
|
|
568
|
+
private setupCallbacks;
|
|
569
|
+
init(): Promise<void>;
|
|
570
|
+
close(): Promise<void>;
|
|
571
|
+
authenticate(token: string): Promise<surrealdb0.Tokens>;
|
|
572
|
+
deauthenticate(): Promise<void>;
|
|
573
|
+
query<Table extends TableNames<S>>(table: Table, options: QueryOptions<TableModel<GetTable<S, Table>>, false>, ttl?: QueryTimeToLive): QueryBuilder<S, Table, SpookyQueryResultPromise>;
|
|
574
|
+
private initQuery;
|
|
575
|
+
queryRaw(sql: string, params: Record<string, any>, ttl: QueryTimeToLive): Promise<string>;
|
|
576
|
+
subscribe(queryHash: string, callback: (records: Record<string, any>[]) => void, options?: {
|
|
577
|
+
immediate?: boolean;
|
|
578
|
+
}): Promise<() => void>;
|
|
579
|
+
run<B extends BackendNames<S>, R extends BackendRoutes<S, B>>(backend: B, path: R, payload: RoutePayload<S, B, R>, options?: RunOptions): Promise<void>;
|
|
580
|
+
create(id: string, data: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
581
|
+
update(table: string, id: string, data: Record<string, unknown>, options?: UpdateOptions): Promise<{
|
|
582
|
+
[x: string]: /*elided*/any;
|
|
583
|
+
}>;
|
|
584
|
+
delete(table: string, id: string): Promise<void>;
|
|
585
|
+
useRemote<T>(fn: (client: Surreal) => Promise<T> | T): Promise<T>;
|
|
586
|
+
private persistClientId;
|
|
587
|
+
private loadOrGenerateClientId;
|
|
588
|
+
}
|
|
589
|
+
//#endregion
|
|
590
|
+
export { AuthEventSystem, AuthEventTypeMap, AuthEventTypes, AuthService, DebounceOptions, EventSubscriptionOptions, type Level, MutationCallback, MutationEvent, MutationEventType, PersistenceClient, QueryConfig, QueryConfigRecord, QueryHash, QueryState, QueryTimeToLive, QueryUpdateCallback, RecordVersionArray, RecordVersionDiff, RunOptions, SpookyClient, SpookyConfig, SpookyQueryResult, SpookyQueryResultPromise, StoreType, UpdateOptions, createAuthEventSystem };
|