spooder 4.4.4 → 4.4.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.
- package/README.md +14 -0
- package/package.json +1 -1
- package/src/api.ts +23 -5
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
package/src/api.ts
CHANGED
|
@@ -369,17 +369,35 @@ export async function db_init_schema_sqlite(db_path: string, schema_dir: string)
|
|
|
369
369
|
return db;
|
|
370
370
|
}
|
|
371
371
|
|
|
372
|
-
|
|
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
|
-
|
|
380
|
-
|
|
379
|
+
if (pool) {
|
|
380
|
+
const pool = mysql.createPool(db_info);
|
|
381
|
+
const connection = await pool.getConnection();
|
|
381
382
|
|
|
382
|
-
|
|
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
403
|
type CookieOptions = {
|
|
@@ -712,7 +730,7 @@ export function serve(port: number) {
|
|
|
712
730
|
const request_time = Date.now() - request_start;
|
|
713
731
|
|
|
714
732
|
const is_known_slow = slow_requests.has(req);
|
|
715
|
-
if (slow_request_callback !== null && request_time > slow_request_threshold && is_known_slow)
|
|
733
|
+
if (slow_request_callback !== null && request_time > slow_request_threshold && !is_known_slow)
|
|
716
734
|
slow_request_callback(req, request_time, url);
|
|
717
735
|
|
|
718
736
|
if (is_known_slow)
|