UNPKG

permix

Version:

Permix is a lightweight, framework-agnostic, type-safe permissions management library for JavaScript applications on the client and server sides.

77 lines (76 loc) 3.18 kB
import { g as Permix } from "../../index-BYcbfVQ7.mjs"; import { Table } from "drizzle-orm"; //#region src/drizzle/legacy/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` whose values are Drizzle `Table` instances. * Relations, enums, helpers, and other non-table exports are filtered out. */ type DrizzleTableKeys<S> = { [K in keyof S]: S[K] extends Table<any> ? K & string : never }[keyof S]; /** * Permix {@link Definition} derived from a Drizzle schema, assigning the same * `actions` tuple to every table. */ type DrizzleDefinition<S, 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, 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 names that were detected in the supplied schema. */ readonly tables: DrizzleTableKeys<S>[]; } /** * Create a type-safe Permix instance whose permission tree mirrors a Drizzle * schema. Every exported table becomes a top-level entity with the same set * of actions (CRUD by default). * * Non-table exports such as `relations`, enums, helpers, etc. are skipped at * both the type and runtime layers, so you can pass `import * as schema` as-is. * * @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, createPermix };