@dbcube/core 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.
package/dist/index.cjs CHANGED
@@ -383,7 +383,7 @@ var Engine = class {
383
383
  };
384
384
 
385
385
  // src/lib/DbConfig.ts
386
- var sqlite3 = __toESM(require("sqlite3"));
386
+ var import_better_sqlite3 = __toESM(require("better-sqlite3"));
387
387
  var path2 = __toESM(require("path"));
388
388
  var import_fs = __toESM(require("fs"));
389
389
  var rootPath = path2.resolve(process.cwd(), "dbcube");
@@ -404,95 +404,63 @@ var SQLite = class {
404
404
  return false;
405
405
  }
406
406
  async connect() {
407
- if (!this.db) {
407
+ return new Promise((resolve2, reject) => {
408
408
  try {
409
- const dbPath = this.database || ":memory:";
410
- const configPath = path2.join(rootPath, dbPath + ".db");
411
- this.db = new sqlite3.Database(configPath, (err) => {
412
- if (err) throw err;
413
- });
409
+ if (!this.db) {
410
+ const dbPath = this.database || ":memory:";
411
+ const configPath = path2.join(rootPath, dbPath + ".db");
412
+ this.db = new import_better_sqlite3.default(configPath);
413
+ }
414
+ resolve2(this.db);
414
415
  } catch (error) {
415
- throw error;
416
+ reject(error);
416
417
  }
417
- }
418
- return this.db;
418
+ });
419
419
  }
420
420
  async disconnect() {
421
- if (this.db) {
422
- return new Promise((resolve2, reject) => {
423
- this.db.close((err) => {
424
- if (err) {
425
- return reject(err);
426
- }
427
- this.db = null;
428
- resolve2();
429
- });
430
- });
431
- }
421
+ return new Promise((resolve2) => {
422
+ if (this.db) {
423
+ this.db.close();
424
+ this.db = null;
425
+ }
426
+ resolve2();
427
+ });
432
428
  }
433
- /**
434
- * Executes a SQL query on the currently set database.
435
- *
436
- * @param {string} sqlQuery - The SQL query to execute.
437
- * @returns {Promise<QueryResult>} - Returns a JSON object with the status, message, and data (if any).
438
- *
439
- * @example
440
- * const result = await db.query('SELECT * FROM users;');
441
- * console.log(result);
442
- * // { status: 'success', message: 'Query executed successfully', data: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }] }
443
- *
444
- * @example
445
- * const result = await db.query('INVALID SQL QUERY;');
446
- * console.log(result);
447
- * // { status: 'error', message: 'SQL syntax error', data: null }
448
- */
449
429
  async query(sqlQuery) {
450
- if (typeof sqlQuery !== "string") {
451
- throw new Error("The SQL query must be a string.");
452
- }
453
- const sqlCommands = sqlQuery.split(";").filter((cmd) => cmd.trim().length > 0);
454
- return new Promise((resolve2) => {
430
+ return new Promise(async (resolve2) => {
455
431
  try {
432
+ if (typeof sqlQuery !== "string") {
433
+ throw new Error("The SQL query must be a string.");
434
+ }
435
+ if (!this.db) {
436
+ await this.connect();
437
+ }
456
438
  if (!this.db) {
457
439
  throw new Error("Database connection is not available.");
458
440
  }
441
+ const sqlCommands = sqlQuery.split(";").filter((cmd) => cmd.trim().length > 0);
459
442
  const results = [];
460
- let commandsProcessed = 0;
461
443
  for (const command of sqlCommands) {
462
444
  const query = `${command};`;
463
445
  const isSelect = query.trim().toLowerCase().startsWith("select");
464
446
  if (isSelect) {
465
- this.db.all(query, [], (err, rows) => {
466
- if (err) {
467
- throw err;
468
- }
469
- results.push(rows);
470
- commandsProcessed++;
471
- if (commandsProcessed === sqlCommands.length) {
472
- resolve2({
473
- status: "success",
474
- message: "Query executed successfully",
475
- data: results.length === 1 ? results[0] : results
476
- });
477
- }
478
- });
447
+ const stmt = this.db.prepare(query);
448
+ const rows = stmt.all();
449
+ results.push(rows);
479
450
  } else {
480
- this.db.run(query, [], function(err) {
481
- if (err) {
482
- throw err;
483
- }
484
- results.push({ changes: this.changes, lastID: this.lastID });
485
- commandsProcessed++;
486
- if (commandsProcessed === sqlCommands.length) {
487
- resolve2({
488
- status: "success",
489
- message: "Query executed successfully",
490
- data: results.length === 1 ? results[0] : results
491
- });
492
- }
451
+ const stmt = this.db.prepare(query);
452
+ const result = stmt.run();
453
+ results.push({
454
+ changes: result.changes,
455
+ lastID: result.lastInsertRowid
493
456
  });
494
457
  }
495
458
  }
459
+ resolve2({
460
+ status: "success",
461
+ message: "Query executed successfully",
462
+ data: results.length === 1 ? results[0] : results
463
+ });
496
464
  } catch (error) {
497
465
  resolve2({
498
466
  status: "error",
@@ -502,52 +470,39 @@ var SQLite = class {
502
470
  }
503
471
  });
504
472
  }
505
- /**
506
- * Executes a SQL query with parameters on the currently set database.
507
- *
508
- * @param {string} sqlQuery - The SQL query to execute with placeholders (?).
509
- * @param {any[]} params - Array of parameters to bind to the query placeholders.
510
- * @returns {Promise<QueryResult>} - Returns a JSON object with the status, message, and data (if any).
511
- *
512
- * @example
513
- * const result = await db.queryWithParameters('INSERT INTO users (name, email) VALUES (?, ?)', ['John', 'john@example.com']);
514
- * console.log(result);
515
- * // { status: 'success', message: 'Query executed successfully', data: { changes: 1, lastID: 3 } }
516
- */
517
473
  async queryWithParameters(sqlQuery, params = []) {
518
- if (typeof sqlQuery !== "string") {
519
- throw new Error("The SQL query must be a string.");
520
- }
521
- if (!Array.isArray(params)) {
522
- throw new Error("Parameters must be an array.");
523
- }
524
- return new Promise((resolve2) => {
474
+ return new Promise(async (resolve2) => {
525
475
  try {
476
+ if (typeof sqlQuery !== "string") {
477
+ throw new Error("The SQL query must be a string.");
478
+ }
479
+ if (!Array.isArray(params)) {
480
+ throw new Error("Parameters must be an array.");
481
+ }
482
+ if (!this.db) {
483
+ await this.connect();
484
+ }
526
485
  if (!this.db) {
527
486
  throw new Error("Database connection is not available.");
528
487
  }
529
488
  const isSelect = sqlQuery.trim().toLowerCase().startsWith("select");
489
+ const stmt = this.db.prepare(sqlQuery);
530
490
  if (isSelect) {
531
- this.db.all(sqlQuery, params, (err, rows) => {
532
- if (err) {
533
- throw err;
534
- }
535
- resolve2({
536
- status: "success",
537
- message: "Query executed successfully",
538
- data: rows
539
- });
491
+ const rows = stmt.all(...params);
492
+ resolve2({
493
+ status: "success",
494
+ message: "Query executed successfully",
495
+ data: rows
540
496
  });
541
497
  } else {
542
- this.db.run(sqlQuery, params, function(err) {
543
- if (err) {
544
- throw err;
498
+ const result = stmt.run(...params);
499
+ resolve2({
500
+ status: "success",
501
+ message: "Query executed successfully",
502
+ data: {
503
+ changes: result.changes,
504
+ lastID: result.lastInsertRowid
545
505
  }
546
- resolve2({
547
- status: "success",
548
- message: "Query executed successfully",
549
- data: { changes: this.changes, lastID: this.lastID }
550
- });
551
506
  });
552
507
  }
553
508
  } catch (error) {