Adding & Deleting Entities (Records)

Intro #

TrackZero uses what is known as Upsert (Update or Insert). Creating an Entity or updating it uses the same operation. This allows undeterministic creation of Entities (Or updating), which means you don’t have to remember the state of an Entity in order to determine whether you need to update or create.

Upsert an Entity #

					import { Entity } from "@leiratech/trackzero-js";
let analyticsSpaceId = "space1";

// Prepare your entity (Order object), if we already have an entity of Type "Order" and Id 1, this will update the information stored in TrackZero with the ones we set here.
let order = new Entity("Orders", 1)
    // Adding attributes (Properties) to send to TrackZero and make it reportable.
    .addAttribute("Total Amount", 99.0)
    .addAttribute("Time", new Date(Date.UTC(1990, 11, 23)))
    // Adding Reference Attributes that will link to other entities.
    // Since the Entity of Type "Product" and Id 758 doesn't exist in our project, it will be created automatically. We can add more attributes to it later.
    .addEntityReferencedAttribute("Items", "Products", 758)
    // Here we are adding an additinoal referenced entity under the same attribute name (Items). So now we have 2 referenced products under the items attribute of the order.
    .addEntityReferencedAttribute("Items", "Products", 214)
    .addEntityReferencedAttribute("Customer", "Users", 9657);
    // To link this order to a country, you can simply pass the lat and long of the order and TrackZero will automatically manage this data appropriately to allow Map Charts.
    .addAutomaticallyTranslatedGeoPoint(41.037086118695825, 28.98489855136287);
// Send the Entity to TrackZero
await instance.upsertEntityAsync(order, analyticsSpaceId)


Delete an Entity #

					await instance.deleteEntityAsync("Orders", 1, analyticsSpaceId);