┌───────────────────────────┐ ┌─────────────────────────────┐
│ Drizzle ORM │ │ HTTP Server with Database │
└─┬─────────────────────────┘ └─────────────────────────┬───┘
│ ^ │
│-- 1. Build query 2. Send built query --│ │
│ │ │
│ ┌───────────────────────────┐ │ │
└─────────────>│ │─────┘ │
│ HTTP Proxy Driver │ │
┌──────────────│ │<─────────────┬───────────┘
│ └───────────────────────────┘ │
│ 3. Execute a query + send raw results back
│-- 4. Map data and return
│
v
Drizzle HTTP 代理
This guide assumes familiarity with:
- Drizzle 数据库 连接基础
HTTP 代理的工作原理以及你可能需要它的原因
当你需要实现自己的驱动程序与数据库的通信时,可以使用 Drizzle Proxy。它可以用于多种情况,例如在查询阶段使用现有驱动程序添加自定义逻辑。最常见的用法是使用 HTTP 驱动程序,它将查询发送到带有数据库的服务器,在数据库上执行查询,并以原始数据作为响应,Drizzle ORM 随后可以将原始数据映射到结果。
How it works under the hood?
Drizzle ORM 还支持使用异步回调函数执行 SQL。
-
sql
是一个带有占位符的查询字符串。 -
params
是一个参数数组。 -
根据 SQL 语句,
method
将设置以下值之一。 -run
、all
、values
或get
。
Drizzle 始终等待 {rows: string[][]}
或 {rows: string[]}
的返回值。
-
当“
method
”等于“get
”时,你应该返回“{rows: string[]}
”值。 -
否则,你应该返回
{rows: string[][]}
。
PostgreSQL
MySQL
SQLite
// Example of driver implementation
import { drizzle } from 'drizzle-orm/pg-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 pg proxy server: ', e.response.data)
return { rows: [] };
}
});
// Example of server implementation
import { Client } from 'pg';
import express from 'express';
const app = express();
app.use(express.json());
const port = 3000;
const client = new Client('postgres://postgres:postgres@localhost:5432/postgres');
app.post('/query', async (req, res) => {
const { sql, params, method } = req.body;
// prevent multiple queries
const sqlBody = sql.replace(/;/g, '');
try {
const result = await client.query({
text: sqlBody,
values: params,
rowMode: method === 'all' ? 'array': undefined,
});
res.send(result.rows);
} catch (e: any) {
res.status(500).json({ error: e });
}
res.status(500).json({ error: 'Unknown method value' });
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});