Database

Relationships

Link documents across collections with one-to-one, one-to-many, and many-to-many relationships.

Relationships connect documents across collections. Instead of embedding data or using IDs manually, you define explicit links that Nuvix manages for you.

Relationship types

TypeDescriptionExample
oneToOneOne document links to exactly one otherUser to Profile
oneToManyOne document links to many othersAuthor to Posts
manyToOneMany documents link to onePosts to Author
manyToManyMany documents link to manyPosts to Tags

Creating relationships

Use the relationship attribute to define connections between collections.

Creating attributes is an administrative task. Use the Nuvix Console or REST API.

# Example: One-to-many (Author has many Posts)
curl -X POST https://api.nuvix.in/v1/schemas/my_app/collections/posts/attributes/relationship \
  -H "X-API-Key: <YOUR_API_KEY>" \
  -d '{
    "relatedCollectionId": "authors",
    "type": "manyToOne",
    "key": "author",
    "twoWay": true,
    "twoWayKey": "posts",
    "onDelete": "setNull"
  }'

Parameters

ParameterTypeRequiredDescription
relatedCollectionIdstringYesThe collection to link to
typestringYesRelationship type
keystringYesAttribute name on this collection
twoWaybooleanNoCreate reverse attribute. Default: false
twoWayKeystringNoAttribute name on related collection
onDeletestringYesAction when related document is deleted

On delete actions

ActionDescription
cascadeDelete related documents
setNullSet reference to null
restrictPrevent deletion if references exist

Creating Linked Documents

One-to-one / Many-to-one

Pass the related document's ID string directly.

// Create post linked to an author
const post = await nx.database
    .schema('my_app')
    .collection('posts')
    .create(ID.unique(), {
        title: 'First Post',
        author: 'author-id-123' // Pass ID string
    });

Many-to-many

For array relationships, use the { set: [...] } syntax.

// Create post with multiple tags
const post = await nx.database
    .schema('my_app')
    .collection('posts')
    .create(ID.unique(), {
        title: 'JavaScript Tutorial',
        tags: { 
            set: ['tag-id-1', 'tag-id-2'] 
        }
    });

Querying relationships

Use the .populate() method to include related document data in your response. This works like an SQL JOIN.

// Get post with author details
const post = await nx.database
    .schema('my_app')
    .collection('posts')
    .populate('author')
    .findById('post123');

console.log('Title:', post.title);
console.log('Author Name:', post.author.name);

Nested selection

You can also specify which fields to select from the related document using a callback.

const posts = await nx.database
    .schema('my_app')
    .collection('posts')
    .select('title')
    .populate('author', (author) => author.select('name', 'email'))
    .find();

posts.documents.forEach(post => {
    console.log(`${post.title} by ${post.author.name}`);
});

Filtering by relationship

Query documents based on related document attributes.

// Get posts by a specific author ID
const posts = await nx.database
    .schema('my_app')
    .collection('posts')
    .equal('author', 'author123')
    .find();

Updating relationships

Changing references (Single)

For single relationships, update by setting a new ID directly.

// Change post author
await nx.database
    .schema('my_app')
    .collection('posts')
    .update('post123', {
        author: 'new-author-id'
    });

Removing references (Single)

Set the reference to null.

await nx.database
    .schema('my_app')
    .collection('posts')
    .update('post123', {
        author: null
    });

Many-to-many updates (Array)

For array relationships, use set, connect, or disconnect.

Replace all tags:

await nx.database
    .schema('my_app')
    .collection('posts')
    .update('post123', {
        tags: { set: ['tag1', 'tag2'] }
    });

Add a tag (connect):

await nx.database
    .schema('my_app')
    .collection('posts')
    .update('post123', {
        tags: { connect: ['new-tag-id'] }
    });

Remove a tag (disconnect):

await nx.database
    .schema('my_app')
    .collection('posts')
    .update('post123', {
        tags: { disconnect: ['old-tag-id'] }
    });

Updating relationship attributes

Modify relationship settings after creation using the REST API.

curl -X PATCH https://api.nuvix.in/v1/schemas/my_app/collections/posts/attributes/relationship/author \
  -H "X-API-Key: <YOUR_API_KEY>" \
  -d '{
    "onDelete": "restrict"
  }'

Only onDelete and newKey can be changed after creation.

How is this guide?

Last update: