Resource identified with niceId now

This commit is contained in:
Owen
2025-09-08 17:50:07 -07:00
parent a947a74194
commit fe6e3b013e
9 changed files with 44 additions and 18 deletions

View File

@@ -1,6 +1,6 @@
import { join } from "path";
import { readFileSync } from "fs";
import { db } from "@server/db";
import { db, resources } from "@server/db";
import { exitNodes, sites } from "@server/db";
import { eq, and } from "drizzle-orm";
import { __DIRNAME } from "@server/lib/consts";
@@ -34,6 +34,25 @@ export async function getUniqueSiteName(orgId: string): Promise<string> {
}
}
export async function getUniqueResourceName(orgId: string): Promise<string> {
let loops = 0;
while (true) {
if (loops > 100) {
throw new Error("Could not generate a unique name");
}
const name = generateName();
const count = await db
.select({ niceId: resources.niceId, orgId: resources.orgId })
.from(resources)
.where(and(eq(resources.niceId, name), eq(resources.orgId, orgId)));
if (count.length === 0) {
return name;
}
loops++;
}
}
export async function getUniqueExitNodeEndpointName(): Promise<string> {
let loops = 0;
const count = await db

View File

@@ -21,6 +21,7 @@ import { subdomainSchema } from "@server/lib/schemas";
import config from "@server/lib/config";
import { OpenAPITags, registry } from "@server/openApi";
import { build } from "@server/build";
import { getUniqueResourceName } from "@server/db/names";
const createResourceParamsSchema = z
.object({
@@ -283,10 +284,13 @@ async function createHttpResource(
let resource: Resource | undefined;
const niceId = await getUniqueResourceName(orgId);
await db.transaction(async (trx) => {
const newResource = await trx
.insert(resources)
.values({
niceId,
fullDomain,
domainId,
orgId,
@@ -391,10 +395,13 @@ async function createRawResource(
let resource: Resource | undefined;
const niceId = await getUniqueResourceName(orgId);
await db.transaction(async (trx) => {
const newResource = await trx
.insert(resources)
.values({
niceId,
orgId,
name,
http,