Getting error while using Drizzle with MySQL2

I am following this guide: https://orm.drizzle.team/docs/get-started-mysql#mysql-2 My setup: - Fork of create-t3-turbo : https://github.com/t3-oss/create-t3-turbo - Trying to replace planetscale with "local docker mysql" Using below code for drizze:
const config: ConnectionOptions = {
host: process.env.DB_HOST!,
user: process.env.DB_USERNAME!,
password: process.env.DB_PASSWORD!,
};
const connection = await mysql.createConnection(config);

export const db = drizzle(connection, { schema: schema, mode: "default" });
const config: ConnectionOptions = {
host: process.env.DB_HOST!,
user: process.env.DB_USERNAME!,
password: process.env.DB_PASSWORD!,
};
const connection = await mysql.createConnection(config);

export const db = drizzle(connection, { schema: schema, mode: "default" });
Getting below error:
⨯ ../../node_modules/.pnpm/[email protected]/node_modules/mysql2/lib/auth_plugins/caching_sha2_password.js:6:15
Module not found: Can't resolve 'crypto'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../../node_modules/.pnpm/[email protected]/node_modules/mysql2/lib/auth_plugins/index.js
../../node_modules/.pnpm/[email protected]/node_modules/mysql2/index.js
../../node_modules/.pnpm/[email protected]/node_modules/mysql2/promise.js
../../packages/db/src/index.ts
../../packages/auth/src/index.ts
./src/app/api/trpc/[trpc]/route.ts
⨯ ../../node_modules/.pnpm/[email protected]/node_modules/mysql2/lib/auth_plugins/caching_sha2_password.js:6:15
Module not found: Can't resolve 'crypto'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../../node_modules/.pnpm/[email protected]/node_modules/mysql2/lib/auth_plugins/index.js
../../node_modules/.pnpm/[email protected]/node_modules/mysql2/index.js
../../node_modules/.pnpm/[email protected]/node_modules/mysql2/promise.js
../../packages/db/src/index.ts
../../packages/auth/src/index.ts
./src/app/api/trpc/[trpc]/route.ts
GitHub
GitHub - t3-oss/create-t3-turbo: Clean and simple starter repo usin...
Clean and simple starter repo using the T3 Stack along with Expo React Native - GitHub - t3-oss/create-t3-turbo: Clean and simple starter repo using the T3 Stack along with Expo React Native
Drizzle ORM - next gen TypeScript ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
6 Replies
Angelelz
Angelelz11mo ago
You might be trying to do database call in the client?
Axat
AxatOP11mo ago
I am not sure, I am just using the t3-turbo stack, and it's using nextjs it should be smart-enough to figure out that it should run on server side, no? @Angelelz
Angelelz
Angelelz11mo ago
You might need to set up MySQL2 as external package in nextjs config Or something like that, I don't remember
Axat
AxatOP11mo ago
I did that for my db package That error is gone now, But getting new error now:
Module not found: Can't resolve 'mysql2/promise'
Module not found: Can't resolve 'mysql2/promise'
Full stacktrace:
○ Compiling /api/trpc/[trpc] ...
⨯ ../../packages/db/src/index.ts:2:0
Module not found: Can't resolve 'mysql2/promise'
1 | import { drizzle } from "drizzle-orm/mysql2";
> 2 | import mysql, { ConnectionOptions } from "mysql2/promise";
3 |
4 | import * as auth from "./schema/auth";
5 | import * as post from "./schema/post";

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../../packages/auth/src/index.ts
./src/app/api/trpc/[trpc]/route.ts
../../node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/build/webpack/loaders/next-edge-app-route-loader/index.js?absolutePagePath=D%3A%5Cdevelopment%5Cakashdevcc%5Cspartan-solar%5Csparta%5Capps%5Cnextjs%5Csrc%5Capp%5Capi%5Ctrpc%5C%5Btrpc%5D%5Croute.ts
○ Compiling /api/trpc/[trpc] ...
⨯ ../../packages/db/src/index.ts:2:0
Module not found: Can't resolve 'mysql2/promise'
1 | import { drizzle } from "drizzle-orm/mysql2";
> 2 | import mysql, { ConnectionOptions } from "mysql2/promise";
3 |
4 | import * as auth from "./schema/auth";
5 | import * as post from "./schema/post";

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../../packages/auth/src/index.ts
./src/app/api/trpc/[trpc]/route.ts
../../node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/build/webpack/loaders/next-edge-app-route-loader/index.js?absolutePagePath=D%3A%5Cdevelopment%5Cakashdevcc%5Cspartan-solar%5Csparta%5Capps%5Cnextjs%5Csrc%5Capp%5Capi%5Ctrpc%5C%5Btrpc%5D%5Croute.ts
Able to resolve it, I had to do pnpm i again at the root, and start the project again. now I am getting this error
>>> tRPC Request from expo-react by undefined
>>> tRPC Error on 'post.all' Error: No database selected
at PromiseConnection.query (webpack-internal:///(rsc)/../../node_modules/.pnpm/[email protected]/node_modules/mysql2/promise.js:82:26)
... 2 lines matching cause stack trace ...
at QueryPromise.then (webpack-internal:///(rsc)/../../node_modules/.pnpm/[email protected]_@[email protected]/node_modules/drizzle-orm/query-promise.js:28:21) {
code: 'INTERNAL_SERVER_ERROR',
name: 'TRPCError',
[cause]: Error: No database selected
at PromiseConnection.query (webpack-internal:///(rsc)/../../node_modules/.pnpm/[email protected]/node_modules/mysql2/promise.js:82:26)
at MySql2PreparedQuery.execute (webpack-internal:///(rsc)/../../node_modules/.pnpm/[email protected]_@[email protected]/node_modules/drizzle-orm/mysql2/session.js:58:37)
at QueryPromise.execute (webpack-internal:///(rsc)/../../node_modules/.pnpm/[email protected]_@[email protected]/node_modules/drizzle-orm/mysql-core/query-builders/query.js:100:31)
at QueryPromise.then (webpack-internal:///(rsc)/../../node_modules/.pnpm/[email protected]_@[email protected]/node_modules/drizzle-orm/query-promise.js:28:21) {
code: 'ER_NO_DB_ERROR',
errno: 1046,
sql: 'select `id`, `name`, `content`, `created_at`, `updatedAt` from `t3turbo_post` `post` order by `post`.`id` desc
limit 10',
sqlState: '3D000',
sqlMessage: 'No database selected'
}
}
>>> tRPC Request from expo-react by undefined
>>> tRPC Error on 'post.all' Error: No database selected
at PromiseConnection.query (webpack-internal:///(rsc)/../../node_modules/.pnpm/[email protected]/node_modules/mysql2/promise.js:82:26)
... 2 lines matching cause stack trace ...
at QueryPromise.then (webpack-internal:///(rsc)/../../node_modules/.pnpm/[email protected]_@[email protected]/node_modules/drizzle-orm/query-promise.js:28:21) {
code: 'INTERNAL_SERVER_ERROR',
name: 'TRPCError',
[cause]: Error: No database selected
at PromiseConnection.query (webpack-internal:///(rsc)/../../node_modules/.pnpm/[email protected]/node_modules/mysql2/promise.js:82:26)
at MySql2PreparedQuery.execute (webpack-internal:///(rsc)/../../node_modules/.pnpm/[email protected]_@[email protected]/node_modules/drizzle-orm/mysql2/session.js:58:37)
at QueryPromise.execute (webpack-internal:///(rsc)/../../node_modules/.pnpm/[email protected]_@[email protected]/node_modules/drizzle-orm/mysql-core/query-builders/query.js:100:31)
at QueryPromise.then (webpack-internal:///(rsc)/../../node_modules/.pnpm/[email protected]_@[email protected]/node_modules/drizzle-orm/query-promise.js:28:21) {
code: 'ER_NO_DB_ERROR',
errno: 1046,
sql: 'select `id`, `name`, `content`, `created_at`, `updatedAt` from `t3turbo_post` `post` order by `post`.`id` desc
limit 10',
sqlState: '3D000',
sqlMessage: 'No database selected'
}
}
Angelelz
Angelelz11mo ago
You have to tell MySQL2 what database to query
Axat
AxatOP11mo ago
yes just figured it out, added database field in ConnectionOptions
const config: ConnectionOptions = {
host: process.env.DB_HOST!,
user: process.env.DB_USERNAME!,
password: process.env.DB_PASSWORD!,
database: process.env.DB_NAME!,
};
const connection = await mysql.createConnection(config);
const config: ConnectionOptions = {
host: process.env.DB_HOST!,
user: process.env.DB_USERNAME!,
password: process.env.DB_PASSWORD!,
database: process.env.DB_NAME!,
};
const connection = await mysql.createConnection(config);
Thanks for the help.
Want results from more Discord servers?
Add your server