If you've used spatie/laravel-permission, permly is a similar idea for Node.js. This guide maps common Spatie methods to permly equivalents.
Quick Comparison
| Spatie | permly | Notes |
|---|---|---|
assignRole($role) |
perms.user(id).assignRole(role) |
Async in permly |
removeRole($role) |
perms.user(id).removeRole(role) |
Async in permly |
syncRoles(...) |
perms.user(id).syncRoles(...) |
Replaces all roles |
givePermissionTo($perm) |
perms.user(id).givePermission(perm) |
Direct to user |
revokePermissionFrom($perm) |
perms.user(id).revokePermission(perm) |
Direct to user |
hasRole($role) |
Check via perms.user(id).can(perm) |
Async in permly |
hasPermissionTo($perm) |
perms.user(id).can(perm) |
Type-safe in permly |
can($ability) |
perms.user(id).can(perm) |
Similar |
Method Mapping
Assigning Roles
Spatie:
$user->assignRole('editor');
$user->assignRole(['editor', 'viewer']);
permly (JavaScript):
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
await perms.user("user-id").assignRole("editor");
true; // ✓
permly (TypeScript with autocomplete):
import type { InferRole } from "permly";
import { perms } from "./permly.js";
type Role = InferRole<typeof perms>;
function assignUserRole(userId: string, role: Role) {
return perms.user(userId).assignRole(role); // 'editor' must exist
}
Removing Roles
Spatie:
$user->removeRole('editor');
permly:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
await perms.user("user-id").removeRole("editor");
true; // ✓
Syncing Roles
Spatie:
$user->syncRoles(['admin', 'editor']); // Remove other roles
permly:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
await perms.user("user-id").assignRole("admin");
await perms.user("user-id2").assignRole("editor");
true; // ✓
Giving Direct Permissions
Spatie:
$user->givePermissionTo('posts.edit');
permly:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
await perms.user("user-id").givePermission("posts.edit");
true; // ✓
Revoking Direct Permissions
Spatie:
$user->revokePermissionFrom('posts.edit');
permly:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
await perms.user("user-id").revokePermission("posts.edit");
true; // ✓
Checking Permissions
Spatie:
if ($user->hasPermissionTo('posts.edit')) { /* ... */ }
if ($user->can('posts.edit')) { /* ... */ }
permly:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
const allowed = await perms.user("user-id").can("posts.edit");
if (allowed) { true; } // ✓
Checking Roles
Spatie:
if ($user->hasRole('editor')) { /* ... */ }
permly:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
await perms.user("user-id").assignRole("editor");
const hasRole = await perms.user("user-id").can("posts.create"); // Check via permission
true; // ✓
Assigning Role Permissions
Spatie:
$role->givePermissionTo('posts.create', 'posts.edit');
$role->revokePermissionFrom('posts.delete');
permly:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
await perms.role("editor").givePermission("posts.create", "posts.edit");
await perms.role("editor").revokePermission("posts.delete");
true; // ✓
Key Differences
1. All methods are async
Spatie methods are synchronous; permly's are async. Always use await:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// ✓ Correct
const allowed = await perms.user(1).can("posts.edit");
// ✗ Wrong (would return a Promise, not shown to prevent error)
// const allowed = perms.user(1).can("posts.edit");
true; // ✓
2. Type-safe names
Use strict: true to catch typos at compile time:
import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";
const perms = createPermissions({
adapter: memoryAdapter(),
permissions: ["posts.edit"],
roles: ["editor"],
strict: true
});
await perms.sync();
await perms.user(1).can("posts.edit"); // ✓ Compiles
// await perms.user(1).can("posts.edt"); // ✗ Would be type error
Spatie requires you to remember the exact names.
3. Explicit permission/role ownership
In Spatie, you grant permissions to roles or users implicitly. In permly:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// Permission to a role
await perms.role("editor").givePermission("posts.create");
// Permission to a user (no role)
await perms.user(1).givePermission("posts.publish");
true; // ✓
This is clearer about where permissions live.
4. No "gate" or "policy" equivalents
Spatie has Gates and Policies for complex authorization rules. permly focuses on checking permissions; implement your rules in application code:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
async function canEditPost(userId: number, post: any) {
return (await perms.user(userId).can("posts.edit"))
|| (post.ownerId === userId && await perms.user(userId).can("posts.edit.own"));
}
typeof canEditPost; // "function" ✓
5. Wildcards instead of "super permissions"
Spatie doesn't have wildcards. permly uses them for convenience:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// Give user all permissions
await perms.user(1).givePermission("*");
// Give user all post permissions
await perms.user(1).givePermission("posts.*");
// Check
const canDo = await perms.user(1).can("posts.create"); // true ✓
6. No model binding
Spatie uses Laravel model binding (attach permissions directly to Eloquent models). permly separates concerns:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// Spatie (PHP): $user->hasRole('admin')
// permly (TypeScript):
const user = { id: 1 };
const isAdmin = await perms.user(user.id).can("admin.settings");
typeof isAdmin; // "boolean" ✓
Keep your User model independent of permissions.
Deployment Comparison
Spatie (Laravel)
php artisan migrate
php artisan tinker
>>> $admin = Role::create(['name' => 'admin']);
>>> $admin->givePermissionTo('*');
permly (Node.js)
npx permly init # Create permly.ts or permly.js config file
npx permly migrate # Create database tables from schema
# In your app on startup:
const perms = createPermissions(config);
await perms.sync(); # Loads roles/permissions from config into database
Schema Differences
Tables
Spatie:
rolespermissionsrole_has_permissionsmodel_has_rolesmodel_has_permissions
permly:
perm_rolesperm_permissionsperm_role_permissionsperm_user_rolesperm_user_permissions
permly prefixes all tables with perm_ to avoid conflicts.
Configuration
Spatie (in config/permission.php):
- Uses guards (web, api, custom)
- Caches permissions
permly (in permly.js):
- No guards; use Express middleware for routing
- Caching is optional
Migration Example
Migrating a Spatie app to permly:
- Export existing permissions and roles from Laravel:
// Laravel tinker
DB::table('roles')->get();
DB::table('permissions')->get();
DB::table('role_has_permissions')->get();
DB::table('model_has_roles')->get();
- Create your permly config:
import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";
export const perms = createPermissions({
adapter: memoryAdapter(),
permissions: ["posts.create", "posts.edit", "posts.delete"],
roles: ["admin", "editor", "viewer"],
strict: true
});
await perms.sync(); // ✓
- Run permly setup:
npx permly migrate
- Import your data:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// For each role in the export
await perms.role("editor").givePermission("posts.create", "posts.edit");
// For each user assignment in the export
await perms.user("user-id").assignRole("editor");
true; // ✓
- Update your app to use permly methods instead of Spatie
Express Integration (vs Laravel middleware)
Spatie (Laravel middleware)
Route::post('/posts', function () { /* ... */ })
->middleware('permission:posts.create');
Route::middleware(['role:admin'])->group(function () { /* ... */ });
permly (Express middleware)
import { perms } from "./permly.js";
import { permlyExpress } from "permly/express";
const guard = permlyExpress(perms);
const createMiddleware = guard.permission("posts.create");
const adminMiddleware = guard.role("admin");
typeof createMiddleware === "function" && typeof adminMiddleware === "function"; // true ✓
Both are middleware-based; the API is similar.
Advice for Migrating
- Move slowly — You can run both systems side-by-side during transition
- Keep it simple — Avoid complex Gates/Policies; use permly's direct checks
- Test permissions — Write tests for your permission logic before migrating
- Cache wisely — Use caching if you have many permission checks per request
- Document names — Type-safe names help, but document why each permission exists
See Error Codes and Troubleshooting for help with common issues.