✨ feat: create db seed for initialization
This commit is contained in:
@ -9,6 +9,9 @@
|
|||||||
"lint": "bunx eslint",
|
"lint": "bunx eslint",
|
||||||
"commit": "bun x git-cz",
|
"commit": "bun x git-cz",
|
||||||
"push": "bun scripts/git-multipush.ts",
|
"push": "bun scripts/git-multipush.ts",
|
||||||
|
"prisma:generate": "bunx prisma generate",
|
||||||
|
"prisma:dbml": "bunx prisma db pull && bunx prisma dbml --output ./prisma/dbml/schema.dbml",
|
||||||
|
"prisma:seed": "bun run ./prisma/seed/index.ts",
|
||||||
"route:sync": "bun run ./scripts/sync-routes.ts",
|
"route:sync": "bun run ./scripts/sync-routes.ts",
|
||||||
"env:publish": "bun run ./scripts/create-example-env.ts"
|
"env:publish": "bun run ./scripts/create-example-env.ts"
|
||||||
},
|
},
|
||||||
|
|||||||
@ -230,6 +230,7 @@ Table user_preferences {
|
|||||||
Table user_roles {
|
Table user_roles {
|
||||||
id String [pk]
|
id String [pk]
|
||||||
name String [unique, not null]
|
name String [unique, not null]
|
||||||
|
description String
|
||||||
primaryColor String
|
primaryColor String
|
||||||
secondaryColor String
|
secondaryColor String
|
||||||
pictureImage String
|
pictureImage String
|
||||||
|
|||||||
@ -268,6 +268,7 @@ model UserPreference {
|
|||||||
model UserRole {
|
model UserRole {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
name String @db.VarChar(255) @unique
|
name String @db.VarChar(255) @unique
|
||||||
|
description String? @db.Text
|
||||||
primaryColor String? @db.VarChar(10)
|
primaryColor String? @db.VarChar(10)
|
||||||
secondaryColor String? @db.VarChar(10)
|
secondaryColor String? @db.VarChar(10)
|
||||||
pictureImage String? @db.Text
|
pictureImage String? @db.Text
|
||||||
|
|||||||
25
prisma/seed/index.ts
Normal file
25
prisma/seed/index.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import { prisma } from "../../src/utils/databases/prisma/connection";
|
||||||
|
import { userRoleSeed } from "./userRole.seed";
|
||||||
|
import { userSystemSeed } from "./userSystem.seed";
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
console.log("🌱 Running all seeds...");
|
||||||
|
console.log("🔌 Connecting to database...");
|
||||||
|
|
||||||
|
const systemUserId = await userSystemSeed();
|
||||||
|
await userRoleSeed(systemUserId.id);
|
||||||
|
|
||||||
|
console.log("🌳 All seeds completed");
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
||||||
|
.catch((e) => {
|
||||||
|
console.error(e);
|
||||||
|
process.exit(1);
|
||||||
|
})
|
||||||
|
.finally(async () => {
|
||||||
|
console.log(
|
||||||
|
"🔌 Disconnecting from database (this may take a few seconds)...",
|
||||||
|
);
|
||||||
|
await prisma.$disconnect();
|
||||||
|
});
|
||||||
48
prisma/seed/userRole.seed.ts
Normal file
48
prisma/seed/userRole.seed.ts
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
import { prisma } from "../../src/utils/databases/prisma/connection";
|
||||||
|
|
||||||
|
export const userRoleSeed = async (systemId: string) => {
|
||||||
|
const roles = [
|
||||||
|
{
|
||||||
|
name: "ADMIN",
|
||||||
|
description: "Administrator with full access",
|
||||||
|
isSuperadmin: true,
|
||||||
|
canEditMedia: true,
|
||||||
|
canManageMedia: true,
|
||||||
|
canEditEpisodes: true,
|
||||||
|
canManageEpisodes: true,
|
||||||
|
canEditComment: true,
|
||||||
|
canManageComment: true,
|
||||||
|
canEditUser: true,
|
||||||
|
canManageUser: true,
|
||||||
|
canEditSystem: true,
|
||||||
|
canManageSystem: true,
|
||||||
|
createdBy: systemId,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "USER",
|
||||||
|
description: "Regular user with limited access",
|
||||||
|
isSuperadmin: false,
|
||||||
|
canEditMedia: false,
|
||||||
|
canManageMedia: false,
|
||||||
|
canEditEpisodes: false,
|
||||||
|
canManageEpisodes: false,
|
||||||
|
canEditComment: false,
|
||||||
|
canManageComment: false,
|
||||||
|
canEditUser: false,
|
||||||
|
canManageUser: false,
|
||||||
|
canEditSystem: false,
|
||||||
|
canManageSystem: false,
|
||||||
|
createdBy: systemId,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
await prisma.$transaction(
|
||||||
|
roles.map((role) =>
|
||||||
|
prisma.userRole.upsert({
|
||||||
|
where: { name: role.name },
|
||||||
|
update: {},
|
||||||
|
create: role,
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
};
|
||||||
19
prisma/seed/userSystem.seed.ts
Normal file
19
prisma/seed/userSystem.seed.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { prisma } from "../../src/utils/databases/prisma/connection";
|
||||||
|
|
||||||
|
export const userSystemSeed = async () => {
|
||||||
|
const payload = {
|
||||||
|
name: "SYSTEM",
|
||||||
|
username: process.env.DEFAULT_ADMIN_USERNAME || "system",
|
||||||
|
email: process.env.DEFAULT_ADMIN_EMAIL || "system@example.com",
|
||||||
|
password:
|
||||||
|
process.env.DEFAULT_ADMIN_PASSWORD ||
|
||||||
|
"$2a$12$ynOrzVCvRdejGp/7KJW4lOAwRzFYhSHDE.Dp3Fqh3sXAq1BIwfwc6",
|
||||||
|
};
|
||||||
|
|
||||||
|
return await prisma.user.upsert({
|
||||||
|
where: { username: payload.username },
|
||||||
|
update: {},
|
||||||
|
create: payload,
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
};
|
||||||
1147
structure.svg
1147
structure.svg
File diff suppressed because it is too large
Load Diff
|
Before Width: | Height: | Size: 113 KiB |
Reference in New Issue
Block a user