openbase-js 0.1.6 → 0.1.7
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/index.cjs +65 -0
- package/package.json +1 -1
package/index.cjs
CHANGED
|
@@ -24,6 +24,25 @@ class QueryBuilder {
|
|
|
24
24
|
return this;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
on(event, callback) {
|
|
28
|
+
if (!this._realtimeSub) {
|
|
29
|
+
this._realtimeSub = new RealtimeSubscription(
|
|
30
|
+
this._baseUrl, this._apiKey, this._dbName, this._table
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
this._realtimeSub.on(event, callback);
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
subscribe() {
|
|
38
|
+
if (!this._realtimeSub) {
|
|
39
|
+
this._realtimeSub = new RealtimeSubscription(
|
|
40
|
+
this._baseUrl, this._apiKey, this._dbName, this._table
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
return this._realtimeSub.subscribe();
|
|
44
|
+
}
|
|
45
|
+
|
|
27
46
|
// ─── Filter operators ──────────────────────────────────────────────────────
|
|
28
47
|
|
|
29
48
|
eq(column, value) {
|
|
@@ -336,6 +355,52 @@ class StorageClient {
|
|
|
336
355
|
}
|
|
337
356
|
}
|
|
338
357
|
|
|
358
|
+
// ─── Realtime Subscription ────────────────────────────────────────────────────
|
|
359
|
+
|
|
360
|
+
class RealtimeSubscription {
|
|
361
|
+
constructor(baseUrl, apiKey, dbName, table) {
|
|
362
|
+
this._baseUrl = baseUrl.replace('http://', 'ws://').replace('https://', 'wss://');
|
|
363
|
+
this._apiKey = apiKey;
|
|
364
|
+
this._dbName = dbName;
|
|
365
|
+
this._table = table;
|
|
366
|
+
this._listeners = {}; // { INSERT: [fn], UPDATE: [fn], DELETE: [fn] }
|
|
367
|
+
this._ws = null;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
on(event, callback) {
|
|
371
|
+
if (!this._listeners[event]) this._listeners[event] = [];
|
|
372
|
+
this._listeners[event].push(callback);
|
|
373
|
+
return this;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
subscribe() {
|
|
377
|
+
const url = `${this._baseUrl}/realtime/${this._dbName}/${this._table}?token=${this._apiKey}`;
|
|
378
|
+
this._ws = new WebSocket(url);
|
|
379
|
+
|
|
380
|
+
this._ws.onmessage = (e) => {
|
|
381
|
+
try {
|
|
382
|
+
const payload = JSON.parse(e.data);
|
|
383
|
+
if (payload.event === 'PING') return;
|
|
384
|
+
const handlers = this._listeners[payload.event] || [];
|
|
385
|
+
handlers.forEach(fn => fn(payload));
|
|
386
|
+
} catch { }
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
this._ws.onerror = (err) => {
|
|
390
|
+
console.error('[openbase] Realtime error:', err);
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
return this;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
unsubscribe() {
|
|
397
|
+
if (this._ws) {
|
|
398
|
+
this._ws.close();
|
|
399
|
+
this._ws = null;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
339
404
|
// ─── Client ───────────────────────────────────────────────────────────────────
|
|
340
405
|
|
|
341
406
|
class OpenbaseClient {
|