diff --git a/.changeset/gentle-yaks-cough.md b/.changeset/gentle-yaks-cough.md new file mode 100644 index 0000000..87769ca --- /dev/null +++ b/.changeset/gentle-yaks-cough.md @@ -0,0 +1,5 @@ +--- +'@emigrate/mysql': patch +--- + +Unreference all connections when run using Bun, to not keep the process open unnecessarily long diff --git a/packages/mysql/package.json b/packages/mysql/package.json index afa751d..e162e75 100644 --- a/packages/mysql/package.json +++ b/packages/mysql/package.json @@ -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" diff --git a/packages/mysql/src/index.ts b/packages/mysql/src/index.ts index 5b97560..22b8271 100644 --- a/packages/mysql/src/index.ts +++ b/packages/mysql/src/index.ts @@ -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 { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d6a9a95..e880a00 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -131,11 +131,11 @@ importers: specifier: workspace:* version: link:../tsconfig '@types/bun': - specifier: 1.0.5 - version: 1.0.5 + specifier: 1.1.2 + version: 1.1.2 bun-types: - specifier: 1.0.26 - version: 1.0.26 + specifier: 1.1.8 + version: 1.1.8 packages/plugin-generate-js: dependencies: @@ -1791,6 +1791,12 @@ packages: bun-types: 1.0.26 dev: true + /@types/bun@1.1.2: + resolution: {integrity: sha512-pRBDD3EDqPf83qe95i3EpYu5G2J8bbb78a3736vnCm2K8YWtEE5cvJUq2jkKvJhW07YTfQtbImywIwRhWL8z3Q==} + dependencies: + bun-types: 1.1.8 + dev: true + /@types/debug@4.1.12: resolution: {integrity: sha512-vIChWdVG3LG1SMxEvI/AK+FWJthlrqlTu7fbrlywTkkaONwk/UAGaULXRlf8vkzFBLVm0zkMdCquhL5aOjhXPQ==} dependencies: @@ -1891,6 +1897,12 @@ packages: undici-types: 5.26.5 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: resolution: {integrity: sha512-37i+OaWTh9qeK4LSHPsyRC7NahnGotNuZvjLSgcPzblpHB3rrCJxAOgI5gCdKm7coonsaX1Of0ILiTcnZjbfxA==} dev: false @@ -2707,6 +2719,13 @@ packages: '@types/ws': 8.5.10 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: resolution: {integrity: sha512-PKA4BeSvBpQKQ8iPOGCSiell+N8P+Tf1DlwqmYhpe2gAhKPHn8EYOxVT+ShuGmhg8lN8XiSlS80yiExKXrURlw==} engines: {node: '>=12'}