copyInConfig.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. import { db } from "@server/db";
  2. import { exitNodes, orgs, resources } from "../db/schema";
  3. import config from "@server/lib/config";
  4. import { eq, ne } from "drizzle-orm";
  5. import logger from "@server/logger";
  6. export async function copyInConfig() {
  7. const domain = config.getBaseDomain();
  8. const endpoint = config.getRawConfig().gerbil.base_endpoint;
  9. // update the domain on all of the orgs where the domain is not equal to the new domain
  10. // TODO: eventually each org could have a unique domain that we do not want to overwrite, so this will be unnecessary
  11. await db.update(orgs).set({ domain }).where(ne(orgs.domain, domain));
  12. // TODO: eventually each exit node could have a different endpoint
  13. await db.update(exitNodes).set({ endpoint }).where(ne(exitNodes.endpoint, endpoint));
  14. // update all resources fullDomain to use the new domain
  15. await db.transaction(async (trx) => {
  16. const allResources = await trx.select().from(resources);
  17. for (const resource of allResources) {
  18. const fullDomain = `${resource.subdomain}.${domain}`;
  19. await trx
  20. .update(resources)
  21. .set({ fullDomain })
  22. .where(eq(resources.resourceId, resource.resourceId));
  23. }
  24. });
  25. logger.info(`Updated orgs with new domain (${domain})`);
  26. }