shogun-core 6.9.7 → 6.9.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/README.md +66 -65
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -89,92 +89,93 @@ const shogun = new ShogunCore({
|
|
|
89
89
|
// All other APIs work the same way!
|
|
90
90
|
```
|
|
91
91
|
|
|
92
|
-
##
|
|
92
|
+
## Database API Reference
|
|
93
|
+
|
|
94
|
+
The `shogun.db` instance provides a high-level API for interacting with the decentralized database. Shogun Core automatically handles the differences between **Gun** and **Holster** backends.
|
|
95
|
+
|
|
96
|
+
### 1. Connection & Session Management
|
|
97
|
+
These methods work with both backends.
|
|
98
|
+
|
|
99
|
+
- `db.isLoggedIn(): boolean`: Returns `true` if a user is currently authenticated.
|
|
100
|
+
- `db.getCurrentUser(): { pub: string; user?: any } | null`: Returns the current user's public key and instance.
|
|
101
|
+
- `db.getUserPub(): string | null`: Returns the current user's public key.
|
|
102
|
+
- `db.onAuth(callback: (user: any) => void): () => void`: Listens for authentication changes. Returns an unsubscribe function.
|
|
103
|
+
- `db.restoreSession(): Promise<RestoreResult>`: Attempts to restore a previous session from `sessionStorage`.
|
|
104
|
+
|
|
105
|
+
### 2. Promise-based Advanced Utilities (Firegun API)
|
|
106
|
+
*Note: These methods currently provide full Promise support and auto-retries primarily when using the **Gun** backend.*
|
|
93
107
|
|
|
94
108
|
```typescript
|
|
95
109
|
const db = shogun.db;
|
|
96
110
|
|
|
97
|
-
//
|
|
98
|
-
await db.
|
|
99
|
-
name: 'Alice Smith',
|
|
100
|
-
email: 'alice@example.com',
|
|
101
|
-
});
|
|
111
|
+
// Fetch data with auto-retry and 5s timeout
|
|
112
|
+
const data = await db.Get('public/posts/123');
|
|
102
113
|
|
|
103
|
-
//
|
|
104
|
-
|
|
114
|
+
// Save data (supports deep object merging)
|
|
115
|
+
await db.Put('public/settings', { theme: 'dark' });
|
|
105
116
|
|
|
106
|
-
//
|
|
107
|
-
await db
|
|
108
|
-
.get('users')
|
|
109
|
-
.get('alice')
|
|
110
|
-
.get('profile')
|
|
111
|
-
.get('name')
|
|
112
|
-
.put('Alice Johnson');
|
|
117
|
+
// Insert into a collection with a random key
|
|
118
|
+
await db.Set('public/logs', { event: 'login', time: Date.now() });
|
|
113
119
|
|
|
114
|
-
//
|
|
115
|
-
db.
|
|
116
|
-
console.log(`User ${userId}:`, user);
|
|
117
|
-
});
|
|
118
|
-
```
|
|
120
|
+
// Recursive load of nested nodes
|
|
121
|
+
const fullData = await db.Load('public/complex-node');
|
|
119
122
|
|
|
120
|
-
|
|
123
|
+
// "Delete" a node (Tombstoning)
|
|
124
|
+
await db.Del('public/temp-data');
|
|
121
125
|
|
|
122
|
-
|
|
126
|
+
// Deeply nullify all keys in a node
|
|
127
|
+
await db.purge('public/old-config');
|
|
128
|
+
```
|
|
123
129
|
|
|
130
|
+
### 3. Real-time Subscriptions
|
|
124
131
|
```typescript
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
// Promise-based Root GET with auto-retry
|
|
128
|
-
const data = await db.Get('public/posts/123');
|
|
132
|
+
// Listen to changes with an identifier for easy unsubscription
|
|
133
|
+
db.On('public/feed', (data) => console.log('Update:', data), 'myListener');
|
|
129
134
|
|
|
130
|
-
//
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
// Listen to node changes with easy unsubscribe
|
|
134
|
-
await db.On(
|
|
135
|
-
'public/feed',
|
|
136
|
-
(data) => console.log('Feed updated:', data),
|
|
137
|
-
'myFeedListener',
|
|
138
|
-
);
|
|
139
|
-
await db.Off('myFeedListener'); // Cleanly remove the listener
|
|
140
|
-
|
|
141
|
-
// Content Addressed Storage (CAS)
|
|
142
|
-
// Hashes the data automatically using SHA-256 and saves it immutably
|
|
143
|
-
const ack = await db.addContentAdressing('#immutable-posts', {
|
|
144
|
-
text: 'Hello Web3!',
|
|
145
|
-
});
|
|
135
|
+
// Stop listening
|
|
136
|
+
db.Off('myListener');
|
|
146
137
|
|
|
147
|
-
//
|
|
148
|
-
|
|
138
|
+
// One-time fetch of initial state + future changes
|
|
139
|
+
db.Listen('public/status', (status) => console.log('Status:', status));
|
|
140
|
+
```
|
|
149
141
|
|
|
150
|
-
|
|
151
|
-
|
|
142
|
+
### 4. User-Space Operations (Gun Only)
|
|
143
|
+
These methods automatically prefix the path with `~pubkey/` of the logged-in user.
|
|
152
144
|
|
|
153
|
-
|
|
154
|
-
|
|
145
|
+
- `db.userGet(path: string)`: Read from current user's graph.
|
|
146
|
+
- `db.userPut(path: string, data: any)`: Write to current user's graph.
|
|
147
|
+
- `db.userDel(path: string)`: Delete node from user's graph.
|
|
148
|
+
- `db.userLoad(path: string)`: Recursively load user-space data.
|
|
155
149
|
|
|
156
|
-
|
|
157
|
-
|
|
150
|
+
### 5. Advanced Features
|
|
151
|
+
- **Reactive Streams**: `db.rx()` returns an RxJS helper for observable-based data handling.
|
|
152
|
+
- **Content Addressing**: `db.addContentAdressing('#key', data)` hashes data using SHA-256 for immutable storage.
|
|
153
|
+
- **Security**: `db.generatePublicCert()` creates a public certificate for P2P interactions.
|
|
154
|
+
- **Cleanup**: `db.aggressiveAuthCleanup()` forcefully clears all local auth state.
|
|
158
155
|
|
|
159
|
-
|
|
160
|
-
await db.purge('~pubkey/public/posts/123');
|
|
161
|
-
```
|
|
156
|
+
## Authentication API
|
|
162
157
|
|
|
163
|
-
|
|
158
|
+
Shogun Core provides a unified authentication interface. Plugins (Web3, WebAuthn, etc.) extend this system.
|
|
164
159
|
|
|
165
|
-
###
|
|
160
|
+
### Core Methods
|
|
166
161
|
|
|
167
162
|
```typescript
|
|
168
|
-
//
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
163
|
+
// 1. Traditional Signup/Login
|
|
164
|
+
await shogun.signUp('alice', 'Password123!');
|
|
165
|
+
await shogun.login('alice', 'Password123!');
|
|
166
|
+
|
|
167
|
+
// 2. Pair-based Authentication (Gun Only)
|
|
168
|
+
const pair = await shogun.db.crypto.createPair();
|
|
169
|
+
await shogun.loginWithPair('alice', pair);
|
|
170
|
+
|
|
171
|
+
// 3. Mnemonic Seed Authentication
|
|
172
|
+
const mnemonic = 'word1 word2 ...';
|
|
173
|
+
await shogun.loginWithSeed('alice', mnemonic);
|
|
174
|
+
|
|
175
|
+
// 4. Session Check & Logout
|
|
176
|
+
if (shogun.isLoggedIn()) {
|
|
177
|
+
console.log('User Pub:', shogun.db.getUserPub());
|
|
178
|
+
shogun.logout();
|
|
178
179
|
}
|
|
179
180
|
```
|
|
180
181
|
|