How to use ChangeTracker
Track changes in your data and display them in your frontend in minutes.
InstallationServer
Install the Node SDK (more coming soon)
command line
npm install @absolute/change-tracker-node-sdk# oryarn add @absolute/change-tracker-node-sdk
Copied!Basic trackingServer
Map your data before and after your changes. Send the changes to ChangeTracker.
The following Node code snippets assume
"type": "module"
in your package.json. Replace import
with require
if you are using CommonJS. server.mjs
0import { modelTracker, changeTracker } from '@absolute/change-tracker-node-sdk'1
2// Initialize ChangeTracker3const ct = changeTracker(YOUR_HOSTNAME, READ_SECRET, WRITE_SECRET)4
5// Initial data6let data = { name: 'John' }7
8// Snapshot before changes9const prevMap = modelTracker.mapAll(data).toRow('id')10
11// Your data changes12data.name = 'Jane'13
14// Snapshot after changes15const nextMap = modelTracker.mapAll(data).toRow('id')16
17// Store changes in ChangeTracker18ct.store("table", "you@company.com", "Comment", prevMap, nextMap)
Copied!Result:
Info / Linked tables | Field name | State | Current value |
---|---|---|---|
name | Jane | ||
Ignore attributesServer
Filter out the noise by ignoring irrelevant attributes.
server.mjs
0let data = {1 last_edited_on: '2021-09-19T08:25:33.498Z',2 price: 1233}4
5const prevMap = modelTracker6 .mapAll(data)7 .ignore('last_edited_on')8 .toRow('id')9
10data.price = 12511
12const nextMap = modelTracker13 .mapAll(data)14 .ignore('last_edited_on')15 .toRow('id')16
17ct.store("table", "you@company.com", "Comment", prevMap, nextMap)
Copied!Result:
Info / Linked tables | Field name | State | Current value |
---|---|---|---|
price | 125 | ||
Nested objectsServer
Display 2 or more levels of changes in your data as a flat snapshot.
server.mjs
0let data = {1 price: '200',2 customer: {3 address: '4992 Harper Street'4 }5}6
7const prevMap = modelTracker8 .mapAll(data)9 .map('customer.address', 'address')10 .toRow('id')11 12data.customer.address = "2411 Coolidge Street";13
14const nextMap = modelTracker15 .mapAll(data)16 .map('customer.address', 'address')17 .toRow('id')18
19ct.store("table", "you@company.com", "Comment", prevMap, nextMap)
Copied!Result:
Info / Linked tables | Field name | State | Current value |
---|---|---|---|
2002411 Coolidge Street | |||
Linked tablesServer
More often than not, multiple sets of data change at the same time (think cascades). Here's how to show that.
server.mjs
0let data = {1 id: 'order_213',2 address: '2411 Coolidge Street',3 lines: [4 {5 product_id: 'prod_650',6 quantity: 27 },8 ]9}10
11const toTable = (data) => modelTracker.toTable(12 'lines',13 data.lines.map((l) =>14 modelTracker.mapAll(l).toRow(l.product_id)15 )16)17
18const prevMap = modelTracker19.mapAll(data)20.toRow(data.id, [toTable(data)])21
22data.lines.push({ 23 product_id: 'prod_639',24 quantity: 525})26
27const nextMap = modelTracker28.mapAll(data)29.toRow(data.id, [toTable(data)])30
31ct.store("table", "you@company.com", "Comment", prevMap, nextMap)
Copied!Result:
Info / Linked tables | Field name | State | Current value |
---|---|---|---|
address | 2411 Coolidge Street | ||
quantity | 2 | ||
quantity | 5 | ||
Display changes on the frontendServerClient
Create an endpoint to return your token.
server.mjs
0import { modelTracker, changeTracker } from '@absolute/change-tracker-node-sdk'1import { app } from './init-server'2// Your previously initialized ChangeTracker3const ct = changeTracker(hostname, READ_SECRET, WRITE_SECRET)4
5app.get("/get-token", async (req, res) => {6 const {query: { tableName, entityId }} = req7 const token = await ct.getToken(tableName, entityId);8 res.send(token);9});
Copied!Add ChangeTracker's script to your app, before your app script.
index.html
0<script type="text/javascript" src="https://cdn.changetracker.it/preloader.js?auto=true&global=true"></script>
Copied!Open ChangeTracker in your app to see changes.
app.js
0async function getToken(callback) {1 const response = await fetch(`${api}/get-token`);2 const token = await response.text();3 return callback(token);4}5
6yourButton.addEventListener("click", () =>7 ChangeTracker.open({8 getToken,9 serviceUrl: `https://${hostName}.hosts.changetracker.it`,10 })11);
Copied!