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

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

View file

@ -41,9 +41,11 @@ export type MysqlLoaderOptions = {
connection: ConnectionOptions | string;
};
const getConnection = async (connection: ConnectionOptions | string) => {
if (typeof connection === 'string') {
const uri = new URL(connection);
const getConnection = async (options: ConnectionOptions | string) => {
let connection: Connection;
if (typeof options === 'string') {
const uri = new URL(options);
// client side connectTimeout is unstable in mysql2 library
// 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('multipleStatements', 'true');
return createConnection(uri.toString());
connection = await createConnection(uri.toString());
} else {
connection = await createConnection({
...options,
// client side connectTimeout is unstable in mysql2 library
// it throws an error you can't catch and crashes node
// best to leave this at 0 (disabled)
connectTimeout: 0,
multipleStatements: true,
});
}
return createConnection({
...connection,
// client side connectTimeout is unstable in mysql2 library
// it throws an error you can't catch and crashes node
// best to leave this at 0 (disabled)
connectTimeout: 0,
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) => {
@ -354,12 +364,6 @@ export const createMysqlLoader = ({ connection }: MysqlLoaderOptions): LoaderPlu
const contents = await fs.readFile(migration.filePath, 'utf8');
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 {
await conn.query(contents);
} finally {