permix
Version:
Permix is a lightweight, framework-agnostic, type-safe permissions management library for JavaScript applications on the client and server sides.
86 lines (85 loc) • 3.67 kB
text/typescript
import { g as Permix, i as PermixError } from "../index-BYcbfVQ7.mjs";
import { ExtractTablesFromSchema } from "drizzle-orm";
//#region src/drizzle/errors.d.ts
declare class PermixInvalidActionsError extends PermixError {
constructor();
}
//#endregion
//#region src/drizzle/permix.d.ts
/**
* The default CRUD action set used when no `actions` are provided.
*/
declare const DEFAULT_DRIZZLE_ACTIONS: readonly ["create", "read", "update", "delete"];
type DefaultDrizzleAction = (typeof DEFAULT_DRIZZLE_ACTIONS)[number];
/**
* Keys of `S` that Drizzle v1 recognises as schema entries (tables and views).
* Relations, enums, helpers, and other non-entity exports are filtered out.
*/
type DrizzleTableKeys<S extends Record<string, unknown>> = keyof ExtractTablesFromSchema<S> & string;
/**
* Permix {@link import('../core/definitions').Definition} derived from a
* Drizzle schema, assigning the same `actions` tuple to every table/view.
*/
type DrizzleDefinition<S extends Record<string, unknown>, Actions extends readonly string[] = readonly DefaultDrizzleAction[]> = { [K in DrizzleTableKeys<S>]: [...Actions] };
type ActionMap<Actions extends readonly string[]> = Partial<Record<Actions[number], boolean>>;
interface CreateDrizzlePermixOptions<Actions extends readonly string[]> {
/**
* Override the actions generated for every table. Pass an `as const` tuple
* to preserve literal types.
*
* @default ['create', 'read', 'update', 'delete']
*/
actions?: Actions;
}
/**
* Extends the core Permix API with Drizzle-specific metadata.
*/
interface DrizzlePermix<S extends Record<string, unknown>, Actions extends readonly string[]> extends Permix<DrizzleDefinition<S, Actions>> {
/**
* The list of action names that were generated for every table.
*/
readonly actions: Actions;
/**
* The list of table (and view) names detected in the supplied schema.
*/
readonly tables: DrizzleTableKeys<S>[];
}
/**
* Create a type-safe Permix instance whose permission tree mirrors a Drizzle
* **v1** schema. Every exported table (and view) becomes a top-level entity
* with the same set of actions (CRUD by default).
*
* Internally uses Drizzle v1's official `extractTablesFromSchema` helper, so
* `defineRelations(...)` objects, plain helpers, and other non-entity exports
* are skipped automatically — pass `import * as schema` as-is.
*
* If you're still on Drizzle v0 (`drizzle-orm@<1`), import from
* `permix/drizzle/legacy` instead.
*
* @example
* ```ts
* import * as schema from './schema'
* import { createPermix } from 'permix/drizzle'
*
* const permix = createPermix(schema)
*
* permix.setup({
* users: { create: true, read: true, update: false, delete: false },
* posts: { create: true, read: true, update: true, delete: false },
* })
*
* permix.check('users.read') // true
* ```
*
* @example Customising actions
* ```ts
* const permix = createPermix(schema, {
* actions: ['view', 'edit'] as const,
* })
* ```
*/
declare function createPermix<S extends Record<string, unknown>, const Actions extends readonly string[] = readonly DefaultDrizzleAction[]>(schema: S, options?: CreateDrizzlePermixOptions<Actions>): DrizzlePermix<S, Actions>;
/** Return type of {@link createPermix}. */
type DrizzlePermixInstance<S extends Record<string, unknown>, Actions extends readonly string[] = readonly DefaultDrizzleAction[]> = ReturnType<typeof createPermix<S, Actions>>;
//#endregion
export { ActionMap, CreateDrizzlePermixOptions, DEFAULT_DRIZZLE_ACTIONS, DefaultDrizzleAction, DrizzleDefinition, DrizzlePermix, DrizzlePermixInstance, DrizzleTableKeys, PermixInvalidActionsError, createPermix };