Objects & Channels
Nimbu.Object is the base class for channel entries and most SDK resources.
Create
js
const article = new Nimbu.Object('articles', {
title: 'New article',
status: 'draft'
});
await article.save();You can also use subclasses when a file works with one channel often:
js
const Article = Nimbu.Object.extend('articles');
const article = new Article({
title: 'New article'
});Read
js
const article = await new Nimbu.Query('articles').get(articleId);
console.log(article.id);
console.log(article.get('title'));To fetch an object you already have an id for:
js
const article = new Nimbu.Object('articles', { id: articleId });
await article.fetch();Update
js
article.set('status', 'published');
article.set({
published_at: new Date(),
reviewed: true
});
await article.save();save() also accepts attributes directly:
js
await article.save({
status: 'archived'
});Delete
js
const article = await new Nimbu.Query('articles').get(articleId);
await article.destroy();Use wait: true when you need the object state to update after the API confirms deletion:
js
await article.destroy({ wait: true });Attributes
Common helpers:
js
article.get('title');
article.has('title');
article.unset('subtitle');
article.increment('views', 1);
article.keys();
article.pick('title', 'status');
article.omit('body');
article.toJSON();createdAt, updatedAt, and shortId are convenience accessors:
js
article.createdAt;
article.updatedAt;
article.shortId;Change Tracking
The SDK tracks local changes before save:
js
article.set('status', 'published');
if (article.hasChanged('status')) {
await article.save();
}Useful helpers:
js
article.changedAttributes();
article.previous('status');
article.previousAttributes();Include Relations On Fetch
js
const article = new Nimbu.Object('articles', { id: articleId });
await article.fetchWithInclude(['author', 'comments.author']);Equivalent:
js
await article.fetch({
include: ['author', 'comments.author']
});Request Options
Most object reads and writes accept these options:
| Option | Use |
|---|---|
useACL | Respect channel/object ACLs for the request. |
forceChannel | Treat reserved names like products as custom channels. |
locale | Save localized content for a specific locale. |
sessionToken | Act on behalf of a customer session. |
cascadeSave | Save unsaved child objects before saving the parent. |
Example:
js
await article.save(
{ title: 'Nederlandse titel' },
{ locale: 'nl', useACL: true }
);ACLs
js
const acl = article.getACL() || new Nimbu.ACL();
acl.setPublicReadAccess(true);
acl.setLoggedInUpdateAccess(false);
article.setACL(acl);
await article.save();