nexabase-console 1.0.1 → 1.0.3

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
@@ -22,35 +22,36 @@ export interface QueryConstraint {
22
22
  directionStr?: 'asc' | 'desc';
23
23
  limitValue?: number;
24
24
  }
25
- export interface DocumentReference {
25
+ export interface DocumentReference<T = any> {
26
26
  type: 'document';
27
27
  path: string;
28
28
  db: NexaApp;
29
29
  }
30
- export interface CollectionReference {
30
+ export interface CollectionReference<T = any> {
31
31
  type: 'collection';
32
32
  path: string;
33
33
  db: NexaApp;
34
34
  }
35
- export interface Query {
35
+ export interface Query<T = any> {
36
36
  type: 'query' | 'collection';
37
37
  path: string;
38
38
  db: NexaApp;
39
39
  constraints?: QueryConstraint[];
40
40
  }
41
- export interface DocumentSnapshot {
41
+ export interface DocumentSnapshot<T = any> {
42
42
  exists: () => boolean;
43
- data: () => any | null;
43
+ data: () => T | null;
44
44
  }
45
- export interface QueryDocumentSnapshot {
45
+ export interface QueryDocumentSnapshot<T = any> {
46
46
  id: string;
47
- data: () => any;
47
+ data: () => T;
48
48
  }
49
- export interface QuerySnapshot {
50
- docs: QueryDocumentSnapshot[];
51
- forEach: (callback: (doc: QueryDocumentSnapshot) => void) => void;
49
+ export interface QuerySnapshot<T = any> {
50
+ docs: QueryDocumentSnapshot<T>[];
51
+ forEach: (callback: (doc: QueryDocumentSnapshot<T>) => void) => void;
52
52
  empty: boolean;
53
53
  size: number;
54
+ docChanges?: () => any[];
54
55
  }
55
56
  export interface DatabaseReference {
56
57
  get: () => Promise<any>;
@@ -123,17 +124,17 @@ export declare class NexaApp {
123
124
  }
124
125
  export declare const initializeApp: (config: NexaConfig) => NexaApp;
125
126
  export declare const getFirestore: (app: NexaApp) => NexaApp;
126
- export declare const collection: (db: NexaApp, collectionPath: string) => CollectionReference;
127
- export declare const doc: (dbOrCollection: NexaApp | CollectionReference, ...pathSegments: string[]) => DocumentReference;
128
- export declare const query: (queryObject: Query | CollectionReference, ...queryConstraints: QueryConstraint[]) => Query;
127
+ export declare const collection: <T = any>(db: NexaApp, collectionPath: string) => CollectionReference<T>;
128
+ export declare const doc: <T = any>(dbOrCollection: NexaApp | CollectionReference<any>, ...pathSegments: string[]) => DocumentReference<T>;
129
+ export declare const query: <T = any>(queryObject: Query<T> | CollectionReference<T>, ...queryConstraints: QueryConstraint[]) => Query<T>;
129
130
  export declare const where: (fieldPath: string, opStr: "==" | ">" | "<" | ">=" | "<=" | "!=", value: any) => QueryConstraint;
130
131
  export declare const orderBy: (fieldPath: string, directionStr?: "asc" | "desc") => QueryConstraint;
131
132
  export declare const limit: (limitValue: number) => QueryConstraint;
132
- export declare const getDocs: (queryOrCollection: Query | CollectionReference) => Promise<QuerySnapshot>;
133
- export declare const getDoc: (docRef: DocumentReference) => Promise<DocumentSnapshot>;
133
+ export declare const getDocs: <T = any>(queryOrCollection: Query<T> | CollectionReference<T>) => Promise<QuerySnapshot<T>>;
134
+ export declare const getDoc: <T = any>(docRef: DocumentReference<T>) => Promise<DocumentSnapshot<T>>;
134
135
  export declare const setDoc: (docRef: DocumentReference, data: any) => Promise<any>;
135
136
  export declare const updateDoc: (docRef: DocumentReference, data: any) => Promise<any>;
136
137
  export declare const deleteDoc: (docRef: DocumentReference) => Promise<any>;
137
- export declare const onSnapshot: (ref: CollectionReference | DocumentReference, callback: (snapshot: any) => void) => (() => void);
138
+ export declare const onSnapshot: (ref: CollectionReference | DocumentReference | Query, callback: (snapshot: any) => void) => (() => void);
138
139
  export declare const writeBatch: (db: NexaApp) => WriteBatch;
139
140
  export {};
package/dist/index.js CHANGED
@@ -321,8 +321,10 @@ class NexaApp {
321
321
  else if (change.type === 'document_deleted') {
322
322
  await this._deleteCache(change.docPath);
323
323
  }
324
- Object.keys(this._snapshotCallbacks).forEach((pathKey) => {
325
- const cb = this._snapshotCallbacks[pathKey];
324
+ Object.keys(this._snapshotCallbacks).forEach((key) => {
325
+ const cb = this._snapshotCallbacks[key];
326
+ const parts = key.split('_');
327
+ const pathKey = parts.slice(2).join('_');
326
328
  if (pathKey === change.docPath || change.docPath.startsWith(pathKey + '/')) {
327
329
  cb(change);
328
330
  }
@@ -400,10 +402,14 @@ const getDocs = async (queryOrCollection) => {
400
402
  docs: docsArray,
401
403
  forEach: (cb) => docsArray.forEach(cb),
402
404
  empty: docsArray.length === 0,
403
- size: docsArray.length
405
+ size: docsArray.length,
406
+ docChanges: () => docsArray.map(doc => ({ type: 'added', doc }))
404
407
  };
405
408
  }
406
409
  catch (err) {
410
+ if (err && err.response) {
411
+ throw err;
412
+ }
407
413
  const results = [];
408
414
  Object.keys(db._firestoreCache).forEach((p) => {
409
415
  if (p.startsWith(collectionPath + '/')) {
@@ -432,6 +438,15 @@ const getDocs = async (queryOrCollection) => {
432
438
  return val <= c.value;
433
439
  if (c.opStr === '!=')
434
440
  return val !== c.value;
441
+ if (c.opStr === 'in')
442
+ return Array.isArray(c.value) && c.value.includes(val);
443
+ if (c.opStr === 'not-in')
444
+ return Array.isArray(c.value) && !c.value.includes(val);
445
+ if (c.opStr === 'array-contains')
446
+ return Array.isArray(val) && val.includes(c.value);
447
+ if (c.opStr === 'array-contains-any') {
448
+ return Array.isArray(val) && Array.isArray(c.value) && val.some((v) => c.value.includes(v));
449
+ }
435
450
  return true;
436
451
  });
437
452
  }
@@ -440,7 +455,8 @@ const getDocs = async (queryOrCollection) => {
440
455
  docs: filtered,
441
456
  forEach: (cb) => filtered.forEach(cb),
442
457
  empty: filtered.length === 0,
443
- size: filtered.length
458
+ size: filtered.length,
459
+ docChanges: () => filtered.map(doc => ({ type: 'added', doc }))
444
460
  };
445
461
  }
446
462
  };
@@ -456,8 +472,11 @@ const getDoc = async (docRef) => {
456
472
  return { exists: () => !!data, data: () => data };
457
473
  }
458
474
  catch (err) {
475
+ if (err && err.response) {
476
+ throw err;
477
+ }
459
478
  const data = db._firestoreCache[docRef.path];
460
- return { exists: () => !!data, data: () => data || null };
479
+ return { exists: () => !!data, data: () => (data || null) };
461
480
  }
462
481
  };
463
482
  exports.getDoc = getDoc;
@@ -467,8 +486,10 @@ const setDoc = async (docRef, data) => {
467
486
  await db._initPromise;
468
487
  await db._setCache(docRef.path, data);
469
488
  // Trigger snapshot listeners
470
- Object.keys(db._snapshotCallbacks).forEach((pathKey) => {
471
- const cb = db._snapshotCallbacks[pathKey];
489
+ Object.keys(db._snapshotCallbacks).forEach((key) => {
490
+ const cb = db._snapshotCallbacks[key];
491
+ const parts = key.split('_');
492
+ const pathKey = parts.slice(2).join('_');
472
493
  if (pathKey === docRef.path || docRef.path.startsWith(pathKey + '/')) {
473
494
  cb({ type: 'document_written', docPath: docRef.path, data });
474
495
  }
@@ -520,8 +541,10 @@ const deleteDoc = async (docRef) => {
520
541
  await db._initPromise;
521
542
  await db._deleteCache(docRef.path);
522
543
  // Trigger snapshot listeners
523
- Object.keys(db._snapshotCallbacks).forEach((pathKey) => {
524
- const cb = db._snapshotCallbacks[pathKey];
544
+ Object.keys(db._snapshotCallbacks).forEach((key) => {
545
+ const cb = db._snapshotCallbacks[key];
546
+ const parts = key.split('_');
547
+ const pathKey = parts.slice(2).join('_');
525
548
  if (pathKey === docRef.path || docRef.path.startsWith(pathKey + '/')) {
526
549
  cb({ type: 'document_deleted', docPath: docRef.path });
527
550
  }
@@ -551,7 +574,7 @@ const onSnapshot = (ref, callback) => {
551
574
  db._ensureFirestoreSSE();
552
575
  const pathKey = ref.path;
553
576
  const triggerCallback = async () => {
554
- if (ref.type === 'collection') {
577
+ if (ref.type === 'collection' || ref.type === 'query') {
555
578
  const docs = await (0, exports.getDocs)(ref);
556
579
  callback(docs);
557
580
  }
@@ -600,8 +623,10 @@ const writeBatch = (db) => {
600
623
  if (op.type === 'set' || op.type === 'update') {
601
624
  const newData = { ...db._firestoreCache[op.path], ...op.data };
602
625
  await db._setCache(op.path, newData);
603
- Object.keys(db._snapshotCallbacks).forEach((pathKey) => {
604
- const cb = db._snapshotCallbacks[pathKey];
626
+ Object.keys(db._snapshotCallbacks).forEach((key) => {
627
+ const cb = db._snapshotCallbacks[key];
628
+ const parts = key.split('_');
629
+ const pathKey = parts.slice(2).join('_');
605
630
  if (pathKey === op.path || op.path.startsWith(pathKey + '/')) {
606
631
  cb({ type: 'document_written', docPath: op.path, data: db._firestoreCache[op.path] });
607
632
  }
@@ -609,8 +634,10 @@ const writeBatch = (db) => {
609
634
  }
610
635
  else if (op.type === 'delete') {
611
636
  await db._deleteCache(op.path);
612
- Object.keys(db._snapshotCallbacks).forEach((pathKey) => {
613
- const cb = db._snapshotCallbacks[pathKey];
637
+ Object.keys(db._snapshotCallbacks).forEach((key) => {
638
+ const cb = db._snapshotCallbacks[key];
639
+ const parts = key.split('_');
640
+ const pathKey = parts.slice(2).join('_');
614
641
  if (pathKey === op.path || op.path.startsWith(pathKey + '/')) {
615
642
  cb({ type: 'document_deleted', docPath: op.path });
616
643
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nexabase-console",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
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",