feat: 项目初始化

This commit is contained in:
2026-05-22 00:50:13 +08:00
commit 397ccdca13
17 changed files with 11107 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
export default defineEventHandler(async () => {
return prisma.user.findMany({
include: { posts: true },
orderBy: { id: "desc" },
});
});
+10
View File
@@ -0,0 +1,10 @@
export default defineEventHandler(async (event) => {
const body = await readBody<{ email: string; name?: string | null }>(event);
return prisma.user.create({
data: {
email: body.email,
name: body.name,
},
});
});
+38
View File
@@ -0,0 +1,38 @@
import { PrismaMariaDb } from "@prisma/adapter-mariadb";
import { PrismaClient } from "../../prisma/generated/client";
const createMariaDbConfig = () => {
const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) {
throw new Error("DATABASE_URL is not set.");
}
const url = new URL(databaseUrl);
return {
host: url.hostname,
port: Number(url.port || 3306),
user: decodeURIComponent(url.username),
password: decodeURIComponent(url.password),
database: decodeURIComponent(url.pathname.slice(1)),
connectionLimit: 5,
connectTimeout: 15_000,
acquireTimeout: 20_000,
};
};
const prismaClientSingleton = () => {
const adapter = new PrismaMariaDb(createMariaDbConfig());
return new PrismaClient({ adapter });
};
type PrismaClientSingleton = ReturnType<typeof prismaClientSingleton>;
const globalForPrisma = globalThis as unknown as {
prisma: PrismaClientSingleton | undefined;
};
export const prisma = globalForPrisma.prisma ?? prismaClientSingleton();
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;