| 1 | import { randomUUID } from "crypto"; |
| 2 | import { eq } |
| 3 | import { z } |
| 4 | import { ORPCError } |
| 5 | import { db } |
| 6 | import { databaseHosts, databaseEngineTypes |
| 7 | import { encrypt, decrypt |
| 8 | import { dbEngines } |
| 9 | import { recordActivity } |
| 10 | import { adminProcedure, protectedProcedure |
| 11 | |
| 12 | export const databaseHostsRouter = |
| 13 | listAvailableTypes: protectedProcedure |
| 14 | .input(z.object |
| 15 | .handler(async ( |
| 16 | const server = |
| 17 | where: eq(servers |
| 18 | with: { egg: true |
| 19 | }); |
| 20 | if (!server) |
| 21 | if (server.userId |
| 22 | throw new ORPCError( |
| 23 | } |
| 24 | |
| 25 | const rawAllowed = |
| 26 | const allowedTypes = |
| 27 | |
| 28 | const rows = |
| 29 | .select({ type: |
| 30 | .from(databaseHosts); |
| 31 | |
| 32 | const eligible = |
| 33 | if (allowedTypes && |
| 34 | const nodeIds = |
| 35 | return !nodeIds || |
| 36 | }); |
| 37 | |
| 38 | return [...new |
| 39 | }), |
| 40 | |
| 41 | list: adminProcedure.handler |
| 42 | const rows = |
| 43 | return rows.map |
| 44 | }), |
| 45 | |
| 46 | get: adminProcedure |
| 47 | .input(z.object |
| 48 | .handler(async ( |
| 49 | const host = |
| 50 | where: eq(databaseHosts |
| 51 | }); |
| 52 | if (!host) |
| 53 | return { ...host |
| 54 | }), |
| 55 | |
| 56 | create: adminProcedure |
| 57 | .input( |
| 58 | z.object({ |
| 59 | name: z.string |
| 60 | type: z.enum |
| 61 | host: z.string |
| 62 | port: z.number |
| 63 | username: z.string |
| 64 | password: z.string |
| 65 | ssl: z.boolean |
| 66 | maxDatabases: z.number |
| 67 | allowedNodeIds: z.array |
| 68 | }), |
| 69 | ) |
| 70 | .handler(async ( |
| 71 | const { allowedNodeIds, |
| 72 | const id = |
| 73 | await db.insert |
| 74 | id, |
| 75 | ...rest, |
| 76 | password: encrypt(input |
| 77 | allowedNodeIds: allowedNodeIds?.length |
| 78 | }); |
| 79 | const host = |
| 80 | where: eq(databaseHosts |
| 81 | }); |
| 82 | recordActivity({ eventType: |
| 83 | return { ...host |
| 84 | }), |
| 85 | |
| 86 | update: adminProcedure |
| 87 | .input( |
| 88 | z.object({ |
| 89 | id: z.string |
| 90 | name: z.string |
| 91 | type: z.enum |
| 92 | host: z.string |
| 93 | port: z.number |
| 94 | username: z.string |
| 95 | password: z.string |
| 96 | ssl: z.boolean |
| 97 | maxDatabases: z.number |
| 98 | allowedNodeIds: z.array |
| 99 | }), |
| 100 | ) |
| 101 | .handler(async ( |
| 102 | const { id, |
| 103 | await db |
| 104 | .update(databaseHosts) |
| 105 | .set({ |
| 106 | ...data, |
| 107 | ...(password ? |
| 108 | ...(allowedNodeIds !== |
| 109 | ? { allowedNodeIds: allowedNodeIds |
| 110 | : {}), |
| 111 | }) |
| 112 | .where(eq(databaseHosts |
| 113 | const host = |
| 114 | where: eq(databaseHosts |
| 115 | }); |
| 116 | recordActivity({ eventType: |
| 117 | return { ...host |
| 118 | }), |
| 119 | |
| 120 | delete: adminProcedure |
| 121 | .input(z.object |
| 122 | .handler(async ( |
| 123 | const host = |
| 124 | await db.delete |
| 125 | recordActivity({ eventType: |
| 126 | }), |
| 127 | |
| 128 | testConnection: adminProcedure |
| 129 | .input(z.object |
| 130 | .handler(async ( |
| 131 | const host = |
| 132 | where: eq(databaseHosts |
| 133 | }); |
| 134 | if (!host) |
| 135 | |
| 136 | return dbEngines[host |
| 137 | host: host.host |
| 138 | port: host.port |
| 139 | username: host.username |
| 140 | password: decrypt(host |
| 141 | ssl: host.ssl |
| 142 | }); |
| 143 | }), |
| 144 | }; |