Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
gun.rel Relational Data API Guide
Why this API is needed
GUN’s graph model is naturally suited to distributed collaboration, but common needs like “sort by primary key,” “stable pagination,” and “fast lookup by field” become complex and unpredictable when implemented via raw graph traversal.
gun.relprovides a lightweight relational abstraction that maps “tables, primary keys, indexes, and pagination” onto graph nodes and links. This keeps GUN’s distributed properties while enabling efficient, table-like pagination and queries.Problems solved by gun.rel
Conflicts with other APIs
No conflict.
gun.relstores metadata, indexes, and row nodes under the fixed namespacerel:*and writes through therelchain. It can be used alongside other GUN APIs without interfering with the rest of the graph.Usage cautions
gun.userto prevent third‑party tampering.rel:*metadata/index/sequence structures are validated; invalid writes return errors.rel:*index nodes only accept relationship links or deletions; other writes are rejected.How gun.rel works
On first use, the table schema is written to a fixed namespace so all clients can read primary key and index definitions.
A dedicated sequence node stores the current max id; each insert reads, increments, and writes back to keep the primary key monotonic.
Insert/update operations write both the primary index and secondary indexes, each pointing to row nodes.
Pagination scans index key ranges, not the whole table, preserving order with a fixed page size.
Data structure overview
Using
name = messagesas an example:rel:messagesStores schema info (primary key, index fields, etc.).
rel:messages:seqField
valuestores the current sequence value.rel:messages:row:<id>Stores a single row, including the primary key field.
rel:messages:idx:primaryKeys look like
id:0000000001, values are{'#': '<rowSoul>'}.rel:messages:idx:<field>Keys look like
<field>:<value>:0000000001, values are{'#': '<rowSoul>'}.gun.rel sub‑APIs
gun.rel(name, schema): create a relational chain and table definition (internally callsrelSchema)gun.rel().relSchema(schema): initialize schema metadata only (first call only; for staged or delayed init)gun.rel().insert(row): insert a row and return{ id, soul }gun.rel().upsert(id, row): update a row by primary keygun.rel().delete(id): delete a row by primary keygun.rel().page({ startId, limit, reverse }): page by primary keyBasic usage
Examples with other APIs
Using SEA encryption
Combining with gun.get / gun.put
Combining with native graph data
Real‑time subscriptions
Relational container inside the graph
The following sketch shows a relational container mounted inside the graph. You can think of
graph/app/rel-containeras a container that owns therel:*namespace.