🚑 hotfix: seeding system
This commit is contained in:
@ -6,8 +6,8 @@ async function main() {
|
||||
console.log("🌱 Running all seeds...");
|
||||
console.log("🔌 Connecting to database...");
|
||||
|
||||
const systemUserId = await userSystemSeed();
|
||||
await userRoleSeed(systemUserId.id);
|
||||
await userSystemSeed();
|
||||
await userRoleSeed();
|
||||
|
||||
console.log("🌳 All seeds completed");
|
||||
}
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
import { SystemAccountId } from "../../src/config/account/system";
|
||||
import { generateUUIDv7 } from "../../src/helpers/databases/uuidv7";
|
||||
import { prisma } from "../../src/utils/databases/prisma/connection";
|
||||
|
||||
export const userRoleSeed = async (systemId: string) => {
|
||||
export const userRoleSeed = async () => {
|
||||
console.log("🔃 Seeding user roles...", SystemAccountId);
|
||||
const roles = [
|
||||
{
|
||||
id: generateUUIDv7(),
|
||||
@ -18,7 +20,7 @@ export const userRoleSeed = async (systemId: string) => {
|
||||
canManageUser: true,
|
||||
canEditSystem: true,
|
||||
canManageSystem: true,
|
||||
createdBy: systemId,
|
||||
createdBy: SystemAccountId,
|
||||
},
|
||||
{
|
||||
id: generateUUIDv7(),
|
||||
@ -35,17 +37,37 @@ export const userRoleSeed = async (systemId: string) => {
|
||||
canManageUser: false,
|
||||
canEditSystem: false,
|
||||
canManageSystem: false,
|
||||
createdBy: systemId,
|
||||
createdBy: SystemAccountId,
|
||||
},
|
||||
];
|
||||
|
||||
await prisma.$transaction(
|
||||
roles.map((role) =>
|
||||
prisma.userRole.upsert({
|
||||
where: { name: role.name },
|
||||
update: role,
|
||||
create: role,
|
||||
}),
|
||||
),
|
||||
);
|
||||
await prisma.$transaction(async (tx) => {
|
||||
const createdRoles = await Promise.all(
|
||||
roles.map(
|
||||
async (role) =>
|
||||
await tx.userRole.upsert({
|
||||
where: { name: role.name },
|
||||
update: role,
|
||||
create: role,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
await tx.userRoleAssignment.upsert({
|
||||
where: {
|
||||
userId_roleId: {
|
||||
userId: SystemAccountId,
|
||||
roleId: createdRoles.find((r) => r.name === "ADMIN")!.id,
|
||||
},
|
||||
},
|
||||
create: {
|
||||
userId: SystemAccountId,
|
||||
roleId: createdRoles.find((r) => r.name === "ADMIN")!.id,
|
||||
},
|
||||
update: {
|
||||
userId: SystemAccountId,
|
||||
roleId: createdRoles.find((r) => r.name === "ADMIN")!.id,
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import { generateUUIDv7 } from "../../src/helpers/databases/uuidv7";
|
||||
import { createFile } from "../../src/helpers/files/createFile";
|
||||
import { prisma } from "../../src/utils/databases/prisma/connection";
|
||||
|
||||
export const userSystemSeed = async () => {
|
||||
@ -12,10 +13,20 @@ export const userSystemSeed = async () => {
|
||||
"$2a$12$ynOrzVCvRdejGp/7KJW4lOAwRzFYhSHDE.Dp3Fqh3sXAq1BIwfwc6",
|
||||
};
|
||||
|
||||
return await prisma.user.upsert({
|
||||
const insertedUserSystem = await prisma.user.upsert({
|
||||
where: { username: payload.username },
|
||||
update: {},
|
||||
create: payload,
|
||||
select: { id: true },
|
||||
});
|
||||
await createFile(
|
||||
`export const SystemAccountId = "${insertedUserSystem.id}";`,
|
||||
{
|
||||
fileName: "system.ts",
|
||||
targetDir: "src/config/account",
|
||||
overwriteIfExists: true,
|
||||
},
|
||||
);
|
||||
|
||||
return insertedUserSystem;
|
||||
};
|
||||
|
||||
28
src/helpers/files/createFile/index.ts
Normal file
28
src/helpers/files/createFile/index.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
interface CreateFileConfig {
|
||||
targetDir: string;
|
||||
fileName: string;
|
||||
overwriteIfExists?: boolean;
|
||||
}
|
||||
|
||||
export const createFile = async (content: string, config: CreateFileConfig) => {
|
||||
const targetDir = path.join(process.cwd(), config.targetDir);
|
||||
const targetFile = path.join(targetDir, config.fileName);
|
||||
|
||||
// If file exists and overwrite is not allowed, throw an error
|
||||
if (fs.existsSync(targetFile) && !config.overwriteIfExists) {
|
||||
throw new Error(
|
||||
`File ${config.fileName} already exists in ${config.targetDir}`,
|
||||
);
|
||||
}
|
||||
|
||||
// Ensure target directory exists
|
||||
if (!fs.existsSync(targetDir)) {
|
||||
await fs.promises.mkdir(targetDir, { recursive: true });
|
||||
}
|
||||
|
||||
// Write content to the file
|
||||
await fs.promises.writeFile(targetFile, content, "utf8");
|
||||
};
|
||||
@ -2,7 +2,6 @@ import { PrismaClient } from "@prisma/client";
|
||||
import { PrismaPg } from "@prisma/adapter-pg";
|
||||
import pg from "pg";
|
||||
import dotenv from "dotenv";
|
||||
import { generateUUIDv7 } from "../../../helpers/databases/uuidv7";
|
||||
dotenv.config();
|
||||
|
||||
const pool = new pg.Pool({
|
||||
@ -10,26 +9,4 @@ const pool = new pg.Pool({
|
||||
});
|
||||
|
||||
const adapter = new PrismaPg(pool);
|
||||
export const prisma = new PrismaClient({ adapter }).$extends({
|
||||
query: {
|
||||
$allModels: {
|
||||
async create({ args, query }) {
|
||||
if (
|
||||
args.data &&
|
||||
typeof args.data === "object" &&
|
||||
!("id" in args.data)
|
||||
) {
|
||||
// do nothing
|
||||
} else if (args.data && !args.data.id) {
|
||||
(args.data as { id?: string }).id = generateUUIDv7();
|
||||
}
|
||||
return query(args);
|
||||
},
|
||||
async upsert({ args, query }) {
|
||||
const create = args.create as any;
|
||||
if (create && !create.id) create.id = generateUUIDv7();
|
||||
return query(args);
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
export const prisma = new PrismaClient({ adapter });
|
||||
|
||||
Reference in New Issue
Block a user