Listing Entities
Retrieving data is a fundamental operation in any database system. With Onyx Database, you can efficiently list entities using simple methods. This tutorial demonstrates how to retrieve a list of entities.
Steps to List Entities (Onyx Cloud Database)
1
Declare the Schema
Use the provided JSON schema to define 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 "attributes": [
11 {
12 "name": "firstName",
13 "type": "String"
14 },
15 {
16 "name": "lastName",
17 "type": "String"
18 }
19 ],
20 "relationships": [],
21 "indexes": []
22 }
23 ],
24 "revisionDescription": "Initial schema for Player entity"
25}
2
List the Entities
Use the
list
method to retrieve all instances of the entity.1const players = await db.from('Player').list();
3
Print the Retrieved Entities
Iterate over the list of entities and perform operations as needed.
1for (const player of players) {
2 console.log(`${player.lastName}, ${player.firstName}`);
3}
Important Notes
- The
list
method retrieves all entities of the specified type if no criteria are provided. - For large datasets, consider using pagination or adding query criteria to limit the results.
Troubleshooting
- No Entities Returned: Ensure that entities of the specified type exist in the database.
- Class Not Found: Verify that the entity class is correctly defined and available in the classpath.
- Performance Issues: Use query criteria or pagination to improve performance when dealing with large datasets.
Next Steps
Now that you know how to list entities, you can explore more advanced querying techniques: