Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

1.7 KB
Raw Blame
import { create } from "@bufbuild/protobuf";
import { router } from "@/app/router";
import { getDatabaseByName } from "@/stores/app/databaseAccess";
import { UNKNOWN_ID } from "@/types";
import type { Changelog } from "@/types/proto-es/v1/changelog_service_pb";
import { ChangelogSchema } from "@/types/proto-es/v1/changelog_service_pb";
import type { Database } from "@/types/proto-es/v1/database_service_pb";
import { autoDatabaseChangelogRoute } from "../auto-route";

export const extractChangelogUID = (name: string) => {
  const pattern = /(?:^|\/)changelogs\/([^/]+)(?:$|\/)/;
  const matches = name.match(pattern);
  return matches?.[1] ?? "";
};

export const extractDatabaseNameAndChangelogUID = (changelogName: string) => {
  const parts = changelogName.split("/changelogs/");
  if (parts.length !== 2) {
    throw new Error("Invalid changelog name");
  }
  return {
    databaseName: parts[0],
    changelogUID: extractChangelogUID(changelogName),
  };
};

export const isValidChangelogName = (name: string | undefined) => {
  if (!name) {
    return false;
  }
  const uid = extractChangelogUID(name);
  return uid && uid !== String(UNKNOWN_ID);
};

export const changelogLink = (changelog: Changelog): string => {
  const { changelogUID, databaseName } = extractDatabaseNameAndChangelogUID(
    changelog.name
  );
  const composedDatabase = getDatabaseByName(databaseName);
  return router.resolve(
    autoDatabaseChangelogRoute(composedDatabase, changelogUID)
  ).href;
};

export const mockLatestChangelog = (
  database: Database,
  schema: string = ""
) => {
  return create(ChangelogSchema, {
    name: `${database.name}/changelogs/${UNKNOWN_ID}`,
    schema: schema,
    schemaSize: BigInt(new TextEncoder().encode(schema).length),
  });
};