🚑 hotfix: seeding system
This commit is contained in:
@ -6,8 +6,8 @@ async function main() {
|
|||||||
console.log("🌱 Running all seeds...");
|
console.log("🌱 Running all seeds...");
|
||||||
console.log("🔌 Connecting to database...");
|
console.log("🔌 Connecting to database...");
|
||||||
|
|
||||||
const systemUserId = await userSystemSeed();
|
await userSystemSeed();
|
||||||
await userRoleSeed(systemUserId.id);
|
await userRoleSeed();
|
||||||
|
|
||||||
console.log("🌳 All seeds completed");
|
console.log("🌳 All seeds completed");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,9 @@
|
|||||||
|
import { SystemAccountId } from "../../src/config/account/system";
|
||||||
import { generateUUIDv7 } from "../../src/helpers/databases/uuidv7";
|
import { generateUUIDv7 } from "../../src/helpers/databases/uuidv7";
|
||||||
import { prisma } from "../../src/utils/databases/prisma/connection";
|
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 = [
|
const roles = [
|
||||||
{
|
{
|
||||||
id: generateUUIDv7(),
|
id: generateUUIDv7(),
|
||||||
@ -18,7 +20,7 @@ export const userRoleSeed = async (systemId: string) => {
|
|||||||
canManageUser: true,
|
canManageUser: true,
|
||||||
canEditSystem: true,
|
canEditSystem: true,
|
||||||
canManageSystem: true,
|
canManageSystem: true,
|
||||||
createdBy: systemId,
|
createdBy: SystemAccountId,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: generateUUIDv7(),
|
id: generateUUIDv7(),
|
||||||
@ -35,17 +37,37 @@ export const userRoleSeed = async (systemId: string) => {
|
|||||||
canManageUser: false,
|
canManageUser: false,
|
||||||
canEditSystem: false,
|
canEditSystem: false,
|
||||||
canManageSystem: false,
|
canManageSystem: false,
|
||||||
createdBy: systemId,
|
createdBy: SystemAccountId,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
await prisma.$transaction(
|
await prisma.$transaction(async (tx) => {
|
||||||
roles.map((role) =>
|
const createdRoles = await Promise.all(
|
||||||
prisma.userRole.upsert({
|
roles.map(
|
||||||
where: { name: role.name },
|
async (role) =>
|
||||||
update: role,
|
await tx.userRole.upsert({
|
||||||
create: role,
|
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 { generateUUIDv7 } from "../../src/helpers/databases/uuidv7";
|
||||||
|
import { createFile } from "../../src/helpers/files/createFile";
|
||||||
import { prisma } from "../../src/utils/databases/prisma/connection";
|
import { prisma } from "../../src/utils/databases/prisma/connection";
|
||||||
|
|
||||||
export const userSystemSeed = async () => {
|
export const userSystemSeed = async () => {
|
||||||
@ -12,10 +13,20 @@ export const userSystemSeed = async () => {
|
|||||||
"$2a$12$ynOrzVCvRdejGp/7KJW4lOAwRzFYhSHDE.Dp3Fqh3sXAq1BIwfwc6",
|
"$2a$12$ynOrzVCvRdejGp/7KJW4lOAwRzFYhSHDE.Dp3Fqh3sXAq1BIwfwc6",
|
||||||
};
|
};
|
||||||
|
|
||||||
return await prisma.user.upsert({
|
const insertedUserSystem = await prisma.user.upsert({
|
||||||
where: { username: payload.username },
|
where: { username: payload.username },
|
||||||
update: {},
|
update: {},
|
||||||
create: payload,
|
create: payload,
|
||||||
select: { id: true },
|
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 { PrismaPg } from "@prisma/adapter-pg";
|
||||||
import pg from "pg";
|
import pg from "pg";
|
||||||
import dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
import { generateUUIDv7 } from "../../../helpers/databases/uuidv7";
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
const pool = new pg.Pool({
|
const pool = new pg.Pool({
|
||||||
@ -10,26 +9,4 @@ const pool = new pg.Pool({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const adapter = new PrismaPg(pool);
|
const adapter = new PrismaPg(pool);
|
||||||
export const prisma = new PrismaClient({ adapter }).$extends({
|
export const prisma = new PrismaClient({ adapter });
|
||||||
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);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|||||||
Reference in New Issue
Block a user