fix: prisma url in schema + first user becomes ADMIN

- Add url = env("DATABASE_URL") back to schema (required by Prisma 7.5)
- Simplify prisma.config.ts
- First registered user auto-promoted to ADMIN via databaseHooks
- Subsequent users get HOST role
This commit is contained in:
2026-03-24 03:54:55 +03:00
parent e59ba0ab97
commit a6c744223b
4 changed files with 27 additions and 8 deletions

View File

@@ -173,8 +173,8 @@ DEV_ACCESS_KEY=mySecretKey123
## Prisma 7 Notes ## Prisma 7 Notes
- `prisma.config.ts` in project root (required by Prisma 7) - `prisma.config.ts` in project root (required by Prisma 7)
- `datasourceUrl` passed via PrismaClient constructor, not in schema - `url = env("DATABASE_URL")` in schema datasource — standard approach, works with CLI and PrismaClient
- Schema has no `url` in datasource block — only `provider` - `new PrismaClient()` without arguments — URL resolved from schema's `env("DATABASE_URL")`
- Use `-- --webpack` flag for `next build` on Windows (Turbopack WASM issue) - Use `-- --webpack` flag for `next build` on Windows (Turbopack WASM issue)
## Auth Notes ## Auth Notes

View File

@@ -4,9 +4,4 @@ import { defineConfig } from "prisma/config";
export default defineConfig({ export default defineConfig({
earlyAccess: true, earlyAccess: true,
schema: path.join(__dirname, "prisma", "schema.prisma"), schema: path.join(__dirname, "prisma", "schema.prisma"),
migrate: { });
async url() {
return process.env.DATABASE_URL!;
},
},
} as any);

View File

@@ -9,6 +9,7 @@ generator client {
datasource db { datasource db {
provider = "postgresql" provider = "postgresql"
url = env("DATABASE_URL")
} }
// ======================== ENUMS ============================= // ======================== ENUMS =============================

View File

@@ -43,4 +43,27 @@ export const auth = betterAuth({
expiresIn: 60 * 60 * 24 * 7, // 7 days expiresIn: 60 * 60 * 24 * 7, // 7 days
updateAge: 60 * 60 * 24, // 1 day updateAge: 60 * 60 * 24, // 1 day
}, },
user: {
additionalFields: {
role: {
type: "string",
defaultValue: "HOST",
input: false,
},
},
},
databaseHooks: {
user: {
create: {
before: async (user) => {
// Первый зарегистрированный пользователь → ADMIN
const count = await prisma.user.count();
if (count === 0) {
return { data: { ...user, role: "ADMIN" } };
}
return { data: { ...user, role: "HOST" } };
},
},
},
},
}); });