fix(mysql): close database connections gracefully when using Bun

This commit is contained in:
Joakim Carlstein 2024-06-24 15:50:50 +02:00 committed by Joakim Carlstein
parent cf620a191d
commit 57498db248
4 changed files with 52 additions and 24 deletions

View file

@ -0,0 +1,5 @@
---
'@emigrate/mysql': patch
---
Unreference all connections when run using Bun, to not keep the process open unnecessarily long

View file

@ -45,8 +45,8 @@
}, },
"devDependencies": { "devDependencies": {
"@emigrate/tsconfig": "workspace:*", "@emigrate/tsconfig": "workspace:*",
"@types/bun": "1.0.5", "@types/bun": "1.1.2",
"bun-types": "1.0.26" "bun-types": "1.1.8"
}, },
"volta": { "volta": {
"extends": "../../package.json" "extends": "../../package.json"

View file

@ -41,9 +41,11 @@ export type MysqlLoaderOptions = {
connection: ConnectionOptions | string; connection: ConnectionOptions | string;
}; };
const getConnection = async (connection: ConnectionOptions | string) => { const getConnection = async (options: ConnectionOptions | string) => {
if (typeof connection === 'string') { let connection: Connection;
const uri = new URL(connection);
if (typeof options === 'string') {
const uri = new URL(options);
// client side connectTimeout is unstable in mysql2 library // client side connectTimeout is unstable in mysql2 library
// it throws an error you can't catch and crashes node // it throws an error you can't catch and crashes node
@ -51,17 +53,25 @@ const getConnection = async (connection: ConnectionOptions | string) => {
uri.searchParams.set('connectTimeout', '0'); uri.searchParams.set('connectTimeout', '0');
uri.searchParams.set('multipleStatements', 'true'); uri.searchParams.set('multipleStatements', 'true');
return createConnection(uri.toString()); connection = await createConnection(uri.toString());
} } else {
connection = await createConnection({
return createConnection({ ...options,
...connection,
// client side connectTimeout is unstable in mysql2 library // client side connectTimeout is unstable in mysql2 library
// it throws an error you can't catch and crashes node // it throws an error you can't catch and crashes node
// best to leave this at 0 (disabled) // best to leave this at 0 (disabled)
connectTimeout: 0, connectTimeout: 0,
multipleStatements: true, multipleStatements: true,
}); });
}
if (process.isBun) {
// @ts-expect-error the connection is not in the types but it's there
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
connection.connection.stream.unref();
}
return connection;
}; };
const getPool = (connection: PoolOptions | string) => { const getPool = (connection: PoolOptions | string) => {
@ -354,12 +364,6 @@ export const createMysqlLoader = ({ connection }: MysqlLoaderOptions): LoaderPlu
const contents = await fs.readFile(migration.filePath, 'utf8'); const contents = await fs.readFile(migration.filePath, 'utf8');
const conn = await getConnection(connection); const conn = await getConnection(connection);
if (process.isBun) {
// @ts-expect-error the connection is not in the types but it's there
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
conn.connection.stream.unref();
}
try { try {
await conn.query(contents); await conn.query(contents);
} finally { } finally {

27
pnpm-lock.yaml generated
View file

@ -131,11 +131,11 @@ importers:
specifier: workspace:* specifier: workspace:*
version: link:../tsconfig version: link:../tsconfig
'@types/bun': '@types/bun':
specifier: 1.0.5 specifier: 1.1.2
version: 1.0.5 version: 1.1.2
bun-types: bun-types:
specifier: 1.0.26 specifier: 1.1.8
version: 1.0.26 version: 1.1.8
packages/plugin-generate-js: packages/plugin-generate-js:
dependencies: dependencies:
@ -1791,6 +1791,12 @@ packages:
bun-types: 1.0.26 bun-types: 1.0.26
dev: true dev: true
/@types/bun@1.1.2:
resolution: {integrity: sha512-pRBDD3EDqPf83qe95i3EpYu5G2J8bbb78a3736vnCm2K8YWtEE5cvJUq2jkKvJhW07YTfQtbImywIwRhWL8z3Q==}
dependencies:
bun-types: 1.1.8
dev: true
/@types/debug@4.1.12: /@types/debug@4.1.12:
resolution: {integrity: sha512-vIChWdVG3LG1SMxEvI/AK+FWJthlrqlTu7fbrlywTkkaONwk/UAGaULXRlf8vkzFBLVm0zkMdCquhL5aOjhXPQ==} resolution: {integrity: sha512-vIChWdVG3LG1SMxEvI/AK+FWJthlrqlTu7fbrlywTkkaONwk/UAGaULXRlf8vkzFBLVm0zkMdCquhL5aOjhXPQ==}
dependencies: dependencies:
@ -1891,6 +1897,12 @@ packages:
undici-types: 5.26.5 undici-types: 5.26.5
dev: true dev: true
/@types/node@20.12.14:
resolution: {integrity: sha512-scnD59RpYD91xngrQQLGkE+6UrHUPzeKZWhhjBSa3HSkwjbQc38+q3RoIVEwxQGRw3M+j5hpNAM+lgV3cVormg==}
dependencies:
undici-types: 5.26.5
dev: true
/@types/normalize-package-data@2.4.4: /@types/normalize-package-data@2.4.4:
resolution: {integrity: sha512-37i+OaWTh9qeK4LSHPsyRC7NahnGotNuZvjLSgcPzblpHB3rrCJxAOgI5gCdKm7coonsaX1Of0ILiTcnZjbfxA==} resolution: {integrity: sha512-37i+OaWTh9qeK4LSHPsyRC7NahnGotNuZvjLSgcPzblpHB3rrCJxAOgI5gCdKm7coonsaX1Of0ILiTcnZjbfxA==}
dev: false dev: false
@ -2707,6 +2719,13 @@ packages:
'@types/ws': 8.5.10 '@types/ws': 8.5.10
dev: true dev: true
/bun-types@1.1.8:
resolution: {integrity: sha512-dwhfuUKSGK8hm5Llcvb5+ejRh+4mIt8ibObJVKhZBsi0ScpXmt+AlaS1eDW6uRXCHj084Qt0kIqAJ08/7ZGC9Q==}
dependencies:
'@types/node': 20.12.14
'@types/ws': 8.5.10
dev: true
/bundle-name@3.0.0: /bundle-name@3.0.0:
resolution: {integrity: sha512-PKA4BeSvBpQKQ8iPOGCSiell+N8P+Tf1DlwqmYhpe2gAhKPHn8EYOxVT+ShuGmhg8lN8XiSlS80yiExKXrURlw==} resolution: {integrity: sha512-PKA4BeSvBpQKQ8iPOGCSiell+N8P+Tf1DlwqmYhpe2gAhKPHn8EYOxVT+ShuGmhg8lN8XiSlS80yiExKXrURlw==}
engines: {node: '>=12'} engines: {node: '>=12'}