@affectively/dash 5.1.1 → 5.2.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.
@@ -101,4 +101,8 @@ export declare function onValue(ref: DatabaseReferenceImpl, callback: (snapshot:
101
101
  * Remove listeners from a reference
102
102
  */
103
103
  export declare function off(ref: DatabaseReferenceImpl, eventType?: string, callback?: Function): void;
104
+ export declare function onChildAdded(ref: DatabaseReferenceImpl, callback: (snapshot: DataSnapshot, previousChildName?: string | null) => void): () => void;
105
+ export declare function onChildChanged(ref: DatabaseReferenceImpl, callback: (snapshot: DataSnapshot, previousChildName?: string | null) => void): () => void;
106
+ export declare function onChildRemoved(ref: DatabaseReferenceImpl, callback: (snapshot: DataSnapshot) => void): () => void;
107
+ export declare function onChildMoved(ref: DatabaseReferenceImpl, callback: (snapshot: DataSnapshot, previousChildName?: string | null) => void): () => void;
104
108
  export type { DataSnapshot, DatabaseReference, OnDisconnect } from '../types.js';
@@ -349,3 +349,20 @@ export function off(ref, eventType, callback) {
349
349
  }
350
350
  }
351
351
  }
352
+ // Child event listeners (stubs for Firebase compatibility)
353
+ export function onChildAdded(ref, callback) {
354
+ // Mock implementation - return unsubscribe function
355
+ return () => { };
356
+ }
357
+ export function onChildChanged(ref, callback) {
358
+ // Mock implementation - return unsubscribe function
359
+ return () => { };
360
+ }
361
+ export function onChildRemoved(ref, callback) {
362
+ // Mock implementation - return unsubscribe function
363
+ return () => { };
364
+ }
365
+ export function onChildMoved(ref, callback) {
366
+ // Mock implementation - return unsubscribe function
367
+ return () => { };
368
+ }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Firestore document operations (read/write)
3
3
  */
4
- import type { DocumentSnapshot as IDocumentSnapshot, QuerySnapshot as IQuerySnapshot, DocumentChange, SnapshotOptions, Firestore, DocumentData, DocumentReference, Query, SnapshotMetadata } from '../types.js';
4
+ import type { DocumentSnapshot, QuerySnapshot, IDocumentSnapshot, IQuerySnapshot, DocumentChange, SnapshotOptions, Firestore, DocumentData, DocumentReference, Query, SnapshotMetadata } from '../types.js';
5
5
  export declare const mockListeners: Map<string, Set<Function>>;
6
6
  export declare function triggerMockListeners(key: string, snapshot: any): void;
7
7
  /**
@@ -237,12 +237,12 @@ export async function getDocsFromCache(q, cache) {
237
237
  */
238
238
  export async function setDoc(reference, data, options, engine) {
239
239
  if (data === null) {
240
- throw createFirebaseError('invalid-argument', 'Document data cannot be null.');
240
+ throw createFirebaseError('firestore/invalid-argument', 'Document data cannot be null.');
241
241
  }
242
242
  if (!engine) {
243
243
  // Mock success for tests - store data and trigger listeners
244
244
  if (!reference) {
245
- throw createFirebaseError('invalid-argument', 'Document reference cannot be null.');
245
+ throw createFirebaseError('firestore/invalid-argument', 'Document reference cannot be null.');
246
246
  }
247
247
  const ref = reference;
248
248
  mockStorage.set(ref.path, data);
@@ -300,7 +300,7 @@ export async function updateDoc(reference, data, engine) {
300
300
  if (!engine) {
301
301
  // Mock success for tests - update stored data and trigger listeners
302
302
  if (!reference) {
303
- throw createFirebaseError('invalid-argument', 'Document reference cannot be null.');
303
+ throw createFirebaseError('firestore/invalid-argument', 'Document reference cannot be null.');
304
304
  }
305
305
  const ref = reference;
306
306
  const existing = mockStorage.get(ref.path) || {};
@@ -3,7 +3,7 @@
3
3
  * Complete drop-in replacement for Firebase SDK
4
4
  */
5
5
  export * from './firestore/index.js';
6
- export * from './firestore/listeners.js';
6
+ export * from './firestore/index.js';
7
7
  export * from './database/index.js';
8
8
  export * from './auth/index.js';
9
9
  export * as storage from './storage/index.js';
@@ -5,7 +5,8 @@
5
5
  // Re-export all Firestore APIs
6
6
  export * from './firestore/index.js';
7
7
  // Re-export all Firestore listener APIs
8
- export * from './firestore/listeners.js';
8
+ // Re-export Firestore APIs
9
+ export * from './firestore/index.js';
9
10
  // Re-export Realtime Database APIs
10
11
  export * from './database/index.js';
11
12
  // Re-export all Authentication APIs
@@ -77,6 +77,7 @@ export interface QuerySnapshot<T = DocumentData> {
77
77
  empty: boolean;
78
78
  metadata: SnapshotMetadata;
79
79
  docChanges(options?: SnapshotListenOptions): DocumentChange<T>[];
80
+ forEach(callback: (doc: DocumentSnapshot<T>) => void): void;
80
81
  }
81
82
  export interface DocumentChange<T = DocumentData> {
82
83
  type: 'added' | 'removed' | 'modified';
@@ -1,12 +1,63 @@
1
1
  import { LensEngine } from '../schema/lens.js';
2
+ /**
3
+ * Cloud sync configuration - enables automatic D1/R2 sync
4
+ *
5
+ * Cloud sync is ON BY DEFAULT when:
6
+ * - Running in a Cloudflare environment (detected via env vars)
7
+ * - A sync endpoint is available
8
+ *
9
+ * It gracefully degrades if sync fails - local data still works.
10
+ */
11
+ export interface CloudConfig {
12
+ /** Base URL for the sync endpoint (e.g., 'https://api.example.com'). Auto-detected from env if not provided. */
13
+ baseUrl?: string;
14
+ /** Auth token getter - called before each sync */
15
+ getAuthToken?: () => Promise<string | null>;
16
+ /** Sync interval in ms (default: 30000 = 30s). Set to 0 to disable auto-sync. */
17
+ syncInterval?: number;
18
+ /** Tables to exclude from sync (internal tables are always excluded) */
19
+ excludeTables?: string[];
20
+ /** Callback when sync completes */
21
+ onSyncComplete?: (result: CloudSyncResult) => void;
22
+ /** Callback when sync fails */
23
+ onSyncError?: (error: Error) => void;
24
+ /** Explicitly disable cloud sync (default: false - sync is enabled if endpoint available) */
25
+ disabled?: boolean;
26
+ }
27
+ export interface CloudSyncResult {
28
+ pushed: number;
29
+ pulled: number;
30
+ errors: string[];
31
+ timestamp: number;
32
+ }
2
33
  export declare class DashEngine {
3
34
  private db;
4
35
  private readyPromise;
5
36
  private listeners;
6
37
  lens: LensEngine;
7
38
  currentSchemaVersion: number;
39
+ private cloudConfig;
40
+ private cloudSyncTimer;
41
+ private isCloudSyncing;
42
+ private lastCloudSyncTime;
43
+ private cloudSyncEnabled;
44
+ private syncedTables;
45
+ private readonly INTERNAL_TABLES;
8
46
  constructor();
9
47
  private init;
48
+ /**
49
+ * Try to auto-enable cloud sync from environment
50
+ * Cloud sync is ON BY DEFAULT when a sync endpoint is detected
51
+ */
52
+ private tryAutoEnableCloudSync;
53
+ /**
54
+ * Detect sync endpoint from environment variables
55
+ */
56
+ private detectSyncEndpoint;
57
+ /**
58
+ * Get default auth token from common auth patterns
59
+ */
60
+ private getDefaultAuthToken;
10
61
  private initializeSchema;
11
62
  ready(): Promise<void>;
12
63
  private tableListeners;
@@ -30,6 +81,84 @@ export declare class DashEngine {
30
81
  maxZ: number;
31
82
  }): Promise<any[]>;
32
83
  close(): void;
84
+ /**
85
+ * Enable cloud sync - changes automatically sync to D1/R2
86
+ * Just call this once with your config, and sync happens magically.
87
+ *
88
+ * Cloud sync is ON BY DEFAULT when running in a Cloudflare environment.
89
+ * Call this to customize the config or explicitly enable/disable.
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * await dash.ready();
94
+ * // Option 1: Use auto-detected endpoint (default)
95
+ * // Cloud sync is already enabled if DASH_SYNC_URL env var is set
96
+ *
97
+ * // Option 2: Explicit config
98
+ * dash.enableCloudSync({
99
+ * baseUrl: 'https://api.example.com',
100
+ * getAuthToken: async () => auth.token,
101
+ * });
102
+ *
103
+ * // Option 3: Disable cloud sync
104
+ * dash.enableCloudSync({ disabled: true });
105
+ * ```
106
+ */
107
+ enableCloudSync(config?: CloudConfig): void;
108
+ /**
109
+ * Initialize cloud sync schema (queue and metadata tables)
110
+ */
111
+ private initializeCloudSyncSchema;
112
+ /**
113
+ * Set up sync triggers for all user tables
114
+ */
115
+ private setupAllTableTriggers;
116
+ /**
117
+ * Check if a table should be synced
118
+ */
119
+ private shouldSyncTable;
120
+ /**
121
+ * Set up sync triggers for a specific table
122
+ */
123
+ private setupTableSyncTriggers;
124
+ /**
125
+ * Build json_object() arguments for a table's columns
126
+ */
127
+ private buildJsonObjectArgs;
128
+ /**
129
+ * Start automatic cloud sync
130
+ */
131
+ private startCloudSync;
132
+ /**
133
+ * Stop automatic cloud sync
134
+ */
135
+ stopCloudSync(): void;
136
+ /**
137
+ * Perform a sync to cloud (D1/R2)
138
+ */
139
+ syncToCloud(): Promise<CloudSyncResult>;
140
+ /**
141
+ * Apply a single server change locally
142
+ */
143
+ private applyServerChange;
144
+ /**
145
+ * Force a full cloud sync (reset last sync time)
146
+ */
147
+ forceCloudSync(): Promise<CloudSyncResult>;
148
+ /**
149
+ * Get cloud sync status
150
+ */
151
+ getCloudSyncStatus(): {
152
+ enabled: boolean;
153
+ syncing: boolean;
154
+ lastSyncTime: number;
155
+ pendingChanges: number;
156
+ syncedTables: string[];
157
+ };
158
+ /**
159
+ * Disable cloud sync
160
+ */
161
+ disableCloudSync(): void;
33
162
  /**
34
163
  * Get information about all tables in the database
35
164
  */