GadgetRecord records

When working with an instance of a record in Gadget (either results returned from the client package or the context.record in your code effects), you are working with a GadgetRecord. GadgetRecord is a wrapper class around the properties of your object that provides additional helper functionality such as change tracking.

GadgetRecord properties

Every GadgetRecord shares a common set of of Gadget system properties such as id, updatedAt, createdAt, and state. In addition to these Gadget system properties, any model fields you've added to your model will also be included in the GadgetRecord. These properties can be get and set via their apiIdentifier.

TypeScript
1const user =
2 await GadgetClient.user.findOne(
3 "some-id"
4 );
5
6// Gadget system properties
7console.log(user.id); // "1"
8console.log(user.state); // "created"
9console.log(user.createdAt); // Tue Jun 15 2021 10:30:59 GMT-0400
10console.log(user.updatedAt); // Tue Jun 15 2021 12:18:01 GMT-0400
11
12// Model field properties
13console.log(user.name); // "Jane Doe"
14console.log(user.email); // "[email protected]"

Change tracking (dirty tracking)

GadgetRecord instances track changes made to their local state, and provide a change tracking API for determining what properties have changed (locally) on a record.

You can use this change tracking in your own applications via the client package in the following manner:

TypeScript
1const user =
2 await GadgetClient.user.findOne(
3 "some-id"
4 );
5
6user.name = "A new name";
7
8const { changed, current, previous } =
9 user.changes("name");
10
11console.log(changed); // true
12console.log(current); // "A new name";
13console.log(previous); // "The old name";

By working with this single GadgetRecord instance user in your front-end, you could use the change tracking API to determine if there is anything that needs to be persisted via one of your actions.

Additionally, you are also working with GadgetRecord instances when using the context.record in your run effects, conditions, and validations.

If you wanted to determine whether or not a change was made to the record in a run effect of an update action, you could do the following:

TypeScript
1module.exports = async ({
2 api,
3 record,
4 params,
5 logger,
6}) => {
7 if (record.changed("title")) {
8 const { current, previous } =
9 record.changes("title");
10
11 console.log(current); // the new attribute set via params
12 console.log(previous); // the current value in the database
13 }
14};

GadgetRecord API

The GadgetRecord API extends your records with helper functions to make working with records easier.

Get all changes to the record

Get a list of all changes, keyed by field apiIdentifier since this record was instantiated.

TypeScript
1const record = new GadgetRecord<any>({
2 title: "Old title",
3 body: "Old body",
4});
5record.title = "New title";
6record.body = "Old body";
7record.price = 123.45;
8console.log(record.changes());
9//{
10// title: { changed: true, current: "New title", previous: "Old title" },
11// price: { changed: true, current: 123.45, previous: undefined }
12//}

Get changes to one field of the record

Get any changes made to a specific apiIdentifier of the record since this record was instantiated.

TypeScript
1const record = new GadgetRecord<any>({
2 title: "Old title",
3});
4record.title = "New title";
5console.log(record.changes("title"));
6// { changed: true, current: "New title", previous: "Old title" }

Determine whether or not any fields changed on the record

Determine if any changes have been made to any keys on the record.

TypeScript
const record = new GadgetRecord<any>({
title: "Old title",
});
record.title = "New title";
console.log(record.changed()); // true

Determine if one field changed on the record

Determine if a specific apiIdentifier changed on the record.

TypeScript
const record = new GadgetRecord<any>({
title: "Old title",
});
record.title = "New title";
console.log(record.changed("title")); // true

Revert all changes to the record

Resets the record state to the state it was instantiated with. All changes are reverted.

TypeScript
1const record = new GadgetRecord<any>({
2 title: "Old title",
3 body: "Old body",
4});
5record.title = "New title";
6record.body = "New body";
7record.price = 123.45;
8console.log(record.changes());
9//{
10// title: { changed: true, current: "New title", previous: "Old title" },
11// body: { changed: true, current: "New body", previous: "Old body" },
12// price: { changed: true, current: 123.45, previous: undefined }
13//}
14console.log(record.title); // "New title"
15console.log(record.body); // "New body"
16
17record.revertChanges();
18console.log(record.changes()); // {}
19console.log(record.changed()); // false
20
21console.log(record.title); // "Old title"
22console.log(record.body); // "Old body"

Flush changes to the record

Flushes all changes to the record and resets the state of change tracking to the current state of the record.

TypeScript
1const record = new GadgetRecord<any>({
2 title: "Old title",
3 body: "Old body",
4});
5record.title = "New title";
6record.body = "New body";
7record.price = 123.45;
8console.log(record.changes());
9//{
10// title: { changed: true, current: "New title", previous: "Old title" },
11// body: { changed: true, current: "New body", previous: "Old body" },
12// price: { changed: true, current: 123.45, previous: undefined }
13//}
14console.log(record.title); // "New title"
15console.log(record.body); // "New body"
16
17record.flushChanges();
18console.log(record.changes()); // {}
19console.log(record.changed()); // false
20
21console.log(record.title); // "New title"
22console.log(record.body); // "New body"

Get a JSON representation of the record

Return a JSON representation of the keys (by apiIdentifier) and values of your record.

TypeScript
1const record = new GadgetRecord<any>({
2 title: "A title",
3 tags: ["Cool", "New", "Stuff"],
4 price: 123.45,
5});
6console.log(record.toJSON());
7//{
8// "title": "A title",
9// "tags": ["Cool", "New", "Stuff"],
10// "price": 123.45
11//}

Get and set properties that share the same name as GadgetRecord functions

If the apiIdentifier of your model field has the same name as one of the GadgetRecord functions described above, you will need to work with it via the getField and setField functions.

TypeScript
1const record = new GadgetRecord<any>({
2 title: "Something",
3 changed: true,
4});
5
6console.log(record.changed()); // false, change tracking function uses changed()
7console.log(record.getField("changed")); // true
8record.setField("changed", false);
9console.log(record.getField("changed")); // false

ChangeTracking contexts

GadgetRecord change tracking allows you to track changes on the record in different contexts. By default, all functions use the ChangeTracking.SinceLoaded context. Gadget also uses the ChangeTracking.SinceLastPersisted context in order to differentiate between changes that have already been persisted by the Create record and Update record effects, and changes that have been made to the record since the start of action execution.

In order for developers to worry less about the exact order in which their effects are run, the change tracking API assumes the ChangeTracking.SinceLoaded context by default. You may also inquire about changes since ChangeTracking.SinceLastPersisted by adding it as an option to your change tracking function calls.

TypeScript
1import {
2 GadgetRecord,
3 ChangeTracking,
4} from "@gadget-client/model-testing/src/GadgetRecord";
5
6function changedProperties(
7 model: ModelBlob,
8 record: GadgetRecord<BaseRecord>
9) {
10 const changes = record.changes();
11 const attributes = Object.keys(
12 changes
13 ).reduce((attrs, key) => {
14 attrs[key] = record[key];
15 return attrs;
16 }, {} as any);
17 return attributes;
18}
19
20const record = new GadgetRecord<any>({
21 id: "123",
22 title: "Something old",
23});
24
25record.title = "Something new";
26await GadgetClient.record.update(
27 record.id,
28 { record: changedProperties(record) }
29);
30record.flushChanges(
31 ChangeTracking.SinceLastPersisted
32); // this applies the current state of the record to the ChangeTracking.SinceLastPersisted change tracking context
33
34console.log(
35 record.changed(
36 ChangeTracking.SinceLastPersisted
37 )
38); // false since these changes have been flushed
39
40console.log(record.changed()); // true, ChangeTracking.SinceLoaded context hasn't changed
41console.log(
42 record.changed(
43 ChangeTracking.SinceLoaded
44 )
45); // true; equivalent to above
46
47console.log(record.changes()); // { title: { changed: true, current: "Something new", previous: "Something old" } }
48console.log(
49 record.changes(
50 ChangeTracking.SinceLoaded
51 )
52); // same as above
53console.log(
54 record.changes(
55 ChangeTracking.SinceLastPersisted
56 )
57); // {}, because we flushed it above
58
59record.revertChanges(); // reverts changes to the record, in accordance with the ChangeTracking.SinceLoaded context
60record.revertChanges(
61 ChangeTracking.SinceLoaded
62); // equivalent to line above, no effect
63
64console.log(record.title); // "Something old"
65console.log(record.changed()); // false
66console.log(record.changes()); // {}
67
68console.log(
69 record.changed(
70 ChangeTracking.SinceLastPersisted
71 )
72); // true; reverted since we last persisted
73console.log(
74 record.changes(
75 "title",
76 ChangeTracking.SinceLastPersisted
77 )
78); // { changed: true, current: "Something old", previous: "Something new" }

Working with GadgetRecord in code effects

When writing custom code effects, conditions, and model field validations you have access to a shared GadgetRecord in context.

Most commonly, you will be working with the context.record inside custom code effects (run effects, success effects, failure effects). Gadget will instantiate an instance of a GadgetRecord when it starts executing your action. This same instance will be shared between all validations and effects that run during that action.

The typical GadgetRecord lifecycle is as follows:

  1. Instantiate a new GadgetRecord. All fields are undefined, no changes are tracked.
  2. Run the Apply params effect. This effect is the first effect by default in your Create and Update actions. After this effect runs, your context.record will have changes() that correspond to the current state of the record.
  3. Run the Create Record or Update Record effect. These effects will first validate your record and then, if valid, persist the record in your database. They will also clear the changes() state of the record after persisting it.