Saltar al contenido principal

Crear Datos

What happens if you are not programming in javascript?

If you are not programming in javascript, you can use the api directly, see TOTALUM API DOCUMENTATION

If you are programming in javascript, you can use the totalumSdk

Note: If you use totalumSdk inside a totalum plugin, you don't need to authenticate, you can start using totalum sdk functions like this: modules.totalumSdk.crud.createItem(); etc...

Si tienes dudas de como instalar y usar TotalumSdk mira la documentación: Instalación SDK de Totalum, y Uso del SDK

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.createItem(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.createItem(tableElementName, clientToCreate);

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.createItem('order', orderToCreate);

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.createItem('product', productToCreate);

const productId = result.data.data.insertedId; // 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.addManyToManyReferenceItem('product', productId, propertyName, clientIdToAddInProduct);

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