cpns / packages /api /src /lib /ownership.ts
rogasper's picture
chore(release): v0.0.1
439093c
Raw
History Blame Contribute Delete
550 Bytes
import { eq } from "drizzle-orm";
import { throwForbidden, throwNotFound } from "./errors";
interface OwnedRow {
creatorUserId: string | null;
}
export function assertOwnership(row: OwnedRow | null | undefined, userId: string, resource = "Resource"): void {
if (!row) throwNotFound(resource);
if (row.creatorUserId !== userId) throwForbidden();
}
export function ownershipFilter<TTable extends Record<string, any>>(
table: TTable,
userId: string,
creatorColumn: keyof TTable,
) {
return eq(table[creatorColumn as string], userId);
}