nexabase-console 1.0.0 → 1.0.2
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/README.md +10 -47
- package/dist/index.d.ts +2 -1
- package/dist/index.js +41 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# NexaBase JavaScript SDK (`nexabase`)
|
|
1
|
+
# NexaBase JavaScript SDK (`nexabase-console`)
|
|
2
2
|
|
|
3
3
|
SDK Client Resmi untuk **NexaBase**: Platform sinkronisasi database modern yang menyatukan fungsionalitas Firestore NoSQL, Real-Time Database, File Storage, dan Edge Authentication dengan dukungan **Offline-First (IndexedDB)** serta **Passwordless OTP**.
|
|
4
4
|
|
|
@@ -19,45 +19,9 @@ SDK Client Resmi untuk **NexaBase**: Platform sinkronisasi database modern yang
|
|
|
19
19
|
Instal package melalui npm atau yarn:
|
|
20
20
|
|
|
21
21
|
```bash
|
|
22
|
-
npm install nexabase
|
|
22
|
+
npm install nexabase-console
|
|
23
23
|
# atau
|
|
24
|
-
yarn add nexabase
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
---
|
|
28
|
-
|
|
29
|
-
## ⚡ Panduan Rilis (Publishing)
|
|
30
|
-
|
|
31
|
-
Jika Anda ingin mengunggah SDK ini ke repositori **GitHub** Anda sendiri atau merilisnya ke registry **NPM**, ikuti langkah mudah berikut:
|
|
32
|
-
|
|
33
|
-
### 1. Inisialisasi Repositori Git & GitHub
|
|
34
|
-
```bash
|
|
35
|
-
# Masuk ke direktori SDK
|
|
36
|
-
cd nexabase-sdk
|
|
37
|
-
|
|
38
|
-
# Inisialisasi Git
|
|
39
|
-
git init
|
|
40
|
-
git add .
|
|
41
|
-
git commit -m "Inisialisasi rilis perdana NexaBase SDK v1.0.0"
|
|
42
|
-
|
|
43
|
-
# Tambahkan remote repositori GitHub Anda
|
|
44
|
-
git remote add origin https://github.com/USERNAME/REPOS_NAME.git
|
|
45
|
-
git branch -M main
|
|
46
|
-
git push -u origin main
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
### 2. Publikasi ke NPM Registry
|
|
50
|
-
Pastikan Anda sudah memiliki akun di [npmjs.com](https://www.npmjs.com/).
|
|
51
|
-
|
|
52
|
-
```bash
|
|
53
|
-
# Login ke akun NPM Anda melalui terminal
|
|
54
|
-
npm login
|
|
55
|
-
|
|
56
|
-
# Jalankan build compiler TypeScript
|
|
57
|
-
npm run build
|
|
58
|
-
|
|
59
|
-
# Publikasikan ke registry NPM secara publik
|
|
60
|
-
npm publish --access public
|
|
24
|
+
yarn add nexabase-console
|
|
61
25
|
```
|
|
62
26
|
|
|
63
27
|
---
|
|
@@ -67,12 +31,11 @@ npm publish --access public
|
|
|
67
31
|
### 1. Inisialisasi Aplikasi
|
|
68
32
|
|
|
69
33
|
```javascript
|
|
70
|
-
import { initializeApp, getFirestore } from 'nexabase';
|
|
34
|
+
import { initializeApp, getFirestore } from 'nexabase-console';
|
|
71
35
|
|
|
72
36
|
const app = initializeApp({
|
|
73
37
|
projectId: 'PROYEK_NEXABASE_ID_ANDA',
|
|
74
|
-
apiKey: 'NEXABASE_API_KEY_ANDA'
|
|
75
|
-
endpoint: 'https://nexabase-server.yourdomain.com' // Alamat URL server backend
|
|
38
|
+
apiKey: 'NEXABASE_API_KEY_ANDA' // Opsional, default endpoint mengarah ke https://db.nexabase.id
|
|
76
39
|
});
|
|
77
40
|
|
|
78
41
|
const db = getFirestore(app);
|
|
@@ -84,7 +47,7 @@ const db = getFirestore(app);
|
|
|
84
47
|
|
|
85
48
|
#### Menulis Data Baru (Set Doc)
|
|
86
49
|
```javascript
|
|
87
|
-
import { doc, setDoc } from 'nexabase';
|
|
50
|
+
import { doc, setDoc } from 'nexabase-console';
|
|
88
51
|
|
|
89
52
|
const main = async () => {
|
|
90
53
|
const profileRef = doc(db, 'users', 'budi_santoso');
|
|
@@ -103,7 +66,7 @@ main();
|
|
|
103
66
|
|
|
104
67
|
#### Memperbarui Sebagian Data (Update Doc)
|
|
105
68
|
```javascript
|
|
106
|
-
import { doc, updateDoc } from 'nexabase';
|
|
69
|
+
import { doc, updateDoc } from 'nexabase-console';
|
|
107
70
|
|
|
108
71
|
const editData = async () => {
|
|
109
72
|
const profileRef = doc(db, 'users', 'budi_santoso');
|
|
@@ -115,7 +78,7 @@ const editData = async () => {
|
|
|
115
78
|
|
|
116
79
|
#### Membaca Satu Dokumen (Get Doc)
|
|
117
80
|
```javascript
|
|
118
|
-
import { doc, getDoc } from 'nexabase';
|
|
81
|
+
import { doc, getDoc } from 'nexabase-console';
|
|
119
82
|
|
|
120
83
|
const readData = async () => {
|
|
121
84
|
const profileRef = doc(db, 'users', 'budi_santoso');
|
|
@@ -131,7 +94,7 @@ const readData = async () => {
|
|
|
131
94
|
|
|
132
95
|
#### Mendengarkan Perubahan Data secara Real-Time (onSnapshot)
|
|
133
96
|
```javascript
|
|
134
|
-
import { doc, onSnapshot } from 'nexabase';
|
|
97
|
+
import { doc, onSnapshot } from 'nexabase-console';
|
|
135
98
|
|
|
136
99
|
const listenLive = () => {
|
|
137
100
|
const docRef = doc(db, 'chats', 'global_room');
|
|
@@ -148,7 +111,7 @@ const listenLive = () => {
|
|
|
148
111
|
|
|
149
112
|
#### Menggunakan Atomic Write Batch
|
|
150
113
|
```javascript
|
|
151
|
-
import { doc, writeBatch } from 'nexabase';
|
|
114
|
+
import { doc, writeBatch } from 'nexabase-console';
|
|
152
115
|
|
|
153
116
|
const executeBatch = async () => {
|
|
154
117
|
const batch = writeBatch(db);
|
package/dist/index.d.ts
CHANGED
|
@@ -51,6 +51,7 @@ export interface QuerySnapshot {
|
|
|
51
51
|
forEach: (callback: (doc: QueryDocumentSnapshot) => 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>;
|
|
@@ -134,6 +135,6 @@ export declare const getDoc: (docRef: DocumentReference) => Promise<DocumentSnap
|
|
|
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
|
@@ -29,7 +29,7 @@ class NexaApp {
|
|
|
29
29
|
this.token = savedToken;
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
|
-
this.endpoint = config.endpoint ||
|
|
32
|
+
this.endpoint = config.endpoint || 'https://db.nexabase.id';
|
|
33
33
|
this.client = axios_1.default.create({
|
|
34
34
|
baseURL: this.endpoint,
|
|
35
35
|
headers: {
|
|
@@ -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((
|
|
325
|
-
const cb = this._snapshotCallbacks[
|
|
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,6 +472,9 @@ 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
479
|
return { exists: () => !!data, data: () => data || null };
|
|
461
480
|
}
|
|
@@ -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((
|
|
471
|
-
const cb = db._snapshotCallbacks[
|
|
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((
|
|
524
|
-
const cb = db._snapshotCallbacks[
|
|
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((
|
|
604
|
-
const cb = db._snapshotCallbacks[
|
|
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((
|
|
613
|
-
const cb = db._snapshotCallbacks[
|
|
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