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 #

				
					var 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.
Entity order = new Entity("Orders", 1)
    // Adding attributes (Properties) to send to TrackZero and make it reportable.
    .AddAttribute("Quantity", 99.0)
    .AddAttribute("Total Amount", 99.0)
    .AddAttribute("Time", DateTimeOffset.UtcNow)
    // 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("Ship To", "Addresses", 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 trackZeroClient.UpsertEntityAsync(order, analyticsSpaceId)
    // If the context/scheduler do not matter to your application flow, use ConfigureAwait(false) as it aids performance.
    // For more details on ConfigureAwait, check this great blog post https://devblogs.microsoft.com/dotnet/configureawait-faq/
    .ConfigureAwait(false);
				
			

Delete an Entity #

				
					await trackZeroClient.DeleteEntityAsync(new EntityReference("Orders", 1), analyticsSpaceId).ConfigureAwait(false);