@prisma/adapter-mariadb 7.10.0-dev.4 → 7.10.0-dev.5

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
@@ -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");
@@ -443,7 +444,7 @@ var PrismaMariaDbAdapter = class extends MariaDbQueryable {
443
444
  }
444
445
  }
445
446
  async dispose() {
446
- await this.client.end();
447
+ return this.release?.();
447
448
  }
448
449
  underlyingDriver() {
449
450
  return this.client;
@@ -453,47 +454,73 @@ var PrismaMariaDbAdapterFactory = class {
453
454
  provider = "mysql";
454
455
  adapterName = name;
455
456
  #capabilities;
456
- #config;
457
+ #poolOrConfig;
457
458
  #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
- }
459
+ #externalPoolClaimed = false;
460
+ /**
461
+ * Accepts either connection settings for a pool the adapter creates and owns, or an existing
462
+ * pool. Settings the adapter would normally apply to its own pool (such as defaulting
463
+ * `prepareCacheLength` to 0) are not applied to an externally created pool.
464
+ */
465
+ constructor(poolOrConfig, options) {
466
+ this.#poolOrConfig = isPool(poolOrConfig) ? { type: "pool", pool: poolOrConfig } : { type: "config", config: normalizeConfig(poolOrConfig) };
477
467
  this.#options = options;
478
468
  }
479
469
  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")) {
470
+ const poolOrConfig = this.#poolOrConfig;
471
+ const ownsPool = poolOrConfig.type === "config" || this.#options?.disposeExternalPool === true;
472
+ if (poolOrConfig.type === "pool" && ownsPool) {
473
+ if (this.#externalPoolClaimed) {
485
474
  throw new Error(
486
- "error parsing connection string, format must be 'mariadb://[<user>[:<password>]@]<host>[:<port>]/[<db>[?<opt1>=<value1>[&<opt2>=<value2>]]]'"
475
+ "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
476
  );
488
477
  }
489
- throw error;
478
+ this.#externalPoolClaimed = true;
490
479
  }
480
+ const pool = poolOrConfig.type === "pool" ? poolOrConfig.pool : createPool2(poolOrConfig.config);
491
481
  if (this.#capabilities === void 0) {
492
482
  this.#capabilities = await getCapabilities(pool);
493
483
  }
494
- return new PrismaMariaDbAdapter(pool, this.#capabilities, this.#options);
484
+ return new PrismaMariaDbAdapter(pool, this.#capabilities, this.#options, async () => {
485
+ if (ownsPool) {
486
+ await pool.end();
487
+ }
488
+ });
495
489
  }
496
490
  };
491
+ function isPool(poolOrConfig) {
492
+ return typeof poolOrConfig === "object" && typeof poolOrConfig.getConnection === "function";
493
+ }
494
+ function normalizeConfig(config) {
495
+ if (typeof config === "string") {
496
+ try {
497
+ const url = new URL(config);
498
+ if (!url.searchParams.has("prepareCacheLength")) {
499
+ url.searchParams.set("prepareCacheLength", "0");
500
+ }
501
+ return rewriteConnectionString(url).toString();
502
+ } catch {
503
+ debug("Failed to parse the connection string, passing it to the driver as-is");
504
+ return config;
505
+ }
506
+ }
507
+ if (config.prepareCacheLength === void 0) {
508
+ return { ...config, prepareCacheLength: 0 };
509
+ }
510
+ return config;
511
+ }
512
+ function createPool2(config) {
513
+ try {
514
+ return mariadb.createPool(config);
515
+ } catch (error) {
516
+ if (error instanceof Error && error.message.startsWith("error parsing connection string")) {
517
+ throw new Error(
518
+ "error parsing connection string, format must be 'mariadb://[<user>[:<password>]@]<host>[:<port>]/[<db>[?<opt1>=<value1>[&<opt2>=<value2>]]]'"
519
+ );
520
+ }
521
+ throw error;
522
+ }
523
+ }
497
524
  async function getCapabilities(pool) {
498
525
  const tag = "[js::getCapabilities]";
499
526
  try {
package/dist/index.mjs CHANGED
@@ -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");
@@ -407,7 +408,7 @@ var PrismaMariaDbAdapter = class extends MariaDbQueryable {
407
408
  }
408
409
  }
409
410
  async dispose() {
410
- await this.client.end();
411
+ return this.release?.();
411
412
  }
412
413
  underlyingDriver() {
413
414
  return this.client;
@@ -417,47 +418,73 @@ var PrismaMariaDbAdapterFactory = class {
417
418
  provider = "mysql";
418
419
  adapterName = name;
419
420
  #capabilities;
420
- #config;
421
+ #poolOrConfig;
421
422
  #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
- }
423
+ #externalPoolClaimed = false;
424
+ /**
425
+ * Accepts either connection settings for a pool the adapter creates and owns, or an existing
426
+ * pool. Settings the adapter would normally apply to its own pool (such as defaulting
427
+ * `prepareCacheLength` to 0) are not applied to an externally created pool.
428
+ */
429
+ constructor(poolOrConfig, options) {
430
+ this.#poolOrConfig = isPool(poolOrConfig) ? { type: "pool", pool: poolOrConfig } : { type: "config", config: normalizeConfig(poolOrConfig) };
441
431
  this.#options = options;
442
432
  }
443
433
  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")) {
434
+ const poolOrConfig = this.#poolOrConfig;
435
+ const ownsPool = poolOrConfig.type === "config" || this.#options?.disposeExternalPool === true;
436
+ if (poolOrConfig.type === "pool" && ownsPool) {
437
+ if (this.#externalPoolClaimed) {
449
438
  throw new Error(
450
- "error parsing connection string, format must be 'mariadb://[<user>[:<password>]@]<host>[:<port>]/[<db>[?<opt1>=<value1>[&<opt2>=<value2>]]]'"
439
+ "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
440
  );
452
441
  }
453
- throw error;
442
+ this.#externalPoolClaimed = true;
454
443
  }
444
+ const pool = poolOrConfig.type === "pool" ? poolOrConfig.pool : createPool2(poolOrConfig.config);
455
445
  if (this.#capabilities === void 0) {
456
446
  this.#capabilities = await getCapabilities(pool);
457
447
  }
458
- return new PrismaMariaDbAdapter(pool, this.#capabilities, this.#options);
448
+ return new PrismaMariaDbAdapter(pool, this.#capabilities, this.#options, async () => {
449
+ if (ownsPool) {
450
+ await pool.end();
451
+ }
452
+ });
459
453
  }
460
454
  };
455
+ function isPool(poolOrConfig) {
456
+ return typeof poolOrConfig === "object" && typeof poolOrConfig.getConnection === "function";
457
+ }
458
+ function normalizeConfig(config) {
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
+ return rewriteConnectionString(url).toString();
466
+ } catch {
467
+ debug("Failed to parse the connection string, passing it to the driver as-is");
468
+ return config;
469
+ }
470
+ }
471
+ if (config.prepareCacheLength === void 0) {
472
+ return { ...config, prepareCacheLength: 0 };
473
+ }
474
+ return config;
475
+ }
476
+ function createPool2(config) {
477
+ try {
478
+ return mariadb.createPool(config);
479
+ } catch (error) {
480
+ if (error instanceof Error && error.message.startsWith("error parsing connection string")) {
481
+ throw new Error(
482
+ "error parsing connection string, format must be 'mariadb://[<user>[:<password>]@]<host>[:<port>]/[<db>[?<opt1>=<value1>[&<opt2>=<value2>]]]'"
483
+ );
484
+ }
485
+ throw error;
486
+ }
487
+ }
461
488
  async function getCapabilities(pool) {
462
489
  const tag = "[js::getCapabilities]";
463
490
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prisma/adapter-mariadb",
3
- "version": "7.10.0-dev.4",
3
+ "version": "7.10.0-dev.5",
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.4"
35
+ "@prisma/driver-adapter-utils": "7.10.0-dev.5"
36
+ },
37
+ "devDependencies": {
38
+ "@prisma/debug": "7.10.0-dev.5"
36
39
  },
37
40
  "scripts": {
38
41
  "dev": "DEV=true tsx helpers/build.ts",