permix
Version:
Permix is a lightweight, framework-agnostic, type-safe permissions management library for JavaScript applications on the client and server sides.
56 lines (55 loc) • 1.67 kB
JavaScript
import { createPermix as createPermix$1 } from "../../core/index.mjs";
import { t as PermixInvalidActionsError } from "../../errors-C6Zlceok.mjs";
import { Table, is } from "drizzle-orm";
//#region src/drizzle/legacy/permix.ts
/**
* The default CRUD action set used when no `actions` are provided.
*/
const DEFAULT_DRIZZLE_ACTIONS = [
"create",
"read",
"update",
"delete"
];
/**
* 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,
* })
* ```
*/
function createPermix(schema, options = {}) {
const actions = options.actions ?? DEFAULT_DRIZZLE_ACTIONS;
if (!Array.isArray(actions) || actions.length === 0) throw new PermixInvalidActionsError();
const tables = Object.entries(schema).filter(([, value]) => is(value, Table)).map(([key]) => key);
const permix = createPermix$1();
return Object.assign(permix, {
actions,
tables
});
}
//#endregion
export { DEFAULT_DRIZZLE_ACTIONS, createPermix };