Skip to main content

Create Data


📚 Setup Required: For installation and usage of the Totalum SDK or API, see the Installation Guide.


create item

Use Case:

Create an for from your element table.

// create item from your_element_table_name, you need to pass at least the required properties
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
const result = await totalumClient.crud.createRecord(tableElementName, {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 create a new client with name John Doe, the email [email protected], the phone +34 123 456 789, and the birthday 1990-01-01.


const tableElementName = 'client';

const clientToCreate = {
name: 'John Doe',
email: '[email protected]',
phone: '+34 123 456 789',
birthday: new Date('1990-01-01')
};

const result = await totalumClient.crud.createRecord(tableElementName, clientToCreate);

// The response contains the full created record
const createdClient = result.data;
const insertedId = createdClient._id; // this is the id of the client you just created
console.log('Created client:', createdClient);

Note: If you want to create 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.createRecord('client', {optionsField: 'option1, option2, option3'});

create item and add reference to another item (One to Many reference)

Use Case:

THIS IS ONLY FOR ONE TO MANY OR MANY TO ONE REFERENCES, IF YOU WANT TO CREATE 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);

// The response contains the full created record
const createdOrder = result.data;
const insertedId = createdOrder._id; // this is the id of the order you just created

create item and add reference to another item (Many to Many reference)

Use Case:

THIS IS ONLY FOR MANY TO MANY REFERENCES, IF YOU WANT TO CREATE 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 orders, and one order can have many clients).

client table properties:

  • name (text)
  • email (text)
  • phone (text)
  • birthday (date)

product table properties:

  • summary (text)
  • date (date)
  • import (number)

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 client and link it to a product you can do it like this:


const productToCreate = {
summary: 'my product summary',
date: new Date(),
import: 1000
};

const result = await totalumClient.crud.createRecord('product', productToCreate);

// The response contains the full created record
const createdProduct = result.data;
const productId = createdProduct._id; // this is the id of the product you just created

const clientIdToAddInProduct = '5f9b2b1b9c6f6b0001a3b2b1'; // this is just an example

const propertyName = 'client'; // this is the name of the property in the product table that is a reference to the client table

const result2 = await totalumClient.crud.addManyToManyReferenceRecord('product', productId, propertyName, clientIdToAddInProduct);

If you want to see more many to many relation examples, see the edit data section.