nexabase-console 2.1.5 → 2.1.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.
@@ -395,17 +395,31 @@ class NexaApp {
395
395
  const isDelete = type === 'document_deleted' || action === 'delete' || type === 'delete';
396
396
  if (targetDocPath && typeof targetDocPath === 'string') {
397
397
  const cleanDocPath = targetDocPath.replace(/^\/+|\/+$/g, '');
398
- if (isDelete) {
399
- this._deleteCache(cleanDocPath);
400
- }
401
- else if (data !== undefined && data !== null) {
402
- this._setCache(cleanDocPath, data);
398
+ const segments = cleanDocPath.split('/');
399
+ const isCollection = segments.length % 2 === 1;
400
+ if (isCollection && data !== null && typeof data === 'object' && !Array.isArray(data)) {
401
+ // Distribute collection payload to individual docs
402
+ if (!isDelete) {
403
+ for (const docId of Object.keys(data)) {
404
+ const docData = data[docId];
405
+ this._setCache(`${cleanDocPath}/${docId}`, docData);
406
+ this._notifySnapshotCallbacks(`${cleanDocPath}/${docId}`, 'document_written', docData);
407
+ }
408
+ }
409
+ this._notifySnapshotCallbacks(cleanDocPath, isDelete ? 'document_deleted' : 'document_written', data);
403
410
  }
404
- const parts = cleanDocPath.split('/');
405
- const colPath = parts.length > 1 ? parts.slice(0, -1).join('/') : '';
406
- this._notifySnapshotCallbacks(cleanDocPath, isDelete ? 'document_deleted' : 'document_written', data);
407
- if (colPath) {
408
- this._notifySnapshotCallbacks(colPath, isDelete ? 'document_deleted' : 'document_written', data);
411
+ else {
412
+ if (isDelete) {
413
+ this._deleteCache(cleanDocPath);
414
+ }
415
+ else if (data !== undefined && data !== null) {
416
+ this._setCache(cleanDocPath, data);
417
+ }
418
+ const colPath = segments.length > 1 ? segments.slice(0, -1).join('/') : '';
419
+ this._notifySnapshotCallbacks(cleanDocPath, isDelete ? 'document_deleted' : 'document_written', data);
420
+ if (colPath) {
421
+ this._notifySnapshotCallbacks(colPath, isDelete ? 'document_deleted' : 'document_written', data);
422
+ }
409
423
  }
410
424
  }
411
425
  else if (collectionPath && typeof collectionPath === 'string') {
@@ -13,6 +13,10 @@ export type WriteResult<T = unknown> = {
13
13
  writtenLocally: true;
14
14
  mutationId: string;
15
15
  };
16
+ export declare const addDoc: <T = any>(reference: any, // CollectionReference
17
+ data: any, options?: {
18
+ idempotencyKey?: string;
19
+ }) => Promise<DocumentReference<T>>;
16
20
  export declare const setDoc: (docRef: DocumentReference, data: any, options?: {
17
21
  merge?: boolean;
18
22
  idempotencyKey?: string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPendingWritesCount = exports.deleteDoc = exports.updateDoc = exports.patchDoc = exports.setDoc = exports.FieldValue = void 0;
3
+ exports.getPendingWritesCount = exports.deleteDoc = exports.updateDoc = exports.patchDoc = exports.setDoc = exports.addDoc = exports.FieldValue = void 0;
4
4
  const helpers_1 = require("../utils/helpers");
5
5
  const NexaError_1 = require("../errors/NexaError");
6
6
  const QueryUtils_1 = require("./QueryUtils");
@@ -217,6 +217,28 @@ const executeWriteMutation = async (docRef, action, data, options) => {
217
217
  });
218
218
  return { status: 'committed', committed: true, writtenLocally: true, mutationId, data: ackView.data };
219
219
  };
220
+ const addDoc = async (reference, // CollectionReference
221
+ data, options) => {
222
+ // We need to import doc, but since writes.ts doesn't have it, we can construct the path.
223
+ // Actually it's easier to just generate an auto ID if we can't import doc.
224
+ // We don't want circular dependencies.
225
+ const db = reference.db;
226
+ const generateAutoId = () => {
227
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
228
+ let autoId = '';
229
+ for (let i = 0; i < 20; i++)
230
+ autoId += chars.charAt(Math.floor(Math.random() * chars.length));
231
+ return autoId;
232
+ };
233
+ const d = {
234
+ type: 'document',
235
+ path: `${reference.path}/${generateAutoId()}`,
236
+ db
237
+ };
238
+ await executeWriteMutation(d, 'set', data, options);
239
+ return d;
240
+ };
241
+ exports.addDoc = addDoc;
220
242
  const setDoc = async (docRef, data, options) => {
221
243
  return executeWriteMutation(docRef, 'set', data, options);
222
244
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nexabase-console",
3
- "version": "2.1.5",
3
+ "version": "2.1.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",