miqro 7.3.4 → 7.3.6

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.
@@ -173,6 +173,29 @@ export const DBConfigSchema = {
173
173
  type: "object",
174
174
  properties: {
175
175
  url: "string?",
176
+ executor: {
177
+ type: "object?",
178
+ properties: {
179
+ prepare: "function",
180
+ query: "function",
181
+ disconnect: "function",
182
+ connect: "function",
183
+ transaction: "function",
184
+ target: "any",
185
+ transformInput: "function",
186
+ transformOutput: "function"
187
+ }
188
+ },
189
+ pool: {
190
+ type: "object?",
191
+ properties: {
192
+ min: "number?",
193
+ max: "number?",
194
+ idleTimeoutMillis: "number?",
195
+ connectionTimeoutMillis: "number?",
196
+ maxLifetimeSeconds: "number?"
197
+ }
198
+ },
176
199
  disabled: "boolean?",
177
200
  storage: "string?",
178
201
  dialect: "string?",
@@ -15,9 +15,11 @@ export class DBManager {
15
15
  this.options?.logger?.debug("setting up db connection [%s]", config.name);
16
16
  this.options?.logger?.trace("creating db connection [%s]", config.name);
17
17
  const db = new Database({
18
+ executor: config.executor,
18
19
  dialect: config.dialect ? config.dialect : "node:sqlite",
19
20
  storage: config.storage ? config.storage : "./db.sqlite3",
20
21
  connectionString: config.url,
22
+ pool: config.pool,
21
23
  logger: this.options?.loggerProvider?.getLogger(`${DB_IDENTIFIER}_${config.name}`)
22
24
  });
23
25
  this.options?.logger?.trace("connecting db connection [%s]", config.name);
@@ -1,4 +1,4 @@
1
- import { Database } from "@miqro/query";
1
+ import { Database, QueryExecutor } from "@miqro/query";
2
2
  import { WebSocketServerOptions, SessionHandlerOptions, Logger, WebSocketServer, ReadBuffer, URLEncodedParser, JSONParser, TextParser, CORS, SessionHandler, RouteOptions, Handler, Request, Response, LogLevel, LoggerTransportWriteArgs, CORSOptions, HandlerWithOptions, ErrorHandler, SchemaProperties, APIRoute as BaseAPIRoute } from "@miqro/core";
3
3
  import { request } from "@miqro/request";
4
4
  import { Parser, ParserInterface } from "@miqro/parser";
@@ -219,9 +219,17 @@ export interface ServerConfig {
219
219
  export interface DBConfig {
220
220
  dialect?: string;
221
221
  storage?: string;
222
+ executor?: QueryExecutor;
222
223
  url?: string;
223
224
  disabled?: boolean;
224
225
  name: string;
226
+ pool?: {
227
+ min?: number;
228
+ max?: number;
229
+ idleTimeoutMillis?: number;
230
+ connectionTimeoutMillis?: number;
231
+ maxLifetimeSeconds?: number;
232
+ };
225
233
  }
226
234
  export interface APIOptions<TBody extends SchemaProperties | string | boolean | undefined = SchemaProperties | string | boolean | undefined, TParams extends SchemaProperties | string | boolean | undefined = SchemaProperties | string | boolean | undefined, TQuery extends SchemaProperties | string | boolean | undefined = SchemaProperties | string | boolean | undefined> extends Partial<RouteOptions<TBody, TParams, TQuery>> {
227
235
  basePath?: string;
package/build/lib.cjs CHANGED
@@ -9614,6 +9614,29 @@ var DBConfigSchema = {
9614
9614
  type: "object",
9615
9615
  properties: {
9616
9616
  url: "string?",
9617
+ executor: {
9618
+ type: "object?",
9619
+ properties: {
9620
+ prepare: "function",
9621
+ query: "function",
9622
+ disconnect: "function",
9623
+ connect: "function",
9624
+ transaction: "function",
9625
+ target: "any",
9626
+ transformInput: "function",
9627
+ transformOutput: "function"
9628
+ }
9629
+ },
9630
+ pool: {
9631
+ type: "object?",
9632
+ properties: {
9633
+ min: "number?",
9634
+ max: "number?",
9635
+ idleTimeoutMillis: "number?",
9636
+ connectionTimeoutMillis: "number?",
9637
+ maxLifetimeSeconds: "number?"
9638
+ }
9639
+ },
9617
9640
  disabled: "boolean?",
9618
9641
  storage: "string?",
9619
9642
  dialect: "string?",
@@ -9894,7 +9917,7 @@ function jsx2HTML(out, runtime) {
9894
9917
  // src/types.ts
9895
9918
  init_lib3();
9896
9919
 
9897
- // node_modules/@miqro/query/build/executors/sqlite3/utils.js
9920
+ // node_modules/@miqro/query/build/executors/sqlite-native/utils.js
9898
9921
  function sqlite3ExecutorPrepare(args) {
9899
9922
  switch (args._type) {
9900
9923
  case "delete": {
@@ -10260,7 +10283,9 @@ var DATABASE_CONFIG_SCHEMA = {
10260
10283
  properties: {
10261
10284
  prepare: "function",
10262
10285
  query: "function",
10263
- disconnect: "function"
10286
+ disconnect: "function",
10287
+ connect: "function",
10288
+ target: "any"
10264
10289
  }
10265
10290
  },
10266
10291
  logger: {
@@ -10271,11 +10296,21 @@ var DATABASE_CONFIG_SCHEMA = {
10271
10296
  error: "function?"
10272
10297
  }
10273
10298
  },
10299
+ pool: {
10300
+ type: "object?",
10301
+ properties: {
10302
+ min: "number?",
10303
+ max: "number?",
10304
+ idleTimeoutMillis: "number?",
10305
+ connectionTimeoutMillis: "number?",
10306
+ maxLifetimeSeconds: "number?"
10307
+ }
10308
+ },
10274
10309
  storage: "string?",
10275
10310
  connectionString: "string?",
10276
10311
  dialect: {
10277
10312
  type: "enum?",
10278
- enumValues: ["node:sqlite", "sqlite3-cli", "sqlite3", "pg"]
10313
+ enumValues: ["node:sqlite", "pg", "pg-pool"]
10279
10314
  }
10280
10315
  }
10281
10316
  };
@@ -10370,146 +10405,188 @@ function pgTransformOutput(type, value) {
10370
10405
  }
10371
10406
  }
10372
10407
 
10373
- // node_modules/@miqro/query/build/executors/sqlite3/lib.js
10374
- var SQLITE_CONFIG_SCHEMA = {
10408
+ // node_modules/@miqro/query/build/executors/pg/lib.js
10409
+ var PG_DATABASE_CONFIG_SCHEMA = {
10375
10410
  type: "object",
10376
10411
  mode: "remove_extra",
10377
10412
  properties: {
10378
- storage: "string"
10413
+ connectionString: "string",
10414
+ pool: {
10415
+ type: "object?",
10416
+ properties: {
10417
+ min: "number?",
10418
+ max: "number?",
10419
+ idleTimeoutMillis: "number?",
10420
+ connectionTimeoutMillis: "number?",
10421
+ maxLifetimeSeconds: "number?"
10422
+ }
10423
+ }
10379
10424
  }
10380
10425
  };
10381
- async function sqlite3Executor(config) {
10382
- const databaseOptions = parser2.parse(config, SQLITE_CONFIG_SCHEMA);
10383
- if (!databaseOptions) {
10384
- throw new Error("config not valid");
10385
- }
10386
- const sqlite3 = await import("sqlite3");
10387
- const driver = new sqlite3.default.Database(databaseOptions.storage);
10388
- return {
10389
- disconnect: async function sqlite3ExecutorDisconnect() {
10390
- await driver.close();
10391
- },
10392
- prepare: sqlite3ExecutorPrepare,
10393
- transformInput: sqliteTransformInput,
10394
- transformOutput: sqliteTransformOutput,
10395
- query: async function sqlite3Executor2(sql, values) {
10396
- return new Promise((resolve24, reject) => {
10397
- const st = driver.prepare(sql, values, function(error2) {
10398
- if (error2) {
10399
- reject(error2);
10400
- } else {
10401
- st.all(function(error3, rows) {
10402
- if (error3) {
10403
- reject(error3);
10404
- } else {
10405
- st.finalize((error4) => {
10406
- if (error4) {
10407
- reject(error4);
10408
- } else {
10409
- resolve24(rows);
10410
- }
10411
- });
10412
- }
10413
- });
10414
- }
10426
+ function postgresExecutorPrepare(args) {
10427
+ switch (args._type) {
10428
+ case "create-table": {
10429
+ const q = args;
10430
+ const sql = `CREATE TABLE${q._ignoreDuplicate ? " IF NOT EXISTS" : ""} ${renderTable(q._table)}${getCreateTableColumns2(q._definition)}`;
10431
+ return sql;
10432
+ }
10433
+ case "insert": {
10434
+ const q = args;
10435
+ const rows = getInsertValues(q._columns, q._values);
10436
+ const returing = q._returning.length === 0 ? "*" : q._returning.join(",");
10437
+ const sql = `INSERT INTO ${renderTable(q._table)}${getInsertColumns(q._columns)}${rows.sql} ${q._ignoreDuplicate ? "ON CONFLICT DO NOTHING " : ""}RETURNING ${returing}`;
10438
+ return {
10439
+ sql,
10440
+ values: rows.values
10441
+ };
10442
+ }
10443
+ case "alter-table": {
10444
+ const q = args;
10445
+ let alters = [];
10446
+ if (q._renameTable) {
10447
+ alters.push({
10448
+ sql: `ALTER TABLE ${renderTable(q._table)} RENAME TO "?"`,
10449
+ values: [q._renameTable]
10415
10450
  });
10416
- });
10451
+ }
10452
+ for (let l of q._changes) {
10453
+ switch (l._action) {
10454
+ case "add":
10455
+ if (!l._definition) {
10456
+ throw new Error("unsupported alter action add without definition");
10457
+ }
10458
+ alters.push({
10459
+ sql: `ALTER TABLE ${renderTable(q._table)} ADD COLUMN ${getCreateTableColumn2(l._column, l._definition, [])}`
10460
+ });
10461
+ break;
10462
+ case "drop":
10463
+ alters.push({
10464
+ sql: `ALTER TABLE ${renderTable(q._table)} DROP COLUMN "${l._column}"`
10465
+ });
10466
+ break;
10467
+ case "rename":
10468
+ alters.push({
10469
+ sql: `ALTER TABLE ${renderTable(q._table)} RENAME COLUMN "${l._column}" TO "${l._newName}"`
10470
+ });
10471
+ break;
10472
+ default:
10473
+ throw new Error("unsupported alter action");
10474
+ }
10475
+ }
10476
+ return alters;
10477
+ }
10478
+ default:
10479
+ return sqlite3ExecutorPrepare(args);
10480
+ }
10481
+ }
10482
+ function pgQuery(client) {
10483
+ return async function postgresExecutor2(sql, values) {
10484
+ try {
10485
+ const result = await client.query(tokens2Postgres(sql, values), values);
10486
+ return result.rows;
10487
+ } catch (e) {
10488
+ throw e;
10417
10489
  }
10418
10490
  };
10419
10491
  }
10420
-
10421
- // node_modules/@miqro/query/build/executors/pg/lib.js
10422
- var PG_DATABASE_CONFIG_SCHEMA = {
10423
- type: "object",
10424
- mode: "remove_extra",
10425
- properties: {
10426
- connectionString: "string"
10427
- }
10428
- };
10429
- async function postgresExecutor(config) {
10492
+ async function postgresExecutor(config, usePool) {
10493
+ const target = new EventTarget();
10494
+ const usingPool = config.pool && usePool === void 0 || usePool === true;
10430
10495
  const databaseOptions = parser2.parse(config, PG_DATABASE_CONFIG_SCHEMA);
10431
10496
  if (!databaseOptions) {
10432
10497
  throw new Error("bad options");
10433
10498
  }
10434
- const pg = await import("pg");
10435
- const driver = new pg.default.Client({
10436
- connectionString: databaseOptions.connectionString
10437
- // "postgresql://postgres:password@localhost:5432/db"
10438
- });
10439
- driver.on("error", (err) => {
10440
- console.error("pg client error:", err);
10441
- });
10442
- await driver.connect();
10499
+ const { Pool, Client } = await import("pg");
10500
+ let driver;
10501
+ if (usingPool) {
10502
+ driver = new Pool({
10503
+ connectionString: databaseOptions.connectionString,
10504
+ // "postgresql://postgres:password@localhost:5432/db",
10505
+ max: databaseOptions.pool?.max,
10506
+ min: databaseOptions.pool?.min,
10507
+ idleTimeoutMillis: databaseOptions.pool?.idleTimeoutMillis,
10508
+ connectionTimeoutMillis: databaseOptions.pool?.connectionTimeoutMillis,
10509
+ maxLifetimeSeconds: databaseOptions.pool?.maxLifetimeSeconds
10510
+ });
10511
+ driver.on("error", (err) => {
10512
+ const errorEvent = new CustomEvent("error", {
10513
+ detail: err
10514
+ });
10515
+ target.dispatchEvent(errorEvent);
10516
+ });
10517
+ } else {
10518
+ driver = new Client({
10519
+ connectionString: databaseOptions.connectionString
10520
+ // "postgresql://postgres:password@localhost:5432/db",
10521
+ });
10522
+ driver.on("error", (err) => {
10523
+ const errorEvent = new CustomEvent("error", {
10524
+ detail: err
10525
+ });
10526
+ target.dispatchEvent(errorEvent);
10527
+ });
10528
+ }
10443
10529
  return {
10444
- disconnect: async function postgresExecutorDisconnect() {
10445
- await driver.end();
10446
- },
10447
- transformInput: pgTransformInput,
10448
- transformOutput: pgTransformOutput,
10449
- prepare: function postgresExecutorPrepare(args) {
10450
- switch (args._type) {
10451
- case "create-table": {
10452
- const q = args;
10453
- const sql = `CREATE TABLE${q._ignoreDuplicate ? " IF NOT EXISTS" : ""} ${renderTable(q._table)}${getCreateTableColumns2(q._definition)}`;
10454
- return sql;
10455
- }
10456
- case "insert": {
10457
- const q = args;
10458
- const rows = getInsertValues(q._columns, q._values);
10459
- const returing = q._returning.length === 0 ? "*" : q._returning.join(",");
10460
- const sql = `INSERT INTO ${renderTable(q._table)}${getInsertColumns(q._columns)}${rows.sql} ${q._ignoreDuplicate ? "ON CONFLICT DO NOTHING " : ""}RETURNING ${returing}`;
10461
- return {
10462
- sql,
10463
- values: rows.values
10464
- };
10465
- }
10466
- case "alter-table": {
10467
- const q = args;
10468
- let alters = [];
10469
- if (q._renameTable) {
10470
- alters.push({
10471
- sql: `ALTER TABLE ${renderTable(q._table)} RENAME TO "?"`,
10472
- values: [q._renameTable]
10473
- });
10474
- }
10475
- for (let l of q._changes) {
10476
- switch (l._action) {
10477
- case "add":
10478
- if (!l._definition) {
10479
- throw new Error("unsupported alter action add without definition");
10480
- }
10481
- alters.push({
10482
- sql: `ALTER TABLE ${renderTable(q._table)} ADD COLUMN ${getCreateTableColumn2(l._column, l._definition, [])}`
10483
- });
10484
- break;
10485
- case "drop":
10486
- alters.push({
10487
- sql: `ALTER TABLE ${renderTable(q._table)} DROP COLUMN "${l._column}"`
10488
- });
10489
- break;
10490
- case "rename":
10491
- alters.push({
10492
- sql: `ALTER TABLE ${renderTable(q._table)} RENAME COLUMN "${l._column}" TO "${l._newName}"`
10493
- });
10494
- break;
10495
- default:
10496
- throw new Error("unsupported alter action");
10497
- }
10530
+ target,
10531
+ transaction: async (cb) => {
10532
+ const client = usingPool ? await driver.connect() : await (async () => {
10533
+ const newClient = new Client({
10534
+ connectionString: databaseOptions.connectionString
10535
+ // "postgresql://postgres:password@localhost:5432/db",
10536
+ });
10537
+ newClient.on("error", (err) => {
10538
+ const errorEvent = new CustomEvent("error", {
10539
+ detail: err
10540
+ });
10541
+ target.dispatchEvent(errorEvent);
10542
+ });
10543
+ await newClient.connect();
10544
+ return newClient;
10545
+ })();
10546
+ const newExecutor = {
10547
+ target: new EventTarget(),
10548
+ transaction: async (cb2) => cb2(newExecutor),
10549
+ connect: async () => {
10550
+ },
10551
+ disconnect: async () => {
10552
+ if (usingPool) {
10553
+ await client.release();
10554
+ } else {
10555
+ await client.end();
10498
10556
  }
10499
- return alters;
10500
- }
10501
- default:
10502
- return sqlite3ExecutorPrepare(args);
10503
- }
10504
- },
10505
- query: async function postgresExecutor2(sql, values) {
10557
+ },
10558
+ transformInput: pgTransformInput,
10559
+ transformOutput: pgTransformOutput,
10560
+ prepare: postgresExecutorPrepare,
10561
+ query: pgQuery(client)
10562
+ };
10506
10563
  try {
10507
- const result = await driver.query(tokens2Postgres(sql, values), values);
10508
- return result.rows;
10564
+ await newExecutor.query("BEGIN;");
10565
+ const ret = await cb(newExecutor);
10566
+ await newExecutor.query("COMMIT;");
10567
+ return ret;
10509
10568
  } catch (e) {
10569
+ await newExecutor.query("ROLLBACK;");
10510
10570
  throw e;
10571
+ } finally {
10572
+ newExecutor.disconnect();
10511
10573
  }
10512
- }
10574
+ },
10575
+ connect: async () => {
10576
+ if (usingPool) {
10577
+ const client = await driver.connect();
10578
+ await client.release();
10579
+ } else {
10580
+ await driver.connect();
10581
+ }
10582
+ },
10583
+ disconnect: async function postgresExecutorDisconnect() {
10584
+ await driver.end();
10585
+ },
10586
+ transformInput: pgTransformInput,
10587
+ transformOutput: pgTransformOutput,
10588
+ prepare: postgresExecutorPrepare,
10589
+ query: pgQuery(driver)
10513
10590
  };
10514
10591
  }
10515
10592
  function getCreateTableColumns2(definition) {
@@ -10610,10 +10687,12 @@ async function runStatements(db, stmts, logger) {
10610
10687
 
10611
10688
  // node_modules/@miqro/query/build/query/create-database.js
10612
10689
  var CreateDatabase = class _CreateDatabase {
10690
+ db;
10691
+ _dbName;
10692
+ _type = "create-database";
10613
10693
  constructor(db, _dbName) {
10614
10694
  this.db = db;
10615
10695
  this._dbName = _dbName;
10616
- this._type = "create-database";
10617
10696
  }
10618
10697
  async yield(logger) {
10619
10698
  return runStatements(this.db, this.db.getExecutor().prepare(this), logger);
@@ -10629,12 +10708,15 @@ var CreateDatabase = class _CreateDatabase {
10629
10708
 
10630
10709
  // node_modules/@miqro/query/build/query/create-table.js
10631
10710
  var CreateTable = class _CreateTable {
10711
+ db;
10712
+ _table;
10713
+ _definition;
10714
+ _ignoreDuplicate = false;
10715
+ _type = "create-table";
10632
10716
  constructor(db, _table, _definition = {}) {
10633
10717
  this.db = db;
10634
10718
  this._table = _table;
10635
10719
  this._definition = _definition;
10636
- this._ignoreDuplicate = false;
10637
- this._type = "create-table";
10638
10720
  this._definition = assertNotUndefined2(parser2.parse(_definition, TableSchemaSchema));
10639
10721
  }
10640
10722
  column(column, definition) {
@@ -10669,11 +10751,13 @@ var CreateTable = class _CreateTable {
10669
10751
 
10670
10752
  // node_modules/@miqro/query/build/query/drop-table.js
10671
10753
  var DropTable = class _DropTable {
10754
+ db;
10755
+ _table;
10756
+ _type = "drop-table";
10757
+ _ignoreDuplicate = false;
10672
10758
  constructor(db, _table) {
10673
10759
  this.db = db;
10674
10760
  this._table = _table;
10675
- this._type = "drop-table";
10676
- this._ignoreDuplicate = false;
10677
10761
  }
10678
10762
  prepare() {
10679
10763
  return getStatements(this.db.getExecutor().prepare(this));
@@ -10694,12 +10778,16 @@ var DropTable = class _DropTable {
10694
10778
 
10695
10779
  // node_modules/@miqro/query/build/query/alter-table.js
10696
10780
  var AlterTable = class _AlterTable {
10781
+ db;
10782
+ _table;
10783
+ _inTransaction;
10784
+ _type = "alter-table";
10785
+ _renameTable;
10786
+ _changes = [];
10697
10787
  constructor(db, _table, _inTransaction = false) {
10698
10788
  this.db = db;
10699
10789
  this._table = _table;
10700
10790
  this._inTransaction = _inTransaction;
10701
- this._type = "alter-table";
10702
- this._changes = [];
10703
10791
  }
10704
10792
  rename(name) {
10705
10793
  this._renameTable = name;
@@ -10744,14 +10832,17 @@ var AlterTable = class _AlterTable {
10744
10832
 
10745
10833
  // node_modules/@miqro/query/build/query/insert.js
10746
10834
  var Insert = class _Insert {
10835
+ db;
10836
+ _table;
10837
+ _type = "insert";
10838
+ _columns = [];
10839
+ _returning = [];
10840
+ _values = [];
10841
+ _ignoreDuplicate = false;
10842
+ _schema;
10747
10843
  constructor(db, _table, schema) {
10748
10844
  this.db = db;
10749
10845
  this._table = _table;
10750
- this._type = "insert";
10751
- this._columns = [];
10752
- this._returning = [];
10753
- this._values = [];
10754
- this._ignoreDuplicate = false;
10755
10846
  this._schema = schema;
10756
10847
  }
10757
10848
  value(value) {
@@ -10803,10 +10894,10 @@ var Insert = class _Insert {
10803
10894
 
10804
10895
  // node_modules/@miqro/query/build/executors/where.js
10805
10896
  var Where = class _Where {
10806
- constructor() {
10807
- this._orderBy = [];
10808
- this._filters = [];
10809
- }
10897
+ _orderBy = [];
10898
+ _limitBy;
10899
+ _offsetBy;
10900
+ _filters = [];
10810
10901
  clone() {
10811
10902
  const ret = new _Where();
10812
10903
  ret._filters = structuredClone(this._filters);
@@ -11108,15 +11199,19 @@ async function yieldWithInclude(db, schema, assocMap, include, filters, orderBy,
11108
11199
 
11109
11200
  // node_modules/@miqro/query/build/query/select.js
11110
11201
  var Select = class _Select extends Where {
11202
+ db;
11203
+ _type = "select";
11204
+ _columns = [];
11205
+ _selectFrom = [];
11206
+ _groupBy = [];
11207
+ _joins = [];
11208
+ _orderBy = [];
11209
+ _schema;
11210
+ _assocMap;
11211
+ _include;
11111
11212
  constructor(db, schema, associations) {
11112
11213
  super();
11113
11214
  this.db = db;
11114
- this._type = "select";
11115
- this._columns = [];
11116
- this._selectFrom = [];
11117
- this._groupBy = [];
11118
- this._joins = [];
11119
- this._orderBy = [];
11120
11215
  this._schema = schema;
11121
11216
  if (associations) {
11122
11217
  this._assocMap = new Map(Object.entries(associations));
@@ -11205,13 +11300,14 @@ var Select = class _Select extends Where {
11205
11300
 
11206
11301
  // node_modules/@miqro/query/build/query/count.js
11207
11302
  var Count = class _Count extends Where {
11303
+ db;
11304
+ _type = "count";
11305
+ _selectFrom = [];
11306
+ _groupBy = [];
11307
+ _joins = [];
11208
11308
  constructor(db) {
11209
11309
  super();
11210
11310
  this.db = db;
11211
- this._type = "count";
11212
- this._selectFrom = [];
11213
- this._groupBy = [];
11214
- this._joins = [];
11215
11311
  }
11216
11312
  clone() {
11217
11313
  const ret = new _Count(this.db);
@@ -11264,13 +11360,16 @@ async function parseCountResult(rows) {
11264
11360
 
11265
11361
  // node_modules/@miqro/query/build/query/update.js
11266
11362
  var Update = class _Update extends Where {
11363
+ db;
11364
+ _table;
11365
+ _type = "update";
11366
+ _sets = [];
11367
+ _returning = [];
11368
+ _schema;
11267
11369
  constructor(db, _table, schema) {
11268
11370
  super();
11269
11371
  this.db = db;
11270
11372
  this._table = _table;
11271
- this._type = "update";
11272
- this._sets = [];
11273
- this._returning = [];
11274
11373
  this._schema = schema;
11275
11374
  }
11276
11375
  prepare() {
@@ -11308,12 +11407,15 @@ var Update = class _Update extends Where {
11308
11407
 
11309
11408
  // node_modules/@miqro/query/build/query/delete.js
11310
11409
  var Delete = class _Delete extends Where {
11410
+ db;
11411
+ _table;
11412
+ _type = "delete";
11413
+ _returning = [];
11414
+ _schema;
11311
11415
  constructor(db, _table, schema) {
11312
11416
  super();
11313
11417
  this.db = db;
11314
11418
  this._table = _table;
11315
- this._type = "delete";
11316
- this._returning = [];
11317
11419
  this._schema = schema;
11318
11420
  }
11319
11421
  prepare() {
@@ -11342,11 +11444,13 @@ var Delete = class _Delete extends Where {
11342
11444
 
11343
11445
  // node_modules/@miqro/query/build/query/drop-database.js
11344
11446
  var DropDatabase = class _DropDatabase {
11447
+ db;
11448
+ _dbName;
11449
+ _type = "drop-database";
11450
+ _ignoreDuplicate = false;
11345
11451
  constructor(db, _dbName) {
11346
11452
  this.db = db;
11347
11453
  this._dbName = _dbName;
11348
- this._type = "drop-database";
11349
- this._ignoreDuplicate = false;
11350
11454
  }
11351
11455
  prepare() {
11352
11456
  return getStatements(this.db.getExecutor().prepare(this));
@@ -11366,72 +11470,122 @@ var DropDatabase = class _DropDatabase {
11366
11470
  };
11367
11471
 
11368
11472
  // node_modules/@miqro/query/build/executors/sqlite-native/lib.js
11369
- var SQLITE_CONFIG_SCHEMA2 = {
11473
+ var SQLITE_CONFIG_SCHEMA = {
11370
11474
  type: "object",
11371
11475
  mode: "remove_extra",
11372
11476
  properties: {
11373
11477
  storage: "string"
11374
11478
  }
11375
11479
  };
11480
+ function nativeSqliteQuery(driver) {
11481
+ return async function sqlite3Executor(sql, values) {
11482
+ return new Promise((resolve24, reject) => {
11483
+ try {
11484
+ if (values) {
11485
+ for (let i = 0; i < values.length; i++) {
11486
+ switch (typeof values[i]) {
11487
+ case "object":
11488
+ if (values[i] instanceof Date) {
11489
+ values[i] = values[i].toString();
11490
+ }
11491
+ break;
11492
+ case "boolean":
11493
+ values[i] = values[i] ? 1 : 0;
11494
+ break;
11495
+ }
11496
+ }
11497
+ }
11498
+ const stmt = driver.prepare(sql);
11499
+ stmt.setReadBigInts(true);
11500
+ const ret = values && values.length > 0 ? stmt.all(...values) : stmt.all();
11501
+ resolve24(ret.map((row) => {
11502
+ const out = {};
11503
+ for (const key of Object.keys(row)) {
11504
+ const v = row[key];
11505
+ out[key] = typeof v === "bigint" && v >= Number.MIN_SAFE_INTEGER && v <= Number.MAX_SAFE_INTEGER ? Number(v) : typeof v === "number" ? Number(v) : v;
11506
+ }
11507
+ return out;
11508
+ }));
11509
+ } catch (e) {
11510
+ reject(e);
11511
+ }
11512
+ });
11513
+ };
11514
+ }
11376
11515
  async function nativeSqlite(config) {
11377
- const databaseOptions = parser2.parse(config, SQLITE_CONFIG_SCHEMA2);
11516
+ const databaseOptions = parser2.parse(config, SQLITE_CONFIG_SCHEMA);
11517
+ const target = new EventTarget();
11378
11518
  if (!databaseOptions) {
11379
11519
  throw new Error("config not valid");
11380
11520
  }
11381
11521
  const sqliteModule = await import("node:sqlite");
11382
11522
  const driver = new sqliteModule.DatabaseSync(databaseOptions.storage);
11383
11523
  return {
11524
+ target,
11525
+ transaction: async (cb) => {
11526
+ const client = new sqliteModule.DatabaseSync(databaseOptions.storage);
11527
+ const newExecutor = {
11528
+ target: new EventTarget(),
11529
+ transaction: async (cb2) => cb2(newExecutor),
11530
+ connect: async () => {
11531
+ },
11532
+ disconnect: async () => {
11533
+ if (client.isOpen)
11534
+ return client.close();
11535
+ },
11536
+ prepare: sqlite3ExecutorPrepare,
11537
+ transformInput: sqliteTransformInput,
11538
+ transformOutput: sqliteTransformOutput,
11539
+ query: nativeSqliteQuery(client)
11540
+ };
11541
+ let begin = false;
11542
+ try {
11543
+ await newExecutor.query("BEGIN;");
11544
+ begin = true;
11545
+ const ret = await cb(newExecutor);
11546
+ await newExecutor.query("COMMIT;");
11547
+ return ret;
11548
+ } catch (e) {
11549
+ if (begin) {
11550
+ await newExecutor.query("ROLLBACK;");
11551
+ }
11552
+ throw e;
11553
+ } finally {
11554
+ newExecutor.disconnect();
11555
+ }
11556
+ },
11557
+ connect: async () => {
11558
+ },
11384
11559
  // we just use the sqlite3Executor prepare function
11385
11560
  prepare: sqlite3ExecutorPrepare,
11386
11561
  transformInput: sqliteTransformInput,
11387
11562
  transformOutput: sqliteTransformOutput,
11388
11563
  disconnect: async function sqlite3ExecutorDisconnect() {
11389
- return driver.close();
11564
+ if (driver.isOpen)
11565
+ return driver.close();
11390
11566
  },
11391
- query: async function sqlite3Executor2(sql, values) {
11392
- return new Promise((resolve24, reject) => {
11393
- try {
11394
- if (values) {
11395
- for (let i = 0; i < values.length; i++) {
11396
- switch (typeof values[i]) {
11397
- case "object":
11398
- if (values[i] instanceof Date) {
11399
- values[i] = values[i].toString();
11400
- }
11401
- break;
11402
- case "boolean":
11403
- values[i] = values[i] ? 1 : 0;
11404
- break;
11405
- }
11406
- }
11407
- }
11408
- const stmt = driver.prepare(sql);
11409
- stmt.setReadBigInts(true);
11410
- const ret = values && values.length > 0 ? stmt.all(...values) : stmt.all();
11411
- resolve24(ret.map((row) => {
11412
- const out = {};
11413
- for (const key of Object.keys(row)) {
11414
- const v = row[key];
11415
- out[key] = typeof v === "bigint" && v >= Number.MIN_SAFE_INTEGER && v <= Number.MAX_SAFE_INTEGER ? Number(v) : typeof v === "number" ? Number(v) : v;
11416
- }
11417
- return out;
11418
- }));
11419
- } catch (e) {
11420
- reject(e);
11421
- }
11422
- });
11423
- }
11567
+ query: nativeSqliteQuery(driver)
11424
11568
  };
11425
11569
  }
11426
11570
 
11427
11571
  // node_modules/@miqro/query/build/db.js
11428
- var Database = class {
11572
+ var Database = class _Database extends EventTarget {
11573
+ config;
11574
+ status = "disconnected";
11575
+ executor = null;
11576
+ _errorListener;
11429
11577
  constructor(config) {
11578
+ super();
11430
11579
  this.config = config;
11431
- this.status = "disconnected";
11432
- this.executor = null;
11433
11580
  parser2.parse(config, DATABASE_CONFIG_SCHEMA, "no_extra");
11434
11581
  this.executor = null;
11582
+ this._errorListener = (event) => {
11583
+ const errorEvent = new CustomEvent("error", {
11584
+ detail: event.detail
11585
+ });
11586
+ this.config.logger?.error(event.detail);
11587
+ this.dispatchEvent(errorEvent);
11588
+ };
11435
11589
  }
11436
11590
  async connect() {
11437
11591
  if (this.status !== "disconnected") {
@@ -11443,6 +11597,8 @@ var Database = class {
11443
11597
  this.status = "connecting";
11444
11598
  try {
11445
11599
  this.executor = await getExecutor(this.config);
11600
+ this.executor.target.addEventListener("error", this._errorListener);
11601
+ await this.executor.connect();
11446
11602
  this.status = "connected";
11447
11603
  } catch (e) {
11448
11604
  this.status = "error";
@@ -11456,6 +11612,9 @@ var Database = class {
11456
11612
  this.status = "disconnecting";
11457
11613
  try {
11458
11614
  await this.getExecutor().disconnect();
11615
+ if (this.executor) {
11616
+ this.executor.target.removeEventListener("error", this._errorListener);
11617
+ }
11459
11618
  this.executor = null;
11460
11619
  this.status = "disconnected";
11461
11620
  } catch (e) {
@@ -11481,15 +11640,15 @@ var Database = class {
11481
11640
  }
11482
11641
  }
11483
11642
  async transaction(transactionCB, logger) {
11484
- try {
11485
- await this.query("BEGIN", void 0, logger);
11486
- const ret = await transactionCB(this, logger);
11487
- await this.query("COMMIT", void 0, logger);
11488
- return ret;
11489
- } catch (e) {
11490
- await this.query("ROLLBACK", void 0, logger);
11491
- throw e;
11492
- }
11643
+ const ret = await this.executor?.transaction(async (transactionExecutor) => {
11644
+ const newDB = new _Database({
11645
+ executor: transactionExecutor
11646
+ });
11647
+ await newDB.connect();
11648
+ const ret2 = await transactionCB(newDB, logger);
11649
+ return ret2;
11650
+ });
11651
+ return ret;
11493
11652
  }
11494
11653
  createDatabase(dbName) {
11495
11654
  return new CreateDatabase(this, dbName);
@@ -11527,12 +11686,12 @@ var Database = class {
11527
11686
  };
11528
11687
  async function getExecutor(config) {
11529
11688
  switch (config.dialect) {
11689
+ case "pg-pool":
11690
+ return await postgresExecutor(config, true);
11530
11691
  case "node:sqlite":
11531
11692
  return await nativeSqlite(config);
11532
11693
  case "pg":
11533
11694
  return await postgresExecutor(config);
11534
- case "sqlite3":
11535
- return await sqlite3Executor(config);
11536
11695
  default:
11537
11696
  if (config.executor) {
11538
11697
  const e = config.executor;
@@ -12337,9 +12496,11 @@ var DBManager = class {
12337
12496
  this.options?.logger?.debug("setting up db connection [%s]", config.name);
12338
12497
  this.options?.logger?.trace("creating db connection [%s]", config.name);
12339
12498
  const db = new Database({
12499
+ executor: config.executor,
12340
12500
  dialect: config.dialect ? config.dialect : "node:sqlite",
12341
12501
  storage: config.storage ? config.storage : "./db.sqlite3",
12342
12502
  connectionString: config.url,
12503
+ pool: config.pool,
12343
12504
  logger: this.options?.loggerProvider?.getLogger(`${DB_IDENTIFIER}_${config.name}`)
12344
12505
  });
12345
12506
  this.options?.logger?.trace("connecting db connection [%s]", config.name);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "miqro",
3
- "version": "7.3.4",
3
+ "version": "7.3.6",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "build/esm/src/lib.js",
@@ -46,7 +46,7 @@
46
46
  "@miqro/jsx-dom": "^1.0.6",
47
47
  "@miqro/jsx-node": "^1.0.9",
48
48
  "@miqro/parser": "^2.0.6",
49
- "@miqro/query": "^0.0.9",
49
+ "@miqro/query": "^0.1.1",
50
50
  "@miqro/runner": "^2.0.3",
51
51
  "@miqro/test": "^0.2.10",
52
52
  "@miqro/test-http": "^0.1.4",