fosrl.pangolin/server/setup/copyInConfig.ts

33 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-12-22 12:23:19 -05:00
import { db } from "@server/db";
import { exitNodes, orgs, resources } from "../db/schema";
2025-01-01 21:41:31 -05:00
import config from "@server/lib/config";
2025-01-01 23:03:15 -05:00
import { eq, ne } from "drizzle-orm";
2024-12-22 12:23:19 -05:00
import logger from "@server/logger";
export async function copyInConfig() {
const domain = config.getBaseDomain();
const endpoint = config.getRawConfig().gerbil.base_endpoint;
2024-12-25 15:54:32 -05:00
2024-12-22 12:23:19 -05:00
// update the domain on all of the orgs where the domain is not equal to the new domain
// TODO: eventually each org could have a unique domain that we do not want to overwrite, so this will be unnecessary
await db.update(orgs).set({ domain }).where(ne(orgs.domain, domain));
2025-01-01 23:03:15 -05:00
// TODO: eventually each exit node could have a different endpoint
await db.update(exitNodes).set({ endpoint }).where(ne(exitNodes.endpoint, endpoint));
2025-01-01 23:03:15 -05:00
// update all resources fullDomain to use the new domain
await db.transaction(async (trx) => {
const allResources = await trx.select().from(resources);
for (const resource of allResources) {
const fullDomain = `${resource.subdomain}.${domain}`;
await trx
.update(resources)
.set({ fullDomain })
.where(eq(resources.resourceId, resource.resourceId));
}
});
2024-12-25 15:54:32 -05:00
logger.info(`Updated orgs with new domain (${domain})`);
}