From faeae8fd6cf6cc764497edde8b4f2823dac4b8f2 Mon Sep 17 00:00:00 2001 From: dominicbachmann Date: Wed, 6 Apr 2022 19:34:17 +0200 Subject: [PATCH] Added typings for database/destinations --- src/lib/database/destinations.ts | 93 +++++++++++++++-------- src/lib/types/destinations.ts | 8 ++ src/routes/new/destination/docker.json.ts | 39 +++------- 3 files changed, 79 insertions(+), 61 deletions(-) create mode 100644 src/lib/types/destinations.ts diff --git a/src/lib/database/destinations.ts b/src/lib/database/destinations.ts index 3f3aadec5..b3f9679c4 100644 --- a/src/lib/database/destinations.ts +++ b/src/lib/database/destinations.ts @@ -1,27 +1,48 @@ import { asyncExecShell, getEngine } from '$lib/common'; -import { decrypt, encrypt } from '$lib/crypto'; +import { decrypt } from '$lib/crypto'; import { dockerInstance } from '$lib/docker'; import { startCoolifyProxy } from '$lib/haproxy'; import { getDatabaseImage } from '.'; import { prisma } from './common'; +import type { DestinationDocker, Service, Application, Prisma } from '@prisma/client'; +import type { CreateDockerDestination } from '$lib/types/destinations'; -export async function listDestinations(teamId) { +type DestinationConfigurationObject = { + id: string; + destinationId: string; +}; + +type FindDestinationFromTeam = { + id: string; + teamId: string; +}; + +export async function listDestinations(teamId: string): Promise { return await prisma.destinationDocker.findMany({ where: { teams: { some: { id: teamId } } } }); } -export async function configureDestinationForService({ id, destinationId }) { +export async function configureDestinationForService({ + id, + destinationId +}: DestinationConfigurationObject): Promise { return await prisma.service.update({ where: { id }, data: { destinationDocker: { connect: { id: destinationId } } } }); } -export async function configureDestinationForApplication({ id, destinationId }) { +export async function configureDestinationForApplication({ + id, + destinationId +}: DestinationConfigurationObject): Promise { return await prisma.application.update({ where: { id }, data: { destinationDocker: { connect: { id: destinationId } } } }); } -export async function configureDestinationForDatabase({ id, destinationId }) { +export async function configureDestinationForDatabase({ + id, + destinationId +}: DestinationConfigurationObject): Promise { await prisma.database.update({ where: { id }, data: { destinationDocker: { connect: { id: destinationId } } } @@ -38,13 +59,18 @@ export async function configureDestinationForDatabase({ id, destinationId }) { const host = getEngine(engine); if (type && version) { const baseImage = getDatabaseImage(type); - asyncExecShell( + await asyncExecShell( `DOCKER_HOST=${host} docker pull ${baseImage}:${version} && echo "FROM ${baseImage}:${version}" | docker build --label coolify.image="true" -t "${baseImage}:${version}" -` ); } } } -export async function updateDestination({ id, name, engine, network }) { +export async function updateDestination({ + id, + name, + engine, + network +}: Pick): Promise { return await prisma.destinationDocker.update({ where: { id }, data: { name, engine, network } }); } @@ -54,13 +80,8 @@ export async function newRemoteDestination({ engine, network, isCoolifyProxyUsed, - remoteEngine, - ipAddress, - user, - port, - sshPrivateKey -}) { - const encryptedPrivateKey = encrypt(sshPrivateKey); + remoteEngine +}: CreateDockerDestination): Promise { const destination = await prisma.destinationDocker.create({ data: { name, @@ -68,16 +89,18 @@ export async function newRemoteDestination({ engine, network, isCoolifyProxyUsed, - remoteEngine, - ipAddress, - user, - port, - sshPrivateKey: encryptedPrivateKey + remoteEngine } }); return destination.id; } -export async function newLocalDestination({ name, teamId, engine, network, isCoolifyProxyUsed }) { +export async function newLocalDestination({ + name, + teamId, + engine, + network, + isCoolifyProxyUsed +}: CreateDockerDestination): Promise { const host = getEngine(engine); const docker = dockerInstance({ destinationDocker: { engine, network } }); const found = await docker.engine.listNetworks({ filters: { name: [`^${network}$`] } }); @@ -95,18 +118,14 @@ export async function newLocalDestination({ name, teamId, engine, network, isCoo (destination) => destination.network !== network && destination.isCoolifyProxyUsed === true ); if (proxyConfigured) { - if (proxyConfigured.isCoolifyProxyUsed) { - isCoolifyProxyUsed = true; - } else { - isCoolifyProxyUsed = false; - } + isCoolifyProxyUsed = !!proxyConfigured.isCoolifyProxyUsed; } await prisma.destinationDocker.updateMany({ where: { engine }, data: { isCoolifyProxyUsed } }); } if (isCoolifyProxyUsed) await startCoolifyProxy(engine); return destination.id; } -export async function removeDestination({ id }) { +export async function removeDestination({ id }: Pick): Promise { const destination = await prisma.destinationDocker.delete({ where: { id } }); if (destination.isCoolifyProxyUsed) { const host = getEngine(destination.engine); @@ -123,22 +142,34 @@ export async function removeDestination({ id }) { } } -export async function getDestination({ id, teamId }) { - let destination = await prisma.destinationDocker.findFirst({ +export async function getDestination({ + id, + teamId +}: FindDestinationFromTeam): Promise { + const destination = (await prisma.destinationDocker.findFirst({ where: { id, teams: { some: { id: teamId } } } - }); + })) as DestinationDocker & { sshPrivateKey?: string }; if (destination.remoteEngine) { destination.sshPrivateKey = decrypt(destination.sshPrivateKey); } return destination; } -export async function getDestinationByApplicationId({ id, teamId }) { +export async function getDestinationByApplicationId({ + id, + teamId +}: FindDestinationFromTeam): Promise { return await prisma.destinationDocker.findFirst({ where: { application: { some: { id } }, teams: { some: { id: teamId } } } }); } -export async function setDestinationSettings({ engine, isCoolifyProxyUsed }) { +export async function setDestinationSettings({ + engine, + isCoolifyProxyUsed +}: { + engine: string; + isCoolifyProxyUsed: boolean; +}): Promise { return await prisma.destinationDocker.updateMany({ where: { engine }, data: { isCoolifyProxyUsed } diff --git a/src/lib/types/destinations.ts b/src/lib/types/destinations.ts new file mode 100644 index 000000000..c5a56d772 --- /dev/null +++ b/src/lib/types/destinations.ts @@ -0,0 +1,8 @@ +export type CreateDockerDestination = { + name: string; + engine: string; + remoteEngine: boolean; + network: string; + isCoolifyProxyUsed: boolean; + teamId: string; +}; diff --git a/src/routes/new/destination/docker.json.ts b/src/routes/new/destination/docker.json.ts index 871efe949..f3941e025 100644 --- a/src/routes/new/destination/docker.json.ts +++ b/src/routes/new/destination/docker.json.ts @@ -1,43 +1,22 @@ -import { asyncExecShell, getUserDetails } from '$lib/common'; +import { getUserDetails } from '$lib/common'; import * as db from '$lib/database'; import { ErrorHandler } from '$lib/database'; -import { dockerInstance } from '$lib/docker'; import type { RequestHandler } from '@sveltejs/kit'; +import type { CreateDockerDestination } from '$lib/types/destinations'; export const post: RequestHandler = async (event) => { const { teamId, status, body } = await getUserDetails(event); if (status === 401) return { status, body }; - const { - name, - engine, - network, - isCoolifyProxyUsed, - remoteEngine, - ipAddress, - user, - port, - sshPrivateKey - } = await event.request.json(); + const dockerDestinationProps = { + ...((await event.request.json()) as Omit), + teamId + }; try { - let id = null; - if (remoteEngine) { - id = await db.newRemoteDestination({ - name, - teamId, - engine, - network, - isCoolifyProxyUsed, - remoteEngine, - ipAddress, - user, - port, - sshPrivateKey - }); - } else { - id = await db.newLocalDestination({ name, teamId, engine, network, isCoolifyProxyUsed }); - } + const id = dockerDestinationProps.remoteEngine + ? await db.newRemoteDestination(dockerDestinationProps) + : await db.newLocalDestination(dockerDestinationProps); return { status: 200, body: { id } }; } catch (error) { return ErrorHandler(error);