Guides

Syncing Config

Load roles and permissions from your database

What it does

Before you can check permissions, permly needs to load your roles and permissions from the database. This is called "syncing" config. Sync happens once at startup, then permly caches the config in memory.

When to use it

Sync in two scenarios:

  • At startup — call setupPermissions() or perms.sync() once
  • After config changes — if you modify roles/permissions in the database, call sync() again

Most apps sync once at startup and never again.

How it works

  1. Call setupPermissions() — permly connects to the database and loads config
  2. Creates roles and permissions if they don't exist (first run)
  3. Caches config in memory for fast checks
  4. If config changes in DB — call sync() to refresh the cache

Examples

import { perms, setupPermissions } from "./permly.js";

// Call setupPermissions() at app startup
await setupPermissions();

// Now you can check permissions
const allowed = await perms.user(42).can("posts.edit");
console.log(allowed);
import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

const allowed: boolean = await perms.user(42).can("posts.edit");
console.log(allowed);

Manual sync

If you modify permissions in the database and need to reload:

import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

// Do stuff...

// Config was updated in the database externally
// Refresh permly's cache
await perms.sync();

// Now you see the updated config
const allowed = await perms.user(42).can("posts.edit");
console.log(allowed);
import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

// Config changed
await perms.sync();

const allowed: boolean = await perms.user(42).can("posts.edit");
console.log(allowed);

First run: setupPermissions() creates tables

On the first run, setupPermissions() creates the database tables:

import { perms, setupPermissions } from "./permly.js";

// First run: creates tables, roles, permissions
await setupPermissions();

// Second run: loads existing config
await setupPermissions();

// Later calls are idempotent (safe to call multiple times)
import { perms, setupPermissions } from "./permly.js";

// Idempotent: safe to call multiple times
await setupPermissions();

Check if sync is needed

In development, you might sync every startup. In production, cache the config:

import { perms, setupPermissions } from "./permly.js";

// Always safe to call—it's idempotent
await setupPermissions();

// If you're in development and want to reload:
if (process.env.NODE_ENV === "development") {
  // In development, call sync() to load any manual changes to the database
  await perms.sync();
}
import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

if (process.env.NODE_ENV === "development") {
  await perms.sync();
}

Multi-instance sync

If you have multiple app instances and one updates the database, the others won't see it until they call sync(). For real-time sync across instances, use webhooks or a message queue (advanced).

Common mistakes

Forgetting to call setupPermissions() — If you don't, all permission checks will fail or throw.

Calling sync() too often — Sync is not cheap (database query). Call it only when needed, not on every request.

Not syncing after database updates — If you add a permission to the database, permly won't see it until you call sync().

Assuming sync is automatic — It's not. Roles and permissions don't update in real time across multiple app instances.