Item Operations
An item represents the data corresponding to each row in the datastore. You can save data retrieved through the datastore or create new data with it.
Retrieve items
Retrieves all items in the datastore.
const items = await datastore.items();
Retrieve a specific item
To retrieve a specific item, specify the item ID.
const item = await datastore.item('ITEM_ID');
Create a new item
To create a new item, execute item without any arguments.
const newItem = await datastore.item();
Set values for fields
Next, set values for the fields in this newItem. The following code assumes that the message field has already been prepared.
newItem.set('message', 'Hello, Hexabase!');
Since the return value is itself, the set method can be chained.
newItem
.set('message', 'Hello, Hexabase!')
.set('name', 'Hexabase');
Associating with another item
To automatically link to another item, execute the link method.
newItem.link(anotherItem);
The link method also returns itself, so you can chain methods together.
newItem
.link(anotherItem)
.link(yetAnotherItem);
Remove item association
To remove the association of an item, execute the unlink method.
newItem.unlink(anotherItem);
The unlink method also returns itself, so you can chain methods together.
newItem
.unlink(anotherItem)
.unlink(yetAnotherItem);
Upload a file
To associate a file, first use the file method to retrieve a FileObject.
const file = item.file();
Then, set the Blob and filename to this file.
const blob = new Blob(['Hello world'], { type: 'text/plain' });
file
.set('name', 'test_file.txt')
.set('data', blob);
After that, set it to the field with the file type and save it.
await item
.set('test_file', [file])
.save();
Save an item
To save an item, use the save method.
await newItem.save();
Update items
Updating an item also executes the save method.
newItem.set('message', 'Hello, Hexabase again!');
await newItem.save();
Retrieve the current status
The current status can obtained with the status method.
const status = await newItem.status();
Update status
When updating the status, use the execute method. This operation only changes the status and does not update any data.
await newItem.execute('nextStatus');
You can retrieve the available status actions that the current item can execute using the statusActions method.
const statusActions = await newItem.statusActions();
Delete an item
To delete an item, execute the delete method.
await newItem.delete();
Recieve real-time notifications
To receive real-time notifications for item changes, execute the subscribe method. The first argument specifies the event, and currently, only update is supported.
item.subscribe('update', (item) => {
console.log(item);
});
Create and save comments
Real-time notifications can be received when creating comments. In the following code, the subscribe method of the item is called.
const comment = await item.comment();
commnet.set('comment', 'Hello, Hexabase!');
await comment.save();