tapisaurus 0.0.0-stage → 0.1.0
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 +35 -3
- package/dist/grpc.d.ts +52 -0
- package/dist/grpc.js +145 -0
- package/dist/index.d.ts +208 -0
- package/dist/index.js +345 -0
- package/dist/index.mjs +345 -0
- package/grpc.ts +153 -0
- package/index.ts +431 -0
- package/package.json +35 -6
package/dist/index.js
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* 🔥 TapisaurusDB Official Client SDK for Node.js, Bun, Deno, and Browser.
|
|
4
|
+
*
|
|
5
|
+
* Universal, Zero-Dependency, Type-Safe Client for TapisaurusDB.
|
|
6
|
+
* Supports Document Operations, HNSW Vector ANN, Okapi BM25 Search,
|
|
7
|
+
* TTL In-Memory Cache, Real-Time Change Streams, and JWT RBAC.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { TapisaurusClient } from 'tapisaurus';
|
|
12
|
+
*
|
|
13
|
+
* const db = new TapisaurusClient({
|
|
14
|
+
* endpoint: 'http://localhost:27018',
|
|
15
|
+
* token: process.env.TAPISAURUS_TOKEN,
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* // Or login dynamically:
|
|
19
|
+
* await db.auth.login('admin', 'tapisaurus-admin-2026');
|
|
20
|
+
*
|
|
21
|
+
* // 1. Document Operations (MongoDB style)
|
|
22
|
+
* const users = db.collection('users');
|
|
23
|
+
* await users.insert({ name: 'Ahmad Faiz', role: 'Architect', country: 'Malaysia' });
|
|
24
|
+
* const results = await users.find({ role: 'Architect' });
|
|
25
|
+
*
|
|
26
|
+
* // 2. AI Vector Similarity Search (HNSW)
|
|
27
|
+
* const vectorMatches = await users.vectorSearch([0.95, 0.90, 0.10, 0.05], { topK: 5 });
|
|
28
|
+
*
|
|
29
|
+
* // 3. Okapi BM25 Fuzzy Full-Text Search
|
|
30
|
+
* const searchResults = await users.search('Ahmad Faiz', { fuzzy: true, topK: 10 });
|
|
31
|
+
*
|
|
32
|
+
* // 4. Raw TapisaurusQL / SQL Queries
|
|
33
|
+
* const sqlData = await db.query('SELECT * FROM users WHERE active = true');
|
|
34
|
+
*
|
|
35
|
+
* // 5. Real-Time Change Stream (WebSocket)
|
|
36
|
+
* const unsubscribe = users.watch((event) => {
|
|
37
|
+
* console.log('Real-time mutation:', event.operation_type, event.document_id);
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
42
|
+
if (k2 === undefined) k2 = k;
|
|
43
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
44
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
45
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
46
|
+
}
|
|
47
|
+
Object.defineProperty(o, k2, desc);
|
|
48
|
+
}) : (function(o, m, k, k2) {
|
|
49
|
+
if (k2 === undefined) k2 = k;
|
|
50
|
+
o[k2] = m[k];
|
|
51
|
+
}));
|
|
52
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
53
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
54
|
+
};
|
|
55
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
56
|
+
exports.TapisaurusClient = exports.AuthNamespace = exports.TapisaurusCollection = void 0;
|
|
57
|
+
class TapisaurusCollection {
|
|
58
|
+
client;
|
|
59
|
+
name;
|
|
60
|
+
constructor(client, name) {
|
|
61
|
+
this.client = client;
|
|
62
|
+
this.name = name;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Insert a single document into this collection
|
|
66
|
+
*/
|
|
67
|
+
async insert(doc) {
|
|
68
|
+
const res = await this.client.request(`/v1/collections/${this.name}/insert`, 'POST', doc);
|
|
69
|
+
if (!res.success || !res.data) {
|
|
70
|
+
throw new Error(res.error || `Failed to insert document into '${this.name}'`);
|
|
71
|
+
}
|
|
72
|
+
return res.data;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Find documents with a MongoDB-style filter or empty for all
|
|
76
|
+
*/
|
|
77
|
+
async find(filter) {
|
|
78
|
+
const filterJson = filter ? JSON.stringify(filter) : '{}';
|
|
79
|
+
const query = `db.${this.name}.find(${filterJson})`;
|
|
80
|
+
return this.client.query(query);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Find a single document by filter
|
|
84
|
+
*/
|
|
85
|
+
async findOne(filter) {
|
|
86
|
+
const results = await this.find(filter);
|
|
87
|
+
return results.length > 0 ? results[0] : null;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Delete a document by ID
|
|
91
|
+
*/
|
|
92
|
+
async deleteById(id) {
|
|
93
|
+
const res = await this.client.request(`/v1/collections/${this.name}/documents/${encodeURIComponent(id)}`, 'DELETE');
|
|
94
|
+
if (!res.success || !res.data) {
|
|
95
|
+
throw new Error(res.error || `Failed to delete document '${id}' from '${this.name}'`);
|
|
96
|
+
}
|
|
97
|
+
return res.data;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Count documents in this collection
|
|
101
|
+
*/
|
|
102
|
+
async count(filter) {
|
|
103
|
+
const filterJson = filter ? JSON.stringify(filter) : '';
|
|
104
|
+
const query = `db.${this.name}.count(${filterJson})`;
|
|
105
|
+
return this.client.query(query);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Vector Similarity Search (HNSW Cosine/L2/Dot Product < 1ms)
|
|
109
|
+
*/
|
|
110
|
+
async vectorSearch(vector, options = {}) {
|
|
111
|
+
const topK = options.topK ?? 10;
|
|
112
|
+
const query = `FIND ${this.name} VECTOR NEAR ${JSON.stringify(vector)} TOP ${topK}`;
|
|
113
|
+
return this.client.query(query);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Okapi BM25 Fuzzy Full-Text Search
|
|
117
|
+
*/
|
|
118
|
+
async search(queryText, options = {}) {
|
|
119
|
+
const res = await this.client.request(`/v1/collections/${this.name}/search`, 'POST', {
|
|
120
|
+
query: queryText,
|
|
121
|
+
fuzzy: options.fuzzy ?? true,
|
|
122
|
+
topK: options.topK ?? 10,
|
|
123
|
+
});
|
|
124
|
+
if (!res.success || !res.data) {
|
|
125
|
+
throw new Error(res.error || `Text search failed in '${this.name}'`);
|
|
126
|
+
}
|
|
127
|
+
return res.data;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Complex Aggregation Pipeline ($match, $group, $sort, $project, $limit)
|
|
131
|
+
*/
|
|
132
|
+
async aggregate(pipeline) {
|
|
133
|
+
const res = await this.client.request(`/v1/collections/${this.name}/aggregate`, 'POST', { pipeline });
|
|
134
|
+
if (!res.success || !res.data) {
|
|
135
|
+
throw new Error(res.error || `Aggregation failed in '${this.name}'`);
|
|
136
|
+
}
|
|
137
|
+
return res.data;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Real-Time Change Stream Watcher for this collection (WebSocket)
|
|
141
|
+
*/
|
|
142
|
+
watch(callback) {
|
|
143
|
+
return this.client.watchCollection(this.name, callback);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Get collection storage & index statistics
|
|
147
|
+
*/
|
|
148
|
+
async stats() {
|
|
149
|
+
const res = await this.client.request(`/v1/collections/${this.name}/stats`, 'GET');
|
|
150
|
+
if (!res.success || !res.data) {
|
|
151
|
+
throw new Error(res.error || `Failed to fetch stats for '${this.name}'`);
|
|
152
|
+
}
|
|
153
|
+
return res.data;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* In-Memory Cache TTL Statistics
|
|
157
|
+
*/
|
|
158
|
+
async ttlStats() {
|
|
159
|
+
const res = await this.client.request(`/v1/collections/${this.name}/ttl/stats`, 'GET');
|
|
160
|
+
return res.data;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Manually purge expired TTL entries
|
|
164
|
+
*/
|
|
165
|
+
async purgeExpired() {
|
|
166
|
+
const res = await this.client.request(`/v1/collections/${this.name}/ttl/purge`, 'POST');
|
|
167
|
+
return res.data;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
exports.TapisaurusCollection = TapisaurusCollection;
|
|
171
|
+
class AuthNamespace {
|
|
172
|
+
client;
|
|
173
|
+
constructor(client) {
|
|
174
|
+
this.client = client;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Login with username and password to obtain a signed JWT
|
|
178
|
+
*/
|
|
179
|
+
async login(username, password) {
|
|
180
|
+
const res = await this.client.request('/v1/auth/login', 'POST', { username, password });
|
|
181
|
+
if (!res.success || !res.data) {
|
|
182
|
+
throw new Error(res.error || 'Authentication failed');
|
|
183
|
+
}
|
|
184
|
+
this.client.setToken(res.data.token);
|
|
185
|
+
return res.data;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Inspect currently authenticated identity & role
|
|
189
|
+
*/
|
|
190
|
+
async whoami() {
|
|
191
|
+
const res = await this.client.request('/v1/auth/whoami', 'GET');
|
|
192
|
+
if (!res.success || !res.data) {
|
|
193
|
+
throw new Error(res.error || 'Not authenticated');
|
|
194
|
+
}
|
|
195
|
+
return res.data;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Generate an application JWT token (Admin role required)
|
|
199
|
+
*/
|
|
200
|
+
async generateToken(username, role, validDays = 30) {
|
|
201
|
+
const res = await this.client.request('/v1/auth/token', 'POST', { username, role, valid_seconds: validDays * 86400 });
|
|
202
|
+
if (!res.success || !res.data) {
|
|
203
|
+
throw new Error(res.error || 'Failed to generate token');
|
|
204
|
+
}
|
|
205
|
+
return res.data;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
exports.AuthNamespace = AuthNamespace;
|
|
209
|
+
class TapisaurusClient {
|
|
210
|
+
baseUrl;
|
|
211
|
+
token;
|
|
212
|
+
auth;
|
|
213
|
+
constructor(options = 'http://localhost:27018') {
|
|
214
|
+
if (typeof options === 'string') {
|
|
215
|
+
this.baseUrl = options.replace(/\/+$/, '');
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
this.baseUrl = (options.endpoint || 'http://localhost:27018').replace(/\/+$/, '');
|
|
219
|
+
this.token = options.token || options.apiKey;
|
|
220
|
+
}
|
|
221
|
+
this.auth = new AuthNamespace(this);
|
|
222
|
+
}
|
|
223
|
+
setToken(token) {
|
|
224
|
+
this.token = token;
|
|
225
|
+
}
|
|
226
|
+
getToken() {
|
|
227
|
+
return this.token;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Access a specific collection
|
|
231
|
+
*/
|
|
232
|
+
collection(name) {
|
|
233
|
+
return new TapisaurusCollection(this, name);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Execute any query string (SQL, MongoDB JSON, or TapisaurusQL)
|
|
237
|
+
*/
|
|
238
|
+
async query(queryString) {
|
|
239
|
+
const res = await this.request('/v1/query', 'POST', { query: queryString });
|
|
240
|
+
if (!res.success) {
|
|
241
|
+
throw new Error(res.error || 'Query execution failed');
|
|
242
|
+
}
|
|
243
|
+
if (res.data && typeof res.data === 'object') {
|
|
244
|
+
if ('Documents' in res.data)
|
|
245
|
+
return res.data.Documents;
|
|
246
|
+
if ('Count' in res.data)
|
|
247
|
+
return res.data.Count;
|
|
248
|
+
if ('Inserted' in res.data)
|
|
249
|
+
return res.data.Inserted;
|
|
250
|
+
if ('Updated' in res.data)
|
|
251
|
+
return res.data.Updated;
|
|
252
|
+
if ('Deleted' in res.data)
|
|
253
|
+
return res.data.Deleted;
|
|
254
|
+
if ('Success' in res.data)
|
|
255
|
+
return res.data.Success;
|
|
256
|
+
}
|
|
257
|
+
return res.data;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Subscribe to global real-time change streams across all collections
|
|
261
|
+
*/
|
|
262
|
+
watch(callback) {
|
|
263
|
+
const wsTokenParam = this.token ? `?token=${encodeURIComponent(this.token)}` : '';
|
|
264
|
+
const wsUrl = `${this.baseUrl.replace(/^http/, 'ws')}/v1/subscribe${wsTokenParam}`;
|
|
265
|
+
const ws = new (typeof WebSocket !== 'undefined' ? WebSocket : require('ws'))(wsUrl);
|
|
266
|
+
ws.onmessage = (msg) => {
|
|
267
|
+
try {
|
|
268
|
+
const raw = typeof msg.data === 'string' ? msg.data : msg.data.toString();
|
|
269
|
+
const data = JSON.parse(raw);
|
|
270
|
+
if (data.status === 'connected')
|
|
271
|
+
return;
|
|
272
|
+
callback(data);
|
|
273
|
+
}
|
|
274
|
+
catch { }
|
|
275
|
+
};
|
|
276
|
+
return () => {
|
|
277
|
+
try {
|
|
278
|
+
ws.close();
|
|
279
|
+
}
|
|
280
|
+
catch { }
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Watch mutations on a specific collection
|
|
285
|
+
*/
|
|
286
|
+
watchCollection(collection, callback) {
|
|
287
|
+
const wsTokenParam = this.token ? `?token=${encodeURIComponent(this.token)}` : '';
|
|
288
|
+
const wsUrl = `${this.baseUrl.replace(/^http/, 'ws')}/v1/collections/${collection}/watch${wsTokenParam}`;
|
|
289
|
+
const ws = new (typeof WebSocket !== 'undefined' ? WebSocket : require('ws'))(wsUrl);
|
|
290
|
+
ws.onmessage = (msg) => {
|
|
291
|
+
try {
|
|
292
|
+
const raw = typeof msg.data === 'string' ? msg.data : msg.data.toString();
|
|
293
|
+
const data = JSON.parse(raw);
|
|
294
|
+
if (data.status === 'connected')
|
|
295
|
+
return;
|
|
296
|
+
callback(data);
|
|
297
|
+
}
|
|
298
|
+
catch { }
|
|
299
|
+
};
|
|
300
|
+
return () => {
|
|
301
|
+
try {
|
|
302
|
+
ws.close();
|
|
303
|
+
}
|
|
304
|
+
catch { }
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Check database server health
|
|
309
|
+
*/
|
|
310
|
+
async health() {
|
|
311
|
+
const res = await this.request('/v1/health', 'GET');
|
|
312
|
+
return res.data;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Fetch server telemetry & Prometheus metrics
|
|
316
|
+
*/
|
|
317
|
+
async metrics() {
|
|
318
|
+
const url = `${this.baseUrl}/v1/metrics`;
|
|
319
|
+
const response = await fetch(url);
|
|
320
|
+
return response.text();
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Internal HTTP request dispatcher
|
|
324
|
+
*/
|
|
325
|
+
async request(path, method, body) {
|
|
326
|
+
const url = `${this.baseUrl}${path}`;
|
|
327
|
+
const headers = {
|
|
328
|
+
'Content-Type': 'application/json',
|
|
329
|
+
'Accept': 'application/json',
|
|
330
|
+
};
|
|
331
|
+
if (this.token) {
|
|
332
|
+
headers['Authorization'] = `Bearer ${this.token}`;
|
|
333
|
+
}
|
|
334
|
+
const response = await fetch(url, {
|
|
335
|
+
method,
|
|
336
|
+
headers,
|
|
337
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
338
|
+
});
|
|
339
|
+
const data = await response.json();
|
|
340
|
+
return data;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
exports.TapisaurusClient = TapisaurusClient;
|
|
344
|
+
exports.default = TapisaurusClient;
|
|
345
|
+
__exportStar(require("./grpc"), exports);
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* 🔥 TapisaurusDB Official Client SDK for Node.js, Bun, Deno, and Browser.
|
|
4
|
+
*
|
|
5
|
+
* Universal, Zero-Dependency, Type-Safe Client for TapisaurusDB.
|
|
6
|
+
* Supports Document Operations, HNSW Vector ANN, Okapi BM25 Search,
|
|
7
|
+
* TTL In-Memory Cache, Real-Time Change Streams, and JWT RBAC.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { TapisaurusClient } from 'tapisaurus';
|
|
12
|
+
*
|
|
13
|
+
* const db = new TapisaurusClient({
|
|
14
|
+
* endpoint: 'http://localhost:27018',
|
|
15
|
+
* token: process.env.TAPISAURUS_TOKEN,
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* // Or login dynamically:
|
|
19
|
+
* await db.auth.login('admin', 'tapisaurus-admin-2026');
|
|
20
|
+
*
|
|
21
|
+
* // 1. Document Operations (MongoDB style)
|
|
22
|
+
* const users = db.collection('users');
|
|
23
|
+
* await users.insert({ name: 'Ahmad Faiz', role: 'Architect', country: 'Malaysia' });
|
|
24
|
+
* const results = await users.find({ role: 'Architect' });
|
|
25
|
+
*
|
|
26
|
+
* // 2. AI Vector Similarity Search (HNSW)
|
|
27
|
+
* const vectorMatches = await users.vectorSearch([0.95, 0.90, 0.10, 0.05], { topK: 5 });
|
|
28
|
+
*
|
|
29
|
+
* // 3. Okapi BM25 Fuzzy Full-Text Search
|
|
30
|
+
* const searchResults = await users.search('Ahmad Faiz', { fuzzy: true, topK: 10 });
|
|
31
|
+
*
|
|
32
|
+
* // 4. Raw TapisaurusQL / SQL Queries
|
|
33
|
+
* const sqlData = await db.query('SELECT * FROM users WHERE active = true');
|
|
34
|
+
*
|
|
35
|
+
* // 5. Real-Time Change Stream (WebSocket)
|
|
36
|
+
* const unsubscribe = users.watch((event) => {
|
|
37
|
+
* console.log('Real-time mutation:', event.operation_type, event.document_id);
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
42
|
+
if (k2 === undefined) k2 = k;
|
|
43
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
44
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
45
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
46
|
+
}
|
|
47
|
+
Object.defineProperty(o, k2, desc);
|
|
48
|
+
}) : (function(o, m, k, k2) {
|
|
49
|
+
if (k2 === undefined) k2 = k;
|
|
50
|
+
o[k2] = m[k];
|
|
51
|
+
}));
|
|
52
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
53
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
54
|
+
};
|
|
55
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
56
|
+
exports.TapisaurusClient = exports.AuthNamespace = exports.TapisaurusCollection = void 0;
|
|
57
|
+
class TapisaurusCollection {
|
|
58
|
+
client;
|
|
59
|
+
name;
|
|
60
|
+
constructor(client, name) {
|
|
61
|
+
this.client = client;
|
|
62
|
+
this.name = name;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Insert a single document into this collection
|
|
66
|
+
*/
|
|
67
|
+
async insert(doc) {
|
|
68
|
+
const res = await this.client.request(`/v1/collections/${this.name}/insert`, 'POST', doc);
|
|
69
|
+
if (!res.success || !res.data) {
|
|
70
|
+
throw new Error(res.error || `Failed to insert document into '${this.name}'`);
|
|
71
|
+
}
|
|
72
|
+
return res.data;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Find documents with a MongoDB-style filter or empty for all
|
|
76
|
+
*/
|
|
77
|
+
async find(filter) {
|
|
78
|
+
const filterJson = filter ? JSON.stringify(filter) : '{}';
|
|
79
|
+
const query = `db.${this.name}.find(${filterJson})`;
|
|
80
|
+
return this.client.query(query);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Find a single document by filter
|
|
84
|
+
*/
|
|
85
|
+
async findOne(filter) {
|
|
86
|
+
const results = await this.find(filter);
|
|
87
|
+
return results.length > 0 ? results[0] : null;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Delete a document by ID
|
|
91
|
+
*/
|
|
92
|
+
async deleteById(id) {
|
|
93
|
+
const res = await this.client.request(`/v1/collections/${this.name}/documents/${encodeURIComponent(id)}`, 'DELETE');
|
|
94
|
+
if (!res.success || !res.data) {
|
|
95
|
+
throw new Error(res.error || `Failed to delete document '${id}' from '${this.name}'`);
|
|
96
|
+
}
|
|
97
|
+
return res.data;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Count documents in this collection
|
|
101
|
+
*/
|
|
102
|
+
async count(filter) {
|
|
103
|
+
const filterJson = filter ? JSON.stringify(filter) : '';
|
|
104
|
+
const query = `db.${this.name}.count(${filterJson})`;
|
|
105
|
+
return this.client.query(query);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Vector Similarity Search (HNSW Cosine/L2/Dot Product < 1ms)
|
|
109
|
+
*/
|
|
110
|
+
async vectorSearch(vector, options = {}) {
|
|
111
|
+
const topK = options.topK ?? 10;
|
|
112
|
+
const query = `FIND ${this.name} VECTOR NEAR ${JSON.stringify(vector)} TOP ${topK}`;
|
|
113
|
+
return this.client.query(query);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Okapi BM25 Fuzzy Full-Text Search
|
|
117
|
+
*/
|
|
118
|
+
async search(queryText, options = {}) {
|
|
119
|
+
const res = await this.client.request(`/v1/collections/${this.name}/search`, 'POST', {
|
|
120
|
+
query: queryText,
|
|
121
|
+
fuzzy: options.fuzzy ?? true,
|
|
122
|
+
topK: options.topK ?? 10,
|
|
123
|
+
});
|
|
124
|
+
if (!res.success || !res.data) {
|
|
125
|
+
throw new Error(res.error || `Text search failed in '${this.name}'`);
|
|
126
|
+
}
|
|
127
|
+
return res.data;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Complex Aggregation Pipeline ($match, $group, $sort, $project, $limit)
|
|
131
|
+
*/
|
|
132
|
+
async aggregate(pipeline) {
|
|
133
|
+
const res = await this.client.request(`/v1/collections/${this.name}/aggregate`, 'POST', { pipeline });
|
|
134
|
+
if (!res.success || !res.data) {
|
|
135
|
+
throw new Error(res.error || `Aggregation failed in '${this.name}'`);
|
|
136
|
+
}
|
|
137
|
+
return res.data;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Real-Time Change Stream Watcher for this collection (WebSocket)
|
|
141
|
+
*/
|
|
142
|
+
watch(callback) {
|
|
143
|
+
return this.client.watchCollection(this.name, callback);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Get collection storage & index statistics
|
|
147
|
+
*/
|
|
148
|
+
async stats() {
|
|
149
|
+
const res = await this.client.request(`/v1/collections/${this.name}/stats`, 'GET');
|
|
150
|
+
if (!res.success || !res.data) {
|
|
151
|
+
throw new Error(res.error || `Failed to fetch stats for '${this.name}'`);
|
|
152
|
+
}
|
|
153
|
+
return res.data;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* In-Memory Cache TTL Statistics
|
|
157
|
+
*/
|
|
158
|
+
async ttlStats() {
|
|
159
|
+
const res = await this.client.request(`/v1/collections/${this.name}/ttl/stats`, 'GET');
|
|
160
|
+
return res.data;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Manually purge expired TTL entries
|
|
164
|
+
*/
|
|
165
|
+
async purgeExpired() {
|
|
166
|
+
const res = await this.client.request(`/v1/collections/${this.name}/ttl/purge`, 'POST');
|
|
167
|
+
return res.data;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
exports.TapisaurusCollection = TapisaurusCollection;
|
|
171
|
+
class AuthNamespace {
|
|
172
|
+
client;
|
|
173
|
+
constructor(client) {
|
|
174
|
+
this.client = client;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Login with username and password to obtain a signed JWT
|
|
178
|
+
*/
|
|
179
|
+
async login(username, password) {
|
|
180
|
+
const res = await this.client.request('/v1/auth/login', 'POST', { username, password });
|
|
181
|
+
if (!res.success || !res.data) {
|
|
182
|
+
throw new Error(res.error || 'Authentication failed');
|
|
183
|
+
}
|
|
184
|
+
this.client.setToken(res.data.token);
|
|
185
|
+
return res.data;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Inspect currently authenticated identity & role
|
|
189
|
+
*/
|
|
190
|
+
async whoami() {
|
|
191
|
+
const res = await this.client.request('/v1/auth/whoami', 'GET');
|
|
192
|
+
if (!res.success || !res.data) {
|
|
193
|
+
throw new Error(res.error || 'Not authenticated');
|
|
194
|
+
}
|
|
195
|
+
return res.data;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Generate an application JWT token (Admin role required)
|
|
199
|
+
*/
|
|
200
|
+
async generateToken(username, role, validDays = 30) {
|
|
201
|
+
const res = await this.client.request('/v1/auth/token', 'POST', { username, role, valid_seconds: validDays * 86400 });
|
|
202
|
+
if (!res.success || !res.data) {
|
|
203
|
+
throw new Error(res.error || 'Failed to generate token');
|
|
204
|
+
}
|
|
205
|
+
return res.data;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
exports.AuthNamespace = AuthNamespace;
|
|
209
|
+
class TapisaurusClient {
|
|
210
|
+
baseUrl;
|
|
211
|
+
token;
|
|
212
|
+
auth;
|
|
213
|
+
constructor(options = 'http://localhost:27018') {
|
|
214
|
+
if (typeof options === 'string') {
|
|
215
|
+
this.baseUrl = options.replace(/\/+$/, '');
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
this.baseUrl = (options.endpoint || 'http://localhost:27018').replace(/\/+$/, '');
|
|
219
|
+
this.token = options.token || options.apiKey;
|
|
220
|
+
}
|
|
221
|
+
this.auth = new AuthNamespace(this);
|
|
222
|
+
}
|
|
223
|
+
setToken(token) {
|
|
224
|
+
this.token = token;
|
|
225
|
+
}
|
|
226
|
+
getToken() {
|
|
227
|
+
return this.token;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Access a specific collection
|
|
231
|
+
*/
|
|
232
|
+
collection(name) {
|
|
233
|
+
return new TapisaurusCollection(this, name);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Execute any query string (SQL, MongoDB JSON, or TapisaurusQL)
|
|
237
|
+
*/
|
|
238
|
+
async query(queryString) {
|
|
239
|
+
const res = await this.request('/v1/query', 'POST', { query: queryString });
|
|
240
|
+
if (!res.success) {
|
|
241
|
+
throw new Error(res.error || 'Query execution failed');
|
|
242
|
+
}
|
|
243
|
+
if (res.data && typeof res.data === 'object') {
|
|
244
|
+
if ('Documents' in res.data)
|
|
245
|
+
return res.data.Documents;
|
|
246
|
+
if ('Count' in res.data)
|
|
247
|
+
return res.data.Count;
|
|
248
|
+
if ('Inserted' in res.data)
|
|
249
|
+
return res.data.Inserted;
|
|
250
|
+
if ('Updated' in res.data)
|
|
251
|
+
return res.data.Updated;
|
|
252
|
+
if ('Deleted' in res.data)
|
|
253
|
+
return res.data.Deleted;
|
|
254
|
+
if ('Success' in res.data)
|
|
255
|
+
return res.data.Success;
|
|
256
|
+
}
|
|
257
|
+
return res.data;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Subscribe to global real-time change streams across all collections
|
|
261
|
+
*/
|
|
262
|
+
watch(callback) {
|
|
263
|
+
const wsTokenParam = this.token ? `?token=${encodeURIComponent(this.token)}` : '';
|
|
264
|
+
const wsUrl = `${this.baseUrl.replace(/^http/, 'ws')}/v1/subscribe${wsTokenParam}`;
|
|
265
|
+
const ws = new (typeof WebSocket !== 'undefined' ? WebSocket : require('ws'))(wsUrl);
|
|
266
|
+
ws.onmessage = (msg) => {
|
|
267
|
+
try {
|
|
268
|
+
const raw = typeof msg.data === 'string' ? msg.data : msg.data.toString();
|
|
269
|
+
const data = JSON.parse(raw);
|
|
270
|
+
if (data.status === 'connected')
|
|
271
|
+
return;
|
|
272
|
+
callback(data);
|
|
273
|
+
}
|
|
274
|
+
catch { }
|
|
275
|
+
};
|
|
276
|
+
return () => {
|
|
277
|
+
try {
|
|
278
|
+
ws.close();
|
|
279
|
+
}
|
|
280
|
+
catch { }
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Watch mutations on a specific collection
|
|
285
|
+
*/
|
|
286
|
+
watchCollection(collection, callback) {
|
|
287
|
+
const wsTokenParam = this.token ? `?token=${encodeURIComponent(this.token)}` : '';
|
|
288
|
+
const wsUrl = `${this.baseUrl.replace(/^http/, 'ws')}/v1/collections/${collection}/watch${wsTokenParam}`;
|
|
289
|
+
const ws = new (typeof WebSocket !== 'undefined' ? WebSocket : require('ws'))(wsUrl);
|
|
290
|
+
ws.onmessage = (msg) => {
|
|
291
|
+
try {
|
|
292
|
+
const raw = typeof msg.data === 'string' ? msg.data : msg.data.toString();
|
|
293
|
+
const data = JSON.parse(raw);
|
|
294
|
+
if (data.status === 'connected')
|
|
295
|
+
return;
|
|
296
|
+
callback(data);
|
|
297
|
+
}
|
|
298
|
+
catch { }
|
|
299
|
+
};
|
|
300
|
+
return () => {
|
|
301
|
+
try {
|
|
302
|
+
ws.close();
|
|
303
|
+
}
|
|
304
|
+
catch { }
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Check database server health
|
|
309
|
+
*/
|
|
310
|
+
async health() {
|
|
311
|
+
const res = await this.request('/v1/health', 'GET');
|
|
312
|
+
return res.data;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Fetch server telemetry & Prometheus metrics
|
|
316
|
+
*/
|
|
317
|
+
async metrics() {
|
|
318
|
+
const url = `${this.baseUrl}/v1/metrics`;
|
|
319
|
+
const response = await fetch(url);
|
|
320
|
+
return response.text();
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Internal HTTP request dispatcher
|
|
324
|
+
*/
|
|
325
|
+
async request(path, method, body) {
|
|
326
|
+
const url = `${this.baseUrl}${path}`;
|
|
327
|
+
const headers = {
|
|
328
|
+
'Content-Type': 'application/json',
|
|
329
|
+
'Accept': 'application/json',
|
|
330
|
+
};
|
|
331
|
+
if (this.token) {
|
|
332
|
+
headers['Authorization'] = `Bearer ${this.token}`;
|
|
333
|
+
}
|
|
334
|
+
const response = await fetch(url, {
|
|
335
|
+
method,
|
|
336
|
+
headers,
|
|
337
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
338
|
+
});
|
|
339
|
+
const data = await response.json();
|
|
340
|
+
return data;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
exports.TapisaurusClient = TapisaurusClient;
|
|
344
|
+
exports.default = TapisaurusClient;
|
|
345
|
+
__exportStar(require("./grpc"), exports);
|