spooder 4.4.5 → 4.4.7

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.
Files changed (3) hide show
  1. package/README.md +14 -0
  2. package/package.json +1 -1
  3. package/src/api.ts +25 -7
package/README.md CHANGED
@@ -1398,6 +1398,20 @@ const db = await db_init_schema_mysql({
1398
1398
  }, './schema');
1399
1399
  ```
1400
1400
 
1401
+ ### Pooling
1402
+
1403
+ MySQL supports connection pooling. spooder allows you to create a connection pool instead of a single connection.
1404
+
1405
+ ```ts
1406
+ import { db_init_schema_mysql_pool } from 'spooder';
1407
+ const pool = await db_init_schema_mysql_pool({
1408
+ // connection options
1409
+ connectionLimit: 10
1410
+ });
1411
+
1412
+ const connection = await pool.getConnection();
1413
+ ```
1414
+
1401
1415
  ### Schema Files
1402
1416
 
1403
1417
  The schema directory is expected to contain an SQL file for each table in the database with the file name matching the name of the table.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "spooder",
3
3
  "type": "module",
4
- "version": "4.4.5",
4
+ "version": "4.4.7",
5
5
  "exports": {
6
6
  ".": {
7
7
  "bun": "./src/api.ts",
package/src/api.ts CHANGED
@@ -369,20 +369,38 @@ export async function db_init_schema_sqlite(db_path: string, schema_dir: string)
369
369
  return db;
370
370
  }
371
371
 
372
- export async function db_init_schema_mysql(db_info: mysql_types.ConnectionOptions, schema_dir: string): Promise<mysql_types.Connection> {
372
+ async function _db_init_schema_mysql(db_info: mysql_types.ConnectionOptions, schema_dir: string, pool = false): Promise<mysql_types.Pool | mysql_types.Connection> {
373
373
  if (mysql === undefined)
374
374
  throw new Error('{db_init_schema_mysql} cannot be called without optional dependency {mysql2} installed');
375
375
 
376
376
  // required for parsing multiple statements from schema files
377
377
  db_info.multipleStatements = true;
378
378
 
379
- const db = await mysql.createConnection(db_info);
380
- await db_update_schema_mysql(db, schema_dir);
379
+ if (pool) {
380
+ const pool = mysql.createPool(db_info);
381
+ const connection = await pool.getConnection();
381
382
 
382
- return db;
383
+ await db_update_schema_mysql(connection, schema_dir);
384
+ connection.release();
385
+
386
+ return pool;
387
+ } else {
388
+ const connection = await mysql.createConnection(db_info);
389
+ await db_update_schema_mysql(connection, schema_dir);
390
+
391
+ return connection;
392
+ }
393
+ }
394
+
395
+ export async function db_init_schema_mysql_pool(db_info: mysql_types.ConnectionOptions, schema_dir: string): Promise<mysql_types.Pool> {
396
+ return await _db_init_schema_mysql(db_info, schema_dir, true) as mysql_types.Pool;
397
+ }
398
+
399
+ export async function db_init_schema_mysql(db_info: mysql_types.ConnectionOptions, schema_dir: string): Promise<mysql_types.Connection> {
400
+ return await _db_init_schema_mysql(db_info, schema_dir, false) as mysql_types.Connection;
383
401
  }
384
402
 
385
- type CookieOptions = {
403
+ export type CookieOptions = {
386
404
  same_site?: 'Strict' | 'Lax' | 'None',
387
405
  secure?: boolean,
388
406
  http_only?: boolean,
@@ -529,7 +547,7 @@ function route_directory(route_path: string, dir: string, handler: DirHandler):
529
547
  };
530
548
  }
531
549
 
532
- export function validate_req_json(JSONRequestHandler: JSONRequestHandler): RequestHandler {
550
+ export function validate_req_json(json_handler: JSONRequestHandler): RequestHandler {
533
551
  return async (req: Request, url: URL) => {
534
552
  try {
535
553
  // validate content type header
@@ -542,7 +560,7 @@ export function validate_req_json(JSONRequestHandler: JSONRequestHandler): Reque
542
560
  if (json === null || typeof json !== 'object' || Array.isArray(json))
543
561
  return 400; // Bad Request
544
562
 
545
- return JSONRequestHandler(req, url, json);
563
+ return json_handler(req, url, json);
546
564
  } catch (e) {
547
565
  return 400; // Bad Request
548
566
  }