chore: migrate to PostgreSQL with Prisma
This commit is contained in:
66
scripts/seed.ts
Normal file
66
scripts/seed.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { prisma } from "../lib/prisma";
|
||||
import { getUsers } from "../lib/users";
|
||||
import "dotenv/config";
|
||||
|
||||
async function main() {
|
||||
console.log("Seeding database...");
|
||||
|
||||
// 1. Migrate Users
|
||||
const users = await getUsers();
|
||||
for (const user of users) {
|
||||
console.log(`Migrating user: ${user.email}`);
|
||||
await prisma.user.upsert({
|
||||
where: { email: user.email },
|
||||
update: {
|
||||
name: user.name,
|
||||
password: user.password,
|
||||
role: user.role,
|
||||
domains: user.domains,
|
||||
telegramId: user.telegramId,
|
||||
},
|
||||
create: {
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
password: user.password,
|
||||
role: user.role,
|
||||
domains: user.domains,
|
||||
telegramId: user.telegramId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 2. Migrate Mailbox Mappings
|
||||
const mappingsRaw = process.env.MAIL_USER_MAPPINGS || "{}";
|
||||
try {
|
||||
const mappings = JSON.parse(mappingsRaw);
|
||||
for (const [email, userKey] of Object.entries(mappings)) {
|
||||
const userIndex = parseInt((userKey as string).replace("USER_", ""));
|
||||
const userEmail = process.env[`USER_${userIndex}_EMAIL`];
|
||||
|
||||
if (userEmail) {
|
||||
const dbUser = await prisma.user.findUnique({ where: { email: userEmail } });
|
||||
if (dbUser) {
|
||||
console.log(`Creating mapping: ${email} -> ${userEmail}`);
|
||||
await prisma.mailboxMapping.upsert({
|
||||
where: { email },
|
||||
update: { userId: dbUser.id },
|
||||
create: { email, userId: dbUser.id },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Mapping migration failed:", e);
|
||||
}
|
||||
|
||||
console.log("Seeding complete.");
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user