shogun-core 3.3.6 → 3.3.8

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/gundb/api.js CHANGED
@@ -1,7 +1,13 @@
1
1
  "use strict";
2
2
  /**
3
- * Simplified API layer to reduce complexity for common use cases.
4
- * Provides quick-start methods that wrap the full DataBase functionality.
3
+ * Simplified API layer focused on valuable helper methods.
4
+ * Provides quick-start initialization and high-level convenience methods.
5
+ *
6
+ * For basic operations (get, put, set, remove, auth), use DataBase directly.
7
+ * This class provides:
8
+ * - Quick initialization helpers (QuickStart, AutoQuickStart)
9
+ * - Array/Object conversion utilities for GunDB
10
+ * - High-level user data helpers (profile, settings, collections)
5
11
  */
6
12
  Object.defineProperty(exports, "__esModule", { value: true });
7
13
  exports.AutoQuickStart = exports.QuickStart = exports.SimpleGunAPI = void 0;
@@ -10,7 +16,8 @@ exports.quickStart = quickStart;
10
16
  exports.autoQuickStart = autoQuickStart;
11
17
  const db_1 = require("./db");
12
18
  /**
13
- * Simple API wrapper that provides common operations with minimal complexity.
19
+ * Simple API wrapper that provides high-level helper methods.
20
+ * For basic operations, use the DataBase instance directly via the `database` property.
14
21
  */
15
22
  class SimpleGunAPI {
16
23
  /**
@@ -20,307 +27,24 @@ class SimpleGunAPI {
20
27
  constructor(db) {
21
28
  this.db = db;
22
29
  }
23
- // =========================
24
- // Quick data operations
25
- // =========================
26
- /**
27
- * Get data at a given path.
28
- * @param path The path to retrieve data from.
29
- * @returns The data at the path, or null if not found or on error.
30
- */
31
- async get(path) {
32
- try {
33
- const result = await this.db.getData(path);
34
- return result;
35
- }
36
- catch (error) {
37
- console.warn(`Failed to get data from ${path}:`, error);
38
- return null;
39
- }
40
- }
41
- /**
42
- * Get the Gun node at a given path for chaining operations.
43
- * @param path The path to the node.
44
- * @returns The Gun node.
45
- */
46
- getNode(path) {
47
- return this.db.get(path);
48
- }
49
- /**
50
- * Get the Gun node at a given path for direct chaining.
51
- * @param path The path to the node.
52
- * @returns The Gun node.
53
- */
54
- node(path) {
55
- return this.db.get(path);
56
- }
57
- /**
58
- * Get a chainable wrapper for a Gun node at a given path.
59
- * @param path The path to the node.
60
- * @returns An object with chainable methods: get, put, set, once, then, map.
61
- */
62
- chain(path) {
63
- const node = this.db.get(path);
64
- return {
65
- get: (subPath) => this.chain(`${path}/${subPath}`),
66
- put: async (data) => {
67
- try {
68
- const result = await this.db.put(path, data);
69
- return result;
70
- }
71
- catch (error) {
72
- console.warn(`Failed to put data to ${path}:`, error);
73
- return { err: String(error) };
74
- }
75
- },
76
- set: async (data) => {
77
- try {
78
- const result = await this.db.set(path, data);
79
- return result;
80
- }
81
- catch (error) {
82
- console.warn(`Failed to set data to ${path}:`, error);
83
- return { err: String(error) };
84
- }
85
- },
86
- once: async () => {
87
- try {
88
- return await this.db.getData(path);
89
- }
90
- catch (error) {
91
- console.warn(`Failed to get data from ${path}:`, error);
92
- return null;
93
- }
94
- },
95
- then: async () => {
96
- try {
97
- return await this.db.getData(path);
98
- }
99
- catch (error) {
100
- console.warn(`Failed to get data from ${path}:`, error);
101
- return null;
102
- }
103
- },
104
- map: (callback) => {
105
- return node.map ? node.map(callback) : null;
106
- },
107
- };
108
- }
109
- /**
110
- * Put data at a given path.
111
- * @param path The path to put data to.
112
- * @param data The data to put.
113
- * @returns The GunMessagePut result.
114
- */
115
- async put(path, data) {
116
- try {
117
- const result = await this.db.put(path, data);
118
- return result;
119
- }
120
- catch (error) {
121
- console.warn(`Failed to put data to ${path}:`, error);
122
- return { err: String(error) };
123
- }
124
- }
125
- /**
126
- * Set data at a given path (alternative to put).
127
- * @param path The path to set data to.
128
- * @param data The data to set.
129
- * @returns The GunMessagePut result.
130
- */
131
- async set(path, data) {
132
- try {
133
- const result = await this.db.set(path, data);
134
- return result;
135
- }
136
- catch (error) {
137
- console.warn(`Failed to set data to ${path}:`, error);
138
- return { err: String(error) };
139
- }
140
- }
141
30
  /**
142
- * Remove data at a given path.
143
- * @param path The path to remove data from.
144
- * @returns The GunMessagePut result.
145
- */
146
- async remove(path) {
147
- try {
148
- const result = await this.db.remove(path);
149
- return result;
150
- }
151
- catch (error) {
152
- console.warn(`Failed to remove data from ${path}:`, error);
153
- return { err: String(error) };
154
- }
155
- }
156
- // =========================
157
- // Quick authentication
158
- // =========================
159
- /**
160
- * Log in a user.
161
- * @param username The username.
162
- * @param password The password.
163
- * @returns The user info if successful, or null.
164
- */
165
- async login(username, password) {
166
- try {
167
- const result = await this.db.login(username, password);
168
- if (result.success && result.userPub) {
169
- return {
170
- userPub: result.userPub,
171
- username: result.username || username,
172
- };
173
- }
174
- return null;
175
- }
176
- catch (error) {
177
- console.warn(`Login failed for ${username}:`, error);
178
- return null;
179
- }
180
- }
181
- /**
182
- * Sign up a new user.
183
- * @param username The username.
184
- * @param password The password.
185
- * @returns The user info if successful, or null.
186
- */
187
- async signup(username, password) {
188
- try {
189
- const result = await this.db.signUp(username, password);
190
- if (result.success && result.userPub) {
191
- return {
192
- userPub: result.userPub,
193
- username: result.username || username,
194
- };
195
- }
196
- return null;
197
- }
198
- catch (error) {
199
- console.warn(`Signup failed for ${username}:`, error);
200
- return null;
201
- }
202
- }
203
- /**
204
- * Log out the current user.
205
- */
206
- logout() {
207
- this.db.logout();
208
- }
209
- /**
210
- * Check if a user is currently logged in.
211
- * @returns True if logged in, false otherwise.
212
- */
213
- isLoggedIn() {
214
- return this.db.isLoggedIn();
215
- }
216
- // =========================
217
- // Quick user data operations
218
- // =========================
219
- /**
220
- * Get user data at a given path (requires login).
221
- * @param path The path to the user data.
222
- * @returns The user data, or null if not found or on error.
223
- */
224
- async getUserData(path) {
225
- try {
226
- if (!this.isLoggedIn()) {
227
- console.warn("User not logged in");
228
- return null;
229
- }
230
- if (!this.db.user) {
231
- console.warn("User node not available");
232
- return null;
233
- }
234
- const data = await this.db.user.get(path).once().then();
235
- return data;
236
- }
237
- catch (error) {
238
- console.warn(`Failed to get user data from ${path}:`, error);
239
- return null;
240
- }
241
- }
242
- /**
243
- * Put user data at a given path (requires login).
244
- * @param path The path to put data to.
245
- * @param data The data to put.
246
- * @returns True if successful, false otherwise.
247
- */
248
- async putUserData(path, data) {
249
- try {
250
- if (!this.isLoggedIn()) {
251
- console.warn("User not logged in");
252
- return false;
253
- }
254
- if (!this.db.user) {
255
- console.warn("User node not available");
256
- return false;
257
- }
258
- await this.db.user.get(path).put(data).then();
259
- return true;
260
- }
261
- catch (error) {
262
- console.warn(`Failed to put user data to ${path}:`, error);
263
- return false;
264
- }
265
- }
266
- /**
267
- * Set user data at a given path (alternative to put, requires login).
268
- * @param path The path to set data to.
269
- * @param data The data to set.
270
- * @returns True if successful, false otherwise.
271
- */
272
- async setUserData(path, data) {
273
- try {
274
- if (!this.isLoggedIn()) {
275
- console.warn("User not logged in");
276
- return false;
277
- }
278
- if (!this.db.user) {
279
- console.warn("User node not available");
280
- return false;
281
- }
282
- await this.db.user.get(path).put(data).then();
283
- return true;
284
- }
285
- catch (error) {
286
- console.warn(`Failed to set user data to ${path}:`, error);
287
- return false;
288
- }
289
- }
290
- /**
291
- * Remove user data at a given path (requires login).
292
- * @param path The path to remove data from.
293
- * @returns True if successful, false otherwise.
31
+ * Get direct access to the DataBase instance for full control.
32
+ * Use this for basic operations like get, put, set, remove, login, etc.
294
33
  */
295
- async removeUserData(path) {
296
- try {
297
- if (!this.isLoggedIn()) {
298
- console.warn("User not logged in");
299
- return false;
300
- }
301
- if (!this.db.user) {
302
- console.warn("User node not available");
303
- return false;
304
- }
305
- await this.db.user.get(path).put(null).then();
306
- return true;
307
- }
308
- catch (error) {
309
- console.warn(`Failed to remove user data from ${path}:`, error);
310
- return false;
311
- }
34
+ get database() {
35
+ return this.db;
312
36
  }
313
37
  // =========================
314
38
  // Array utilities for GunDB
315
39
  // =========================
316
40
  /**
317
41
  * Convert an array to an indexed object for GunDB storage.
42
+ * GunDB doesn't store arrays natively, so this converts them to objects indexed by ID.
318
43
  * Example: [{id: '1', ...}, {id: '2', ...}] => { "1": {...}, "2": {...} }
319
- * @param arr The array to convert.
320
- * @returns The indexed object.
321
- * @private
44
+ * @param arr The array to convert (each item must have an 'id' property).
45
+ * @returns The indexed object suitable for GunDB storage.
322
46
  */
323
- getIndexedObjectFromArray(arr) {
47
+ arrayToIndexedObject(arr) {
324
48
  // Filter out null/undefined items and ensure they have valid id
325
49
  const validItems = (arr || []).filter((item) => item &&
326
50
  typeof item === "object" &&
@@ -335,12 +59,12 @@ class SimpleGunAPI {
335
59
  }
336
60
  /**
337
61
  * Convert an indexed object back to an array.
62
+ * Reverses the arrayToIndexedObject conversion.
338
63
  * Example: { "1": {...}, "2": {...} } => [{id: '1', ...}, {id: '2', ...}]
339
64
  * @param indexedObj The indexed object to convert.
340
- * @returns The array.
341
- * @private
65
+ * @returns The array of items.
342
66
  */
343
- getArrayFromIndexedObject(indexedObj) {
67
+ indexedObjectToArray(indexedObj) {
344
68
  if (!indexedObj || typeof indexedObj !== "object") {
345
69
  return [];
346
70
  }
@@ -350,109 +74,17 @@ class SimpleGunAPI {
350
74
  // Filter out null/undefined values and ensure they are valid objects
351
75
  return Object.values(cleanObj).filter((item) => item && typeof item === "object");
352
76
  }
353
- /**
354
- * Convert an array to an indexed object for GunDB storage (public method).
355
- * @param arr The array to convert.
356
- * @returns The indexed object.
357
- */
358
- arrayToIndexedObject(arr) {
359
- return this.getIndexedObjectFromArray(arr);
360
- }
361
- /**
362
- * Convert an indexed object to an array (public method).
363
- * @param indexedObj The indexed object to convert.
364
- * @returns The array.
365
- */
366
- indexedObjectToArray(indexedObj) {
367
- return this.getArrayFromIndexedObject(indexedObj);
368
- }
369
- // =========================
370
- // Path utilities
371
- // =========================
372
- /**
373
- * Get the GunDB user node at a given path (requires login).
374
- * Useful for advanced operations that need direct GunDB node access.
375
- * @param path The path to the user node.
376
- * @returns The Gun node.
377
- * @throws If not logged in.
378
- */
379
- getUserNode(path) {
380
- if (!this.isLoggedIn()) {
381
- throw new Error("User not logged in");
382
- }
383
- // Use the database's path deconstruction
384
- return this.db.getUser().get(path);
385
- }
386
- /**
387
- * Get the GunDB global node at a given path.
388
- * Useful for advanced operations that need direct GunDB node access.
389
- * @param path The path to the global node.
390
- * @returns The Gun node.
391
- */
392
- getGlobalNode(path) {
393
- // Use the database's path deconstruction
394
- return this.db.get(path);
395
- }
396
77
  // =========================
397
- // Quick utility methods
78
+ // High-level user data helpers
398
79
  // =========================
399
80
  /**
400
- * Get the current user info.
401
- * @returns The current user info, or null if not logged in.
81
+ * Get all user data (returns user's entire data tree).
82
+ * Requires user to be logged in.
83
+ * @returns The complete user data tree, or null if not logged in or on error.
402
84
  */
403
- getCurrentUser() {
404
- const user = this.db.getCurrentUser();
405
- if (user) {
406
- return {
407
- pub: user.pub,
408
- username: user.alias,
409
- };
410
- }
411
- return null;
412
- }
413
- /**
414
- * Check if a user exists by alias.
415
- * @param alias The user alias.
416
- * @returns True if the user exists, false otherwise.
417
- */
418
- async userExists(alias) {
419
- try {
420
- const user = await this.db.getUserByAlias(alias);
421
- return user !== null;
422
- }
423
- catch (error) {
424
- console.warn(`Failed to check if user exists: ${alias}`, error);
425
- return false;
426
- }
427
- }
428
- /**
429
- * Get user info by alias.
430
- * @param alias The user alias.
431
- * @returns The user info, or null if not found.
432
- */
433
- async getUser(alias) {
434
- try {
435
- const user = await this.db.getUserByAlias(alias);
436
- if (user) {
437
- return {
438
- userPub: user.userPub,
439
- username: user.username,
440
- };
441
- }
442
- return null;
443
- }
444
- catch (error) {
445
- console.warn(`Failed to get user: ${alias}`, error);
446
- return null;
447
- }
448
- }
449
- /**
450
- * Advanced user space operations
451
- */
452
- // Get all user data (returns user's entire data tree)
453
85
  async getAllUserData() {
454
86
  try {
455
- if (!this.isLoggedIn()) {
87
+ if (!this.db.isLoggedIn()) {
456
88
  console.warn("User not logged in");
457
89
  return null;
458
90
  }
@@ -474,18 +106,20 @@ class SimpleGunAPI {
474
106
  return null;
475
107
  }
476
108
  }
477
- // Update user profile (common use case) - using direct user node
109
+ /**
110
+ * Update user profile with common fields.
111
+ * Provides a standardized location for user profile data.
112
+ * @param profileData Profile data to save (name, email, bio, avatar, etc.)
113
+ * @returns True if successful, false otherwise.
114
+ */
478
115
  async updateProfile(profileData) {
479
116
  try {
480
- if (!this.isLoggedIn()) {
117
+ if (!this.db.isLoggedIn()) {
481
118
  console.warn("User not logged in");
482
119
  return false;
483
120
  }
484
- if (!this.db.user) {
485
- console.warn("User node not available");
486
- return false;
487
- }
488
- await this.db.user.get("profile").put(profileData).then();
121
+ const user = this.db.getUser();
122
+ await user.get("profile").put(profileData).then();
489
123
  return true;
490
124
  }
491
125
  catch (error) {
@@ -493,18 +127,18 @@ class SimpleGunAPI {
493
127
  return false;
494
128
  }
495
129
  }
496
- // Get user profile - using direct user node
130
+ /**
131
+ * Get user profile data.
132
+ * @returns The user profile data, or null if not found or not logged in.
133
+ */
497
134
  async getProfile() {
498
135
  try {
499
- if (!this.isLoggedIn()) {
136
+ if (!this.db.isLoggedIn()) {
500
137
  console.warn("User not logged in");
501
138
  return null;
502
139
  }
503
- if (!this.db.user) {
504
- console.warn("User node not available");
505
- return null;
506
- }
507
- const profileData = await this.db.user.get("profile").once().then();
140
+ const user = this.db.getUser();
141
+ const profileData = await user.get("profile").once().then();
508
142
  return profileData;
509
143
  }
510
144
  catch (error) {
@@ -512,18 +146,20 @@ class SimpleGunAPI {
512
146
  return null;
513
147
  }
514
148
  }
515
- // Save user settings - using direct user node
149
+ /**
150
+ * Save user settings.
151
+ * Provides a standardized location for application settings.
152
+ * @param settings Settings object to save.
153
+ * @returns True if successful, false otherwise.
154
+ */
516
155
  async saveSettings(settings) {
517
156
  try {
518
- if (!this.isLoggedIn()) {
157
+ if (!this.db.isLoggedIn()) {
519
158
  console.warn("User not logged in");
520
159
  return false;
521
160
  }
522
- if (!this.db.user) {
523
- console.warn("User node not available");
524
- return false;
525
- }
526
- await this.db.user.get("settings").put(settings).then();
161
+ const user = this.db.getUser();
162
+ await user.get("settings").put(settings).then();
527
163
  return true;
528
164
  }
529
165
  catch (error) {
@@ -531,18 +167,18 @@ class SimpleGunAPI {
531
167
  return false;
532
168
  }
533
169
  }
534
- // Get user settings - using direct user node
170
+ /**
171
+ * Get user settings.
172
+ * @returns The user settings, or null if not found or not logged in.
173
+ */
535
174
  async getSettings() {
536
175
  try {
537
- if (!this.isLoggedIn()) {
176
+ if (!this.db.isLoggedIn()) {
538
177
  console.warn("User not logged in");
539
178
  return null;
540
179
  }
541
- if (!this.db.user) {
542
- console.warn("User node not available");
543
- return null;
544
- }
545
- const settingsData = await this.db.user.get("settings").once().then();
180
+ const user = this.db.getUser();
181
+ const settingsData = await user.get("settings").once().then();
546
182
  return settingsData;
547
183
  }
548
184
  catch (error) {
@@ -550,18 +186,20 @@ class SimpleGunAPI {
550
186
  return null;
551
187
  }
552
188
  }
553
- // Save user preferences - using direct user node
189
+ /**
190
+ * Save user preferences.
191
+ * Provides a standardized location for user preferences (distinct from settings).
192
+ * @param preferences Preferences object to save.
193
+ * @returns True if successful, false otherwise.
194
+ */
554
195
  async savePreferences(preferences) {
555
196
  try {
556
- if (!this.isLoggedIn()) {
197
+ if (!this.db.isLoggedIn()) {
557
198
  console.warn("User not logged in");
558
199
  return false;
559
200
  }
560
- if (!this.db.user) {
561
- console.warn("User node not available");
562
- return false;
563
- }
564
- await this.db.user.get("preferences").put(preferences).then();
201
+ const user = this.db.getUser();
202
+ await user.get("preferences").put(preferences).then();
565
203
  return true;
566
204
  }
567
205
  catch (error) {
@@ -569,21 +207,18 @@ class SimpleGunAPI {
569
207
  return false;
570
208
  }
571
209
  }
572
- // Get user preferences - using direct user node
210
+ /**
211
+ * Get user preferences.
212
+ * @returns The user preferences, or null if not found or not logged in.
213
+ */
573
214
  async getPreferences() {
574
215
  try {
575
- if (!this.isLoggedIn()) {
216
+ if (!this.db.isLoggedIn()) {
576
217
  console.warn("User not logged in");
577
218
  return null;
578
219
  }
579
- if (!this.db.user) {
580
- console.warn("User node not available");
581
- return null;
582
- }
583
- const preferencesData = await this.db.user
584
- .get("preferences")
585
- .once()
586
- .then();
220
+ const user = this.db.getUser();
221
+ const preferencesData = await user.get("preferences").once().then();
587
222
  return preferencesData;
588
223
  }
589
224
  catch (error) {
@@ -591,18 +226,21 @@ class SimpleGunAPI {
591
226
  return null;
592
227
  }
593
228
  }
594
- // Create a user collection - using direct user node
229
+ /**
230
+ * Create a user collection with initial items.
231
+ * Provides a standardized location for user collections.
232
+ * @param collectionName The name of the collection.
233
+ * @param items The initial items for the collection.
234
+ * @returns True if successful, false otherwise.
235
+ */
595
236
  async createCollection(collectionName, items) {
596
237
  try {
597
- if (!this.isLoggedIn()) {
238
+ if (!this.db.isLoggedIn()) {
598
239
  console.warn("User not logged in");
599
240
  return false;
600
241
  }
601
- if (!this.db.user) {
602
- console.warn("User node not available");
603
- return false;
604
- }
605
- await this.db.user.get(`collections/${collectionName}`).put(items).then();
242
+ const user = this.db.getUser();
243
+ await user.get(`collections/${collectionName}`).put(items).then();
606
244
  return true;
607
245
  }
608
246
  catch (error) {
@@ -610,18 +248,21 @@ class SimpleGunAPI {
610
248
  return false;
611
249
  }
612
250
  }
613
- // Add item to collection - using direct user node
251
+ /**
252
+ * Add an item to a user collection.
253
+ * @param collectionName The name of the collection.
254
+ * @param itemId The ID of the item to add.
255
+ * @param item The item data.
256
+ * @returns True if successful, false otherwise.
257
+ */
614
258
  async addToCollection(collectionName, itemId, item) {
615
259
  try {
616
- if (!this.isLoggedIn()) {
260
+ if (!this.db.isLoggedIn()) {
617
261
  console.warn("User not logged in");
618
262
  return false;
619
263
  }
620
- if (!this.db.user) {
621
- console.warn("User node not available");
622
- return false;
623
- }
624
- await this.db.user
264
+ const user = this.db.getUser();
265
+ await user
625
266
  .get(`collections/${collectionName}/${itemId}`)
626
267
  .put(item)
627
268
  .then();
@@ -632,18 +273,19 @@ class SimpleGunAPI {
632
273
  return false;
633
274
  }
634
275
  }
635
- // Get collection - using direct user node
276
+ /**
277
+ * Get a user collection.
278
+ * @param collectionName The name of the collection.
279
+ * @returns The collection data, or null if not found or not logged in.
280
+ */
636
281
  async getCollection(collectionName) {
637
282
  try {
638
- if (!this.isLoggedIn()) {
283
+ if (!this.db.isLoggedIn()) {
639
284
  console.warn("User not logged in");
640
285
  return null;
641
286
  }
642
- if (!this.db.user) {
643
- console.warn("User node not available");
644
- return null;
645
- }
646
- const collectionData = await this.db.user
287
+ const user = this.db.getUser();
288
+ const collectionData = await user
647
289
  .get(`collections/${collectionName}`)
648
290
  .once()
649
291
  .then();
@@ -654,18 +296,20 @@ class SimpleGunAPI {
654
296
  return null;
655
297
  }
656
298
  }
657
- // Remove item from collection - using direct user node
299
+ /**
300
+ * Remove an item from a user collection.
301
+ * @param collectionName The name of the collection.
302
+ * @param itemId The ID of the item to remove.
303
+ * @returns True if successful, false otherwise.
304
+ */
658
305
  async removeFromCollection(collectionName, itemId) {
659
306
  try {
660
- if (!this.isLoggedIn()) {
307
+ if (!this.db.isLoggedIn()) {
661
308
  console.warn("User not logged in");
662
309
  return false;
663
310
  }
664
- if (!this.db.user) {
665
- console.warn("User node not available");
666
- return false;
667
- }
668
- await this.db.user
311
+ const user = this.db.getUser();
312
+ await user
669
313
  .get(`collections/${collectionName}/${itemId}`)
670
314
  .put(null)
671
315
  .then();