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 Gadget system properties such as id, updatedAt, and createdAt. 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.
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:
By working with this single GadgetRecord instance user in your frontend, 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 actions 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:
JavaScript
1exportconst run:ActionRun=async({ params, record, logger, api })=>{
Return a JSON representation of the keys (by apiIdentifier) and values of your record.
JavaScript
1const record =newGadgetRecord({
2 title:"A title",
3 tags:["Cool","New","Stuff"],
4 price:123.45,
5});
6// {
7// "title": "A title",
8// "tags": ["Cool", "New", "Stuff"],
9// "price": 123.45
10// }
11console.log(record.toJSON());
1const record =newGadgetRecord({
2title:"A title",
3tags:["Cool","New","Stuff"],
4price:123.45,
5});
6// {
7// "title": "A title",
8// "tags": ["Cool", "New", "Stuff"],
9// "price": 123.45
10// }
11console.log(record.toJSON());
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.
JavaScript
1const record =newGadgetRecord({ title:"Something", changed:true});
2
3// false, change tracking function uses changed()
4console.log(record.changed());
5// true
6console.log(record.getField("changed"));
7record.setField("changed",false);
8// false
9console.log(record.getField("changed"));
1const record =newGadgetRecord({title:"Something",changed:true});
2
3// false, change tracking function uses changed()
4console.log(record.changed());
5// true
6console.log(record.getField("changed"));
7record.setField("changed",false);
8// false
9console.log(record.getField("changed"));
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 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.
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.
Each GadgetRecord also has a touch function that can be used to mark the record as changed.
When the record is saved, it's updatedAt field will be updated, even if nothing else about the record has changed.
Example of using record.touch()
JavaScript
1exportconst run:ActionRun=async({ api })=>{
2// get user record from API
3let record =await api.user.findFirst();
4
5// mark the record as changed
6 record.touch();
7
8// save the record, which will change it's `updatedAt`
9awaitsave(record);
10};
1exportconstrun:ActionRun=async({ api })=>{
2// get user record from API
3let record =await api.user.findFirst();
4
5// mark the record as changed
6 record.touch();
7
8// save the record, which will change it's `updatedAt`
9awaitsave(record);
10};
This is useful when you are using realtime queries and want to trigger a re-fetch of the record without changing any of the other fields.
Working with GadgetRecord in actions
When writing actions and model field validations you have access to a shared GadgetRecord in context.
Most commonly, you will be working with the context.record inside actions. Gadget will instantiate an instance of a GadgetRecord when it starts executing your action. This same instance will be shared between all validations and actions that run during that action.
The typical GadgetRecord lifecycle is as follows:
Instantiate a new GadgetRecord. All fields are undefined, no changes are tracked.
Call the applyParams function. This function is added to your create and update actions by default. After this function runs, your context.record will have changes() that correspond to the current state of the record.
Run the save() function. save() will validate your record and then, if valid, persist the record in your database. It will also clear the changes() state of the record after persisting it.
The GadgetRecord type
Every model will have a unique Record type that can be imported from your @gadget-client package.
Using a model's GadgetRecord type
TypeScript
1// import types from your client (requires a user model)
You can also specify what fields on that model are to be included in the record typing using the notation ModelARecord<{fieldA: true; fieldB: true}>. This is useful when querying for model records while using the select option to specify fields and allows you to enforce the selection of certain fields.
For example, you could have a function in a global action that converts a firstName field on a user model to lowercase:
Specify fields on a model GadgetRecord type
TypeScript
1// import types from your client (requires a user model)
10// fetch a list of records using findMany (or the useFindMany hook)
11const users =await api.user.findMany({
12 first:5,
13});
14
15const areUsersVerified =checkIfVerified(users);
16return areUsersVerified;
17};
You can also specify what fields on that model are to be included in the record typing using the notation GadgetRecordList<ModelARecord<{fieldA: true; fieldB: true}>>. This is useful when querying for model records while using the select option to specify fields and allows you to enforce the selection of certain fields.
For example, you can only enforce the selection of the emailVerified field in your typing for your user records:
Specifying fields with GadgetRecordList
TypeScript
1// import types from your client (requires a user model)
12// fetch a list of records using findMany (or the useFindMany hook)
13const users =await api.user.findMany({
14 select:{
15 emailVerified:true,
16},
17});
18
19const areUsersVerified =checkIfVerified(users);
20return areUsersVerified;
21};
The generic GadgetRecord type
You can import a generic GadgetRecord type into your typescript files by importing it from your API client.
This GadgetRecord type accepts a Shape type describing the field typing for the record used in, for example, the function. Shape requires the name of the field and the type to be specified manually.
For example, GadgetRecord<{ firstName: string | null; }> could be used to type a record with a string field called firstName: