-
Notifications
You must be signed in to change notification settings - Fork 27
/
supabase-drizzle-node.ts
37 lines (30 loc) · 1.15 KB
/
supabase-drizzle-node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import type { NextApiRequest, NextApiResponse } from "next";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { pgTable, serial, varchar } from "drizzle-orm/pg-core";
export const employees = pgTable("employees", {
id: serial("emp_no").primaryKey(),
first_name: varchar("first_name", { length: 256 }),
last_name: varchar("last_name", { length: 256 }),
});
const client = postgres(process.env.SUPABASE_DATABASE_URL);
const db = drizzle(client);
const start = Date.now();
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { count } = req.query;
const time = Date.now();
let data = null;
for (let i = 0; i < toNumber(count); i++) {
data = await db.select().from(employees).limit(10);
}
return res.status(200).json({
data,
queryDuration: Date.now() - time,
invocationIsCold: start === time,
});
}
// convert a query parameter to a number, applying a min and max, defaulting to 1
function toNumber(queryParam: string | string[] | null, min = 1, max = 5) {
const num = Number(queryParam);
return Number.isNaN(num) ? 1 : Math.min(Math.max(num, min), max);
}