Files
portabase/src/utils/init.ts
T

73 lines
2.8 KiB
TypeScript
Raw Normal View History

2025-05-16 15:54:17 +02:00
import { env } from "@/env.mjs";
import { db } from "@/db";
import { eq } from "drizzle-orm";
2025-07-16 12:36:11 +02:00
import * as drizzleDb from "@/db";
export function init() {
2025-05-16 15:54:17 +02:00
consoleAscii();
console.log("====Init Functions====");
2024-11-29 19:25:44 +01:00
2025-05-16 15:54:17 +02:00
createDefaultOrganization().then(() => {});
2024-11-29 19:25:44 +01:00
createSettingsIfNotExist()
.then(() => {
2025-05-16 15:54:17 +02:00
console.log("====Initialization completed====");
})
.catch((err) => {
2025-05-16 15:54:17 +02:00
console.error("Error during initialization:", err);
});
}
async function createSettingsIfNotExist() {
const configSettings = {
name: "system",
smtpPassword: env.SMTP_PASSWORD ?? null,
smtpFrom: env.SMTP_FROM ?? null,
smtpHost: env.SMTP_HOST ?? null,
smtpPort: env.SMTP_PORT ?? null,
smtpUser: env.SMTP_USER ?? null,
s3EndPointUrl: env.S3_ENDPOINT ?? null,
s3AccessKeyId: env.S3_ACCESS_KEY ?? null,
s3SecretAccessKey: env.S3_SECRET_KEY ?? null,
S3BucketName: env.S3_BUCKET_NAME ?? null,
2025-05-16 15:54:17 +02:00
};
2025-07-16 12:36:11 +02:00
const [existing] = await db.select().from(drizzleDb.schemas.setting).where(eq(drizzleDb.schemas.setting.name, "system")).limit(1);
2025-05-16 15:54:17 +02:00
if (!existing) {
console.log("====Init Setting : Create ====");
2025-07-16 12:36:11 +02:00
await db.insert(drizzleDb.schemas.setting).values(configSettings);
2024-11-29 19:25:44 +01:00
} else {
2025-05-16 15:54:17 +02:00
console.log("====Init Setting : Update ====");
2025-07-16 12:36:11 +02:00
await db.update(drizzleDb.schemas.setting).set(configSettings).where(eq(drizzleDb.schemas.setting.name, "system"));
}
}
2024-11-29 19:25:44 +01:00
async function createDefaultOrganization() {
const defaultOrganizationConf = {
slug: "default",
name: "Default Organization",
2025-05-16 15:54:17 +02:00
createdAt: new Date(),
};
2024-11-29 19:25:44 +01:00
2025-07-16 12:36:11 +02:00
const [existing] = await db.select().from(drizzleDb.schemas.organization).where(eq(drizzleDb.schemas.organization.slug, "default")).limit(1);
2025-05-16 15:54:17 +02:00
if (!existing) {
console.log("==== Creating default Organization... ====\n");
2025-07-16 12:36:11 +02:00
await db.insert(drizzleDb.schemas.organization).values(defaultOrganizationConf);
2024-11-29 19:25:44 +01:00
}
}
function consoleAscii() {
2025-05-16 15:54:17 +02:00
console.log(
"\n" +
" ____ __ __ _____ \n" +
" / __ \\ ____ _____ / /_ ____ _ / /_ ____ _ _____ ___ / ___/ ___ _____ _ __ ___ _____\n" +
" / /_/ // __ \\ / ___// __// __ // __ \\ / __ // ___// _ \\ \\__ \\ / _ \\ / ___/| | / // _ \\ / ___/\n" +
" / ____// /_/ // / / /_ / /_/ // /_/ // /_/ /(__ )/ __/ ___/ // __// / | |/ // __// / \n" +
"/_/ \\____//_/ \\__/ \\__,_//_.___/ \\__,_//____/ \\___/ /____/ \\___//_/ |___/ \\___//_/ \n" +
" \n"
);
}