nexabase-console 1.0.5 → 1.0.6

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/index.d.ts CHANGED
@@ -152,4 +152,13 @@ export declare const onSnapshot: (ref: CollectionReference | DocumentReference |
152
152
  export declare const enableIndexedDbPersistence: (db: NexaApp) => Promise<void>;
153
153
  export declare const enableOfflinePersistence: (db: NexaApp) => Promise<void>;
154
154
  export declare const writeBatch: (db: NexaApp) => WriteBatch;
155
+ export interface Transaction {
156
+ get: <T = any>(docRef: DocumentReference<T>) => Promise<DocumentSnapshot<T>>;
157
+ set: (docRef: DocumentReference, data: any) => Transaction;
158
+ update: (docRef: DocumentReference, data: any) => Transaction;
159
+ delete: (docRef: DocumentReference) => Transaction;
160
+ }
161
+ export declare const runTransaction: <T = any>(db: NexaApp, updateFunction: (transaction: Transaction) => Promise<T>, options?: {
162
+ maxAttempts?: number;
163
+ }) => Promise<T>;
155
164
  export {};
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.writeBatch = exports.enableOfflinePersistence = exports.enableIndexedDbPersistence = exports.onSnapshot = exports.deleteDoc = exports.updateDoc = exports.setDoc = exports.getDoc = exports.getDocs = exports.getCachedDoc = exports.getCachedDocs = exports.limit = exports.orderBy = exports.where = exports.query = exports.doc = exports.collection = exports.getFirestore = exports.initializeApp = exports.NexaApp = void 0;
6
+ exports.runTransaction = exports.writeBatch = exports.enableOfflinePersistence = exports.enableIndexedDbPersistence = exports.onSnapshot = exports.deleteDoc = exports.updateDoc = exports.setDoc = exports.getDoc = exports.getDocs = exports.getCachedDoc = exports.getCachedDocs = exports.limit = exports.orderBy = exports.where = exports.query = exports.doc = exports.collection = exports.getFirestore = exports.initializeApp = exports.NexaApp = void 0;
7
7
  const axios_1 = __importDefault(require("axios"));
8
8
  // -----------------------------------------------------------------
9
9
  // Main NexaApp SDK Class
@@ -762,3 +762,86 @@ const writeBatch = (db) => {
762
762
  return batchObj;
763
763
  };
764
764
  exports.writeBatch = writeBatch;
765
+ const runTransaction = async (db, updateFunction, options) => {
766
+ if (db._initPromise)
767
+ await db._initPromise;
768
+ const maxAttempts = options?.maxAttempts ?? 5;
769
+ let attempt = 0;
770
+ while (true) {
771
+ attempt++;
772
+ const reads = [];
773
+ const operations = [];
774
+ const transaction = {
775
+ get: async (docRef) => {
776
+ const segments = docRef.path.split('/');
777
+ const docId = segments[segments.length - 1];
778
+ const res = await db.client.get(`/api/firestore/${db.projectId}/documentWithMetadata?docPath=${encodeURIComponent(docRef.path)}`);
779
+ const { data, updatedAt } = res.data;
780
+ reads.push({ path: docRef.path, readUpdatedAt: updatedAt });
781
+ return {
782
+ id: docId,
783
+ ref: docRef,
784
+ exists: () => !!data,
785
+ data: () => data
786
+ };
787
+ },
788
+ set: (docRef, data) => {
789
+ operations.push({ type: 'set', path: docRef.path, data });
790
+ return transaction;
791
+ },
792
+ update: (docRef, data) => {
793
+ operations.push({ type: 'update', path: docRef.path, data });
794
+ return transaction;
795
+ },
796
+ delete: (docRef) => {
797
+ operations.push({ type: 'delete', path: docRef.path });
798
+ return transaction;
799
+ }
800
+ };
801
+ try {
802
+ const result = await updateFunction(transaction);
803
+ if (operations.length > 0 || reads.length > 0) {
804
+ await db.client.post(`/api/firestore/${db.projectId}/transaction`, {
805
+ reads,
806
+ operations
807
+ });
808
+ }
809
+ for (const op of operations) {
810
+ if (op.type === 'set' || op.type === 'update') {
811
+ const newData = { ...db._firestoreCache[op.path], ...op.data };
812
+ await db._setCache(op.path, newData);
813
+ Object.keys(db._snapshotCallbacks).forEach((key) => {
814
+ const cb = db._snapshotCallbacks[key];
815
+ const parts = key.split('_');
816
+ const pathKey = parts.slice(2).join('_');
817
+ if (pathKey === op.path || op.path.startsWith(pathKey + '/')) {
818
+ cb({ type: 'document_written', docPath: op.path, data: db._firestoreCache[op.path] });
819
+ }
820
+ });
821
+ }
822
+ else if (op.type === 'delete') {
823
+ await db._deleteCache(op.path);
824
+ Object.keys(db._snapshotCallbacks).forEach((key) => {
825
+ const cb = db._snapshotCallbacks[key];
826
+ const parts = key.split('_');
827
+ const pathKey = parts.slice(2).join('_');
828
+ if (pathKey === op.path || op.path.startsWith(pathKey + '/')) {
829
+ cb({ type: 'document_deleted', docPath: op.path });
830
+ }
831
+ });
832
+ }
833
+ }
834
+ return result;
835
+ }
836
+ catch (err) {
837
+ const isConflict = err && err.response && err.response.status === 409;
838
+ if (isConflict && attempt < maxAttempts) {
839
+ console.warn(`[NexaBase Transaction] Retrying due to concurrency conflict (Attempt ${attempt}/${maxAttempts})`);
840
+ await new Promise((resolve) => setTimeout(resolve, Math.random() * 100 * attempt));
841
+ continue;
842
+ }
843
+ throw err;
844
+ }
845
+ }
846
+ };
847
+ exports.runTransaction = runTransaction;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nexabase-console",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "SDK Client resmi untuk NexaBase: Platform Sinkronisasi NoSQL, Realtime, File Storage, & Autentikasi Offline-First.",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",