1
0
mirror of https://github.com/stonith404/pingvin-share.git synced 2024-11-11 02:10:14 +01:00
pingvin-share/backend/prisma/seed/config.seed.ts

150 lines
3.6 KiB
TypeScript
Raw Normal View History

2022-12-08 19:14:06 +01:00
import { Prisma, PrismaClient } from "@prisma/client";
2022-12-02 15:10:49 +01:00
import * as crypto from "crypto";
2022-12-08 19:14:06 +01:00
const configVariables: Prisma.ConfigCreateInput[] = [
2022-12-02 15:10:49 +01:00
{
key: "SETUP_FINISHED",
2022-12-02 15:10:49 +01:00
description: "Whether the setup has been finished",
type: "boolean",
value: "false",
secret: false,
locked: true,
},
{
key: "APP_URL",
2022-12-02 15:10:49 +01:00
description: "On which URL Pingvin Share is available",
type: "string",
value: "http://localhost:3000",
secret: false,
},
{
key: "SHOW_HOME_PAGE",
2022-12-02 15:10:49 +01:00
description: "Whether to show the home page",
type: "boolean",
value: "true",
secret: false,
},
{
key: "ALLOW_REGISTRATION",
2022-12-02 15:10:49 +01:00
description: "Whether registration is allowed",
type: "boolean",
value: "true",
secret: false,
},
{
key: "ALLOW_UNAUTHENTICATED_SHARES",
2022-12-02 15:10:49 +01:00
description: "Whether unauthorized users can create shares",
type: "boolean",
value: "false",
secret: false,
},
{
key: "MAX_FILE_SIZE",
2022-12-02 15:10:49 +01:00
description: "Maximum file size in bytes",
type: "number",
value: "1000000000",
secret: false,
},
{
key: "JWT_SECRET",
2022-12-02 15:10:49 +01:00
description: "Long random string used to sign JWT tokens",
type: "string",
value: crypto.randomBytes(256).toString("base64"),
locked: true,
},
{
key: "ENABLE_EMAIL_RECIPIENTS",
2022-12-02 15:10:49 +01:00
description:
"Whether to send emails to recipients. Only set this to true if you entered the host, port, email and password of your SMTP server.",
type: "boolean",
value: "false",
secret: false,
},
{
key: "SMTP_HOST",
2022-12-02 15:10:49 +01:00
description: "Host of the SMTP server",
type: "string",
value: "",
},
{
key: "SMTP_PORT",
2022-12-02 15:10:49 +01:00
description: "Port of the SMTP server",
type: "number",
value: "",
},
{
key: "SMTP_EMAIL",
description: "Email address which the emails get sent from",
type: "string",
value: "",
},
{
key: "SMTP_USERNAME",
description: "Username of the SMTP server",
2022-12-02 15:10:49 +01:00
type: "string",
value: "",
},
{
key: "SMTP_PASSWORD",
2022-12-02 15:10:49 +01:00
description: "Password of the SMTP server",
type: "string",
value: "",
2022-12-08 19:14:06 +01:00
obscured: true,
2022-12-02 15:10:49 +01:00
},
];
const prisma = new PrismaClient();
async function main() {
for (const variable of configVariables) {
const existingConfigVariable = await prisma.config.findUnique({
where: { key: variable.key },
});
// Create a new config variable if it doesn't exist
if (!existingConfigVariable) {
await prisma.config.create({
data: variable,
});
}
}
const configVariablesFromDatabase = await prisma.config.findMany();
// Delete the config variable if it doesn't exist anymore
for (const configVariableFromDatabase of configVariablesFromDatabase) {
const configVariable = configVariables.find(
(v) => v.key == configVariableFromDatabase.key
);
if (!configVariable) {
await prisma.config.delete({
where: { key: configVariableFromDatabase.key },
});
// Update the config variable if the metadata changed
} else if (
JSON.stringify({
key: configVariableFromDatabase.key,
value: configVariableFromDatabase.value,
...configVariable,
}) != JSON.stringify(configVariableFromDatabase)
) {
await prisma.config.update({
where: { key: configVariableFromDatabase.key },
data: configVariables.find(
(v) => v.key == configVariableFromDatabase.key
),
});
}
}
}
main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});