Skip to content

SQL base adapter

@vperms/sql-adapter is a dialect-agnostic base adapter. It maps between the public Subject/PermissionGrant shapes and flat rows, leaving the concrete engine to a driver. The Drizzle adapters are built on top of it, and you can build your own driver on the same contract.

import { VeguiPermsSqlAdapter } from "@vperms/sql-adapter";
const adapter = new VeguiPermsSqlAdapter(myDriver);
await adapter.migrate(); // delegates to the driver

It implements every VeguiPermsAdapter method in terms of SqlAdapterDriver, and adds migrate().

interface SqlAdapterDriver {
migrate(): Promise<void>;
findSubject(workspaceId: string, subjectId: string): Promise<SubjectRecord | null>;
upsertSubject(record: SubjectRecord): Promise<void>;
deleteSubject(workspaceId: string, subjectId: string): Promise<boolean>;
findGrants(workspaceId: string, subjectId: string): Promise<GrantRecord[]>;
upsertGrant(record: GrantRecord): Promise<void>;
deleteGrant(workspaceId: string, subjectId: string, permission: string): Promise<boolean>;
}

Rows are flat:

interface SubjectRecord {
workspaceId: string;
id: string;
type: SubjectType;
parents: string[]; // encode as JSON text, jsonb, ...
}
interface GrantRecord {
workspaceId: string;
subjectId: string;
permission: string;
value: boolean;
}

The driver deals only with rows and has no knowledge of validation, inheritance or matching. upsertSubject must replace the subject and upsertGrant must replace any previous value for the same (workspaceId, subjectId, permission).

@vperms/sql-adapter exports VeguiPermsSqlAdapter, and the types SqlAdapterDriver, SubjectRecord and GrantRecord. The @vperms/drizzle-adapter root entry re-exports them.