fix(mysql): only unreference connections in a Bun environment as it has problems with Node for some reason

This commit is contained in:
Joakim Carlstein 2024-02-12 13:30:08 +01:00 committed by Joakim Carlstein
parent 98e3ed5c1b
commit 17feb2d2c2
4 changed files with 26 additions and 9 deletions

View file

@ -43,7 +43,9 @@
"mysql2": "3.6.5"
},
"devDependencies": {
"@emigrate/tsconfig": "workspace:*"
"@emigrate/tsconfig": "workspace:*",
"@types/bun": "1.0.5",
"bun-types": "1.0.26"
},
"volta": {
"extends": "../../package.json"

View file

@ -171,11 +171,13 @@ export const createMysqlStorage = ({ table = defaultTable, connection }: MysqlSt
async initializeStorage() {
const pool = getPool(connection);
pool.on('connection', (connection) => {
// @ts-expect-error stream is not in the types but it's there
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
connection.stream.unref();
});
if (process.isBun) {
pool.on('connection', (connection) => {
// @ts-expect-error stream is not in the types but it's there
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
connection.stream.unref();
});
}
await pool.query('SELECT 1');
@ -274,9 +276,11 @@ export const createMysqlLoader = ({ connection }: MysqlLoaderOptions): LoaderPlu
const contents = await fs.readFile(migration.filePath, 'utf8');
const conn = await getConnection(connection);
// @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();
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);