@neetru/sdk 2.2.0 → 2.3.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/dist/db.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { C as ConflictRecord, a as CreateOfflineDocumentsOptions, D as DbBatchOp, b as DbChangeType, c as DbCollectionRef, d as DbDoc, e as DbDocRef, f as DbGetResult, g as DbListResult, h as DbQuery, i as DbWhereFilter, N as NeetruDbDocuments, R as RealtimeTransport, S as SyncState } from './collection-ref-DqAAhuhX.cjs';
2
- export { k as DbSqlLease, p as NeetruDb, q as NeetruDbEngine, r as NeetruDbError, s as NeetruDbErrorCode, t as NeetruDbOptions, v as NeetruSqlClient, I as SqlOptions, a4 as createNeetruDb, a6 as createRestSyncTransport, a8 as getWebSocketRealtimeClient } from './types-DlDxttiG.cjs';
2
+ export { k as DbSqlLease, p as NeetruDb, q as NeetruDbEngine, r as NeetruDbError, s as NeetruDbErrorCode, t as NeetruDbOptions, u as NeetruDbTransport, w as NeetruSqlClient, J as SqlOptions, a5 as createNeetruDb, a7 as createRestSyncTransport, a9 as getWebSocketRealtimeClient } from './types-B6YJrynl.cjs';
3
3
  import '@neetru/realtime-protocol';
4
4
  import 'drizzle-orm/node-postgres';
5
5
  import './errors.cjs';
package/dist/db.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { C as ConflictRecord, a as CreateOfflineDocumentsOptions, D as DbBatchOp, b as DbChangeType, c as DbCollectionRef, d as DbDoc, e as DbDocRef, f as DbGetResult, g as DbListResult, h as DbQuery, i as DbWhereFilter, N as NeetruDbDocuments, R as RealtimeTransport, S as SyncState } from './collection-ref-DqAAhuhX.js';
2
- export { k as DbSqlLease, p as NeetruDb, q as NeetruDbEngine, r as NeetruDbError, s as NeetruDbErrorCode, t as NeetruDbOptions, v as NeetruSqlClient, I as SqlOptions, a4 as createNeetruDb, a6 as createRestSyncTransport, a8 as getWebSocketRealtimeClient } from './types-CvTje138.js';
2
+ export { k as DbSqlLease, p as NeetruDb, q as NeetruDbEngine, r as NeetruDbError, s as NeetruDbErrorCode, t as NeetruDbOptions, u as NeetruDbTransport, w as NeetruSqlClient, J as SqlOptions, a5 as createNeetruDb, a7 as createRestSyncTransport, a9 as getWebSocketRealtimeClient } from './types-DvhtZ4bi.js';
3
3
  import '@neetru/realtime-protocol';
4
4
  import 'drizzle-orm/node-postgres';
5
5
  import './errors.js';
package/dist/db.mjs CHANGED
@@ -581,6 +581,125 @@ var LocalStore = class {
581
581
  }
582
582
  };
583
583
 
584
+ // src/db/offline/memory-store.ts
585
+ var MemoryStore = class {
586
+ // store `documents` — key: `${collection}\0${id}`
587
+ _docs = /* @__PURE__ */ new Map();
588
+ // store `mutations` — key: mutationId
589
+ _mutations = /* @__PURE__ */ new Map();
590
+ // store `sync_meta` — key: string
591
+ _meta = /* @__PURE__ */ new Map();
592
+ // store `conflict_log` — autoIncrement int key
593
+ _conflicts = [];
594
+ _conflictSeq = 0;
595
+ // store `query_cache` — key: queryHash
596
+ _queryCache = /* @__PURE__ */ new Map();
597
+ // ─── Ciclo de vida (no-ops) ─────────────────────────────────────────────────
598
+ async open() {
599
+ }
600
+ async close() {
601
+ this._docs.clear();
602
+ this._mutations.clear();
603
+ this._meta.clear();
604
+ this._conflicts.splice(0);
605
+ this._conflictSeq = 0;
606
+ this._queryCache.clear();
607
+ }
608
+ // ─── Documents ─────────────────────────────────────────────────────────────
609
+ async getDoc(collection, id) {
610
+ const key = `${collection}\0${id}`;
611
+ return this._docs.get(key) ?? null;
612
+ }
613
+ async putDoc(doc) {
614
+ const key = `${doc.collection}\0${doc.id}`;
615
+ this._docs.set(key, doc);
616
+ }
617
+ async deleteDoc(collection, id) {
618
+ const key = `${collection}\0${id}`;
619
+ const existing = this._docs.get(key);
620
+ if (!existing) return;
621
+ this._docs.set(key, {
622
+ ...existing,
623
+ meta: {
624
+ ...existing.meta,
625
+ deleted: true,
626
+ updatedAtLocal: Date.now()
627
+ }
628
+ });
629
+ }
630
+ async listDocs(collection, query) {
631
+ const rawDocs = Array.from(this._docs.values()).filter(
632
+ (d) => d.collection === collection
633
+ );
634
+ return QueryEngine.evaluate(rawDocs, query);
635
+ }
636
+ // ─── Sync meta ──────────────────────────────────────────────────────────────
637
+ async getMeta(key) {
638
+ return this._meta.get(key) ?? null;
639
+ }
640
+ async setMeta(key, value) {
641
+ this._meta.set(key, value);
642
+ }
643
+ // ─── Conflict log ────────────────────────────────────────────────────────────
644
+ async appendConflict(record) {
645
+ const id = ++this._conflictSeq;
646
+ const full = { ...record, id };
647
+ this._conflicts.push(full);
648
+ return full;
649
+ }
650
+ async listConflicts(options) {
651
+ if (options?.delivered !== void 0) {
652
+ return this._conflicts.filter((r) => r.delivered === options.delivered);
653
+ }
654
+ return [...this._conflicts];
655
+ }
656
+ async markConflictDelivered(id) {
657
+ const entry = this._conflicts.find((r) => r.id === id);
658
+ if (entry) entry.delivered = true;
659
+ }
660
+ // ─── Mutations ───────────────────────────────────────────────────────────────
661
+ async putMutation(mutation) {
662
+ this._mutations.set(mutation.mutationId, { ...mutation });
663
+ }
664
+ async getMutation(mutationId) {
665
+ return this._mutations.get(mutationId) ?? null;
666
+ }
667
+ async listMutations(options) {
668
+ let all = Array.from(this._mutations.values());
669
+ if (options?.status !== void 0) {
670
+ all = all.filter((m) => m.status === options.status);
671
+ } else if (options?.collection !== void 0 && options.docId !== void 0) {
672
+ all = all.filter(
673
+ (m) => m.collection === options.collection && m.docId === options.docId
674
+ );
675
+ } else if (options?.collection !== void 0) {
676
+ all = all.filter((m) => m.collection === options.collection);
677
+ }
678
+ return all.sort((a, b) => a.seq - b.seq);
679
+ }
680
+ async deleteMutation(mutationId) {
681
+ this._mutations.delete(mutationId);
682
+ }
683
+ // ─── Collection discovery ────────────────────────────────────────────────────
684
+ async listCollections() {
685
+ const collections = /* @__PURE__ */ new Set();
686
+ for (const doc of this._docs.values()) {
687
+ collections.add(doc.collection);
688
+ }
689
+ return Array.from(collections);
690
+ }
691
+ // ─── Query cache ─────────────────────────────────────────────────────────────
692
+ async getQueryCache(queryHash) {
693
+ return this._queryCache.get(queryHash) ?? null;
694
+ }
695
+ async putQueryCache(entry) {
696
+ this._queryCache.set(entry.queryHash, { ...entry });
697
+ }
698
+ async deleteQueryCache(queryHash) {
699
+ this._queryCache.delete(queryHash);
700
+ }
701
+ };
702
+
584
703
  // src/db/offline/mutation-queue.ts
585
704
  function generateUUIDv4() {
586
705
  if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
@@ -2127,6 +2246,9 @@ var TabCoordinator = class {
2127
2246
  };
2128
2247
 
2129
2248
  // src/db/collection-ref.ts
2249
+ function isIndexedDBUnavailable() {
2250
+ return typeof indexedDB === "undefined" || typeof window === "undefined";
2251
+ }
2130
2252
  var COLL_RE = /^[a-z0-9][a-z0-9_-]{0,62}$/;
2131
2253
  function assertValidCollection(name) {
2132
2254
  if (!COLL_RE.test(name)) {
@@ -2668,7 +2790,7 @@ var NeetruDbDocumentsImpl = class {
2668
2790
  }
2669
2791
  };
2670
2792
  async function createOfflineDocumentsNamespace(opts) {
2671
- const store = new LocalStore(opts.dbName);
2793
+ const store = isIndexedDBUnavailable() ? new MemoryStore() : new LocalStore(opts.dbName);
2672
2794
  await store.open();
2673
2795
  const queue = new MutationQueue(store);
2674
2796
  const resolver = new ConflictResolver();
@@ -3438,16 +3560,16 @@ var defaultWebSocketFactory = (url) => {
3438
3560
  // src/db/client-db.ts
3439
3561
  var DATASTORE_COLLECTION_ENDPOINT = (collection) => `/api/sdk/v1/datastore/${encodeURIComponent(collection)}`;
3440
3562
  var DATASTORE_DOC_ENDPOINT = (collection, docId) => `/api/sdk/v1/datastore/${encodeURIComponent(collection)}/${encodeURIComponent(docId)}`;
3441
- function resolveTransport(engine, opts, config) {
3563
+ function resolveTransport(transport, opts, config) {
3442
3564
  if (opts._transport) {
3443
3565
  return opts._transport;
3444
3566
  }
3445
- if (engine === "firestore") {
3567
+ if (transport === "firestore") {
3446
3568
  if (opts.firestoreTransport) {
3447
3569
  return opts.firestoreTransport;
3448
3570
  }
3449
3571
  }
3450
- if (engine === "nosql-vm" && opts.realtimeGatewayUrl) {
3572
+ if (transport === "nosql-vm" && opts.realtimeGatewayUrl) {
3451
3573
  return createWebSocketSyncTransport(opts.realtimeGatewayUrl, config, opts);
3452
3574
  }
3453
3575
  return createRestSyncTransport(config);
@@ -3594,8 +3716,18 @@ function getWebSocketRealtimeClient(gatewayUrl, ticketProvider, dbId, wsFactory)
3594
3716
  return new NeetruRealtimeClient({ gatewayUrl, ticketProvider, dbId, webSocketFactory: wsFactory });
3595
3717
  }
3596
3718
  function createNeetruDb(config, dbOpts = {}) {
3597
- const engine = dbOpts.engine ?? "rest";
3598
- const transport = resolveTransport(engine, dbOpts, config);
3719
+ let resolvedTransport;
3720
+ if (dbOpts.transport !== void 0) {
3721
+ resolvedTransport = dbOpts.transport;
3722
+ } else if (dbOpts.engine !== void 0) {
3723
+ console.warn(
3724
+ '[neetru/sdk] NeetruDbOptions.engine est\xE1 depreciado e ser\xE1 removido em v3.0. Use NeetruDbOptions.transport em vez de engine. Valores v\xE1lidos: "rest" | "firestore" | "nosql-vm". Nota: "engine" no SDK configura o TRANSPORTE (REST/Firestore/WebSocket), n\xE3o o storage engine do admin database (firestore-instance, cloud-sql-postgres, \u2026).'
3725
+ );
3726
+ resolvedTransport = dbOpts.engine;
3727
+ } else {
3728
+ resolvedTransport = "rest";
3729
+ }
3730
+ const transport = resolveTransport(resolvedTransport, dbOpts, config);
3599
3731
  const dbId = dbOpts.dbId ?? "default";
3600
3732
  const dbName = dbOpts.dbName ?? `neetru-db__${config.productId ?? "sdk"}__${dbId}__${config.env}`;
3601
3733
  let _docsPromise = null;