@dbcube/core 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +61 -106
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +2 -34
- package/dist/index.d.ts +2 -34
- package/dist/index.js +61 -106
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -346,7 +346,7 @@ var Engine = class {
|
|
|
346
346
|
};
|
|
347
347
|
|
|
348
348
|
// src/lib/DbConfig.ts
|
|
349
|
-
import
|
|
349
|
+
import Database from "better-sqlite3";
|
|
350
350
|
import * as path2 from "path";
|
|
351
351
|
import fs from "fs";
|
|
352
352
|
var rootPath = path2.resolve(process.cwd(), "dbcube");
|
|
@@ -367,95 +367,63 @@ var SQLite = class {
|
|
|
367
367
|
return false;
|
|
368
368
|
}
|
|
369
369
|
async connect() {
|
|
370
|
-
|
|
370
|
+
return new Promise((resolve2, reject) => {
|
|
371
371
|
try {
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
}
|
|
372
|
+
if (!this.db) {
|
|
373
|
+
const dbPath = this.database || ":memory:";
|
|
374
|
+
const configPath = path2.join(rootPath, dbPath + ".db");
|
|
375
|
+
this.db = new Database(configPath);
|
|
376
|
+
}
|
|
377
|
+
resolve2(this.db);
|
|
377
378
|
} catch (error) {
|
|
378
|
-
|
|
379
|
+
reject(error);
|
|
379
380
|
}
|
|
380
|
-
}
|
|
381
|
-
return this.db;
|
|
381
|
+
});
|
|
382
382
|
}
|
|
383
383
|
async disconnect() {
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
this.db.close(
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
resolve2();
|
|
392
|
-
});
|
|
393
|
-
});
|
|
394
|
-
}
|
|
384
|
+
return new Promise((resolve2) => {
|
|
385
|
+
if (this.db) {
|
|
386
|
+
this.db.close();
|
|
387
|
+
this.db = null;
|
|
388
|
+
}
|
|
389
|
+
resolve2();
|
|
390
|
+
});
|
|
395
391
|
}
|
|
396
|
-
/**
|
|
397
|
-
* Executes a SQL query on the currently set database.
|
|
398
|
-
*
|
|
399
|
-
* @param {string} sqlQuery - The SQL query to execute.
|
|
400
|
-
* @returns {Promise<QueryResult>} - Returns a JSON object with the status, message, and data (if any).
|
|
401
|
-
*
|
|
402
|
-
* @example
|
|
403
|
-
* const result = await db.query('SELECT * FROM users;');
|
|
404
|
-
* console.log(result);
|
|
405
|
-
* // { status: 'success', message: 'Query executed successfully', data: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }] }
|
|
406
|
-
*
|
|
407
|
-
* @example
|
|
408
|
-
* const result = await db.query('INVALID SQL QUERY;');
|
|
409
|
-
* console.log(result);
|
|
410
|
-
* // { status: 'error', message: 'SQL syntax error', data: null }
|
|
411
|
-
*/
|
|
412
392
|
async query(sqlQuery) {
|
|
413
|
-
|
|
414
|
-
throw new Error("The SQL query must be a string.");
|
|
415
|
-
}
|
|
416
|
-
const sqlCommands = sqlQuery.split(";").filter((cmd) => cmd.trim().length > 0);
|
|
417
|
-
return new Promise((resolve2) => {
|
|
393
|
+
return new Promise(async (resolve2) => {
|
|
418
394
|
try {
|
|
395
|
+
if (typeof sqlQuery !== "string") {
|
|
396
|
+
throw new Error("The SQL query must be a string.");
|
|
397
|
+
}
|
|
398
|
+
if (!this.db) {
|
|
399
|
+
await this.connect();
|
|
400
|
+
}
|
|
419
401
|
if (!this.db) {
|
|
420
402
|
throw new Error("Database connection is not available.");
|
|
421
403
|
}
|
|
404
|
+
const sqlCommands = sqlQuery.split(";").filter((cmd) => cmd.trim().length > 0);
|
|
422
405
|
const results = [];
|
|
423
|
-
let commandsProcessed = 0;
|
|
424
406
|
for (const command of sqlCommands) {
|
|
425
407
|
const query = `${command};`;
|
|
426
408
|
const isSelect = query.trim().toLowerCase().startsWith("select");
|
|
427
409
|
if (isSelect) {
|
|
428
|
-
this.db.
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
}
|
|
432
|
-
results.push(rows);
|
|
433
|
-
commandsProcessed++;
|
|
434
|
-
if (commandsProcessed === sqlCommands.length) {
|
|
435
|
-
resolve2({
|
|
436
|
-
status: "success",
|
|
437
|
-
message: "Query executed successfully",
|
|
438
|
-
data: results.length === 1 ? results[0] : results
|
|
439
|
-
});
|
|
440
|
-
}
|
|
441
|
-
});
|
|
410
|
+
const stmt = this.db.prepare(query);
|
|
411
|
+
const rows = stmt.all();
|
|
412
|
+
results.push(rows);
|
|
442
413
|
} else {
|
|
443
|
-
this.db.
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
commandsProcessed++;
|
|
449
|
-
if (commandsProcessed === sqlCommands.length) {
|
|
450
|
-
resolve2({
|
|
451
|
-
status: "success",
|
|
452
|
-
message: "Query executed successfully",
|
|
453
|
-
data: results.length === 1 ? results[0] : results
|
|
454
|
-
});
|
|
455
|
-
}
|
|
414
|
+
const stmt = this.db.prepare(query);
|
|
415
|
+
const result = stmt.run();
|
|
416
|
+
results.push({
|
|
417
|
+
changes: result.changes,
|
|
418
|
+
lastID: result.lastInsertRowid
|
|
456
419
|
});
|
|
457
420
|
}
|
|
458
421
|
}
|
|
422
|
+
resolve2({
|
|
423
|
+
status: "success",
|
|
424
|
+
message: "Query executed successfully",
|
|
425
|
+
data: results.length === 1 ? results[0] : results
|
|
426
|
+
});
|
|
459
427
|
} catch (error) {
|
|
460
428
|
resolve2({
|
|
461
429
|
status: "error",
|
|
@@ -465,52 +433,39 @@ var SQLite = class {
|
|
|
465
433
|
}
|
|
466
434
|
});
|
|
467
435
|
}
|
|
468
|
-
/**
|
|
469
|
-
* Executes a SQL query with parameters on the currently set database.
|
|
470
|
-
*
|
|
471
|
-
* @param {string} sqlQuery - The SQL query to execute with placeholders (?).
|
|
472
|
-
* @param {any[]} params - Array of parameters to bind to the query placeholders.
|
|
473
|
-
* @returns {Promise<QueryResult>} - Returns a JSON object with the status, message, and data (if any).
|
|
474
|
-
*
|
|
475
|
-
* @example
|
|
476
|
-
* const result = await db.queryWithParameters('INSERT INTO users (name, email) VALUES (?, ?)', ['John', 'john@example.com']);
|
|
477
|
-
* console.log(result);
|
|
478
|
-
* // { status: 'success', message: 'Query executed successfully', data: { changes: 1, lastID: 3 } }
|
|
479
|
-
*/
|
|
480
436
|
async queryWithParameters(sqlQuery, params = []) {
|
|
481
|
-
|
|
482
|
-
throw new Error("The SQL query must be a string.");
|
|
483
|
-
}
|
|
484
|
-
if (!Array.isArray(params)) {
|
|
485
|
-
throw new Error("Parameters must be an array.");
|
|
486
|
-
}
|
|
487
|
-
return new Promise((resolve2) => {
|
|
437
|
+
return new Promise(async (resolve2) => {
|
|
488
438
|
try {
|
|
439
|
+
if (typeof sqlQuery !== "string") {
|
|
440
|
+
throw new Error("The SQL query must be a string.");
|
|
441
|
+
}
|
|
442
|
+
if (!Array.isArray(params)) {
|
|
443
|
+
throw new Error("Parameters must be an array.");
|
|
444
|
+
}
|
|
445
|
+
if (!this.db) {
|
|
446
|
+
await this.connect();
|
|
447
|
+
}
|
|
489
448
|
if (!this.db) {
|
|
490
449
|
throw new Error("Database connection is not available.");
|
|
491
450
|
}
|
|
492
451
|
const isSelect = sqlQuery.trim().toLowerCase().startsWith("select");
|
|
452
|
+
const stmt = this.db.prepare(sqlQuery);
|
|
493
453
|
if (isSelect) {
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
status: "success",
|
|
500
|
-
message: "Query executed successfully",
|
|
501
|
-
data: rows
|
|
502
|
-
});
|
|
454
|
+
const rows = stmt.all(...params);
|
|
455
|
+
resolve2({
|
|
456
|
+
status: "success",
|
|
457
|
+
message: "Query executed successfully",
|
|
458
|
+
data: rows
|
|
503
459
|
});
|
|
504
460
|
} else {
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
461
|
+
const result = stmt.run(...params);
|
|
462
|
+
resolve2({
|
|
463
|
+
status: "success",
|
|
464
|
+
message: "Query executed successfully",
|
|
465
|
+
data: {
|
|
466
|
+
changes: result.changes,
|
|
467
|
+
lastID: result.lastInsertRowid
|
|
508
468
|
}
|
|
509
|
-
resolve2({
|
|
510
|
-
status: "success",
|
|
511
|
-
message: "Query executed successfully",
|
|
512
|
-
data: { changes: this.changes, lastID: this.lastID }
|
|
513
|
-
});
|
|
514
469
|
});
|
|
515
470
|
}
|
|
516
471
|
} catch (error) {
|