Best way to setup mysql drizzle db file
Answered
averydelusionalperson posted this in #help-forum
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";
import { migrate } from "drizzle-orm/mysql2/migrator";
const connection = await mysql.createConnection({
host: "127.0.0.1",
port: 3306,
user: "root",
database: "ymts-project-management",
});
export const db = drizzle(connection);
migrate(db, { migrationsFolder: "drizzle" });I have this drizzle db file currently, should I make any changes to this?
Answered by Asian paper wasp
For toy example, that's fine. However, if you are planning to deploy it for real and serve a few number of users:
1. You should use a connection pool (
2. Don't use
3. Connection details, i.e.
4. There is no password required to log in?
1. You should use a connection pool (
mysql.createPool) instead of a single connection.2. Don't use
root user. That's basically THE admin user of the entire MySQL instance. Just create a user with just enough privilege to do what you want.3. Connection details, i.e.
host, port, etc. should be environment variables. This is to make switching between different environments (e.g. prod and dev) easier.4. There is no password required to log in?
4 Replies
Asian paper wasp
For toy example, that's fine. However, if you are planning to deploy it for real and serve a few number of users:
1. You should use a connection pool (
2. Don't use
3. Connection details, i.e.
4. There is no password required to log in?
1. You should use a connection pool (
mysql.createPool) instead of a single connection.2. Don't use
root user. That's basically THE admin user of the entire MySQL instance. Just create a user with just enough privilege to do what you want.3. Connection details, i.e.
host, port, etc. should be environment variables. This is to make switching between different environments (e.g. prod and dev) easier.4. There is no password required to log in?
Answer
@Asian paper wasp For toy example, that's fine. However, if you are planning to deploy it for real and serve a few number of users:
1. You should use a connection pool (`mysql.createPool`) instead of a single connection.
2. Don't use `root` user. That's basically THE admin user of the entire MySQL instance. Just create a user with just enough privilege to do what you want.
3. Connection details, i.e. `host`, `port`, etc. should be environment variables. This is to make switching between different environments (e.g. prod and dev) easier.
4. There is no password required to log in?
thanks for the input, I'm just starting the project so haven't used environment variables yet, and yeah for now using root users.
Will use environment variables
didn't know about the pool, thanks ðŸ‘