spooder 4.5.13 → 4.6.0

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/README.md CHANGED
@@ -1471,18 +1471,18 @@ const db = await mysql.createConnection({
1471
1471
  > [!IMPORTANT]
1472
1472
  > MySQL requires the optional dependency `mysql2` to be installed - this is not automatically installed with spooder. This will be replaced when bun:sql supports MySQL natively.
1473
1473
 
1474
- Database initiation and schema updating can be streamlined with the `db_init_schema_DRIVER` functions. The following examples are equivalent to the above ones.
1474
+ Database initiation and schema updating can be streamlined with the `db_init_DRIVER` functions. The following examples are equivalent to the above ones.
1475
1475
 
1476
1476
  ```ts
1477
1477
  // sqlite example
1478
- import { db_init_schema_sqlite } from 'spooder';
1479
- const db = await db_init_schema_sqlite('./database.sqlite', './schema');
1478
+ import { db_init_sqlite } from 'spooder';
1479
+ const db = await db_init_sqlite('./database.sqlite', './schema');
1480
1480
  ```
1481
1481
 
1482
1482
  ```ts
1483
1483
  // mysql example
1484
- import { db_init_schema_mysql } from 'spooder';
1485
- const db = await db_init_schema_mysql({
1484
+ import { db_init_mysql } from 'spooder';
1485
+ const db = await db_init_mysql({
1486
1486
  // connection options
1487
1487
  // see https://github.com/mysqljs/mysql#connection-options
1488
1488
  }, './schema');
@@ -1490,14 +1490,14 @@ const db = await db_init_schema_mysql({
1490
1490
 
1491
1491
  ### Pooling
1492
1492
 
1493
- MySQL supports connection pooling. spooder allows you to create a connection pool instead of a single connection.
1493
+ Providing `true` to the `pool` parameter of `db_init_mysql` will return a connection pool instead of a single connection.
1494
1494
 
1495
1495
  ```ts
1496
- import { db_init_schema_mysql_pool } from 'spooder';
1497
- const pool = await db_init_schema_mysql_pool({
1496
+ import { db_init_mysql } from 'spooder';
1497
+ const pool = await db_init_mysql({
1498
1498
  // connection options
1499
1499
  connectionLimit: 10
1500
- });
1500
+ }, './schema', true);
1501
1501
 
1502
1502
  const connection = await pool.getConnection();
1503
1503
  ```
package/bun.lock CHANGED
@@ -12,13 +12,13 @@
12
12
  },
13
13
  },
14
14
  "packages": {
15
- "@types/bun": ["@types/bun@1.2.11", "", { "dependencies": { "bun-types": "1.2.11" } }, "sha512-ZLbbI91EmmGwlWTRWuV6J19IUiUC5YQ3TCEuSHI3usIP75kuoA8/0PVF+LTrbEnVc8JIhpElWOxv1ocI1fJBbw=="],
15
+ "@types/bun": ["@types/bun@1.2.12", "", { "dependencies": { "bun-types": "1.2.12" } }, "sha512-lY/GQTXDGsolT/TiH72p1tuyUORuRrdV7VwOTOjDOt8uTBJQOJc5zz3ufwwDl0VBaoxotSk4LdP0hhjLJ6ypIQ=="],
16
16
 
17
- "@types/node": ["@types/node@22.15.3", "", { "dependencies": { "undici-types": "~6.21.0" } }, "sha512-lX7HFZeHf4QG/J7tBZqrCAXwz9J5RD56Y6MpP0eJkka8p+K0RY/yBTW7CYFJ4VGCclxqOLKmiGP5juQc6MKgcw=="],
17
+ "@types/node": ["@types/node@22.15.12", "", { "dependencies": { "undici-types": "~6.21.0" } }, "sha512-K0fpC/ZVeb8G9rm7bH7vI0KAec4XHEhBam616nVJCV51bKzJ6oA3luG4WdKoaztxe70QaNjS/xBmcDLmr4PiGw=="],
18
18
 
19
19
  "aws-ssl-profiles": ["aws-ssl-profiles@1.1.2", "", {}, "sha512-NZKeq9AfyQvEeNlN0zSYAaWrmBffJh3IELMZfRpJVWgrpEbtEpnjvzqBPf+mxoI287JohRDoa+/nsfqqiZmF6g=="],
20
20
 
21
- "bun-types": ["bun-types@1.2.11", "", { "dependencies": { "@types/node": "*" } }, "sha512-dbkp5Lo8HDrXkLrONm6bk+yiiYQSntvFUzQp0v3pzTAsXk6FtgVMjdQ+lzFNVAmQFUkPQZ3WMZqH5tTo+Dp/IA=="],
21
+ "bun-types": ["bun-types@1.2.12", "", { "dependencies": { "@types/node": "*" } }, "sha512-tvWMx5vPqbRXgE8WUZI94iS1xAYs8bkqESR9cxBB1Wi+urvfTrF1uzuDgBHFAdO0+d2lmsbG3HmeKMvUyj6pWA=="],
22
22
 
23
23
  "denque": ["denque@2.1.0", "", {}, "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw=="],
24
24
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "spooder",
3
3
  "type": "module",
4
- "version": "4.5.13",
4
+ "version": "4.6.0",
5
5
  "exports": {
6
6
  ".": {
7
7
  "bun": "./src/api.ts",
package/src/api.ts CHANGED
@@ -449,15 +449,18 @@ export async function db_update_schema_mysql(db: mysql_types.Connection, schema_
449
449
  await db.commit();
450
450
  }
451
451
 
452
- export async function db_init_schema_sqlite(db_path: string, schema_dir: string): Promise<Database> {
452
+ export async function db_init_sqlite(db_path: string, schema_dir?: string): Promise<Database> {
453
453
  const db = new Database(db_path, { create: true });
454
- await db_update_schema_sqlite(db, schema_dir);
454
+
455
+ if (schema_dir !== undefined)
456
+ await db_update_schema_sqlite(db, schema_dir);
457
+
455
458
  return db;
456
459
  }
457
460
 
458
- async function _db_init_schema_mysql(db_info: mysql_types.ConnectionOptions, schema_dir: string, pool = false): Promise<mysql_types.Pool | mysql_types.Connection> {
461
+ export async function db_init_mysql<T extends boolean = false>(db_info: mysql_types.ConnectionOptions, schema_dir?: string, pool: T = false as T): Promise<T extends true ? mysql_types.Pool : mysql_types.Connection> {
459
462
  if (mysql === undefined)
460
- throw new Error('{db_init_schema_mysql} cannot be called without optional dependency {mysql2} installed');
463
+ throw new Error('db_init_mysql cannot be called without optional dependency {mysql2} installed');
461
464
 
462
465
  // required for parsing multiple statements from schema files
463
466
  db_info.multipleStatements = true;
@@ -466,26 +469,22 @@ async function _db_init_schema_mysql(db_info: mysql_types.ConnectionOptions, sch
466
469
  const pool = mysql.createPool(db_info);
467
470
  const connection = await pool.getConnection();
468
471
 
469
- await db_update_schema_mysql(connection, schema_dir);
472
+ if (schema_dir !== undefined)
473
+ await db_update_schema_mysql(connection, schema_dir);
474
+
470
475
  connection.release();
471
476
 
472
- return pool;
477
+ return pool as any;
473
478
  } else {
474
479
  const connection = await mysql.createConnection(db_info);
475
- await db_update_schema_mysql(connection, schema_dir);
480
+
481
+ if (schema_dir !== undefined)
482
+ await db_update_schema_mysql(connection, schema_dir);
476
483
 
477
- return connection;
484
+ return connection as any;
478
485
  }
479
486
  }
480
487
 
481
- export async function db_init_schema_mysql_pool(db_info: mysql_types.ConnectionOptions, schema_dir: string): Promise<mysql_types.Pool> {
482
- return await _db_init_schema_mysql(db_info, schema_dir, true) as mysql_types.Pool;
483
- }
484
-
485
- export async function db_init_schema_mysql(db_info: mysql_types.ConnectionOptions, schema_dir: string): Promise<mysql_types.Connection> {
486
- return await _db_init_schema_mysql(db_info, schema_dir, false) as mysql_types.Connection;
487
- }
488
-
489
488
  export type CookieOptions = {
490
489
  same_site?: 'Strict' | 'Lax' | 'None',
491
490
  secure?: boolean,