mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-10-20 15:35:23 +00:00
5280c861e8
* feat(create-turbo): apply official-starter transform * refactor: move folder * wip: monorepo * feat: add builf * refactor: add pr * update * add .env * refactor: update build * refactor: update build docker * refactor: add progress plain * refactor: remove node pty * refactor: remove * remove * refactor: update * refacotr: uopdate * refactor: add remix app * add env * refactor: add pnpm start * refactor: remove * refactor: remove folders * refactor: remove .dockerfile * chore: update biome * test * choe: add husky * remove .docker folder * feat: add docs website * refactor: add husky * chore(version): bump version * refactor: add new changes * refactor: update circle path * refactor: update * refactor: update * refactor: update dockerfile * refactor: update dockerfile * refactor: update command * refactor: update * refactor: update dockerfile * refactor: add tsx * refactor: update dockerfile * refactor: add deps * refactor: up[date * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: yuodate * refactor: remove * refactor: uncomment * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: updare * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: imprt * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: remove * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: change path * refactor: update * refactor: update * refactor: upoadte * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: add * refactor: update * refactor: update * refactor: add * refactor: update * refactor: remove * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: removed * refactor: update * refactor: update * refactor: update * refactor: add config * refactor: update * refactor: add * refactor: update * refactor: update * refactor: remove * refactor: update * refactor: remove * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: add docs * refactor: update * refactor: add website * refactor: update * refactor: update * refactor: update * refactor: update * refactor: add ignore builds * refactor: update * refactor: update * refactor: add * refactor: update * refactor: update * refactor: remove needs * refactor: update * refactor: update * refactor: add config * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: remove * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: add * refactor: update * refactor: update * refactor: update * refactor: update * refactor: upodate * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update * refactor: update package json * refactor: add biome * refactor: add sponsors * refactor: update * refactor: update * refactor: remove * refactor: update * refactor: update * refactor: update * refactor: update scripts * refactor: remove * refactor: update * refactor: remove --------- Co-authored-by: Turbobot <turbobot@vercel.com>
133 lines
3.2 KiB
TypeScript
133 lines
3.2 KiB
TypeScript
import { generateRandomPassword } from "@/server/auth/random-password";
|
|
import { db } from "@/server/db";
|
|
import { type apiCreatePostgres, backups, postgres } from "@/server/db/schema";
|
|
import { generateAppName } from "@/server/db/schema/utils";
|
|
import { buildPostgres } from "@/server/utils/databases/postgres";
|
|
import { pullImage } from "@/server/utils/docker/utils";
|
|
import { generatePassword } from "@/templates/utils";
|
|
import { TRPCError } from "@trpc/server";
|
|
import { eq, getTableColumns } from "drizzle-orm";
|
|
import { validUniqueServerAppName } from "./project";
|
|
|
|
export type Postgres = typeof postgres.$inferSelect;
|
|
|
|
export const createPostgres = async (input: typeof apiCreatePostgres._type) => {
|
|
input.appName =
|
|
`${input.appName}-${generatePassword(6)}` || generateAppName("postgres");
|
|
if (input.appName) {
|
|
const valid = await validUniqueServerAppName(input.appName);
|
|
|
|
if (!valid) {
|
|
throw new TRPCError({
|
|
code: "CONFLICT",
|
|
message: "Service with this 'AppName' already exists",
|
|
});
|
|
}
|
|
}
|
|
|
|
const newPostgres = await db
|
|
.insert(postgres)
|
|
.values({
|
|
...input,
|
|
databasePassword: input.databasePassword
|
|
? input.databasePassword
|
|
: (await generateRandomPassword()).randomPassword,
|
|
})
|
|
.returning()
|
|
.then((value) => value[0]);
|
|
|
|
if (!newPostgres) {
|
|
throw new TRPCError({
|
|
code: "BAD_REQUEST",
|
|
message: "Error input: Inserting postgresql database",
|
|
});
|
|
}
|
|
|
|
return newPostgres;
|
|
};
|
|
|
|
export const findPostgresById = async (postgresId: string) => {
|
|
const result = await db.query.postgres.findFirst({
|
|
where: eq(postgres.postgresId, postgresId),
|
|
with: {
|
|
project: true,
|
|
mounts: true,
|
|
backups: {
|
|
with: {
|
|
destination: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
if (!result) {
|
|
throw new TRPCError({
|
|
code: "NOT_FOUND",
|
|
message: "Postgres not found",
|
|
});
|
|
}
|
|
return result;
|
|
};
|
|
|
|
export const findPostgresByBackupId = async (backupId: string) => {
|
|
const result = await db
|
|
.select({
|
|
...getTableColumns(postgres),
|
|
})
|
|
.from(postgres)
|
|
.innerJoin(backups, eq(postgres.postgresId, backups.postgresId))
|
|
.where(eq(backups.backupId, backupId))
|
|
.limit(1);
|
|
|
|
if (!result || !result[0]) {
|
|
throw new TRPCError({
|
|
code: "NOT_FOUND",
|
|
message: "Postgres not found",
|
|
});
|
|
}
|
|
return result[0];
|
|
};
|
|
|
|
export const updatePostgresById = async (
|
|
postgresId: string,
|
|
postgresData: Partial<Postgres>,
|
|
) => {
|
|
const result = await db
|
|
.update(postgres)
|
|
.set({
|
|
...postgresData,
|
|
})
|
|
.where(eq(postgres.postgresId, postgresId))
|
|
.returning();
|
|
|
|
return result[0];
|
|
};
|
|
|
|
export const removePostgresById = async (postgresId: string) => {
|
|
const result = await db
|
|
.delete(postgres)
|
|
.where(eq(postgres.postgresId, postgresId))
|
|
.returning();
|
|
|
|
return result[0];
|
|
};
|
|
|
|
export const deployPostgres = async (postgresId: string) => {
|
|
const postgres = await findPostgresById(postgresId);
|
|
try {
|
|
await pullImage(postgres.dockerImage);
|
|
await buildPostgres(postgres);
|
|
await updatePostgresById(postgresId, {
|
|
applicationStatus: "done",
|
|
});
|
|
} catch (error) {
|
|
await updatePostgresById(postgresId, {
|
|
applicationStatus: "error",
|
|
});
|
|
throw new TRPCError({
|
|
code: "INTERNAL_SERVER_ERROR",
|
|
message: `Error on deploy postgres${error}`,
|
|
});
|
|
}
|
|
return postgres;
|
|
};
|