From 17feb2d2c271d81ce682d18b9ca1b4424522e09a Mon Sep 17 00:00:00 2001 From: Joakim Carlstein Date: Mon, 12 Feb 2024 13:30:08 +0100 Subject: [PATCH] fix(mysql): only unreference connections in a Bun environment as it has problems with Node for some reason --- .changeset/sharp-houses-smell.md | 5 +++++ packages/mysql/package.json | 4 +++- packages/mysql/src/index.ts | 20 ++++++++++++-------- pnpm-lock.yaml | 6 ++++++ 4 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 .changeset/sharp-houses-smell.md diff --git a/.changeset/sharp-houses-smell.md b/.changeset/sharp-houses-smell.md new file mode 100644 index 0000000..a2cd931 --- /dev/null +++ b/.changeset/sharp-houses-smell.md @@ -0,0 +1,5 @@ +--- +'@emigrate/mysql': patch +--- + +Only unreference connections in a Bun environment as it crashes Node for some reason, without even throwing an error that is diff --git a/packages/mysql/package.json b/packages/mysql/package.json index 310f6c5..67cd832 100644 --- a/packages/mysql/package.json +++ b/packages/mysql/package.json @@ -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" diff --git a/packages/mysql/src/index.ts b/packages/mysql/src/index.ts index 6c009e9..cc659e6 100644 --- a/packages/mysql/src/index.ts +++ b/packages/mysql/src/index.ts @@ -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); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index de45949..fd9a40c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -130,6 +130,12 @@ importers: '@emigrate/tsconfig': specifier: workspace:* version: link:../tsconfig + '@types/bun': + specifier: 1.0.5 + version: 1.0.5 + bun-types: + specifier: 1.0.26 + version: 1.0.26 packages/plugin-generate-js: dependencies: