PlanetScale
According to the official website, PlanetScale is the world’s most advanced serverless MySQL platform.
With Drizzle ORM you can access PlanetScale over http
through their official database-js
driver from serverless and serverfull environments with our drizzle-orm/planetscale-serverless package.
You can also access PlanetScale through TCP with mysql2 driver — see here.
npm i drizzle-orm @planetscale/database
npm i -D drizzle-kityarn add drizzle-orm @planetscale/database
yarn add -D drizzle-kitpnpm add drizzle-orm @planetscale/database
pnpm add -D drizzle-kitbun add drizzle-orm @planetscale/database
bun add -D drizzle-kitimport { drizzle } from "drizzle-orm/planetscale-serverless";
import { connect } from "@planetscale/database";
// create the connection
const connection = connect({
host: process.env["DATABASE_HOST"],
username: process.env["DATABASE_USERNAME"],
password: process.env["DATABASE_PASSWORD"],
});
const db = drizzle(connection); Make sure to checkout the PlanetScale official MySQL courses, we think they’re outstanding 🙌
MySQL 2
According to the official website,
mysql2 is a MySQL client for Node.js with focus on performance.
Drizzle ORM natively supports mysql2 with drizzle-orm/mysql2 package.
npm i drizzle-orm mysql2
npm i -D drizzle-kityarn add drizzle-orm mysql2
yarn add -D drizzle-kitpnpm add drizzle-orm mysql2
pnpm add -D drizzle-kitbun add drizzle-orm mysql2
bun add -D drizzle-kitThere’re two ways you can connect to the MySQL with mysql2 driver, either single client connection or a pool.
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";
const connection = await mysql.createConnection({
host: "host",
user: "user",
database: "database",
...
});
const db = drizzle(connection); import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";
const poolConnection = mysql.createPool({
host: "host",
user: "user",
database: "database",
...
});
const db = drizzle(poolConnection); For the built in migrate function with DDL migrations we and drivers strongly encourage you to use single client connection.
For querying purposes feel free to use either client or pool based on your business demands.
HTTP proxy
Example of driver implementation
import { drizzle } from 'drizzle-orm/mysql-proxy';
const db = drizzle(async (sql, params, method) => {
try {
const rows = await axios.post('http://localhost:3000/query', { sql, params, method });
return { rows: rows.data };
} catch (e: any) {
console.error('Error from mysql proxy server: ', e.response.data)
return { rows: [] };
}
}); Example of server implementation
import * as mysql from 'mysql2/promise';
import express from 'express';
const app = express();
app.use(express.json());
const port = 3000;
const main = async () => {
const connection = await mysql.createConnection('mysql://root:mysql@127.0.0.1:5432/drizzle');
app.post('/query', async (req, res) => {
const { sql, params, method } = req.body;
// prevent multiple queries
const sqlBody = sql.replace(/;/g, '');
try {
const result = await connection.query({
sql: sqlBody,
values: params,
rowsAsArray: method === 'all',
typeCast: function(field: any, next: any) {
if (field.type === 'TIMESTAMP' || field.type === 'DATETIME' || field.type === 'DATE') {
return field.string();
}
return next();
},
});
} catch (e: any) {
res.status(500).json({ error: e });
}
if (method === 'all') {
res.send(result[0]);
} else if (method === 'execute') {
res.send(result);
}
res.status(500).json({ error: 'Unknown method value' });
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
}
main();