@devbro/neko-sql 0.1.38 → 0.1.39

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,5 +1,6 @@
1
1
  import { EventEmittor } from '@devbro/neko-helper';
2
- import { PoolClient, Pool, PoolConfig } from 'pg';
2
+ import pg, { PoolClient, PoolConfig } from 'pg';
3
+ import pg_cursor from 'pg-cursor';
3
4
  import Database from 'better-sqlite3';
4
5
  import mysql from 'mysql2/promise';
5
6
 
@@ -385,8 +386,10 @@ declare class PostgresqlConnection extends Connection {
385
386
  on(event: connection_events, listener: (...args: any[]) => void): this;
386
387
  off(event: connection_events, listener: (...args: any[]) => void): this;
387
388
  emit(event: connection_events, ...args: any[]): Promise<boolean>;
389
+ static pg: typeof pg;
390
+ static pg_cursor: typeof pg_cursor;
388
391
  connection: PoolClient | undefined;
389
- static pool: Pool;
392
+ static pool: pg.Pool;
390
393
  static defaults: PoolConfig;
391
394
  constructor(params: PoolConfig);
392
395
  connect(): Promise<boolean>;
@@ -461,6 +464,7 @@ declare class SqliteConnection extends Connection {
461
464
  /** Default configuration values for SQLite connections */
462
465
  static defaults: Partial<SqliteConfig>;
463
466
  constructor(params: SqliteConfig);
467
+ static sqlite: typeof Database;
464
468
  /**
465
469
  * Establishes a connection to the SQLite database
466
470
  * Creates or opens the database file specified in the configuration
@@ -576,6 +580,7 @@ declare class MysqlConnection extends Connection {
576
580
  connection: mysql.PoolConnection | undefined;
577
581
  static pool: mysql.Pool;
578
582
  static poolConfig: mysql.PoolOptions;
583
+ static mysql: typeof mysql;
579
584
  static defaults: mysql.PoolOptions;
580
585
  constructor(params: mysql.PoolOptions);
581
586
  connect(): Promise<boolean>;
package/dist/cjs/index.js CHANGED
@@ -60,10 +60,6 @@ var Connection = class {
60
60
  }
61
61
  };
62
62
 
63
- // src/databases/postgresql/PostgresqlConnection.mts
64
- var import_pg = require("pg");
65
- var import_pg2 = require("pg");
66
-
67
63
  // src/Query.mts
68
64
  var Query = class _Query {
69
65
  constructor(connection, grammar) {
@@ -1212,8 +1208,25 @@ var PostgresqlSchemaGrammar = class extends SchemaGrammar {
1212
1208
  };
1213
1209
 
1214
1210
  // src/databases/postgresql/PostgresqlConnection.mts
1215
- var import_pg_cursor = __toESM(require("pg-cursor"), 1);
1216
1211
  var import_neko_helper3 = require("@devbro/neko-helper");
1212
+
1213
+ // src/helper.mts
1214
+ var import_module = require("module");
1215
+ var import_meta = {};
1216
+ var req = (0, import_module.createRequire)(import_meta.url);
1217
+ function loadPackage(name) {
1218
+ try {
1219
+ return req(name);
1220
+ } catch (error) {
1221
+ if (Error.isError(error) && error.code === "MODULE_NOT_FOUND") {
1222
+ error.message = `Package "${name}" is not installed. Please install proper db driver to use this database connection.`;
1223
+ }
1224
+ throw error;
1225
+ }
1226
+ }
1227
+ __name(loadPackage, "loadPackage");
1228
+
1229
+ // src/databases/postgresql/PostgresqlConnection.mts
1217
1230
  var PostgresqlConnection = class _PostgresqlConnection extends Connection {
1218
1231
  static {
1219
1232
  __name(this, "PostgresqlConnection");
@@ -1230,6 +1243,8 @@ var PostgresqlConnection = class _PostgresqlConnection extends Connection {
1230
1243
  emit(event, ...args) {
1231
1244
  return this.eventManager.emit(event, ...args);
1232
1245
  }
1246
+ static pg;
1247
+ static pg_cursor;
1233
1248
  connection;
1234
1249
  static pool;
1235
1250
  static defaults = {
@@ -1244,8 +1259,15 @@ var PostgresqlConnection = class _PostgresqlConnection extends Connection {
1244
1259
  };
1245
1260
  constructor(params) {
1246
1261
  super();
1262
+ if (!_PostgresqlConnection.pg) {
1263
+ _PostgresqlConnection.pg = loadPackage("pg");
1264
+ _PostgresqlConnection.pg_cursor = loadPackage("pg-cursor");
1265
+ }
1247
1266
  if (!_PostgresqlConnection.pool) {
1248
- _PostgresqlConnection.pool = new import_pg2.Pool({ ..._PostgresqlConnection.defaults, ...params });
1267
+ _PostgresqlConnection.pool = new _PostgresqlConnection.pg.Pool({
1268
+ ..._PostgresqlConnection.defaults,
1269
+ ...params
1270
+ });
1249
1271
  }
1250
1272
  }
1251
1273
  async connect() {
@@ -1272,7 +1294,7 @@ var PostgresqlConnection = class _PostgresqlConnection extends Connection {
1272
1294
  return result?.rows;
1273
1295
  }
1274
1296
  async runCursor(sql) {
1275
- return this.connection?.query(new import_pg_cursor.default(sql.sql, sql.bindings));
1297
+ return this.connection?.query(new _PostgresqlConnection.pg_cursor(sql.sql, sql.bindings));
1276
1298
  }
1277
1299
  async disconnect() {
1278
1300
  if (this.connection === void 0) {
@@ -1345,7 +1367,7 @@ var PostgresqlConnection = class _PostgresqlConnection extends Connection {
1345
1367
  await this.runQuery(`CREATE DATABASE ${safeName2}`);
1346
1368
  return;
1347
1369
  }
1348
- const conn = new import_pg.Client({
1370
+ const conn = new _PostgresqlConnection.pg.Client({
1349
1371
  ..._PostgresqlConnection.pool.options,
1350
1372
  database: "postgres"
1351
1373
  });
@@ -1360,7 +1382,7 @@ var PostgresqlConnection = class _PostgresqlConnection extends Connection {
1360
1382
  await this.runQuery(`DROP DATABASE ${safeName2}`);
1361
1383
  return;
1362
1384
  }
1363
- const conn = new import_pg.Client({
1385
+ const conn = new _PostgresqlConnection.pg.Client({
1364
1386
  ..._PostgresqlConnection.pool.options,
1365
1387
  database: "postgres"
1366
1388
  // connect to default 'postgres' database to drop others
@@ -1381,15 +1403,13 @@ var PostgresqlConnection = class _PostgresqlConnection extends Connection {
1381
1403
  }
1382
1404
  async existsDatabase(name) {
1383
1405
  if (!this.isConnected()) {
1384
- const conn = new import_pg.Client({
1406
+ const conn = new _PostgresqlConnection.pg.Client({
1385
1407
  ..._PostgresqlConnection.pool.options,
1386
1408
  database: "postgres"
1387
1409
  });
1388
1410
  await conn.connect();
1389
1411
  const safeName = this.validateAndEscapeIdentifier(name);
1390
- const result2 = await conn.query("SELECT 1 FROM pg_database WHERE datname = $1", [
1391
- safeName
1392
- ]);
1412
+ const result2 = await conn.query("SELECT 1 FROM pg_database WHERE datname = $1", [safeName]);
1393
1413
  await conn.end();
1394
1414
  return result2.rows.length > 0;
1395
1415
  }
@@ -1400,9 +1420,6 @@ var PostgresqlConnection = class _PostgresqlConnection extends Connection {
1400
1420
  }
1401
1421
  };
1402
1422
 
1403
- // src/databases/sqlite/SqliteConnection.mts
1404
- var import_better_sqlite3 = __toESM(require("better-sqlite3"), 1);
1405
-
1406
1423
  // src/databases/sqlite/SqliteQueryGrammar.mts
1407
1424
  var import_neko_helper4 = require("@devbro/neko-helper");
1408
1425
  var SqliteQueryGrammar = class extends QueryGrammar {
@@ -1520,8 +1537,12 @@ var SqliteConnection = class _SqliteConnection extends Connection {
1520
1537
  };
1521
1538
  constructor(params) {
1522
1539
  super();
1540
+ if (!_SqliteConnection.sqlite) {
1541
+ _SqliteConnection.sqlite = loadPackage("better-sqlite3");
1542
+ }
1523
1543
  this.config = { ..._SqliteConnection.defaults, ...params };
1524
1544
  }
1545
+ static sqlite;
1525
1546
  /**
1526
1547
  * Establishes a connection to the SQLite database
1527
1548
  * Creates or opens the database file specified in the configuration
@@ -1529,7 +1550,7 @@ var SqliteConnection = class _SqliteConnection extends Connection {
1529
1550
  async connect() {
1530
1551
  this.eventManager.emit("connect").catch(() => {
1531
1552
  });
1532
- this.connection = new import_better_sqlite3.default(this.config.filename, {
1553
+ this.connection = new _SqliteConnection.sqlite(this.config.filename, {
1533
1554
  readonly: this.config.readonly,
1534
1555
  fileMustExist: this.config.fileMustExist,
1535
1556
  timeout: this.config.timeout,
@@ -1688,7 +1709,7 @@ var SqliteConnection = class _SqliteConnection extends Connection {
1688
1709
  */
1689
1710
  async createDatabase(name) {
1690
1711
  const dbPath = name.endsWith(".db") ? name : `${name}.db`;
1691
- const tempDb = new import_better_sqlite3.default(dbPath);
1712
+ const tempDb = new _SqliteConnection.sqlite(dbPath);
1692
1713
  tempDb.close();
1693
1714
  }
1694
1715
  /**
@@ -1723,9 +1744,6 @@ var SqliteConnection = class _SqliteConnection extends Connection {
1723
1744
  }
1724
1745
  };
1725
1746
 
1726
- // src/databases/mysql/MysqlConnection.mts
1727
- var import_promise = __toESM(require("mysql2/promise"), 1);
1728
-
1729
1747
  // src/databases/mysql/MysqlQueryGrammar.mts
1730
1748
  var MysqlQueryGrammar = class extends QueryGrammar {
1731
1749
  static {
@@ -1817,6 +1835,7 @@ var MysqlConnection = class _MysqlConnection extends Connection {
1817
1835
  connection;
1818
1836
  static pool;
1819
1837
  static poolConfig;
1838
+ static mysql;
1820
1839
  static defaults = {
1821
1840
  port: 3306,
1822
1841
  connectionLimit: 20,
@@ -1827,9 +1846,12 @@ var MysqlConnection = class _MysqlConnection extends Connection {
1827
1846
  };
1828
1847
  constructor(params) {
1829
1848
  super();
1849
+ if (!_MysqlConnection.mysql) {
1850
+ _MysqlConnection.mysql = loadPackage("mysql2/promise");
1851
+ }
1830
1852
  if (!_MysqlConnection.pool) {
1831
1853
  _MysqlConnection.poolConfig = { ..._MysqlConnection.defaults, ...params };
1832
- _MysqlConnection.pool = import_promise.default.createPool(_MysqlConnection.poolConfig);
1854
+ _MysqlConnection.pool = _MysqlConnection.mysql.createPool(_MysqlConnection.poolConfig);
1833
1855
  }
1834
1856
  }
1835
1857
  async connect() {
@@ -1928,19 +1950,18 @@ var MysqlConnection = class _MysqlConnection extends Connection {
1928
1950
  }
1929
1951
  async createDatabase(name) {
1930
1952
  if (!this.isConnected()) {
1931
- const tempConn2 = await import_promise.default.createConnection({
1953
+ const tempConn2 = await _MysqlConnection.mysql.createConnection({
1932
1954
  host: _MysqlConnection.poolConfig.host,
1933
1955
  user: _MysqlConnection.poolConfig.user,
1934
1956
  password: _MysqlConnection.poolConfig.password,
1935
1957
  port: _MysqlConnection.poolConfig.port
1936
1958
  });
1937
1959
  const safeName2 = this.validateAndEscapeIdentifier(name);
1938
- console.log(safeName2);
1939
- let [rows] = await tempConn2.query(`CREATE DATABASE ${safeName2}`);
1960
+ await tempConn2.query(`CREATE DATABASE ${safeName2}`);
1940
1961
  await tempConn2.end();
1941
1962
  return;
1942
1963
  }
1943
- const tempConn = await import_promise.default.createConnection({
1964
+ const tempConn = await _MysqlConnection.mysql.createConnection({
1944
1965
  host: _MysqlConnection.poolConfig.host,
1945
1966
  user: _MysqlConnection.poolConfig.user,
1946
1967
  password: _MysqlConnection.poolConfig.password,
@@ -1954,7 +1975,7 @@ var MysqlConnection = class _MysqlConnection extends Connection {
1954
1975
  if (this.isConnected()) {
1955
1976
  throw new Error("Cannot drop database while connected.");
1956
1977
  }
1957
- const tempConn = await import_promise.default.createConnection({
1978
+ const tempConn = await _MysqlConnection.mysql.createConnection({
1958
1979
  host: _MysqlConnection.poolConfig.host,
1959
1980
  user: _MysqlConnection.poolConfig.user,
1960
1981
  password: _MysqlConnection.poolConfig.password,
@@ -1973,13 +1994,13 @@ var MysqlConnection = class _MysqlConnection extends Connection {
1973
1994
  }
1974
1995
  async existsDatabase(name) {
1975
1996
  if (!this.isConnected()) {
1976
- const tempConn = await import_promise.default.createConnection({
1997
+ const tempConn = await _MysqlConnection.mysql.createConnection({
1977
1998
  host: _MysqlConnection.poolConfig.host,
1978
1999
  user: _MysqlConnection.poolConfig.user,
1979
2000
  password: _MysqlConnection.poolConfig.password,
1980
2001
  port: _MysqlConnection.poolConfig.port
1981
2002
  });
1982
- let [rows2] = await tempConn.query(
2003
+ const [rows2] = await tempConn.query(
1983
2004
  "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?",
1984
2005
  [name]
1985
2006
  );