| 1 | import { randomUUID } from "crypto"; |
| 2 | import { eq } |
| 3 | import { z } |
| 4 | import { db } |
| 5 | import { databaseEngineTypes, eggVariables |
| 6 | import { recordActivity } |
| 7 | import { adminProcedure, protectedProcedure |
| 8 | |
| 9 | const EggVariableSchema = z |
| 10 | name: z.string |
| 11 | description: z.string |
| 12 | envVariable: z.string |
| 13 | defaultValue: z.string |
| 14 | userViewable: z.boolean |
| 15 | userEditable: z.boolean |
| 16 | rules: z.string |
| 17 | }); |
| 18 | |
| 19 | export const eggsRouter = |
| 20 | listAll: adminProcedure.handler |
| 21 | const rows = |
| 22 | with: { nest: true |
| 23 | orderBy: (eggs, |
| 24 | }); |
| 25 | return rows.map |
| 26 | id: e.id |
| 27 | name: e.name |
| 28 | nestId: e.nestId |
| 29 | nestName: e.nest |
| 30 | })); |
| 31 | }), |
| 32 | |
| 33 | listByNest: protectedProcedure |
| 34 | .input(z.object |
| 35 | .handler(async ( |
| 36 | return db.query |
| 37 | where: eq(eggs |
| 38 | with: { variables: true |
| 39 | }); |
| 40 | }), |
| 41 | |
| 42 | get: protectedProcedure |
| 43 | .input(z.object |
| 44 | .handler(async ( |
| 45 | return db.query |
| 46 | where: eq(eggs |
| 47 | with: { variables: true |
| 48 | }); |
| 49 | }), |
| 50 | |
| 51 | create: adminProcedure |
| 52 | .input( |
| 53 | z.object({ |
| 54 | nestId: z.string |
| 55 | name: z.string |
| 56 | description: z.string |
| 57 | dockerImages: z.record |
| 58 | startup: z.string |
| 59 | stopCommand: z.string |
| 60 | features: z.array |
| 61 | allowedDatabaseTypes: z.array |
| 62 | configFiles: z.string |
| 63 | configStartup: z.string |
| 64 | configStop: z.string |
| 65 | configLogs: z.string |
| 66 | scriptInstall: z.string |
| 67 | scriptEntry: z.string |
| 68 | scriptContainer: z.string |
| 69 | scriptExtension: z.string |
| 70 | variables: z.array |
| 71 | }), |
| 72 | ) |
| 73 | .handler(async ( |
| 74 | const { variables, |
| 75 | const id = |
| 76 | const uuid = |
| 77 | |
| 78 | await db.insert |
| 79 | id, |
| 80 | uuid, |
| 81 | dockerImages: JSON.stringify |
| 82 | features: features ? |
| 83 | allowedDatabaseTypes: allowedDatabaseTypes?.length |
| 84 | ...eggData, |
| 85 | }); |
| 86 | |
| 87 | if (variables?.length) |
| 88 | await db.insert |
| 89 | variables.map((v |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | recordActivity({ eventType: |
| 94 | |
| 95 | return db.query |
| 96 | where: eq(eggs |
| 97 | with: { variables: true |
| 98 | }); |
| 99 | }), |
| 100 | |
| 101 | update: adminProcedure |
| 102 | .input( |
| 103 | z.object({ |
| 104 | id: z.string |
| 105 | name: z.string |
| 106 | description: z.string |
| 107 | dockerImages: z.record |
| 108 | startup: z.string |
| 109 | stopCommand: z.string |
| 110 | configStartup: z.string |
| 111 | features: z.array |
| 112 | allowedDatabaseTypes: z.array |
| 113 | scriptInstall: z.string |
| 114 | scriptEntry: z.string |
| 115 | scriptContainer: z.string |
| 116 | scriptExtension: z.string |
| 117 | }), |
| 118 | ) |
| 119 | .handler(async ( |
| 120 | const { id, |
| 121 | await db |
| 122 | .update(eggs) |
| 123 | .set({ |
| 124 | ...data, |
| 125 | ...(dockerImages ? |
| 126 | ...(features !== |
| 127 | ...(allowedDatabaseTypes !== |
| 128 | ? { allowedDatabaseTypes: allowedDatabaseTypes |
| 129 | : {}), |
| 130 | }) |
| 131 | .where(eq(eggs |
| 132 | const egg = |
| 133 | where: eq(eggs |
| 134 | with: { variables: true |
| 135 | }); |
| 136 | recordActivity({ eventType: |
| 137 | return egg; |
| 138 | }), |
| 139 | |
| 140 | delete: adminProcedure |
| 141 | .input(z.object |
| 142 | .handler(async ( |
| 143 | const egg = |
| 144 | await db.delete |
| 145 | recordActivity({ eventType: |
| 146 | }), |
| 147 | |
| 148 | addVariable: adminProcedure |
| 149 | .input( |
| 150 | z.object({ |
| 151 | eggId: z.string |
| 152 | name: z.string |
| 153 | description: z.string |
| 154 | envVariable: z.string |
| 155 | defaultValue: z.string |
| 156 | userViewable: z.boolean |
| 157 | userEditable: z.boolean |
| 158 | rules: z.string |
| 159 | }), |
| 160 | ) |
| 161 | .handler(async ( |
| 162 | const { eggId, |
| 163 | const id = |
| 164 | await db.insert |
| 165 | recordActivity({ eventType: |
| 166 | return db.query |
| 167 | }), |
| 168 | |
| 169 | updateVariable: adminProcedure |
| 170 | .input( |
| 171 | z.object({ |
| 172 | variableId: z.string |
| 173 | name: z.string |
| 174 | description: z.string |
| 175 | envVariable: z.string |
| 176 | defaultValue: z.string |
| 177 | userViewable: z.boolean |
| 178 | userEditable: z.boolean |
| 179 | rules: z.string |
| 180 | }), |
| 181 | ) |
| 182 | .handler(async ( |
| 183 | const { variableId, |
| 184 | const updates: |
| 185 | name?: string |
| 186 | description?: string |
| 187 | envVariable?: string |
| 188 | defaultValue?: string |
| 189 | userViewable?: boolean |
| 190 | userEditable?: boolean |
| 191 | rules?: string |
| 192 | } = {}; |
| 193 | if (name !== |
| 194 | if (description !== |
| 195 | if (envVariable !== |
| 196 | if (defaultValue !== |
| 197 | if (userViewable !== |
| 198 | if (userEditable !== |
| 199 | if (rules !== |
| 200 | if (Object.keys |
| 201 | await db.update |
| 202 | } |
| 203 | const variable = |
| 204 | recordActivity({ eventType: |
| 205 | return variable; |
| 206 | }), |
| 207 | |
| 208 | deleteVariable: adminProcedure |
| 209 | .input(z.object |
| 210 | .handler(async ( |
| 211 | const variable = |
| 212 | await db.delete |
| 213 | recordActivity({ eventType: |
| 214 | }), |
| 215 | |
| 216 | importJson: adminProcedure |
| 217 | .input(z.object |
| 218 | .handler(async ( |
| 219 | const raw = |
| 220 | name: string; |
| 221 | description?: string |
| 222 | startup?: string |
| 223 | config?: { |
| 224 | files?: string |
| 225 | startup?: string |
| 226 | stop?: string |
| 227 | logs?: string |
| 228 | }; |
| 229 | scripts?: { |
| 230 | installation?: { |
| 231 | script?: string |
| 232 | container?: string |
| 233 | entrypoint?: string |
| 234 | }; |
| 235 | }; |
| 236 | variables?: Array |
| 237 | name: string |
| 238 | description?: string |
| 239 | env_variable: string |
| 240 | default_value?: string |
| 241 | user_viewable?: boolean |
| 242 | user_editable?: boolean |
| 243 | rules?: string |
| 244 | }>; |
| 245 | docker_images?: Record |
| 246 | features?: string |
| 247 | }; |
| 248 | |
| 249 | const id = |
| 250 | const uuid = |
| 251 | |
| 252 | await db.insert |
| 253 | id, |
| 254 | uuid, |
| 255 | nestId: input.nestId |
| 256 | name: raw.name |
| 257 | description: raw.description |
| 258 | startup: raw.startup |
| 259 | dockerImages: JSON.stringify |
| 260 | features: raw.features |
| 261 | stopCommand: raw.config |
| 262 | configFiles: raw.config |
| 263 | configStartup: raw.config |
| 264 | configStop: raw.config |
| 265 | configLogs: raw.config |
| 266 | scriptInstall: raw.scripts |
| 267 | scriptContainer: raw.scripts |
| 268 | scriptEntry: raw.scripts |
| 269 | }); |
| 270 | |
| 271 | if (raw.variables |
| 272 | await db.insert |
| 273 | raw.variables.map |
| 274 | id: randomUUID(), |
| 275 | eggId: id, |
| 276 | name: v.name |
| 277 | description: v.description |
| 278 | envVariable: v.env_variable |
| 279 | defaultValue: v.default_value |
| 280 | userViewable: v.user_viewable |
| 281 | userEditable: v.user_editable |
| 282 | rules: v.rules |
| 283 | })), |
| 284 | ); |
| 285 | } |
| 286 | |
| 287 | recordActivity({ eventType: |
| 288 | |
| 289 | return db.query |
| 290 | where: eq(eggs |
| 291 | with: { variables: true |
| 292 | }); |
| 293 | }), |
| 294 | }; |