Update Tutorial

This tutorial demonstrates how easy it is to bulk update entities that meet a given criteria. We will update all quarterbacks that passed for less than 1500 yards and set them to active = false.

Steps to Perform an Update (Onyx Cloud Database)

1

Declare the Schema

Use the provided JSON schema to declare your entity in the Onyx Cloud Database.
1{
2  "tables": [
3    {
4      "name": "Player",
5      "identifier": {
6        "name": "id",
7        "type": "Long",
8        "generator": "Sequence"
9      },
10      "partition": "",
11      "attributes": [
12        {
13          "name": "id",
14          "type": "Long",
15          "isNullable": false
16        },
17        {
18          "name": "firstName",
19          "type": "String",
20          "isNullable": false
21        },
22        {
23          "name": "lastName",
24          "type": "String",
25          "isNullable": false
26        },
27        {
28          "name": "position",
29          "type": "String",
30          "isNullable": false
31        },
32        {
33          "name": "active",
34          "type": "Boolean",
35          "isNullable": false
36        },
37        {
38          "name": "passingYards",
39          "type": "Int",
40          "isNullable": false
41        }
42      ],
43      "relationships": [],
44      "indexes": []
45    }
46  ],
47  "revisionDescription": "Schema for Player entity"
48}
2

Create a Query and QueryCriteria

Create a query to find all quarterbacks with passing yards less than 1500.
1// Create a query to find all QBs with passingYards < 1500
2const query = db.from("Player")
3    .where(eq("position", "QB"))
4    .and(lt("passingYards", 1500));
3

Execute the Query and Print Results

Execute the query and print out the results.
1// Execute the query and print out the results
2const players = await query.list();
3players.forEach(qb => {
4    console.log(`${qb.firstName} ${qb.lastName}: active=${qb.active}`);
5});
The output looks like:
Matt Cassel: active=true
4

Create an AttributeUpdate

Create an AttributeUpdate to set the <code>active</code> attribute to <code>false</code>.
1// Create an AttributeUpdate to set active to false
2const updateActiveToFalse = { active: false };
5

Execute the Update

Invoke the update method on the query to perform the bulk update.
1// Execute the update
2await query.setUpdates(updateActiveToFalse).update();
3
4// Or To put it all together
5await db.from("Player")
6    .where(eq("position", "QB"))
7    .and(lt("passingYards", 1500))
8    .set({ active: false })
9    .update();
6

Re-execute the Query to Verify Updates

Execute the query again to verify that the entities were updated.
1// Re-execute the query to verify the update
2const qbs = await query.list();
3qbs.forEach(qb => {
4    console.log(`${qb.firstName} ${qb.lastName}: active=${qb.active}`);
5});
The output now looks like:
Matt Cassel: active=false

Important Notes

  • The update operation affects all entities that match the query criteria.
  • Be cautious when performing bulk updates to avoid unintended data modifications.
  • Always test your queries and updates in a development environment before executing them in production.

Troubleshooting

  • No Entities Updated: Ensure that your query criteria correctly identifies the entities you intend to update.
  • Invalid AttributeUpdate: Verify that the attribute names and values in your updates are correct and match the entity's attributes.
  • Query Execution Errors: Check for syntax errors or invalid criteria in your query.

Next Steps

Now that you have learned how to perform bulk updates, you can explore more advanced querying techniques: