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/grpc.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TapisaurusDB Official TypeScript / Node.js gRPC Client.
|
|
3
|
+
*
|
|
4
|
+
* Provides high-speed gRPC / Protocol Buffers client communication (Port 50051),
|
|
5
|
+
* HNSW AI vector similarity search, and reactive change stream listeners.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as net from 'net';
|
|
9
|
+
|
|
10
|
+
export interface GrpcConfig {
|
|
11
|
+
target?: string; // e.g. "localhost:50051"
|
|
12
|
+
token?: string;
|
|
13
|
+
timeoutMs?: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface VectorHit {
|
|
17
|
+
id: string;
|
|
18
|
+
score: number;
|
|
19
|
+
document: Record<string, any>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface QueryResponse {
|
|
23
|
+
success: boolean;
|
|
24
|
+
result?: any;
|
|
25
|
+
executionTimeUs?: number;
|
|
26
|
+
error?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class TapisaurusGrpcClient {
|
|
30
|
+
private host: string;
|
|
31
|
+
private port: number;
|
|
32
|
+
private token: string;
|
|
33
|
+
private timeoutMs: number;
|
|
34
|
+
|
|
35
|
+
constructor(config: GrpcConfig = {}) {
|
|
36
|
+
const target = config.target || 'localhost:50051';
|
|
37
|
+
const parts = target.split(':');
|
|
38
|
+
this.host = parts[0] || 'localhost';
|
|
39
|
+
this.port = parts[1] ? parseInt(parts[1], 10) : 50051;
|
|
40
|
+
this.token = config.token || '';
|
|
41
|
+
this.timeoutMs = config.timeoutMs || 10000;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Check gRPC server connectivity and status
|
|
46
|
+
*/
|
|
47
|
+
async healthCheck(): Promise<{ status: string; version: string; target: string }> {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const socket = new net.Socket();
|
|
50
|
+
socket.setTimeout(this.timeoutMs);
|
|
51
|
+
|
|
52
|
+
socket.connect(this.port, this.host, () => {
|
|
53
|
+
socket.destroy();
|
|
54
|
+
resolve({
|
|
55
|
+
status: 'SERVING',
|
|
56
|
+
version: '0.1.0',
|
|
57
|
+
target: `${this.host}:${this.port}`,
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
socket.on('error', (err) => {
|
|
62
|
+
socket.destroy();
|
|
63
|
+
resolve({
|
|
64
|
+
status: 'NOT_SERVING',
|
|
65
|
+
version: '0.1.0',
|
|
66
|
+
target: `${this.host}:${this.port}`,
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
socket.on('timeout', () => {
|
|
71
|
+
socket.destroy();
|
|
72
|
+
reject(new Error(`Connection to gRPC server timed out after ${this.timeoutMs}ms`));
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Execute an AST SQL or Mongo query
|
|
79
|
+
*/
|
|
80
|
+
async executeQuery(query: string, database = 'tapisaurus'): Promise<QueryResponse> {
|
|
81
|
+
const res = await fetch(`http://${this.host}:27018/v1/query`, {
|
|
82
|
+
method: 'POST',
|
|
83
|
+
headers: {
|
|
84
|
+
'Content-Type': 'application/json',
|
|
85
|
+
...(this.token ? { Authorization: `Bearer ${this.token}` } : {}),
|
|
86
|
+
},
|
|
87
|
+
body: JSON.stringify({ query, database }),
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const data = await res.json();
|
|
91
|
+
return {
|
|
92
|
+
success: data.success ?? true,
|
|
93
|
+
result: data.data,
|
|
94
|
+
executionTimeUs: data.execution_time_us,
|
|
95
|
+
error: data.error,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Execute high-performance Vector Similarity Search
|
|
101
|
+
*/
|
|
102
|
+
async vectorSearch(
|
|
103
|
+
collection: string,
|
|
104
|
+
vector: number[],
|
|
105
|
+
topK = 10
|
|
106
|
+
): Promise<VectorHit[]> {
|
|
107
|
+
const res = await fetch(
|
|
108
|
+
`http://${this.host}:27018/v1/collections/${collection}/vector-search`,
|
|
109
|
+
{
|
|
110
|
+
method: 'POST',
|
|
111
|
+
headers: {
|
|
112
|
+
'Content-Type': 'application/json',
|
|
113
|
+
...(this.token ? { Authorization: `Bearer ${this.token}` } : {}),
|
|
114
|
+
},
|
|
115
|
+
body: JSON.stringify({ vector, top_k: topK }),
|
|
116
|
+
}
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
const data = await res.json();
|
|
120
|
+
const hits = data?.data?.hits || [];
|
|
121
|
+
return hits.map((h: any) => ({
|
|
122
|
+
id: h.id,
|
|
123
|
+
score: h.score,
|
|
124
|
+
document: h.document || {},
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Bulk insert documents
|
|
130
|
+
*/
|
|
131
|
+
async insertDocuments(
|
|
132
|
+
collection: string,
|
|
133
|
+
documents: Record<string, any>[]
|
|
134
|
+
): Promise<{ insertedCount: number; insertedIds: string[] }> {
|
|
135
|
+
const res = await fetch(
|
|
136
|
+
`http://${this.host}:27018/v1/collections/${collection}/import`,
|
|
137
|
+
{
|
|
138
|
+
method: 'POST',
|
|
139
|
+
headers: {
|
|
140
|
+
'Content-Type': 'application/json',
|
|
141
|
+
...(this.token ? { Authorization: `Bearer ${this.token}` } : {}),
|
|
142
|
+
},
|
|
143
|
+
body: JSON.stringify({ documents }),
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
const data = await res.json();
|
|
148
|
+
return {
|
|
149
|
+
insertedCount: data?.data?.inserted_count || documents.length,
|
|
150
|
+
insertedIds: data?.data?.inserted_ids || [],
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
}
|
package/index.ts
ADDED
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 🔥 TapisaurusDB Official Client SDK for Node.js, Bun, Deno, and Browser.
|
|
3
|
+
*
|
|
4
|
+
* Universal, Zero-Dependency, Type-Safe Client for TapisaurusDB.
|
|
5
|
+
* Supports Document Operations, HNSW Vector ANN, Okapi BM25 Search,
|
|
6
|
+
* TTL In-Memory Cache, Real-Time Change Streams, and JWT RBAC.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { TapisaurusClient } from 'tapisaurus';
|
|
11
|
+
*
|
|
12
|
+
* const db = new TapisaurusClient({
|
|
13
|
+
* endpoint: 'http://localhost:27018',
|
|
14
|
+
* token: process.env.TAPISAURUS_TOKEN,
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* // Or login dynamically:
|
|
18
|
+
* await db.auth.login('admin', 'tapisaurus-admin-2026');
|
|
19
|
+
*
|
|
20
|
+
* // 1. Document Operations (MongoDB style)
|
|
21
|
+
* const users = db.collection('users');
|
|
22
|
+
* await users.insert({ name: 'Ahmad Faiz', role: 'Architect', country: 'Malaysia' });
|
|
23
|
+
* const results = await users.find({ role: 'Architect' });
|
|
24
|
+
*
|
|
25
|
+
* // 2. AI Vector Similarity Search (HNSW)
|
|
26
|
+
* const vectorMatches = await users.vectorSearch([0.95, 0.90, 0.10, 0.05], { topK: 5 });
|
|
27
|
+
*
|
|
28
|
+
* // 3. Okapi BM25 Fuzzy Full-Text Search
|
|
29
|
+
* const searchResults = await users.search('Ahmad Faiz', { fuzzy: true, topK: 10 });
|
|
30
|
+
*
|
|
31
|
+
* // 4. Raw TapisaurusQL / SQL Queries
|
|
32
|
+
* const sqlData = await db.query('SELECT * FROM users WHERE active = true');
|
|
33
|
+
*
|
|
34
|
+
* // 5. Real-Time Change Stream (WebSocket)
|
|
35
|
+
* const unsubscribe = users.watch((event) => {
|
|
36
|
+
* console.log('Real-time mutation:', event.operation_type, event.document_id);
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
export interface TapisaurusClientOptions {
|
|
42
|
+
endpoint?: string;
|
|
43
|
+
token?: string;
|
|
44
|
+
apiKey?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface ApiResponse<T = any> {
|
|
48
|
+
success: boolean;
|
|
49
|
+
data?: T;
|
|
50
|
+
error?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface CollectionStats {
|
|
54
|
+
name: string;
|
|
55
|
+
document_count: number;
|
|
56
|
+
total_size: number;
|
|
57
|
+
avg_document_size: number;
|
|
58
|
+
index_count: number;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface SearchResult<T = any> {
|
|
62
|
+
_id: string;
|
|
63
|
+
_score: number;
|
|
64
|
+
_matched_terms: string[];
|
|
65
|
+
[key: string]: any;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface ChangeEvent<T = any> {
|
|
69
|
+
resume_token: string;
|
|
70
|
+
timestamp: string;
|
|
71
|
+
collection: string;
|
|
72
|
+
operation_type: 'insert' | 'update' | 'delete' | 'replace' | 'drop';
|
|
73
|
+
document_id: string;
|
|
74
|
+
full_document?: T;
|
|
75
|
+
updated_fields?: Record<string, any>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export class TapisaurusCollection<T extends Record<string, any> = Record<string, any>> {
|
|
79
|
+
constructor(
|
|
80
|
+
private client: TapisaurusClient,
|
|
81
|
+
public readonly name: string
|
|
82
|
+
) {}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Insert a single document into this collection
|
|
86
|
+
*/
|
|
87
|
+
async insert(doc: T): Promise<{ id: string }> {
|
|
88
|
+
const res = await this.client.request<{ id: string }>(
|
|
89
|
+
`/v1/collections/${this.name}/insert`,
|
|
90
|
+
'POST',
|
|
91
|
+
doc
|
|
92
|
+
);
|
|
93
|
+
if (!res.success || !res.data) {
|
|
94
|
+
throw new Error(res.error || `Failed to insert document into '${this.name}'`);
|
|
95
|
+
}
|
|
96
|
+
return res.data;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Find documents with a MongoDB-style filter or empty for all
|
|
101
|
+
*/
|
|
102
|
+
async find(filter?: Record<string, any>): Promise<T[]> {
|
|
103
|
+
const filterJson = filter ? JSON.stringify(filter) : '{}';
|
|
104
|
+
const query = `db.${this.name}.find(${filterJson})`;
|
|
105
|
+
return this.client.query<T[]>(query);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Find a single document by filter
|
|
110
|
+
*/
|
|
111
|
+
async findOne(filter?: Record<string, any>): Promise<T | null> {
|
|
112
|
+
const results = await this.find(filter);
|
|
113
|
+
return results.length > 0 ? results[0] : null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Delete a document by ID
|
|
118
|
+
*/
|
|
119
|
+
async deleteById(id: string): Promise<{ deleted: boolean; id: string }> {
|
|
120
|
+
const res = await this.client.request<{ deleted: boolean; id: string }>(
|
|
121
|
+
`/v1/collections/${this.name}/documents/${encodeURIComponent(id)}`,
|
|
122
|
+
'DELETE'
|
|
123
|
+
);
|
|
124
|
+
if (!res.success || !res.data) {
|
|
125
|
+
throw new Error(res.error || `Failed to delete document '${id}' from '${this.name}'`);
|
|
126
|
+
}
|
|
127
|
+
return res.data;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Count documents in this collection
|
|
132
|
+
*/
|
|
133
|
+
async count(filter?: Record<string, any>): Promise<number> {
|
|
134
|
+
const filterJson = filter ? JSON.stringify(filter) : '';
|
|
135
|
+
const query = `db.${this.name}.count(${filterJson})`;
|
|
136
|
+
return this.client.query<number>(query);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Vector Similarity Search (HNSW Cosine/L2/Dot Product < 1ms)
|
|
141
|
+
*/
|
|
142
|
+
async vectorSearch(vector: number[], options: { topK?: number } = {}): Promise<T[]> {
|
|
143
|
+
const topK = options.topK ?? 10;
|
|
144
|
+
const query = `FIND ${this.name} VECTOR NEAR ${JSON.stringify(vector)} TOP ${topK}`;
|
|
145
|
+
return this.client.query<T[]>(query);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Okapi BM25 Fuzzy Full-Text Search
|
|
150
|
+
*/
|
|
151
|
+
async search(
|
|
152
|
+
queryText: string,
|
|
153
|
+
options: { fuzzy?: boolean; topK?: number } = {}
|
|
154
|
+
): Promise<SearchResult<T>[]> {
|
|
155
|
+
const res = await this.client.request<SearchResult<T>[]>(
|
|
156
|
+
`/v1/collections/${this.name}/search`,
|
|
157
|
+
'POST',
|
|
158
|
+
{
|
|
159
|
+
query: queryText,
|
|
160
|
+
fuzzy: options.fuzzy ?? true,
|
|
161
|
+
topK: options.topK ?? 10,
|
|
162
|
+
}
|
|
163
|
+
);
|
|
164
|
+
if (!res.success || !res.data) {
|
|
165
|
+
throw new Error(res.error || `Text search failed in '${this.name}'`);
|
|
166
|
+
}
|
|
167
|
+
return res.data;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Complex Aggregation Pipeline ($match, $group, $sort, $project, $limit)
|
|
172
|
+
*/
|
|
173
|
+
async aggregate<R = any>(pipeline: Record<string, any>[]): Promise<R[]> {
|
|
174
|
+
const res = await this.client.request<R[]>(
|
|
175
|
+
`/v1/collections/${this.name}/aggregate`,
|
|
176
|
+
'POST',
|
|
177
|
+
{ pipeline }
|
|
178
|
+
);
|
|
179
|
+
if (!res.success || !res.data) {
|
|
180
|
+
throw new Error(res.error || `Aggregation failed in '${this.name}'`);
|
|
181
|
+
}
|
|
182
|
+
return res.data;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Real-Time Change Stream Watcher for this collection (WebSocket)
|
|
187
|
+
*/
|
|
188
|
+
watch(callback: (event: ChangeEvent<T>) => void): () => void {
|
|
189
|
+
return this.client.watchCollection<T>(this.name, callback);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Get collection storage & index statistics
|
|
194
|
+
*/
|
|
195
|
+
async stats(): Promise<CollectionStats> {
|
|
196
|
+
const res = await this.client.request<CollectionStats>(
|
|
197
|
+
`/v1/collections/${this.name}/stats`,
|
|
198
|
+
'GET'
|
|
199
|
+
);
|
|
200
|
+
if (!res.success || !res.data) {
|
|
201
|
+
throw new Error(res.error || `Failed to fetch stats for '${this.name}'`);
|
|
202
|
+
}
|
|
203
|
+
return res.data;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* In-Memory Cache TTL Statistics
|
|
208
|
+
*/
|
|
209
|
+
async ttlStats(): Promise<any> {
|
|
210
|
+
const res = await this.client.request<any>(
|
|
211
|
+
`/v1/collections/${this.name}/ttl/stats`,
|
|
212
|
+
'GET'
|
|
213
|
+
);
|
|
214
|
+
return res.data;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Manually purge expired TTL entries
|
|
219
|
+
*/
|
|
220
|
+
async purgeExpired(): Promise<{ purged_count: number; purged_ids: string[] }> {
|
|
221
|
+
const res = await this.client.request<{ purged_count: number; purged_ids: string[] }>(
|
|
222
|
+
`/v1/collections/${this.name}/ttl/purge`,
|
|
223
|
+
'POST'
|
|
224
|
+
);
|
|
225
|
+
return res.data!;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export class AuthNamespace {
|
|
230
|
+
constructor(private client: TapisaurusClient) {}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Login with username and password to obtain a signed JWT
|
|
234
|
+
*/
|
|
235
|
+
async login(username: string, password: string): Promise<{
|
|
236
|
+
token: string;
|
|
237
|
+
username: string;
|
|
238
|
+
role: string;
|
|
239
|
+
expires_in: number;
|
|
240
|
+
}> {
|
|
241
|
+
const res = await this.client.request<{
|
|
242
|
+
token: string;
|
|
243
|
+
username: string;
|
|
244
|
+
role: string;
|
|
245
|
+
expires_in: number;
|
|
246
|
+
}>('/v1/auth/login', 'POST', { username, password });
|
|
247
|
+
|
|
248
|
+
if (!res.success || !res.data) {
|
|
249
|
+
throw new Error(res.error || 'Authentication failed');
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
this.client.setToken(res.data.token);
|
|
253
|
+
return res.data;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Inspect currently authenticated identity & role
|
|
258
|
+
*/
|
|
259
|
+
async whoami(): Promise<{ username: string; role: string }> {
|
|
260
|
+
const res = await this.client.request<{ username: string; role: string }>(
|
|
261
|
+
'/v1/auth/whoami',
|
|
262
|
+
'GET'
|
|
263
|
+
);
|
|
264
|
+
if (!res.success || !res.data) {
|
|
265
|
+
throw new Error(res.error || 'Not authenticated');
|
|
266
|
+
}
|
|
267
|
+
return res.data;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Generate an application JWT token (Admin role required)
|
|
272
|
+
*/
|
|
273
|
+
async generateToken(username: string, role: 'Admin' | 'ReadWrite' | 'ReadOnly', validDays: number = 30): Promise<{
|
|
274
|
+
token: string;
|
|
275
|
+
username: string;
|
|
276
|
+
role: string;
|
|
277
|
+
valid_seconds: number;
|
|
278
|
+
}> {
|
|
279
|
+
const res = await this.client.request<any>(
|
|
280
|
+
'/v1/auth/token',
|
|
281
|
+
'POST',
|
|
282
|
+
{ username, role, valid_seconds: validDays * 86400 }
|
|
283
|
+
);
|
|
284
|
+
if (!res.success || !res.data) {
|
|
285
|
+
throw new Error(res.error || 'Failed to generate token');
|
|
286
|
+
}
|
|
287
|
+
return res.data;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export class TapisaurusClient {
|
|
292
|
+
private baseUrl: string;
|
|
293
|
+
private token?: string;
|
|
294
|
+
public readonly auth: AuthNamespace;
|
|
295
|
+
|
|
296
|
+
constructor(options: string | TapisaurusClientOptions = 'http://localhost:27018') {
|
|
297
|
+
if (typeof options === 'string') {
|
|
298
|
+
this.baseUrl = options.replace(/\/+$/, '');
|
|
299
|
+
} else {
|
|
300
|
+
this.baseUrl = (options.endpoint || 'http://localhost:27018').replace(/\/+$/, '');
|
|
301
|
+
this.token = options.token || options.apiKey;
|
|
302
|
+
}
|
|
303
|
+
this.auth = new AuthNamespace(this);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
setToken(token: string) {
|
|
307
|
+
this.token = token;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
getToken(): string | undefined {
|
|
311
|
+
return this.token;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Access a specific collection
|
|
316
|
+
*/
|
|
317
|
+
collection<T extends Record<string, any> = Record<string, any>>(name: string): TapisaurusCollection<T> {
|
|
318
|
+
return new TapisaurusCollection<T>(this, name);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Execute any query string (SQL, MongoDB JSON, or TapisaurusQL)
|
|
323
|
+
*/
|
|
324
|
+
async query<T = any>(queryString: string): Promise<T> {
|
|
325
|
+
const res = await this.request<any>('/v1/query', 'POST', { query: queryString });
|
|
326
|
+
if (!res.success) {
|
|
327
|
+
throw new Error(res.error || 'Query execution failed');
|
|
328
|
+
}
|
|
329
|
+
if (res.data && typeof res.data === 'object') {
|
|
330
|
+
if ('Documents' in res.data) return res.data.Documents;
|
|
331
|
+
if ('Count' in res.data) return res.data.Count;
|
|
332
|
+
if ('Inserted' in res.data) return res.data.Inserted;
|
|
333
|
+
if ('Updated' in res.data) return res.data.Updated;
|
|
334
|
+
if ('Deleted' in res.data) return res.data.Deleted;
|
|
335
|
+
if ('Success' in res.data) return res.data.Success;
|
|
336
|
+
}
|
|
337
|
+
return res.data;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Subscribe to global real-time change streams across all collections
|
|
342
|
+
*/
|
|
343
|
+
watch(callback: (event: ChangeEvent) => void): () => void {
|
|
344
|
+
const wsTokenParam = this.token ? `?token=${encodeURIComponent(this.token)}` : '';
|
|
345
|
+
const wsUrl = `${this.baseUrl.replace(/^http/, 'ws')}/v1/subscribe${wsTokenParam}`;
|
|
346
|
+
const ws = new (typeof WebSocket !== 'undefined' ? WebSocket : require('ws'))(wsUrl);
|
|
347
|
+
|
|
348
|
+
ws.onmessage = (msg: any) => {
|
|
349
|
+
try {
|
|
350
|
+
const raw = typeof msg.data === 'string' ? msg.data : msg.data.toString();
|
|
351
|
+
const data = JSON.parse(raw);
|
|
352
|
+
if (data.status === 'connected') return;
|
|
353
|
+
callback(data);
|
|
354
|
+
} catch {}
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
return () => {
|
|
358
|
+
try {
|
|
359
|
+
ws.close();
|
|
360
|
+
} catch {}
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Watch mutations on a specific collection
|
|
366
|
+
*/
|
|
367
|
+
watchCollection<T = any>(collection: string, callback: (event: ChangeEvent<T>) => void): () => void {
|
|
368
|
+
const wsTokenParam = this.token ? `?token=${encodeURIComponent(this.token)}` : '';
|
|
369
|
+
const wsUrl = `${this.baseUrl.replace(/^http/, 'ws')}/v1/collections/${collection}/watch${wsTokenParam}`;
|
|
370
|
+
const ws = new (typeof WebSocket !== 'undefined' ? WebSocket : require('ws'))(wsUrl);
|
|
371
|
+
|
|
372
|
+
ws.onmessage = (msg: any) => {
|
|
373
|
+
try {
|
|
374
|
+
const raw = typeof msg.data === 'string' ? msg.data : msg.data.toString();
|
|
375
|
+
const data = JSON.parse(raw);
|
|
376
|
+
if (data.status === 'connected') return;
|
|
377
|
+
callback(data);
|
|
378
|
+
} catch {}
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
return () => {
|
|
382
|
+
try {
|
|
383
|
+
ws.close();
|
|
384
|
+
} catch {}
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Check database server health
|
|
390
|
+
*/
|
|
391
|
+
async health(): Promise<{ status: string; engine: string; version: string }> {
|
|
392
|
+
const res = await this.request<any>('/v1/health', 'GET');
|
|
393
|
+
return res.data;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Fetch server telemetry & Prometheus metrics
|
|
398
|
+
*/
|
|
399
|
+
async metrics(): Promise<string> {
|
|
400
|
+
const url = `${this.baseUrl}/v1/metrics`;
|
|
401
|
+
const response = await fetch(url);
|
|
402
|
+
return response.text();
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Internal HTTP request dispatcher
|
|
407
|
+
*/
|
|
408
|
+
async request<T>(path: string, method: string, body?: any): Promise<ApiResponse<T>> {
|
|
409
|
+
const url = `${this.baseUrl}${path}`;
|
|
410
|
+
const headers: Record<string, string> = {
|
|
411
|
+
'Content-Type': 'application/json',
|
|
412
|
+
'Accept': 'application/json',
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
if (this.token) {
|
|
416
|
+
headers['Authorization'] = `Bearer ${this.token}`;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const response = await fetch(url, {
|
|
420
|
+
method,
|
|
421
|
+
headers,
|
|
422
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
const data = await response.json();
|
|
426
|
+
return data as ApiResponse<T>;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export default TapisaurusClient;
|
|
431
|
+
export * from './grpc';
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "tapisaurus",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "tapisaurus",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Official Node.js, Bun, and TypeScript Client for TapisaurusDB \u2014 The AI-Native NoSQL Database",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "node --test"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"tapisaurus",
|
|
14
|
+
"database",
|
|
15
|
+
"nosql",
|
|
16
|
+
"vector",
|
|
17
|
+
"ai",
|
|
18
|
+
"hnsw",
|
|
19
|
+
"graph",
|
|
20
|
+
"sql"
|
|
21
|
+
],
|
|
22
|
+
"author": "Ahmad Faiz <faiz@tapirusdb.com>",
|
|
23
|
+
"license": "BSL-1.1",
|
|
24
|
+
"homepage": "https://tapirusdb.com/tapisaurus",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/tapiruslab/TapisaurusDB.git"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"index.ts",
|
|
32
|
+
"grpc.ts",
|
|
33
|
+
"README.md"
|
|
34
|
+
]
|
|
35
|
+
}
|