|
| 1 | +# SpiceDB Schema Language Guide |
| 2 | + |
| 3 | +## Identifier Naming Conventions |
| 4 | + |
| 5 | +### Private/Internal Identifiers |
| 6 | + |
| 7 | +SpiceDB supports using underscore (`_`) as a prefix for identifiers to establish a convention for marking definitions as "private" or "internal". This is particularly useful for: |
| 8 | + |
| 9 | +- **Synthetic permissions**: Permissions that exist only to compose other permissions |
| 10 | +- **Internal relations**: Relations not meant to be directly referenced by application code |
| 11 | +- **Implementation details**: Parts of your schema that may change without affecting the public API |
| 12 | + |
| 13 | +### Examples |
| 14 | + |
| 15 | +```zed |
| 16 | +definition document { |
| 17 | + // Public relation - exposed to application code |
| 18 | + relation viewer: user |
| 19 | + |
| 20 | + // Private relation - used internally for permission composition |
| 21 | + relation _internal_viewer: user |
| 22 | + |
| 23 | + // Public permission using private components |
| 24 | + permission view = viewer + _internal_viewer |
| 25 | +} |
| 26 | +
|
| 27 | +definition _internal_resource { |
| 28 | + // This entire resource type is marked as internal |
| 29 | + relation owner: user |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +### Best Practices |
| 34 | + |
| 35 | +1. **Use underscore prefix for synthetic permissions**: |
| 36 | + ```zed |
| 37 | + definition resource { |
| 38 | + relation owner: user |
| 39 | + relation editor: user |
| 40 | + |
| 41 | + // Private synthetic permission |
| 42 | + permission _can_write = owner + editor |
| 43 | + |
| 44 | + // Public permissions built on private ones |
| 45 | + permission edit = _can_write |
| 46 | + permission delete = owner |
| 47 | + } |
| 48 | + ``` |
| 49 | + |
| 50 | +2. **Mark implementation details as private**: |
| 51 | + ```zed |
| 52 | + definition folder { |
| 53 | + relation parent: folder |
| 54 | + relation viewer: user |
| 55 | + |
| 56 | + // Private permission for traversal logic |
| 57 | + permission _parent_view = parent->view |
| 58 | + |
| 59 | + // Public permission combining direct and inherited access |
| 60 | + permission view = viewer + _parent_view |
| 61 | + } |
| 62 | + ``` |
| 63 | + |
| 64 | +3. **Future module system compatibility**: When SpiceDB introduces a module system for composing schemas via libraries, underscore-prefixed identifiers will help clearly distinguish between public and private APIs. |
| 65 | + |
| 66 | +### Technical Details |
| 67 | + |
| 68 | +- Identifiers can begin with either a letter (`a-z`) or underscore (`_`) |
| 69 | +- After the first character, identifiers can contain letters, numbers, and underscores |
| 70 | +- Identifiers must end with an alphanumeric character (not an underscore) |
| 71 | +- Valid: `_private`, `_internal_relation`, `_helper123` |
| 72 | +- Invalid: `_trailing_` (must end with alphanumeric), `123_start` (must start with letter or underscore) |
| 73 | +- Note: `__double` is technically valid by the regex pattern but discouraged for readability |
| 74 | + |
| 75 | +This naming convention is enforced at the schema level and is compatible with all SpiceDB operations and queries. |
0 commit comments