@cfast/permissions 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -292,4 +292,31 @@ type GrantedAction = {
292
292
  */
293
293
  declare function getGrantedActions(grants: Grant[], table: DrizzleTable): GrantedAction[];
294
294
 
295
- export { CrudAction, DrizzleTable, Grant, type GrantedAction, PermissionAction, PermissionCheckResult, PermissionDescriptor, Permissions, PermissionsConfig, SchemaMap, SubjectInput, type UserWithRoles, WhereClause, WithLookups, can, checkPermissions, definePermissions, getGrantedActions, grant, resolveGrants };
295
+ /**
296
+ * Resolves table-level CRUD permissions for every table in a schema.
297
+ *
298
+ * Iterates each key in the schema map, extracts its table name, and calls
299
+ * {@link can} for each of the four CRUD actions (`read`, `create`, `update`,
300
+ * `delete`). Returns a flat, serializable map suitable for embedding in
301
+ * loader data and sending to the client.
302
+ *
303
+ * This is a pure grant-structural check — no database access, no SQL.
304
+ * Row-level `where` clauses on grants are ignored; the result reflects
305
+ * whether the user has *any* grant for the action on the table.
306
+ *
307
+ * @param grants - The user's resolved permission grants.
308
+ * @param schema - A schema map (e.g. `import * as schema from "./schema"`).
309
+ * @returns A record keyed by SQL table name, each mapping CRUD actions to booleans.
310
+ *
311
+ * @example
312
+ * ```ts
313
+ * import { resolveTablePermissions } from "@cfast/permissions";
314
+ * import * as schema from "../db/schema";
315
+ *
316
+ * const perms = resolveTablePermissions(grants, schema);
317
+ * // { posts: { read: true, create: true, update: false, delete: false }, ... }
318
+ * ```
319
+ */
320
+ declare function resolveTablePermissions(grants: Grant[], schema: SchemaMap): Record<string, Record<CrudAction, boolean>>;
321
+
322
+ export { CrudAction, DrizzleTable, Grant, type GrantedAction, PermissionAction, PermissionCheckResult, PermissionDescriptor, Permissions, PermissionsConfig, SchemaMap, SubjectInput, type UserWithRoles, WhereClause, WithLookups, can, checkPermissions, definePermissions, getGrantedActions, grant, resolveGrants, resolveTablePermissions };
package/dist/index.js CHANGED
@@ -253,6 +253,23 @@ function getGrantedActions(grants, table) {
253
253
  }
254
254
  return result;
255
255
  }
256
+
257
+ // src/resolve-table-permissions.ts
258
+ function resolveTablePermissions(grants, schema) {
259
+ const result = {};
260
+ for (const key of Object.keys(schema)) {
261
+ const table = schema[key];
262
+ const tableName = getTableName(table);
263
+ if (tableName === "unknown") continue;
264
+ if (result[tableName]) continue;
265
+ const perms = {};
266
+ for (const action of CRUD_ACTIONS) {
267
+ perms[action] = can(grants, action, table);
268
+ }
269
+ result[tableName] = perms;
270
+ }
271
+ return result;
272
+ }
256
273
  export {
257
274
  CRUD_ACTIONS,
258
275
  ForbiddenError,
@@ -263,5 +280,6 @@ export {
263
280
  getGrantedActions,
264
281
  getTableName,
265
282
  grant,
266
- resolveGrants
283
+ resolveGrants,
284
+ resolveTablePermissions
267
285
  };
package/llms.txt CHANGED
@@ -229,6 +229,18 @@ Used internally by `@cfast/db` to build row-level `_can` annotations on query re
229
229
  const CRUD_ACTIONS: readonly CrudAction[] = ["read", "create", "update", "delete"];
230
230
  ```
231
231
 
232
+ ### `resolveTablePermissions(grants, schema): Record<string, Record<CrudAction, boolean>>`
233
+ ```typescript
234
+ import { resolveTablePermissions } from "@cfast/permissions";
235
+ import * as schema from "../db/schema";
236
+
237
+ const perms = resolveTablePermissions(grants, schema);
238
+ // { posts: { read: true, create: true, update: false, delete: false }, ... }
239
+ ```
240
+ Pure grant-structural check for every table in the schema. No DB, no SQL. Returns a serializable map. Skips non-table entries (e.g. relation objects) and deduplicates when multiple JS keys map to the same SQL name.
241
+
242
+ Used internally by `cfastJson()` from `@cfast/actions` to embed table permissions in loader data.
243
+
232
244
  ### Client entrypoint
233
245
  ```typescript
234
246
  import { ForbiddenError, can } from "@cfast/permissions/client";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cfast/permissions",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Isomorphic, composable permission system with Drizzle-native row-level access control",
5
5
  "keywords": [
6
6
  "cfast",