@prisma/adapter-mariadb 7.10.0-dev.2 → 7.10.0-dev.21

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.d.mts CHANGED
@@ -38,14 +38,20 @@ export declare class PrismaMariaDb implements SqlDriverAdapterFactory {
38
38
  #private;
39
39
  readonly provider = "mysql";
40
40
  readonly adapterName: string;
41
- constructor(config: mariadb.PoolConfig | string, options?: PrismaMariadbOptions);
41
+ /**
42
+ * Accepts either connection settings for a pool the adapter creates and owns, or an existing
43
+ * pool. Settings the adapter would normally apply to its own pool (such as defaulting
44
+ * `prepareCacheLength` to 0) are not applied to an externally created pool.
45
+ */
46
+ constructor(poolOrConfig: mariadb.Pool | mariadb.PoolConfig | string, options?: PrismaMariadbOptions);
42
47
  connect(): Promise<PrismaMariaDbAdapter>;
43
48
  }
44
49
 
45
50
  declare class PrismaMariaDbAdapter extends MariaDbQueryable<mariadb.Pool> implements SqlDriverAdapter {
46
51
  private readonly capabilities;
47
52
  protected readonly mariadbOptions?: PrismaMariadbOptions | undefined;
48
- constructor(client: mariadb.Pool, capabilities: Capabilities, mariadbOptions?: PrismaMariadbOptions | undefined);
53
+ private readonly release?;
54
+ constructor(client: mariadb.Pool, capabilities: Capabilities, mariadbOptions?: PrismaMariadbOptions | undefined, release?: (() => Promise<void>) | undefined);
49
55
  executeScript(_script: string): Promise<void>;
50
56
  getConnectionInfo(): ConnectionInfo;
51
57
  startTransaction(isolationLevel?: IsolationLevel): Promise<Transaction>;
@@ -53,13 +59,18 @@ declare class PrismaMariaDbAdapter extends MariaDbQueryable<mariadb.Pool> implem
53
59
  underlyingDriver(): mariadb.Pool;
54
60
  }
55
61
 
56
- declare type PrismaMariadbOptions = {
62
+ export declare type PrismaMariadbOptions = {
57
63
  /** The name of the database to use in generated queries */
58
64
  database?: string;
59
65
  /** Use the driver's text protocol (`query`) instead of the binary protocol (`execute`). */
60
66
  useTextProtocol?: boolean;
61
67
  /** Callback attached to transaction connection `error` events. */
62
68
  onConnectionError?: (err: mariadb.SqlError) => void;
69
+ /**
70
+ * Whether to call `pool.end()` on an externally provided pool when the adapter is disposed.
71
+ * Defaults to `false`.
72
+ */
73
+ disposeExternalPool?: boolean;
63
74
  };
64
75
 
65
76
  export { }
package/dist/index.d.ts CHANGED
@@ -38,14 +38,20 @@ export declare class PrismaMariaDb implements SqlDriverAdapterFactory {
38
38
  #private;
39
39
  readonly provider = "mysql";
40
40
  readonly adapterName: string;
41
- constructor(config: mariadb.PoolConfig | string, options?: PrismaMariadbOptions);
41
+ /**
42
+ * Accepts either connection settings for a pool the adapter creates and owns, or an existing
43
+ * pool. Settings the adapter would normally apply to its own pool (such as defaulting
44
+ * `prepareCacheLength` to 0) are not applied to an externally created pool.
45
+ */
46
+ constructor(poolOrConfig: mariadb.Pool | mariadb.PoolConfig | string, options?: PrismaMariadbOptions);
42
47
  connect(): Promise<PrismaMariaDbAdapter>;
43
48
  }
44
49
 
45
50
  declare class PrismaMariaDbAdapter extends MariaDbQueryable<mariadb.Pool> implements SqlDriverAdapter {
46
51
  private readonly capabilities;
47
52
  protected readonly mariadbOptions?: PrismaMariadbOptions | undefined;
48
- constructor(client: mariadb.Pool, capabilities: Capabilities, mariadbOptions?: PrismaMariadbOptions | undefined);
53
+ private readonly release?;
54
+ constructor(client: mariadb.Pool, capabilities: Capabilities, mariadbOptions?: PrismaMariadbOptions | undefined, release?: (() => Promise<void>) | undefined);
49
55
  executeScript(_script: string): Promise<void>;
50
56
  getConnectionInfo(): ConnectionInfo;
51
57
  startTransaction(isolationLevel?: IsolationLevel): Promise<Transaction>;
@@ -53,13 +59,18 @@ declare class PrismaMariaDbAdapter extends MariaDbQueryable<mariadb.Pool> implem
53
59
  underlyingDriver(): mariadb.Pool;
54
60
  }
55
61
 
56
- declare type PrismaMariadbOptions = {
62
+ export declare type PrismaMariadbOptions = {
57
63
  /** The name of the database to use in generated queries */
58
64
  database?: string;
59
65
  /** Use the driver's text protocol (`query`) instead of the binary protocol (`execute`). */
60
66
  useTextProtocol?: boolean;
61
67
  /** Callback attached to transaction connection `error` events. */
62
68
  onConnectionError?: (err: mariadb.SqlError) => void;
69
+ /**
70
+ * Whether to call `pool.end()` on an externally provided pool when the adapter is disposed.
71
+ * Defaults to `false`.
72
+ */
73
+ disposeExternalPool?: boolean;
63
74
  };
64
75
 
65
76
  export { }
package/dist/index.js CHANGED
@@ -371,7 +371,7 @@ var MariaDbTransaction = class extends MariaDbQueryable {
371
371
  this.onError(err);
372
372
  } finally {
373
373
  this.cleanup?.();
374
- await this.client.end();
374
+ await releaseConnection(this.client);
375
375
  }
376
376
  }
377
377
  async rollback() {
@@ -382,7 +382,7 @@ var MariaDbTransaction = class extends MariaDbQueryable {
382
382
  this.onError(err);
383
383
  } finally {
384
384
  this.cleanup?.();
385
- await this.client.end();
385
+ await releaseConnection(this.client);
386
386
  }
387
387
  }
388
388
  async createSavepoint(name2) {
@@ -396,10 +396,11 @@ var MariaDbTransaction = class extends MariaDbQueryable {
396
396
  }
397
397
  };
398
398
  var PrismaMariaDbAdapter = class extends MariaDbQueryable {
399
- constructor(client, capabilities, mariadbOptions) {
399
+ constructor(client, capabilities, mariadbOptions, release) {
400
400
  super(client, mariadbOptions);
401
401
  this.capabilities = capabilities;
402
402
  this.mariadbOptions = mariadbOptions;
403
+ this.release = release;
403
404
  }
404
405
  executeScript(_script) {
405
406
  throw new Error("Not implemented yet");
@@ -423,7 +424,7 @@ var PrismaMariaDbAdapter = class extends MariaDbQueryable {
423
424
  };
424
425
  conn.on("error", onError);
425
426
  const cleanup = () => {
426
- conn.removeListener("error", onError);
427
+ conn.off("error", onError);
427
428
  };
428
429
  try {
429
430
  const tx = new MariaDbTransaction(conn, this.mariadbOptions, options, cleanup);
@@ -437,13 +438,16 @@ var PrismaMariaDbAdapter = class extends MariaDbQueryable {
437
438
  await tx.conn.query({ sql: "BEGIN" }).catch(this.onError.bind(this));
438
439
  return tx;
439
440
  } catch (error) {
440
- await conn.end();
441
- cleanup();
441
+ try {
442
+ cleanup();
443
+ } finally {
444
+ await releaseConnection(conn);
445
+ }
442
446
  this.onError(error);
443
447
  }
444
448
  }
445
449
  async dispose() {
446
- await this.client.end();
450
+ return this.release?.();
447
451
  }
448
452
  underlyingDriver() {
449
453
  return this.client;
@@ -453,47 +457,73 @@ var PrismaMariaDbAdapterFactory = class {
453
457
  provider = "mysql";
454
458
  adapterName = name;
455
459
  #capabilities;
456
- #config;
460
+ #poolOrConfig;
457
461
  #options;
458
- constructor(config, options) {
459
- if (typeof config === "string") {
460
- try {
461
- const url = new URL(config);
462
- if (!url.searchParams.has("prepareCacheLength")) {
463
- url.searchParams.set("prepareCacheLength", "0");
464
- }
465
- this.#config = rewriteConnectionString(url).toString();
466
- } catch (error) {
467
- debug("Error parsing connection string: %O", error);
468
- this.#config = config;
469
- }
470
- } else {
471
- if (config.prepareCacheLength === void 0) {
472
- this.#config = { ...config, prepareCacheLength: 0 };
473
- } else {
474
- this.#config = config;
475
- }
476
- }
462
+ #externalPoolClaimed = false;
463
+ /**
464
+ * Accepts either connection settings for a pool the adapter creates and owns, or an existing
465
+ * pool. Settings the adapter would normally apply to its own pool (such as defaulting
466
+ * `prepareCacheLength` to 0) are not applied to an externally created pool.
467
+ */
468
+ constructor(poolOrConfig, options) {
469
+ this.#poolOrConfig = isPool(poolOrConfig) ? { type: "pool", pool: poolOrConfig } : { type: "config", config: normalizeConfig(poolOrConfig) };
477
470
  this.#options = options;
478
471
  }
479
472
  async connect() {
480
- let pool;
481
- try {
482
- pool = mariadb.createPool(this.#config);
483
- } catch (error) {
484
- if (error instanceof Error && error.message.startsWith("error parsing connection string")) {
473
+ const poolOrConfig = this.#poolOrConfig;
474
+ const ownsPool = poolOrConfig.type === "config" || this.#options?.disposeExternalPool === true;
475
+ if (poolOrConfig.type === "pool" && ownsPool) {
476
+ if (this.#externalPoolClaimed) {
485
477
  throw new Error(
486
- "error parsing connection string, format must be 'mariadb://[<user>[:<password>]@]<host>[:<port>]/[<db>[?<opt1>=<value1>[&<opt2>=<value2>]]]'"
478
+ "connect() can only be called once when `disposeExternalPool` is true, because the adapter ends the pool it was given and cannot create a replacement from it. Either pass connection settings instead of a pool, or set `disposeExternalPool: false` and end the pool yourself once you no longer need the adapter."
487
479
  );
488
480
  }
489
- throw error;
481
+ this.#externalPoolClaimed = true;
490
482
  }
483
+ const pool = poolOrConfig.type === "pool" ? poolOrConfig.pool : createPool2(poolOrConfig.config);
491
484
  if (this.#capabilities === void 0) {
492
485
  this.#capabilities = await getCapabilities(pool);
493
486
  }
494
- return new PrismaMariaDbAdapter(pool, this.#capabilities, this.#options);
487
+ return new PrismaMariaDbAdapter(pool, this.#capabilities, this.#options, async () => {
488
+ if (ownsPool) {
489
+ await pool.end();
490
+ }
491
+ });
495
492
  }
496
493
  };
494
+ function isPool(poolOrConfig) {
495
+ return typeof poolOrConfig === "object" && typeof poolOrConfig.getConnection === "function";
496
+ }
497
+ function normalizeConfig(config) {
498
+ if (typeof config === "string") {
499
+ try {
500
+ const url = new URL(config);
501
+ if (!url.searchParams.has("prepareCacheLength")) {
502
+ url.searchParams.set("prepareCacheLength", "0");
503
+ }
504
+ return rewriteConnectionString(url).toString();
505
+ } catch {
506
+ debug("Failed to parse the connection string, passing it to the driver as-is");
507
+ return config;
508
+ }
509
+ }
510
+ if (config.prepareCacheLength === void 0) {
511
+ return { ...config, prepareCacheLength: 0 };
512
+ }
513
+ return config;
514
+ }
515
+ function createPool2(config) {
516
+ try {
517
+ return mariadb.createPool(config);
518
+ } catch (error) {
519
+ if (error instanceof Error && error.message.startsWith("error parsing connection string")) {
520
+ throw new Error(
521
+ "error parsing connection string, format must be 'mariadb://[<user>[:<password>]@]<host>[:<port>]/[<db>[?<opt1>=<value1>[&<opt2>=<value2>]]]'"
522
+ );
523
+ }
524
+ throw error;
525
+ }
526
+ }
497
527
  async function getCapabilities(pool) {
498
528
  const tag = "[js::getCapabilities]";
499
529
  try {
@@ -530,6 +560,14 @@ function rewriteConnectionString(url) {
530
560
  }
531
561
  return url;
532
562
  }
563
+ async function releaseConnection(conn) {
564
+ const poolConn = conn;
565
+ if (typeof poolConn.release === "function") {
566
+ await poolConn.release();
567
+ } else {
568
+ await conn.end();
569
+ }
570
+ }
533
571
  // Annotate the CommonJS export names for ESM import in node:
534
572
  0 && (module.exports = {
535
573
  PrismaMariaDb
package/dist/index.mjs CHANGED
@@ -335,7 +335,7 @@ var MariaDbTransaction = class extends MariaDbQueryable {
335
335
  this.onError(err);
336
336
  } finally {
337
337
  this.cleanup?.();
338
- await this.client.end();
338
+ await releaseConnection(this.client);
339
339
  }
340
340
  }
341
341
  async rollback() {
@@ -346,7 +346,7 @@ var MariaDbTransaction = class extends MariaDbQueryable {
346
346
  this.onError(err);
347
347
  } finally {
348
348
  this.cleanup?.();
349
- await this.client.end();
349
+ await releaseConnection(this.client);
350
350
  }
351
351
  }
352
352
  async createSavepoint(name2) {
@@ -360,10 +360,11 @@ var MariaDbTransaction = class extends MariaDbQueryable {
360
360
  }
361
361
  };
362
362
  var PrismaMariaDbAdapter = class extends MariaDbQueryable {
363
- constructor(client, capabilities, mariadbOptions) {
363
+ constructor(client, capabilities, mariadbOptions, release) {
364
364
  super(client, mariadbOptions);
365
365
  this.capabilities = capabilities;
366
366
  this.mariadbOptions = mariadbOptions;
367
+ this.release = release;
367
368
  }
368
369
  executeScript(_script) {
369
370
  throw new Error("Not implemented yet");
@@ -387,7 +388,7 @@ var PrismaMariaDbAdapter = class extends MariaDbQueryable {
387
388
  };
388
389
  conn.on("error", onError);
389
390
  const cleanup = () => {
390
- conn.removeListener("error", onError);
391
+ conn.off("error", onError);
391
392
  };
392
393
  try {
393
394
  const tx = new MariaDbTransaction(conn, this.mariadbOptions, options, cleanup);
@@ -401,13 +402,16 @@ var PrismaMariaDbAdapter = class extends MariaDbQueryable {
401
402
  await tx.conn.query({ sql: "BEGIN" }).catch(this.onError.bind(this));
402
403
  return tx;
403
404
  } catch (error) {
404
- await conn.end();
405
- cleanup();
405
+ try {
406
+ cleanup();
407
+ } finally {
408
+ await releaseConnection(conn);
409
+ }
406
410
  this.onError(error);
407
411
  }
408
412
  }
409
413
  async dispose() {
410
- await this.client.end();
414
+ return this.release?.();
411
415
  }
412
416
  underlyingDriver() {
413
417
  return this.client;
@@ -417,47 +421,73 @@ var PrismaMariaDbAdapterFactory = class {
417
421
  provider = "mysql";
418
422
  adapterName = name;
419
423
  #capabilities;
420
- #config;
424
+ #poolOrConfig;
421
425
  #options;
422
- constructor(config, options) {
423
- if (typeof config === "string") {
424
- try {
425
- const url = new URL(config);
426
- if (!url.searchParams.has("prepareCacheLength")) {
427
- url.searchParams.set("prepareCacheLength", "0");
428
- }
429
- this.#config = rewriteConnectionString(url).toString();
430
- } catch (error) {
431
- debug("Error parsing connection string: %O", error);
432
- this.#config = config;
433
- }
434
- } else {
435
- if (config.prepareCacheLength === void 0) {
436
- this.#config = { ...config, prepareCacheLength: 0 };
437
- } else {
438
- this.#config = config;
439
- }
440
- }
426
+ #externalPoolClaimed = false;
427
+ /**
428
+ * Accepts either connection settings for a pool the adapter creates and owns, or an existing
429
+ * pool. Settings the adapter would normally apply to its own pool (such as defaulting
430
+ * `prepareCacheLength` to 0) are not applied to an externally created pool.
431
+ */
432
+ constructor(poolOrConfig, options) {
433
+ this.#poolOrConfig = isPool(poolOrConfig) ? { type: "pool", pool: poolOrConfig } : { type: "config", config: normalizeConfig(poolOrConfig) };
441
434
  this.#options = options;
442
435
  }
443
436
  async connect() {
444
- let pool;
445
- try {
446
- pool = mariadb.createPool(this.#config);
447
- } catch (error) {
448
- if (error instanceof Error && error.message.startsWith("error parsing connection string")) {
437
+ const poolOrConfig = this.#poolOrConfig;
438
+ const ownsPool = poolOrConfig.type === "config" || this.#options?.disposeExternalPool === true;
439
+ if (poolOrConfig.type === "pool" && ownsPool) {
440
+ if (this.#externalPoolClaimed) {
449
441
  throw new Error(
450
- "error parsing connection string, format must be 'mariadb://[<user>[:<password>]@]<host>[:<port>]/[<db>[?<opt1>=<value1>[&<opt2>=<value2>]]]'"
442
+ "connect() can only be called once when `disposeExternalPool` is true, because the adapter ends the pool it was given and cannot create a replacement from it. Either pass connection settings instead of a pool, or set `disposeExternalPool: false` and end the pool yourself once you no longer need the adapter."
451
443
  );
452
444
  }
453
- throw error;
445
+ this.#externalPoolClaimed = true;
454
446
  }
447
+ const pool = poolOrConfig.type === "pool" ? poolOrConfig.pool : createPool2(poolOrConfig.config);
455
448
  if (this.#capabilities === void 0) {
456
449
  this.#capabilities = await getCapabilities(pool);
457
450
  }
458
- return new PrismaMariaDbAdapter(pool, this.#capabilities, this.#options);
451
+ return new PrismaMariaDbAdapter(pool, this.#capabilities, this.#options, async () => {
452
+ if (ownsPool) {
453
+ await pool.end();
454
+ }
455
+ });
459
456
  }
460
457
  };
458
+ function isPool(poolOrConfig) {
459
+ return typeof poolOrConfig === "object" && typeof poolOrConfig.getConnection === "function";
460
+ }
461
+ function normalizeConfig(config) {
462
+ if (typeof config === "string") {
463
+ try {
464
+ const url = new URL(config);
465
+ if (!url.searchParams.has("prepareCacheLength")) {
466
+ url.searchParams.set("prepareCacheLength", "0");
467
+ }
468
+ return rewriteConnectionString(url).toString();
469
+ } catch {
470
+ debug("Failed to parse the connection string, passing it to the driver as-is");
471
+ return config;
472
+ }
473
+ }
474
+ if (config.prepareCacheLength === void 0) {
475
+ return { ...config, prepareCacheLength: 0 };
476
+ }
477
+ return config;
478
+ }
479
+ function createPool2(config) {
480
+ try {
481
+ return mariadb.createPool(config);
482
+ } catch (error) {
483
+ if (error instanceof Error && error.message.startsWith("error parsing connection string")) {
484
+ throw new Error(
485
+ "error parsing connection string, format must be 'mariadb://[<user>[:<password>]@]<host>[:<port>]/[<db>[?<opt1>=<value1>[&<opt2>=<value2>]]]'"
486
+ );
487
+ }
488
+ throw error;
489
+ }
490
+ }
461
491
  async function getCapabilities(pool) {
462
492
  const tag = "[js::getCapabilities]";
463
493
  try {
@@ -494,6 +524,14 @@ function rewriteConnectionString(url) {
494
524
  }
495
525
  return url;
496
526
  }
527
+ async function releaseConnection(conn) {
528
+ const poolConn = conn;
529
+ if (typeof poolConn.release === "function") {
530
+ await poolConn.release();
531
+ } else {
532
+ await conn.end();
533
+ }
534
+ }
497
535
  export {
498
536
  PrismaMariaDbAdapterFactory as PrismaMariaDb
499
537
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prisma/adapter-mariadb",
3
- "version": "7.10.0-dev.2",
3
+ "version": "7.10.0-dev.21",
4
4
  "description": "Prisma's driver adapter for \"mariadb\"",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -32,7 +32,10 @@
32
32
  "sideEffects": false,
33
33
  "dependencies": {
34
34
  "mariadb": "3.4.5",
35
- "@prisma/driver-adapter-utils": "7.10.0-dev.2"
35
+ "@prisma/driver-adapter-utils": "7.10.0-dev.21"
36
+ },
37
+ "devDependencies": {
38
+ "@prisma/debug": "7.10.0-dev.21"
36
39
  },
37
40
  "scripts": {
38
41
  "dev": "DEV=true tsx helpers/build.ts",