Skip to content

Commit 9b4a37e

Browse files
authored
Merge pull request #3800 from Dokploy/3765-missing-mounts-endpoint-for-managed-database-services
feat: enhance mount API to support service type filtering and organiz…
2 parents e62bb75 + a467920 commit 9b4a37e

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

apps/dokploy/server/api/routers/mount.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,70 @@
11
import {
2+
checkServiceAccess,
23
createMount,
34
deleteMount,
45
findApplicationById,
6+
findComposeById,
7+
findMariadbById,
8+
findMongoById,
59
findMountById,
610
findMountOrganizationId,
11+
findMountsByApplicationId,
12+
findMySqlById,
13+
findPostgresById,
14+
findRedisById,
715
getServiceContainer,
816
updateMount,
917
} from "@dokploy/server";
18+
import type { ServiceType } from "@dokploy/server/db/schema/mount";
1019
import { TRPCError } from "@trpc/server";
1120
import { z } from "zod";
1221
import {
1322
apiCreateMount,
23+
apiFindMountByApplicationId,
1424
apiFindOneMount,
1525
apiRemoveMount,
1626
apiUpdateMount,
1727
} from "@/server/db/schema";
1828
import { createTRPCRouter, protectedProcedure } from "../trpc";
1929

30+
async function getServiceOrganizationId(
31+
serviceId: string,
32+
serviceType: ServiceType,
33+
): Promise<string | null> {
34+
switch (serviceType) {
35+
case "application": {
36+
const app = await findApplicationById(serviceId);
37+
return app?.environment?.project?.organizationId ?? null;
38+
}
39+
case "postgres": {
40+
const postgres = await findPostgresById(serviceId);
41+
return postgres?.environment?.project?.organizationId ?? null;
42+
}
43+
case "mariadb": {
44+
const mariadb = await findMariadbById(serviceId);
45+
return mariadb?.environment?.project?.organizationId ?? null;
46+
}
47+
case "mongo": {
48+
const mongo = await findMongoById(serviceId);
49+
return mongo?.environment?.project?.organizationId ?? null;
50+
}
51+
case "mysql": {
52+
const mysql = await findMySqlById(serviceId);
53+
return mysql?.environment?.project?.organizationId ?? null;
54+
}
55+
case "redis": {
56+
const redis = await findRedisById(serviceId);
57+
return redis?.environment?.project?.organizationId ?? null;
58+
}
59+
case "compose": {
60+
const compose = await findComposeById(serviceId);
61+
return compose?.environment?.project?.organizationId ?? null;
62+
}
63+
default:
64+
return null;
65+
}
66+
}
67+
2068
export const mountRouter = createTRPCRouter({
2169
create: protectedProcedure
2270
.input(apiCreateMount)
@@ -71,4 +119,35 @@ export const mountRouter = createTRPCRouter({
71119
);
72120
return mounts;
73121
}),
122+
listByServiceId: protectedProcedure
123+
.input(apiFindMountByApplicationId)
124+
.query(async ({ input, ctx }) => {
125+
console.log("input", input);
126+
if (ctx.user.role === "member") {
127+
await checkServiceAccess(
128+
ctx.user.id,
129+
input.serviceId,
130+
ctx.session.activeOrganizationId,
131+
"access",
132+
);
133+
}
134+
const organizationId = await getServiceOrganizationId(
135+
input.serviceId,
136+
input.serviceType,
137+
);
138+
if (
139+
organizationId === null ||
140+
organizationId !== ctx.session.activeOrganizationId
141+
) {
142+
throw new TRPCError({
143+
code: "UNAUTHORIZED",
144+
message:
145+
"You are not authorized to access this service or it does not exist",
146+
});
147+
}
148+
return await findMountsByApplicationId(
149+
input.serviceId,
150+
input.serviceType,
151+
);
152+
}),
74153
});

packages/server/src/db/schema/mount.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,20 @@ export const apiRemoveMount = createSchema
146146
export const apiFindMountByApplicationId = createSchema
147147
.extend({
148148
serviceId: z.string().min(1),
149+
serviceType: z.enum([
150+
"application",
151+
"postgres",
152+
"mysql",
153+
"mariadb",
154+
"mongo",
155+
"redis",
156+
"compose",
157+
]),
149158
})
150159
.pick({
151160
serviceId: true,
152161
serviceType: true,
153-
})
154-
.required();
162+
});
155163

156164
export const apiUpdateMount = createSchema.partial().extend({
157165
mountId: z.string().min(1),

packages/server/src/services/mount.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ export const findMountsByApplicationId = async (
263263
case "redis":
264264
sqlChunks.push(eq(mounts.redisId, serviceId));
265265
break;
266+
case "compose":
267+
sqlChunks.push(eq(mounts.composeId, serviceId));
268+
break;
266269
default:
267270
throw new Error(`Unknown service type: ${serviceType}`);
268271
}

0 commit comments

Comments
 (0)