Example Description
In this example, we demonstrate how to perform CRUD (create, read, update, and delete) operations using the Data API.
How We Built It
Collection Data
To demonstrate the CRUD operations we created a collection named 'Greetings'. The collection holds the greeting "Hello World!" in a number of different languages. The collection has two fields with the keys `language` and `greeting`.
Page Elements
The HOME page contains:
A repeater for displaying data from the database collection
A small form with two text inputs for creating and updating collection items
Three buttons used to perform the read, update, and delete operations
Code
The HOME page contains code for performing the CRUD operations as well as code for implementing the UI functionality of handling button clicks, displaying retrieved items, selecting items, and the like. There is no need to look at this code to understand the CRUD operations, so we will not analyze it here.
Each of the four CRUD operations is encapsulated in a function.
createGreeting(): This function builds an object from the `language` and `greeting` arguments that are passed to it. We then use this object when calling the `insert` function to create a new 'Greetings' item. Note how the properties of the object we use match up with the field keys of the 'Greetings' collection,
readGreetings(): This function reads all the items in the 'Greetings' collection. We build a query by calling the `query()` function. We add a call to the `ascending()` function to retrieve the items sorted by language. We can also add filtering functions like `eq()`, `startsWith()`, and `contains()` to refine our query. Once the collection is built, we call the `find()` function to run the query.
updateGreeting(): This function builds an object from the `itemId`, `language`, and `greeting` arguments that are passed to it. The object contains an `_id` value, which indicates which item will be updated. We then use this object when calling the `update` function to update the 'Greetings' item.
deleteGreeting(): This function deletes a single 'Greetings' item. We call the `remove()` function with a value from one of the 'Greetings' items' `_id` fields.
Related Examples
Did this help?
|
Thanks for your feedback!
Add to Cart Gallery
Let customers to add products to their cart
INTERMEDIATE
Hide and Show Elements
Hide and show elements in response to user interactions
BEGGINER
Hide and Show Elements
Hide and show elements in response to user interactions
BEGGINER