toridb 1.0.0 → 1.0.1

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.
@@ -1,682 +1,669 @@
1
- import require$$0 from 'net';
1
+ import net from 'net';
2
2
 
3
- function getDefaultExportFromCjs (x) {
4
- return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
3
+ /**
4
+ * Command Compiler: Translates JS Objects and Types to ToriDB DSL
5
+ */
6
+ class Compiler {
7
+ /**
8
+ * Translates a JS Object to a ToriDB Filter string.
9
+ * { age: { $gt: 18 }, status: 'active' } -> "age > 18 AND status = 'active'"
10
+ */
11
+ static compileFilter(filter) {
12
+ if (!filter || typeof filter !== 'object') return "";
13
+
14
+ const parts = [];
15
+ for (const [key, value] of Object.entries(filter)) {
16
+ if (key === '$or' && Array.isArray(value)) {
17
+ parts.push(`(${value.map(f => this.compileFilter(f)).join(" OR ")})`);
18
+ } else if (key === '$and' && Array.isArray(value)) {
19
+ parts.push(`(${value.map(f => this.compileFilter(f)).join(" AND ")})`);
20
+ } else if (typeof value === 'object' && value !== null) {
21
+ // Handle operators: { $gt: 10 }
22
+ for (const [op, val] of Object.entries(value)) {
23
+ const symbol = this._mapOperator(op);
24
+ parts.push(`${key} ${symbol} ${this._escape(val)}`);
25
+ }
26
+ } else {
27
+ // Basic equality
28
+ parts.push(`${key} = ${this._escape(value)}`);
29
+ }
30
+ }
31
+ return parts.join(" AND ");
32
+ }
33
+
34
+ /**
35
+ * Translates a Blueprint definition to ToriDB Table Definition format.
36
+ * { id: { type: 'INT', primary: true }, user_id: { type: 'INT', references: 'users.id' } }
37
+ * -> "id:INT:pk user_id:INT:fk(users.id)"
38
+ */
39
+ static compileBlueprint(name, blueprint) {
40
+ const cols = [];
41
+ for (const [colName, config] of Object.entries(blueprint)) {
42
+ let type = typeof config === 'string' ? config : config.type;
43
+ let pk = (config.primary || config.is_pk) ? ":pk" : "";
44
+ let fk = "";
45
+
46
+ if (config.references) {
47
+ const [refTable, refCol] = config.references.split('.');
48
+ fk = `:fk(${refTable}.${refCol})`;
49
+ }
50
+
51
+ // Auto-map JS types if not specified as SQL types
52
+ if (type === 'Number') type = 'INT';
53
+ if (type === 'String') type = 'STRING';
54
+ if (type === 'Object') type = 'JSON';
55
+ if (type === 'Boolean') type = 'BOOL';
56
+
57
+ cols.push(`${colName}:${type}${pk}${fk}`);
58
+ }
59
+ return `CREATE TABLE ${name} ${cols.join(" ")}`;
60
+ }
61
+
62
+ /**
63
+ * Maps MongoDB-style operators to ToriDB SQL operators.
64
+ * @param {string} op - The operator to map (e.g., "$gt").
65
+ * @returns {string} The corresponding SQL operator or the operator itself if no map exists.
66
+ * @private
67
+ */
68
+ static _mapOperator(op) {
69
+ const maps = {
70
+ '$gt': '>', '$gte': '>=', '$lt': '<', '$lte': '<=',
71
+ '$ne': '!=', '$eq': '=', '$like': 'LIKE', '$in': 'IN'
72
+ };
73
+ return maps[op] || op;
74
+ }
75
+
76
+ /**
77
+ * Escapes values for safe inclusion in ToriDB queries.
78
+ * Handles strings, arrays, and objects (JSON).
79
+ * @param {any} val - The value to escape.
80
+ * @returns {string} The escaped and formatted value string.
81
+ * @private
82
+ */
83
+ static _escape(val) {
84
+ if (typeof val === 'string') {
85
+ // If it's a list for IN operator, don't wrap the whole thing in quotes
86
+ // but wrap individual elements if they are strings.
87
+ // However, the server's IN expects (val1,val2)
88
+ return `"${val.replace(/"/g, '\\"')}"`;
89
+ }
90
+ if (Array.isArray(val)) {
91
+ return `(${val.map(v => this._escape(v)).join(",")})`;
92
+ }
93
+ if (typeof val === 'object' && val !== null) return `"${JSON.stringify(val).replace(/"/g, '\\"')}"`;
94
+ return String(val);
95
+ }
5
96
  }
6
97
 
7
98
  /**
8
99
  * High-Performance RESP (REdis Serialization Protocol) Parser
9
100
  * Handles Simple Strings, Errors, Integers, Bulk Strings, and Nested Arrays.
10
- */
11
-
12
- var resp;
13
- var hasRequiredResp;
14
-
15
- function requireResp () {
16
- if (hasRequiredResp) return resp;
17
- hasRequiredResp = 1;
18
- class RespParser {
19
- constructor() {
20
- this.reset();
21
- }
22
-
23
- reset() {
24
- this.buffer = Buffer.alloc(0);
25
- }
26
-
27
- feed(data) {
28
- this.buffer = Buffer.concat([this.buffer, data]);
29
- }
30
-
31
- /**
32
- * Tries to parse the next complete RESP value from the buffer.
33
- * @returns {any | undefined} The parsed value or undefined if incomplete.
34
- */
35
- parseNext() {
36
- if (this.buffer.length === 0) return undefined;
37
-
38
- const result = this._decode(0);
39
- if (result) {
40
- this.buffer = this.buffer.slice(result.bytesRead);
41
- return result.value;
42
- }
43
- return undefined;
44
- }
45
-
46
- _decode(offset) {
47
- if (offset >= this.buffer.length) return null;
48
-
49
- const prefix = this.buffer[offset];
50
- const lineEnd = this.buffer.indexOf('\r\n', offset);
51
- if (lineEnd === -1) return null;
52
-
53
- const content = this.buffer.slice(offset + 1, lineEnd).toString();
54
- const bytesBeforeData = lineEnd - offset + 2;
55
-
56
- switch (prefix) {
57
- case 43: // '+' Simple String
58
- return { value: content, bytesRead: bytesBeforeData };
59
-
60
- case 45: // '-' Error
61
- return { value: new Error(content), bytesRead: bytesBeforeData };
62
-
63
- case 58: // ':' Integer
64
- return { value: parseInt(content, 10), bytesRead: bytesBeforeData };
65
-
66
- case 36: { // '$' Bulk String
67
- const length = parseInt(content, 10);
68
- if (length === -1) return { value: null, bytesRead: bytesBeforeData };
69
-
70
- const dataEnd = lineEnd + 2 + length;
71
- if (this.buffer.length < dataEnd + 2) return null;
72
-
73
- const value = this.buffer.slice(lineEnd + 2, dataEnd).toString();
74
- // Verify CRLF
75
- if (this.buffer[dataEnd] !== 13 || this.buffer[dataEnd + 1] !== 10) {
76
- throw new Error("Invalid Bulk String terminator");
77
- }
78
- return { value, bytesRead: bytesBeforeData + length + 2 };
79
- }
80
-
81
- case 42: { // '*' Array
82
- const count = parseInt(content, 10);
83
- if (count === -1) return { value: null, bytesRead: bytesBeforeData };
84
- if (count === 0) return { value: [], bytesRead: bytesBeforeData };
85
-
86
- let totalRead = bytesBeforeData;
87
- const items = [];
88
- for (let i = 0; i < count; i++) {
89
- const item = this._decode(offset + totalRead);
90
- if (!item) return null;
91
- items.push(item.value);
92
- totalRead += item.bytesRead;
93
- }
94
- return { value: items, bytesRead: totalRead };
95
- }
96
-
97
- default:
98
- // Attempt to handle unexpected data as a simple string line
99
- return { value: content, bytesRead: bytesBeforeData };
100
- }
101
- }
102
-
103
- /**
104
- * Serializes a command into a RESP Array.
105
- * @param {string[]} args
106
- */
107
- static encode(args) {
108
- let res = `*${args.length}\r\n`;
109
- for (const arg of args) {
110
- const s = String(arg);
111
- res += `$${Buffer.byteLength(s)}\r\n${s}\r\n`;
112
- }
113
- return Buffer.from(res);
114
- }
115
- }
116
-
117
- resp = { RespParser };
118
- return resp;
101
+ */
102
+ class RespParser {
103
+ /**
104
+ * Initializes or resets the parser state.
105
+ */
106
+ constructor() {
107
+ this.reset();
108
+ }
109
+
110
+ /**
111
+ * Clears the current data buffer.
112
+ */
113
+ reset() {
114
+ this.buffer = Buffer.alloc(0);
115
+ }
116
+
117
+ /**
118
+ * Feeds raw binary data into the parser's buffer.
119
+ * @param {Buffer} data - The raw data received from the socket.
120
+ */
121
+ feed(data) {
122
+ this.buffer = Buffer.concat([this.buffer, data]);
123
+ }
124
+
125
+ /**
126
+ * Tries to parse the next complete RESP value from the buffer.
127
+ * @returns {any | undefined} The parsed value or undefined if incomplete.
128
+ */
129
+ parseNext() {
130
+ if (this.buffer.length === 0) return undefined;
131
+
132
+ const result = this._decode(0);
133
+ if (result) {
134
+ this.buffer = this.buffer.slice(result.bytesRead);
135
+ return result.value;
136
+ }
137
+ return undefined;
138
+ }
139
+
140
+ /**
141
+ * Decodes a single RESP value from the buffer starting at a given offset.
142
+ * Internal recursive method for handling nested arrays and bulk strings.
143
+ * @param {number} offset - The buffer offset to start parsing from.
144
+ * @returns {Object|null} An object with {value, bytesRead} or null if incomplete.
145
+ * @private
146
+ */
147
+ _decode(offset) {
148
+ if (offset >= this.buffer.length) return null;
149
+
150
+ const prefix = this.buffer[offset];
151
+ const lineEnd = this.buffer.indexOf('\r\n', offset);
152
+ if (lineEnd === -1) return null;
153
+
154
+ const content = this.buffer.slice(offset + 1, lineEnd).toString();
155
+ const bytesBeforeData = lineEnd - offset + 2;
156
+
157
+ switch (prefix) {
158
+ case 43: // '+' Simple String
159
+ return { value: content, bytesRead: bytesBeforeData };
160
+
161
+ case 45: // '-' Error
162
+ return { value: new Error(content), bytesRead: bytesBeforeData };
163
+
164
+ case 58: // ':' Integer
165
+ return { value: parseInt(content, 10), bytesRead: bytesBeforeData };
166
+
167
+ case 36: { // '$' Bulk String
168
+ const length = parseInt(content, 10);
169
+ if (length === -1) return { value: null, bytesRead: bytesBeforeData };
170
+
171
+ const dataEnd = lineEnd + 2 + length;
172
+ if (this.buffer.length < dataEnd + 2) return null;
173
+
174
+ const value = this.buffer.slice(lineEnd + 2, dataEnd).toString();
175
+ // Verify CRLF
176
+ if (this.buffer[dataEnd] !== 13 || this.buffer[dataEnd + 1] !== 10) {
177
+ throw new Error("Invalid Bulk String terminator");
178
+ }
179
+ return { value, bytesRead: bytesBeforeData + length + 2 };
180
+ }
181
+
182
+ case 42: { // '*' Array
183
+ const count = parseInt(content, 10);
184
+ if (count === -1) return { value: null, bytesRead: bytesBeforeData };
185
+ if (count === 0) return { value: [], bytesRead: bytesBeforeData };
186
+
187
+ let totalRead = bytesBeforeData;
188
+ const items = [];
189
+ for (let i = 0; i < count; i++) {
190
+ const item = this._decode(offset + totalRead);
191
+ if (!item) return null;
192
+ items.push(item.value);
193
+ totalRead += item.bytesRead;
194
+ }
195
+ return { value: items, bytesRead: totalRead };
196
+ }
197
+
198
+ default:
199
+ // Attempt to handle unexpected data as a simple string line
200
+ return { value: content, bytesRead: bytesBeforeData };
201
+ }
202
+ }
203
+
204
+ /**
205
+ * Serializes a command into a RESP Array.
206
+ * @param {string[]} args
207
+ */
208
+ static encode(args) {
209
+ let res = `*${args.length}\r\n`;
210
+ for (const arg of args) {
211
+ const s = String(arg);
212
+ res += `$${Buffer.byteLength(s)}\r\n${s}\r\n`;
213
+ }
214
+ return Buffer.from(res);
215
+ }
119
216
  }
120
217
 
121
218
  /**
122
- * Command Compiler: Translates JS Objects and Types to ToriDB DSL
123
- */
124
-
125
- var compiler;
126
- var hasRequiredCompiler;
127
-
128
- function requireCompiler () {
129
- if (hasRequiredCompiler) return compiler;
130
- hasRequiredCompiler = 1;
131
- class Compiler {
132
- /**
133
- * Translates a JS Object to a ToriDB Filter string.
134
- * { age: { $gt: 18 }, status: 'active' } -> "age > 18 AND status = 'active'"
135
- */
136
- static compileFilter(filter) {
137
- if (!filter || typeof filter !== 'object') return "";
138
-
139
- const parts = [];
140
- for (const [key, value] of Object.entries(filter)) {
141
- if (key === '$or' && Array.isArray(value)) {
142
- parts.push(`(${value.map(f => this.compileFilter(f)).join(" OR ")})`);
143
- } else if (key === '$and' && Array.isArray(value)) {
144
- parts.push(`(${value.map(f => this.compileFilter(f)).join(" AND ")})`);
145
- } else if (typeof value === 'object' && value !== null) {
146
- // Handle operators: { $gt: 10 }
147
- for (const [op, val] of Object.entries(value)) {
148
- const symbol = this._mapOperator(op);
149
- parts.push(`${key} ${symbol} ${this._escape(val)}`);
150
- }
151
- } else {
152
- // Basic equality
153
- parts.push(`${key} = ${this._escape(value)}`);
154
- }
155
- }
156
- return parts.join(" AND ");
157
- }
158
-
159
- /**
160
- * Translates a Blueprint definition to ToriDB Table Definition format.
161
- * { id: { type: 'INT', primary: true }, user_id: { type: 'INT', references: 'users.id' } }
162
- * -> "id:INT:pk user_id:INT:fk(users.id)"
163
- */
164
- static compileBlueprint(name, blueprint) {
165
- const cols = [];
166
- for (const [colName, config] of Object.entries(blueprint)) {
167
- let type = typeof config === 'string' ? config : config.type;
168
- let pk = (config.primary || config.is_pk) ? ":pk" : "";
169
- let fk = "";
170
-
171
- if (config.references) {
172
- const [refTable, refCol] = config.references.split('.');
173
- fk = `:fk(${refTable}.${refCol})`;
174
- }
175
-
176
- // Auto-map JS types if not specified as SQL types
177
- if (type === 'Number') type = 'INT';
178
- if (type === 'String') type = 'STRING';
179
- if (type === 'Object') type = 'JSON';
180
- if (type === 'Boolean') type = 'BOOL';
181
-
182
- cols.push(`${colName}:${type}${pk}${fk}`);
183
- }
184
- return `CREATE TABLE ${name} ${cols.join(" ")}`;
185
- }
186
-
187
- static _mapOperator(op) {
188
- const maps = {
189
- '$gt': '>', '$gte': '>=', '$lt': '<', '$lte': '<=',
190
- '$ne': '!=', '$eq': '=', '$like': 'LIKE', '$in': 'IN'
191
- };
192
- return maps[op] || op;
193
- }
194
-
195
- static _escape(val) {
196
- if (typeof val === 'string') {
197
- // If it's a list for IN operator, don't wrap the whole thing in quotes
198
- // but wrap individual elements if they are strings.
199
- // However, the server's IN expects (val1,val2)
200
- return `"${val.replace(/"/g, '\\"')}"`;
201
- }
202
- if (Array.isArray(val)) {
203
- return `(${val.map(v => this._escape(v)).join(",")})`;
204
- }
205
- if (typeof val === 'object' && val !== null) return `"${JSON.stringify(val).replace(/"/g, '\\"')}"`;
206
- return String(val);
207
- }
208
- }
209
-
210
- compiler = { Compiler };
211
- return compiler;
212
- }
213
-
214
- var sdk$1;
215
- var hasRequiredSdk;
216
-
217
- function requireSdk () {
218
- if (hasRequiredSdk) return sdk$1;
219
- hasRequiredSdk = 1;
220
- const net = require$$0;
221
- const { RespParser } = requireResp();
222
- const { Compiler } = requireCompiler();
223
-
224
- /**
225
- * Main client class for interacting with ToriDB.
226
- * Supports Key-Value, NoSQL structures (List, Set, Hash, JSON), and Relational modeling.
227
- */
228
- class ToriDB {
229
- /**
230
- * Represents a database schema definition.
231
- */
232
- static Blueprint = class {
233
- /**
234
- * @param {Object} definition - The schema definition object.
235
- */
236
- constructor(definition) {
237
- this.definition = definition;
238
- }
239
- };
240
-
241
- /**
242
- * Creates a new ToriDB client instance.
243
- * @param {string} [uri="db://127.0.0.1:8569"] - Connection URI (e.g., db://user:pass@host:port/db)
244
- */
245
- constructor(uri = "db://127.0.0.1:8569") {
246
- this.host = "127.0.0.1";
247
- this.port = 8569;
248
- this.user = "default";
249
- this.password = null;
250
- this._parseUri(uri);
251
-
252
- this.parser = new RespParser();
253
- this.socket = new net.Socket();
254
- this.isConnected = false;
255
- this.pendingRequests = [];
256
-
257
- this._setupSocket();
258
-
259
- // Managers
260
- this.system = new SystemManager(this);
261
- }
262
-
263
- /**
264
- * Parses the connection URI.
265
- * @param {string} uri - The connection URI to parse.
266
- * @private
267
- */
268
- _parseUri(uri) {
269
- const regex = /^db:\/\/(?:(?:([^:]+):([^@+]+)[@+])?)([^:/\?]+)(?::(\d+))?(?:(?:\/([^?]+))?)?/;
270
- const match = uri.match(regex);
271
- if (match) {
272
- this.user = match[1] || "default";
273
- this.password = match[2] || null;
274
- this.host = match[3];
275
- this.port = parseInt(match[4], 10) || 8569;
276
- }
277
- }
278
-
279
- /**
280
- * Sets up socket events for data handling, errors, and closure.
281
- * @private
282
- */
283
- _setupSocket() {
284
- this.socket.on('data', (data) => {
285
- this.parser.feed(data);
286
- let response;
287
- while ((response = this.parser.parseNext()) !== undefined) {
288
- const req = this.pendingRequests.shift();
289
- if (req) {
290
- if (response instanceof Error) req.reject(response);
291
- else req.resolve(response);
292
- }
293
- }
294
- });
295
-
296
- this.socket.on('error', (err) => {
297
- this.isConnected = false;
298
- this.pendingRequests.forEach(req => req.reject(err));
299
- this.pendingRequests = [];
300
- });
301
-
302
- this.socket.on('close', () => {
303
- this.isConnected = false;
304
- });
305
- }
306
-
307
- /**
308
- * Establishes a connection to the ToriDB server.
309
- * @returns {Promise<void>}
310
- * @throws {Error} If connection or authentication fails.
311
- */
312
- async connect() {
313
- if (this.isConnected) return;
314
- return new Promise((resolve, reject) => {
315
- this.socket.connect(this.port, this.host, async () => {
316
- this.isConnected = true;
317
- if (this.password) {
318
- try {
319
- await this.execute("AUTH", this.user, this.password);
320
- } catch (e) {
321
- this.disconnect();
322
- return reject(new Error(`Authentication failed: ${e.message}`));
323
- }
324
- }
325
- resolve();
326
- });
327
- this.socket.once('error', reject);
328
- });
329
- }
330
-
331
- /**
332
- * Disconnects from the ToriDB server.
333
- */
334
- disconnect() {
335
- this.socket.destroy();
336
- this.isConnected = false;
337
- }
338
-
339
- /**
340
- * Executes a command on the ToriDB server.
341
- * @param {...any} args - Command arguments.
342
- * @returns {Promise<any>} The server response.
343
- */
344
- async execute(...args) {
345
- if (!this.isConnected) await this.connect();
346
- return new Promise((resolve, reject) => {
347
- this.pendingRequests.push({ resolve, reject });
348
- this.socket.write(RespParser.encode(args));
349
- });
350
- }
351
-
352
- /**
353
- * Executes a raw query string.
354
- * @param {string} s - The query string.
355
- * @returns {Promise<any>}
356
- */
357
- async query(s) {
358
- return this.execute(...s.match(/(?:[^\s"]+|"[^"]*")+/g).map(p => p.replace(/"/g, '')));
359
- }
360
-
361
- // --- Key-Value & TTL ---
362
- /**
363
- * Retrieves the value of a key.
364
- * @param {string} key - The key to get.
365
- * @returns {Promise<any>}
366
- */
367
- async get(key) { return this.execute("GET", key); }
368
-
369
- /**
370
- * Sets the value of a key.
371
- * @param {string} key - The key to set.
372
- * @param {any} val - The value to store (objects are JSON-stringified).
373
- * @returns {Promise<string>} OK on success.
374
- */
375
- async set(key, val) {
376
- const s = typeof val === 'object' ? JSON.stringify(val) : String(val);
377
- return this.execute("SET", key, s);
378
- }
379
- /**
380
- * Sets the value of a key with an expiration time.
381
- * @param {string} key - The key to set.
382
- * @param {any} val - The value to store.
383
- * @param {number} ttl - Time to live in seconds.
384
- * @returns {Promise<string>} OK on success.
385
- */
386
- async setEx(key, val, ttl) {
387
- const s = typeof val === 'object' ? JSON.stringify(val) : String(val);
388
- return this.execute("SETEX", key, s, String(ttl));
389
- }
390
- /**
391
- * Gets the remaining time to live of a key.
392
- * @param {string} key - The key.
393
- * @returns {Promise<number>} TTL in seconds, or -1 if no TTL, or -2 if key doesn't exist.
394
- */
395
- async ttl(key) { return this.execute("TTL", key); }
396
-
397
- /**
398
- * Increments the integer value of a key by one.
399
- * @param {string} key - The key to increment.
400
- * @returns {Promise<number>} The value after increment.
401
- */
402
- async incr(key) { return this.execute("INCR", key); }
403
-
404
- /**
405
- * Decrements the integer value of a key by one.
406
- * @param {string} key - The key to decrement.
407
- * @returns {Promise<number>} The value after decrement.
408
- */
409
- async decr(key) { return this.execute("DECR", key); }
410
-
411
- // --- NoSQL Structures ---
412
- /**
413
- * Accesses list operations for a given key.
414
- * @param {string} key - The list key.
415
- * @returns {Object} An object with list operations (push, rpush, pop, rpop, range).
416
- */
417
- list(key) {
418
- return {
419
- push: (...vals) => this.execute("LPUSH", key, ...vals.map(String)),
420
- rpush: (...vals) => this.execute("RPUSH", key, ...vals.map(String)),
421
- pop: (count = 1) => this.execute("LPOP", key, String(count)),
422
- rpop: (count = 1) => this.execute("RPOP", key, String(count)),
423
- range: (start, stop) => this.execute("LRANGE", key, String(start), String(stop))
424
- };
425
- }
426
-
427
- /**
428
- * Accesses set operations for a given key.
429
- * @param {string} key - The set key.
430
- * @returns {Object} An object with set operations (add, members).
431
- */
432
- setOf(key) {
433
- return {
434
- add: (...members) => this.execute("SADD", key, ...members.map(String)),
435
- members: () => this.execute("SMEMBERS", key)
436
- };
437
- }
438
-
439
- /**
440
- * Accesses hash operations for a given key.
441
- * @param {string} key - The hash key.
442
- * @returns {Object} An object with hash operations (set, get, all).
443
- */
444
- hash(key) {
445
- return {
446
- set: (field, val) => this.execute("HSET", key, field, String(val)),
447
- get: (field) => this.execute("HGET", key, field),
448
- all: () => this.execute("HGETALL", key)
449
- };
450
- }
451
-
452
- /**
453
- * Accesses sorted set operations for a given key.
454
- * @param {string} key - The sorted set key.
455
- * @returns {Object} An object with sorted set operations (add, range, score).
456
- */
457
- sortedSet(key) {
458
- return {
459
- add: (score, member) => this.execute("ZADD", key, String(score), member),
460
- range: (start, stop) => this.execute("ZRANGE", key, String(start), String(stop)),
461
- score: (member) => this.execute("ZSCORE", key, member)
462
- };
463
- }
464
-
465
- /**
466
- * Accesses JSON operations for a given key.
467
- * @param {string} key - The JSON key.
468
- * @returns {Object} An object with JSON operations (get, set).
469
- */
470
- json(key) {
471
- return {
472
- get: (path = "$") => this.execute("JSON.GET", key, path),
473
- set: (path, val) => {
474
- const s = typeof val === 'object' ? JSON.stringify(val) : JSON.stringify(val);
475
- return this.execute("JSON.SET", key, path, s);
476
- }
477
- };
478
- }
479
-
480
- // --- Relational (Models) ---
481
- /**
482
- * Defines or accesses a model for relational-like operations.
483
- * @param {string} name - The model name (table name).
484
- * @param {ToriDB.Blueprint} blueprint - The schema definition.
485
- * @returns {Object} An object with model operations (create, find, findById, update, delete, etc.).
486
- */
487
- model(name, blueprint) {
488
- const createCmd = Compiler.compileBlueprint(name, blueprint.definition);
489
- this.execute(...createCmd.match(/(?:[^\s"]+|"[^"]*")+/g).map(p => p.replace(/"/g, ''))).catch(() => { });
490
-
491
- return {
492
- create: (data) => this.execute("INSERT", name, ...Object.values(data).map(v => typeof v === 'object' ? JSON.stringify(v) : String(v))),
493
- find: (filter) => new QueryBuilder(this, name, filter),
494
- findById: (id) => new QueryBuilder(this, name, { id }).execute().then(r => r[0] || null),
495
- update: (filter, data) => {
496
- const where = Compiler.compileFilter(filter);
497
- const [col, val] = Object.entries(data)[0];
498
- const sVal = typeof val === 'object' ? JSON.stringify(val) : String(val);
499
- return this.execute("UPDATE", name, "SET", col, "=", sVal, "WHERE", where);
500
- },
501
- delete: (filter) => this.execute("DELETE", "FROM", name, "WHERE", Compiler.compileFilter(filter)),
502
- createIndex: (idxName, col) => this.execute("CREATE", "INDEX", idxName, "ON", name, `(${col})`),
503
- addColumn: (col, type) => this.execute("ALTER", "TABLE", name, "ADD", `${col}:${type}`),
504
- dropColumn: (col) => this.execute("ALTER", "TABLE", name, "DROP", col)
505
- };
506
- }
507
-
508
- /**
509
- * Accesses a table for query operations.
510
- * @param {string} name - The table name.
511
- * @returns {Object} An object with a find method.
512
- */
513
- table(name) {
514
- return { find: (filter) => new QueryBuilder(this, name, filter) };
515
- }
516
- }
517
-
518
- /**
519
- * Manager for system-level operations (ACL, Cluster, Replication, etc.).
520
- */
521
- class SystemManager {
522
- /**
523
- * @param {ToriDB} client - The ToriDB client instance.
524
- */
525
- constructor(client) { this.client = client; }
526
-
527
- /**
528
- * Retrieves server information and statistics.
529
- * @returns {Promise<string>}
530
- */
531
- async info() { return this.client.execute("INFO"); }
532
-
533
- /**
534
- * Synchronously saves the dataset to disk.
535
- * @returns {Promise<string>}
536
- */
537
- async save() { return this.client.execute("SAVE"); }
538
-
539
- /**
540
- * Triggers an AOF rewrite in the background.
541
- * @returns {Promise<string>}
542
- */
543
- async rewriteAof() { return this.client.execute("REWRITEAOF"); }
544
-
545
- /**
546
- * Accesses ACL (Access Control List) operations.
547
- * @returns {Object} An object with ACL operations (createUser, getUser, listUsers, deleteUser).
548
- */
549
- get acl() {
550
- return {
551
- createUser: (u, p, rules) => this.client.execute("ACL", "SETUSER", u, p, ...rules),
552
- getUser: (u) => this.client.execute("ACL", "GETUSER", u),
553
- listUsers: () => this.client.execute("ACL", "LIST"),
554
- deleteUser: (u) => this.client.execute("ACL", "DELUSER", u)
555
- };
556
- }
557
-
558
- /**
559
- * Accesses cluster management operations.
560
- * @returns {Object} An object with cluster operations (meet, slots, info).
561
- */
562
- get cluster() {
563
- return {
564
- meet: (h, p) => this.client.execute("CLUSTER", "MEET", h, String(p)),
565
- slots: () => this.client.execute("CLUSTER", "SLOTS"),
566
- info: () => this.client.execute("CLUSTER", "INFO")
567
- };
568
- }
569
-
570
- /**
571
- * Accesses replication management operations.
572
- * @returns {Object} An object with replication operations (slaveOf, stop).
573
- */
574
- get replication() {
575
- return {
576
- slaveOf: (h, p) => this.client.execute("REPLICAOF", h, String(p)),
577
- stop: () => this.client.execute("REPLICAOF", "NO", "ONE")
578
- };
579
- }
580
-
581
- /**
582
- * Accesses client management operations.
583
- * @returns {Object} An object with client operations (list, kill).
584
- */
585
- get clients() {
586
- return {
587
- list: () => this.client.execute("CLIENT", "LIST"),
588
- kill: (addr) => this.client.execute("CLIENT", "KILL", addr)
589
- };
590
- }
591
- }
592
-
593
- /**
594
- * Helper class for building and executing SQL-like queries.
595
- */
596
- class QueryBuilder {
597
- /**
598
- * @param {ToriDB} client - The ToriDB client instance.
599
- * @param {string} target - The table or model name.
600
- * @param {Object} [filter={}] - Initial filter object.
601
- */
602
- constructor(client, target, filter = {}) {
603
- this.client = client;
604
- this.target = target;
605
- this.params = {
606
- filter: Compiler.compileFilter(filter),
607
- limit: null,
608
- offset: null,
609
- orderBy: null,
610
- groupBy: null,
611
- select: "*"
612
- };
613
- }
614
-
615
- /**
616
- * Specifies the fields to select.
617
- * @param {string|string[]|Object} fields - Fields to select.
618
- * @returns {QueryBuilder} This instance for chaining.
619
- */
620
- select(fields) {
621
- if (typeof fields === 'string') this.params.select = fields;
622
- else if (Array.isArray(fields)) this.params.select = fields.join(", ");
623
- else if (typeof fields === 'object') {
624
- this.params.select = Object.entries(fields).map(([k, v]) => `${v} AS ${k}`).join(", ");
625
- }
626
- return this;
627
- }
628
-
629
- /**
630
- * Limits the number of results.
631
- * @param {number} n - The limit count.
632
- * @returns {QueryBuilder} This instance for chaining.
633
- */
634
- limit(n) { this.params.limit = n; return this; }
635
-
636
- /**
637
- * Offsets the results.
638
- * @param {number} n - The offset count.
639
- * @returns {QueryBuilder} This instance for chaining.
640
- */
641
- offset(n) { this.params.offset = n; return this; }
642
-
643
- /**
644
- * Orders the results by a column.
645
- * @param {string} col - The column name.
646
- * @param {string} [dir="ASC"] - Sort direction (ASC or DESC).
647
- * @returns {QueryBuilder} This instance for chaining.
648
- */
649
- orderBy(col, dir = "ASC") { this.params.orderBy = `${col} ${dir.toUpperCase()}`; return this; }
650
-
651
- /**
652
- * Groups the results by columns.
653
- * @param {string|string[]} cols - The columns to group by.
654
- * @returns {QueryBuilder} This instance for chaining.
655
- */
656
- groupBy(cols) { this.params.groupBy = Array.isArray(cols) ? cols.join(", ") : cols; return this; }
657
-
658
- /**
659
- * Compiles and executes the query.
660
- * @returns {Promise<any[]>} The query results.
661
- */
662
- async execute() {
663
- const args = ["SELECT", this.params.select, "FROM", this.target];
664
- if (this.params.filter) { args.push("WHERE"); args.push(this.params.filter); }
665
- if (this.params.groupBy) { args.push("GROUP"); args.push("BY"); args.push(this.params.groupBy); }
666
- if (this.params.orderBy) { args.push("ORDER"); args.push("BY"); args.push(this.params.orderBy); }
667
- if (this.params.limit) { args.push("LIMIT"); args.push(String(this.params.limit)); }
668
- if (this.params.offset) { args.push("OFFSET"); args.push(String(this.params.offset)); }
669
-
670
- return this.client.execute(...args);
671
- }
672
- }
673
-
674
- sdk$1 = { ToriDB };
675
- return sdk$1;
219
+ * Main client class for interacting with ToriDB.
220
+ * Supports Key-Value, NoSQL structures (List, Set, Hash, JSON), and Relational modeling.
221
+ */
222
+ class ToriDB {
223
+ /**
224
+ * Represents a database schema definition.
225
+ */
226
+ static Blueprint = class {
227
+ /**
228
+ * @param {Object} definition - The schema definition object.
229
+ */
230
+ constructor(definition) {
231
+ this.definition = definition;
232
+ }
233
+ };
234
+
235
+ /**
236
+ * Creates a new ToriDB client instance.
237
+ * @param {string} [uri="db://127.0.0.1:8569"] - Connection URI (e.g., db://user:pass@host:port/db)
238
+ */
239
+ constructor(uri = "db://127.0.0.1:8569") {
240
+ this.host = "127.0.0.1";
241
+ this.port = 8569;
242
+ this.user = "default";
243
+ this.password = null;
244
+ this._parseUri(uri);
245
+
246
+ this.parser = new RespParser();
247
+ this.socket = new net.Socket();
248
+ this.isConnected = false;
249
+ this.pendingRequests = [];
250
+
251
+ this._setupSocket();
252
+
253
+ // Managers
254
+ this.system = new SystemManager(this);
255
+ }
256
+
257
+ /**
258
+ * Parses the connection URI.
259
+ * @param {string} uri - The connection URI to parse.
260
+ * @private
261
+ */
262
+ _parseUri(uri) {
263
+ const regex = /^db:\/\/(?:(?:([^:]+):([^@+]+)[@+])?)([^:/\?]+)(?::(\d+))?(?:(?:\/([^?]+))?)?/;
264
+ const match = uri.match(regex);
265
+ if (match) {
266
+ this.user = match[1] || "default";
267
+ this.password = match[2] || null;
268
+ this.host = match[3];
269
+ this.port = parseInt(match[4], 10) || 8569;
270
+ }
271
+ }
272
+
273
+ /**
274
+ * Sets up socket events for data handling, errors, and closure.
275
+ * @private
276
+ */
277
+ _setupSocket() {
278
+ this.socket.on('data', (data) => {
279
+ this.parser.feed(data);
280
+ let response;
281
+ while ((response = this.parser.parseNext()) !== undefined) {
282
+ const req = this.pendingRequests.shift();
283
+ if (req) {
284
+ if (response instanceof Error) req.reject(response);
285
+ else req.resolve(response);
286
+ }
287
+ }
288
+ });
289
+
290
+ this.socket.on('error', (err) => {
291
+ this.isConnected = false;
292
+ this.pendingRequests.forEach(req => req.reject(err));
293
+ this.pendingRequests = [];
294
+ });
295
+
296
+ this.socket.on('close', () => {
297
+ this.isConnected = false;
298
+ });
299
+ }
300
+
301
+ /**
302
+ * Establishes a connection to the ToriDB server.
303
+ * @returns {Promise<void>}
304
+ * @throws {Error} If connection or authentication fails.
305
+ */
306
+ async connect() {
307
+ if (this.isConnected) return;
308
+ return new Promise((resolve, reject) => {
309
+ this.socket.connect(this.port, this.host, async () => {
310
+ this.isConnected = true;
311
+ if (this.password) {
312
+ try {
313
+ await this.execute("AUTH", this.user, this.password);
314
+ } catch (e) {
315
+ this.disconnect();
316
+ return reject(new Error(`Authentication failed: ${e.message}`));
317
+ }
318
+ }
319
+ resolve();
320
+ });
321
+ this.socket.once('error', reject);
322
+ });
323
+ }
324
+
325
+ /**
326
+ * Disconnects from the ToriDB server.
327
+ */
328
+ disconnect() {
329
+ this.socket.destroy();
330
+ this.isConnected = false;
331
+ }
332
+
333
+ /**
334
+ * Executes a command on the ToriDB server.
335
+ * @param {...any} args - Command arguments.
336
+ * @returns {Promise<any>} The server response.
337
+ */
338
+ async execute(...args) {
339
+ if (!this.isConnected) await this.connect();
340
+ return new Promise((resolve, reject) => {
341
+ this.pendingRequests.push({ resolve, reject });
342
+ this.socket.write(RespParser.encode(args));
343
+ });
344
+ }
345
+
346
+ /**
347
+ * Executes a raw query string.
348
+ * @param {string} s - The query string.
349
+ * @returns {Promise<any>}
350
+ */
351
+ async query(s) {
352
+ return this.execute(...s.match(/(?:[^\s"]+|"[^"]*")+/g).map(p => p.replace(/"/g, '')));
353
+ }
354
+
355
+ // --- Key-Value & TTL ---
356
+ /**
357
+ * Retrieves the value of a key.
358
+ * @param {string} key - The key to get.
359
+ * @returns {Promise<any>}
360
+ */
361
+ async get(key) { return this.execute("GET", key); }
362
+
363
+ /**
364
+ * Sets the value of a key.
365
+ * @param {string} key - The key to set.
366
+ * @param {any} val - The value to store (objects are JSON-stringified).
367
+ * @returns {Promise<string>} OK on success.
368
+ */
369
+ async set(key, val) {
370
+ const s = typeof val === 'object' ? JSON.stringify(val) : String(val);
371
+ return this.execute("SET", key, s);
372
+ }
373
+ /**
374
+ * Sets the value of a key with an expiration time.
375
+ * @param {string} key - The key to set.
376
+ * @param {any} val - The value to store.
377
+ * @param {number} ttl - Time to live in seconds.
378
+ * @returns {Promise<string>} OK on success.
379
+ */
380
+ async setEx(key, val, ttl) {
381
+ const s = typeof val === 'object' ? JSON.stringify(val) : String(val);
382
+ return this.execute("SETEX", key, s, String(ttl));
383
+ }
384
+ /**
385
+ * Gets the remaining time to live of a key.
386
+ * @param {string} key - The key.
387
+ * @returns {Promise<number>} TTL in seconds, or -1 if no TTL, or -2 if key doesn't exist.
388
+ */
389
+ async ttl(key) { return this.execute("TTL", key); }
390
+
391
+ /**
392
+ * Increments the integer value of a key by one.
393
+ * @param {string} key - The key to increment.
394
+ * @returns {Promise<number>} The value after increment.
395
+ */
396
+ async incr(key) { return this.execute("INCR", key); }
397
+
398
+ /**
399
+ * Decrements the integer value of a key by one.
400
+ * @param {string} key - The key to decrement.
401
+ * @returns {Promise<number>} The value after decrement.
402
+ */
403
+ async decr(key) { return this.execute("DECR", key); }
404
+
405
+ // --- NoSQL Structures ---
406
+ /**
407
+ * Accesses list operations for a given key.
408
+ * @param {string} key - The list key.
409
+ * @returns {Object} An object with list operations (push, rpush, pop, rpop, range).
410
+ */
411
+ list(key) {
412
+ return {
413
+ push: (...vals) => this.execute("LPUSH", key, ...vals.map(String)),
414
+ rpush: (...vals) => this.execute("RPUSH", key, ...vals.map(String)),
415
+ pop: (count = 1) => this.execute("LPOP", key, String(count)),
416
+ rpop: (count = 1) => this.execute("RPOP", key, String(count)),
417
+ range: (start, stop) => this.execute("LRANGE", key, String(start), String(stop))
418
+ };
419
+ }
420
+
421
+ /**
422
+ * Accesses set operations for a given key.
423
+ * @param {string} key - The set key.
424
+ * @returns {Object} An object with set operations (add, members).
425
+ */
426
+ setOf(key) {
427
+ return {
428
+ add: (...members) => this.execute("SADD", key, ...members.map(String)),
429
+ members: () => this.execute("SMEMBERS", key)
430
+ };
431
+ }
432
+
433
+ /**
434
+ * Accesses hash operations for a given key.
435
+ * @param {string} key - The hash key.
436
+ * @returns {Object} An object with hash operations (set, get, all).
437
+ */
438
+ hash(key) {
439
+ return {
440
+ set: (field, val) => this.execute("HSET", key, field, String(val)),
441
+ get: (field) => this.execute("HGET", key, field),
442
+ all: () => this.execute("HGETALL", key)
443
+ };
444
+ }
445
+
446
+ /**
447
+ * Accesses sorted set operations for a given key.
448
+ * @param {string} key - The sorted set key.
449
+ * @returns {Object} An object with sorted set operations (add, range, score).
450
+ */
451
+ sortedSet(key) {
452
+ return {
453
+ add: (score, member) => this.execute("ZADD", key, String(score), member),
454
+ range: (start, stop) => this.execute("ZRANGE", key, String(start), String(stop)),
455
+ score: (member) => this.execute("ZSCORE", key, member)
456
+ };
457
+ }
458
+
459
+ /**
460
+ * Accesses JSON operations for a given key.
461
+ * @param {string} key - The JSON key.
462
+ * @returns {Object} An object with JSON operations (get, set).
463
+ */
464
+ json(key) {
465
+ return {
466
+ get: (path = "$") => this.execute("JSON.GET", key, path),
467
+ set: (path, val) => {
468
+ const s = typeof val === 'object' ? JSON.stringify(val) : JSON.stringify(val);
469
+ return this.execute("JSON.SET", key, path, s);
470
+ }
471
+ };
472
+ }
473
+
474
+ // --- Relational (Models) ---
475
+ /**
476
+ * Defines or accesses a model for relational-like operations.
477
+ * @param {string} name - The model name (table name).
478
+ * @param {ToriDB.Blueprint} blueprint - The schema definition.
479
+ * @returns {Object} An object with model operations (create, find, findById, update, delete, etc.).
480
+ */
481
+ model(name, blueprint) {
482
+ const createCmd = Compiler.compileBlueprint(name, blueprint.definition);
483
+ this.execute(...createCmd.match(/(?:[^\s"]+|"[^"]*")+/g).map(p => p.replace(/"/g, ''))).catch(() => { });
484
+
485
+ return {
486
+ create: (data) => this.execute("INSERT", name, ...Object.values(data).map(v => typeof v === 'object' ? JSON.stringify(v) : String(v))),
487
+ find: (filter) => new QueryBuilder(this, name, filter),
488
+ findById: (id) => new QueryBuilder(this, name, { id }).execute().then(r => r[0] || null),
489
+ update: (filter, data) => {
490
+ const where = Compiler.compileFilter(filter);
491
+ const [col, val] = Object.entries(data)[0];
492
+ const sVal = typeof val === 'object' ? JSON.stringify(val) : String(val);
493
+ return this.execute("UPDATE", name, "SET", col, "=", sVal, "WHERE", where);
494
+ },
495
+ delete: (filter) => this.execute("DELETE", "FROM", name, "WHERE", Compiler.compileFilter(filter)),
496
+ createIndex: (idxName, col) => this.execute("CREATE", "INDEX", idxName, "ON", name, `(${col})`),
497
+ addColumn: (col, type) => this.execute("ALTER", "TABLE", name, "ADD", `${col}:${type}`),
498
+ dropColumn: (col) => this.execute("ALTER", "TABLE", name, "DROP", col)
499
+ };
500
+ }
501
+
502
+ /**
503
+ * Accesses a table for query operations.
504
+ * @param {string} name - The table name.
505
+ * @returns {Object} An object with a find method.
506
+ */
507
+ table(name) {
508
+ return { find: (filter) => new QueryBuilder(this, name, filter) };
509
+ }
510
+ }
511
+
512
+ /**
513
+ * Manager for system-level operations (ACL, Cluster, Replication, etc.).
514
+ */
515
+ class SystemManager {
516
+ /**
517
+ * @param {ToriDB} client - The ToriDB client instance.
518
+ */
519
+ constructor(client) { this.client = client; }
520
+
521
+ /**
522
+ * Retrieves server information and statistics.
523
+ * @returns {Promise<string>}
524
+ */
525
+ async info() { return this.client.execute("INFO"); }
526
+
527
+ /**
528
+ * Synchronously saves the dataset to disk.
529
+ * @returns {Promise<string>}
530
+ */
531
+ async save() { return this.client.execute("SAVE"); }
532
+
533
+ /**
534
+ * Triggers an AOF rewrite in the background.
535
+ * @returns {Promise<string>}
536
+ */
537
+ async rewriteAof() { return this.client.execute("REWRITEAOF"); }
538
+
539
+ /**
540
+ * Accesses ACL (Access Control List) operations.
541
+ * @returns {Object} An object with ACL operations (createUser, getUser, listUsers, deleteUser).
542
+ */
543
+ get acl() {
544
+ return {
545
+ createUser: (u, p, rules) => this.client.execute("ACL", "SETUSER", u, p, ...rules),
546
+ getUser: (u) => this.client.execute("ACL", "GETUSER", u),
547
+ listUsers: () => this.client.execute("ACL", "LIST"),
548
+ deleteUser: (u) => this.client.execute("ACL", "DELUSER", u)
549
+ };
550
+ }
551
+
552
+ /**
553
+ * Accesses cluster management operations.
554
+ * @returns {Object} An object with cluster operations (meet, slots, info).
555
+ */
556
+ get cluster() {
557
+ return {
558
+ meet: (h, p) => this.client.execute("CLUSTER", "MEET", h, String(p)),
559
+ slots: () => this.client.execute("CLUSTER", "SLOTS"),
560
+ info: () => this.client.execute("CLUSTER", "INFO")
561
+ };
562
+ }
563
+
564
+ /**
565
+ * Accesses replication management operations.
566
+ * @returns {Object} An object with replication operations (slaveOf, stop).
567
+ */
568
+ get replication() {
569
+ return {
570
+ slaveOf: (h, p) => this.client.execute("REPLICAOF", h, String(p)),
571
+ stop: () => this.client.execute("REPLICAOF", "NO", "ONE")
572
+ };
573
+ }
574
+
575
+ /**
576
+ * Accesses client management operations.
577
+ * @returns {Object} An object with client operations (list, kill).
578
+ */
579
+ get clients() {
580
+ return {
581
+ list: () => this.client.execute("CLIENT", "LIST"),
582
+ kill: (addr) => this.client.execute("CLIENT", "KILL", addr)
583
+ };
584
+ }
585
+ }
586
+
587
+ /**
588
+ * Helper class for building and executing SQL-like queries.
589
+ */
590
+ class QueryBuilder {
591
+ /**
592
+ * @param {ToriDB} client - The ToriDB client instance.
593
+ * @param {string} target - The table or model name.
594
+ * @param {Object} [filter={}] - Initial filter object.
595
+ */
596
+ constructor(client, target, filter = {}) {
597
+ this.client = client;
598
+ this.target = target;
599
+ this.params = {
600
+ filter: Compiler.compileFilter(filter),
601
+ limit: null,
602
+ offset: null,
603
+ orderBy: null,
604
+ groupBy: null,
605
+ select: "*"
606
+ };
607
+ }
608
+
609
+ /**
610
+ * Specifies the fields to select.
611
+ * @param {string|string[]|Object} fields - Fields to select.
612
+ * @returns {QueryBuilder} This instance for chaining.
613
+ */
614
+ select(fields) {
615
+ if (typeof fields === 'string') this.params.select = fields;
616
+ else if (Array.isArray(fields)) this.params.select = fields.join(", ");
617
+ else if (typeof fields === 'object') {
618
+ this.params.select = Object.entries(fields).map(([k, v]) => `${v} AS ${k}`).join(", ");
619
+ }
620
+ return this;
621
+ }
622
+
623
+ /**
624
+ * Limits the number of results.
625
+ * @param {number} n - The limit count.
626
+ * @returns {QueryBuilder} This instance for chaining.
627
+ */
628
+ limit(n) { this.params.limit = n; return this; }
629
+
630
+ /**
631
+ * Offsets the results.
632
+ * @param {number} n - The offset count.
633
+ * @returns {QueryBuilder} This instance for chaining.
634
+ */
635
+ offset(n) { this.params.offset = n; return this; }
636
+
637
+ /**
638
+ * Orders the results by a column.
639
+ * @param {string} col - The column name.
640
+ * @param {string} [dir="ASC"] - Sort direction (ASC or DESC).
641
+ * @returns {QueryBuilder} This instance for chaining.
642
+ */
643
+ orderBy(col, dir = "ASC") { this.params.orderBy = `${col} ${dir.toUpperCase()}`; return this; }
644
+
645
+ /**
646
+ * Groups the results by columns.
647
+ * @param {string|string[]} cols - The columns to group by.
648
+ * @returns {QueryBuilder} This instance for chaining.
649
+ */
650
+ groupBy(cols) { this.params.groupBy = Array.isArray(cols) ? cols.join(", ") : cols; return this; }
651
+
652
+ /**
653
+ * Compiles and executes the query.
654
+ * @returns {Promise<any[]>} The query results.
655
+ */
656
+ async execute() {
657
+ const args = ["SELECT", this.params.select, "FROM", this.target];
658
+ if (this.params.filter) { args.push("WHERE"); args.push(this.params.filter); }
659
+ if (this.params.groupBy) { args.push("GROUP"); args.push("BY"); args.push(this.params.groupBy); }
660
+ if (this.params.orderBy) { args.push("ORDER"); args.push("BY"); args.push(this.params.orderBy); }
661
+ if (this.params.limit) { args.push("LIMIT"); args.push(String(this.params.limit)); }
662
+ if (this.params.offset) { args.push("OFFSET"); args.push(String(this.params.offset)); }
663
+
664
+ return this.client.execute(...args);
665
+ }
676
666
  }
677
667
 
678
- var sdkExports = requireSdk();
679
- var sdk = /*@__PURE__*/getDefaultExportFromCjs(sdkExports);
680
-
681
- export { sdk as default };
668
+ export { ToriDB };
682
669
  //# sourceMappingURL=toridb.esm.js.map