Edit Data
📚 Setup Required: For installation and usage of the Totalum SDK or API, see the Installation Guide.
edit item by id
Use Case:
Edit an item by id from your element table.
// edit item by id from your_element_table_name, you can edit 1 or multiple properties at the same time (like a patch)
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
let your_item_id = 'your_item_id'; // replace 'your_item_id' with the id of the item object
const result = await totalumClient.crud.editRecordById(tableElementName, your_item_id, {your_item_property: 'new value'});
Example:
Imagine you have a table called client with the following properties:
name(text)email(text)phone(text)birthday(date)
And you want to edit the client with id 5f9b2b1b9c6f6b0001a3b2b1 and change the name to John Doe, the phone to +34 123 456 789, and the birthday to 1990-01-01.
const tableElementName = 'client';
let your_item_id = '5f9b2b1b9c6f6b0001a3b2b1';
const result = await totalumClient.crud.editRecordById(tableElementName, your_item_id, {name: 'John Doe', phone: '+34 123 456 789', birthday: new Date('1990-01-01')});
// The response contains the full updated record
const updatedClient = result.data;
console.log('Updated client:', updatedClient);
Note: If you want to edit a field that is a type options, and select multiple options is enabled, you must put the options separated by commas, like this:
const result = await totalumClient.crud.editRecordById('client', your_item_id, {optionsField: 'option1, option2, option3'});
Add or edit an item reference to another item (add or edit reference) (One to Many reference)
Use Case:
THIS IS ONLY FOR ONE TO MANY OR MANY TO ONE REFERENCES, IF YOU WANT TO ADD OR EDIT A MANY TO MANY REFERENCE, SEE THE NEXT SECTION.
Imagine you have a table called client and other called order that are referenced in a one to many relationship, (one client can have many orders).
client table properties:
name(text)email(text)phone(text)birthday(date)
order table properties:
summary(text)date(date)import(number)client(reference to client table)
As is a one to many relationship, this tables are linked by a reference in the order table. So you see a property called client in the order table that is a reference to the client table.
So, If you want to create a new order and link it to a client you can do it like this:
let clientIdToAddInOrder = '5f9b2b1b9c6f6b0001a3b2b1';
const orderToCreate = {
summary: 'my order summary',
date: new Date(),
import: 1000,
client: clientIdToAddInOrder // this is the reference to the client
};
const result = await totalumClient.crud.createRecord('order', orderToCreate);
If you want to take an existing order and link it to a client you can do it like this:
let clientIdToAddInOrder = '5f9b2b1b9c6f6b0001a3b2b1';
let orderId = '5f9b2b1b9c6f6b0001a3b2b1';
const result = await totalumClient.crud.editRecordById('order', orderId, {client: clientIdToAddInOrder});
If you want to remove the reference to a client from an order you can do it like this:
let orderId = '5f9b2b1b9c6f6b0001a3b2b1';
const result = await totalumClient.crud.editRecordById('order', orderId, {client: null});
// now this order has no client linked
Add or edit an item to another item (add or edit reference) (Many to Many reference)
Use Case:
THIS IS ONLY FOR MANY TO MANY REFERENCES, IF YOU WANT TO ADD OR EDIT A ONE TO MANY OR MANY TO ONE REFERENCE, SEE THE PREVIOUS SECTION.
Imagine you have a table called client and other called product that are referenced in a many to many relationship, (one client can have many products, and one product can have many clients).
client table properties:
name(text)email(text)phone(text)birthday(date)
product table properties:
name(text)price(number)category(options)
As is a many to many relationship, this tables are linked by a a third join table. So you don't see id reference in the client or product tables.
So, if you want to create a new product and link it to a client you can do it like this:
const productToCreate = {
name: 'my product name',
price: 1000,
category: 'my category'
};
const result = await totalumClient.crud.createRecord('product', productToCreate);
// The response contains the full created record
const createdProduct = result.data;
const productId = createdProduct._id;
const clientId = '5f9b2b1b9c6f6b0001a3b2b1';
const propertyName = 'products'; // this is the name of the property in the client table that references the product table
const result = await totalumClient.crud.addManyToManyReferenceRecord('client', clientId, propertyName, productId);
If you want to take an existing product and link it to a client you can do it like this:
const clientId = '5f9b2b1b9c6f6b0001a3b2b1';
const productId = '5f9b2b1b9c6f6b0001a3b2b1';
const propertyName = 'products'; // this is the name of the property in the client table that references the product table
const result = await totalumClient.crud.addManyToManyReferenceRecord('client', clientId, propertyName, productId);
If you want to remove the reference to a product from a client you can do it like this:
const clientId = '5f9b2b1b9c6f6b0001a3b2b1';
const productId = '5f9b2b1b9c6f6b0001a3b2b1';
const propertyName = 'products'; // this is the name of the property in the client table that references the product table
const result = await totalumClient.crud.dropManyToManyReferenceRecord('client', clientId, propertyName, productId);