Some changes on prisma
This commit is contained in:
134
prisma/seed.ts
134
prisma/seed.ts
@ -1,77 +1,77 @@
|
||||
import { PrismaClient } from "@/generated/prisma/client";
|
||||
import { PrismaPg } from "@prisma/adapter-pg";
|
||||
import { hashPassword } from "better-auth/crypto";
|
||||
// import { PrismaClient } from "@/generated/prisma/client";
|
||||
// import { PrismaPg } from "@prisma/adapter-pg";
|
||||
// import { hashPassword } from "better-auth/crypto";
|
||||
|
||||
const connectionString = `${process.env.DATABASE_URL}`
|
||||
// const connectionString = `${process.env.DATABASE_URL}`
|
||||
|
||||
const adapter = new PrismaPg({ connectionString })
|
||||
const prisma = new PrismaClient({ adapter })
|
||||
// const adapter = new PrismaPg({ connectionString })
|
||||
// const prisma = new PrismaClient({ adapter })
|
||||
|
||||
function uid() {
|
||||
return crypto.randomUUID();
|
||||
}
|
||||
// function uid() {
|
||||
// return crypto.randomUUID();
|
||||
// }
|
||||
|
||||
async function main() {
|
||||
const email = "admin@gaertan.art";
|
||||
const name = "Admin";
|
||||
const password = "037Ikk7qmCamW5iYBimcwiPXNELzktIRG9ndiIkA3u";
|
||||
// async function main() {
|
||||
// const email = "admin@gaertan.art";
|
||||
// const name = "Admin";
|
||||
// const password = "037Ikk7qmCamW5iYBimcwiPXNELzktIRG9ndiIkA3u";
|
||||
|
||||
const passwordHash = await hashPassword(password);
|
||||
// const passwordHash = await hashPassword(password);
|
||||
|
||||
// 1) Ensure user exists (your User.id has no default, so we must set it)
|
||||
const user = await prisma.user.upsert({
|
||||
where: { email },
|
||||
update: {
|
||||
name,
|
||||
// optional: mark verified for initial admin
|
||||
emailVerified: true,
|
||||
},
|
||||
create: {
|
||||
id: uid(),
|
||||
email,
|
||||
name,
|
||||
emailVerified: true,
|
||||
},
|
||||
});
|
||||
// // 1) Ensure user exists (your User.id has no default, so we must set it)
|
||||
// const user = await prisma.user.upsert({
|
||||
// where: { email },
|
||||
// update: {
|
||||
// name,
|
||||
// // optional: mark verified for initial admin
|
||||
// emailVerified: true,
|
||||
// },
|
||||
// create: {
|
||||
// id: uid(),
|
||||
// email,
|
||||
// name,
|
||||
// emailVerified: true,
|
||||
// },
|
||||
// });
|
||||
|
||||
// 2) Ensure credential account exists for this user
|
||||
// Better Auth expects providerId="credential" and accountId=userId for credential accounts. :contentReference[oaicite:2]{index=2}
|
||||
const existingCredential = await prisma.account.findFirst({
|
||||
where: {
|
||||
userId: user.id,
|
||||
providerId: "credential",
|
||||
accountId: user.id,
|
||||
},
|
||||
});
|
||||
// // 2) Ensure credential account exists for this user
|
||||
// // Better Auth expects providerId="credential" and accountId=userId for credential accounts. :contentReference[oaicite:2]{index=2}
|
||||
// const existingCredential = await prisma.account.findFirst({
|
||||
// where: {
|
||||
// userId: user.id,
|
||||
// providerId: "credential",
|
||||
// accountId: user.id,
|
||||
// },
|
||||
// });
|
||||
|
||||
if (!existingCredential) {
|
||||
await prisma.account.create({
|
||||
data: {
|
||||
id: uid(),
|
||||
userId: user.id,
|
||||
providerId: "credential",
|
||||
accountId: user.id,
|
||||
password: passwordHash,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// If it exists, keep it consistent (optionally reset password on seed)
|
||||
await prisma.account.update({
|
||||
where: { id: existingCredential.id },
|
||||
data: { password: passwordHash },
|
||||
});
|
||||
}
|
||||
// if (!existingCredential) {
|
||||
// await prisma.account.create({
|
||||
// data: {
|
||||
// id: uid(),
|
||||
// userId: user.id,
|
||||
// providerId: "credential",
|
||||
// accountId: user.id,
|
||||
// password: passwordHash,
|
||||
// },
|
||||
// });
|
||||
// } else {
|
||||
// // If it exists, keep it consistent (optionally reset password on seed)
|
||||
// await prisma.account.update({
|
||||
// where: { id: existingCredential.id },
|
||||
// data: { password: passwordHash },
|
||||
// });
|
||||
// }
|
||||
|
||||
console.log("Seeded/updated admin user:");
|
||||
console.log(" email:", email);
|
||||
console.log(" password:", password);
|
||||
}
|
||||
// console.log("Seeded/updated admin user:");
|
||||
// console.log(" email:", email);
|
||||
// console.log(" password:", password);
|
||||
// }
|
||||
|
||||
main()
|
||||
.catch((err) => {
|
||||
console.error("Seed failed:", err);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
// main()
|
||||
// .catch((err) => {
|
||||
// console.error("Seed failed:", err);
|
||||
// process.exit(1);
|
||||
// })
|
||||
// .finally(async () => {
|
||||
// await prisma.$disconnect();
|
||||
// });
|
||||
|
||||
Reference in New Issue
Block a user