Added types for database/databases
This commit is contained in:
parent
faeae8fd6c
commit
e042c5cfde
@ -1,15 +1,21 @@
|
||||
import { decrypt, encrypt } from '$lib/crypto';
|
||||
import * as db from '$lib/database';
|
||||
import cuid from 'cuid';
|
||||
import { generatePassword } from '.';
|
||||
import { prisma, ErrorHandler } from './common';
|
||||
import getPort, { portNumbers } from 'get-port';
|
||||
import { prisma } from './common';
|
||||
import { asyncExecShell, getEngine, removeContainer } from '$lib/common';
|
||||
import type { Database, DatabaseSettings, DestinationDocker } from '@prisma/client';
|
||||
|
||||
export async function listDatabases(teamId) {
|
||||
export async function listDatabases(teamId: string): Promise<Database[]> {
|
||||
return await prisma.database.findMany({ where: { teams: { some: { id: teamId } } } });
|
||||
}
|
||||
export async function newDatabase({ name, teamId }) {
|
||||
|
||||
export async function newDatabase({
|
||||
name,
|
||||
teamId
|
||||
}: {
|
||||
name: string;
|
||||
teamId: string;
|
||||
}): Promise<Database> {
|
||||
const dbUser = cuid();
|
||||
const dbUserPassword = encrypt(generatePassword());
|
||||
const rootUser = cuid();
|
||||
@ -30,7 +36,13 @@ export async function newDatabase({ name, teamId }) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function getDatabase({ id, teamId }) {
|
||||
export async function getDatabase({
|
||||
id,
|
||||
teamId
|
||||
}: {
|
||||
id: string;
|
||||
teamId: string;
|
||||
}): Promise<Database & { destinationDocker: DestinationDocker; settings: DatabaseSettings }> {
|
||||
const body = await prisma.database.findFirst({
|
||||
where: { id, teams: { some: { id: teamId } } },
|
||||
include: { destinationDocker: true, settings: true }
|
||||
@ -39,16 +51,22 @@ export async function getDatabase({ id, teamId }) {
|
||||
if (body.dbUserPassword) body.dbUserPassword = decrypt(body.dbUserPassword);
|
||||
if (body.rootUserPassword) body.rootUserPassword = decrypt(body.rootUserPassword);
|
||||
|
||||
return { ...body };
|
||||
return body;
|
||||
}
|
||||
|
||||
export async function removeDatabase({ id }) {
|
||||
export async function removeDatabase({ id }: { id: string }): Promise<void> {
|
||||
await prisma.databaseSettings.deleteMany({ where: { databaseId: id } });
|
||||
await prisma.database.delete({ where: { id } });
|
||||
return;
|
||||
}
|
||||
|
||||
export async function configureDatabaseType({ id, type }) {
|
||||
export async function configureDatabaseType({
|
||||
id,
|
||||
type
|
||||
}: {
|
||||
id: string;
|
||||
type: string;
|
||||
}): Promise<Database> {
|
||||
return await prisma.database.update({
|
||||
where: { id },
|
||||
data: { type }
|
||||
@ -64,7 +82,7 @@ export async function setDatabase({
|
||||
version?: string;
|
||||
isPublic?: boolean;
|
||||
appendOnly?: boolean;
|
||||
}) {
|
||||
}): Promise<Database> {
|
||||
return await prisma.database.update({
|
||||
where: { id },
|
||||
data: {
|
||||
@ -82,7 +100,16 @@ export async function updateDatabase({
|
||||
rootUser,
|
||||
rootUserPassword,
|
||||
version
|
||||
}) {
|
||||
}: {
|
||||
id: string;
|
||||
name: string;
|
||||
defaultDatabase: string;
|
||||
dbUser: string;
|
||||
dbUserPassword: string;
|
||||
rootUser: string;
|
||||
rootUserPassword: string;
|
||||
version: string;
|
||||
}): Promise<Database> {
|
||||
const encryptedDbUserPassword = dbUserPassword && encrypt(dbUserPassword);
|
||||
const encryptedRootUserPassword = rootUserPassword && encrypt(rootUserPassword);
|
||||
return await prisma.database.update({
|
||||
@ -99,7 +126,9 @@ export async function updateDatabase({
|
||||
});
|
||||
}
|
||||
|
||||
export async function stopDatabase(database) {
|
||||
export async function stopDatabase(
|
||||
database: Database & { destinationDocker: DestinationDocker }
|
||||
): Promise<boolean> {
|
||||
let everStarted = false;
|
||||
const {
|
||||
id,
|
||||
|
Loading…
x
Reference in New Issue
Block a user