2. Reading records
To retrieve documents or a document from the database, you can make use of the following functions:
find_all(): This function is used to retrieve all documents from the database.find_by_pk(): This function is used to retrieve a document by its primary key (or ID).find_one(): This function is used to retrieve a single document based on a specific condition.find_many(): This function is used to retrieve multiple documents based on a specific condition.
1. find_all()
This method is used to retrieve all the records that are in the database table. Below are examples demonstrating how to do it:
users = pg_loom.find_all(
instance=User,
select=["id", "username"],
limit=3,
offset=0,
order=[Order(column="id", order="DESC")],
)
print(users) # ? [{'id': 1, 'username': '@miller'}]
The find_all() method takes in the following arguments:
Argument |
Description |
Type |
Default |
Required |
|---|---|---|---|---|
|
The model class to retrieve documents from. |
|
|
|
|
Collection or a string of fields to select from the documents. |
|
|
|
|
Maximum number of documents to retrieve. |
|
|
|
|
Number of documents to skip before retrieving. |
|
|
|
|
Collection of |
|
|
|
|
Collection or a |
|
|
|
|
Collection of |
|
|
|
|
Boolean telling |
|
|
Tip
👍 Pro Tip: A collection can be any python iterable, the supported iterables are list, set, tuple.
2. find_many()
Here is an example demonstrating the usage of the find_many() function with specific filters.
users = mysql_loom.find_many(
User,
filters=[Filter(column="username", value="@miller")],
select=["id", "username"],
offset=0,
limit=10,
)
print(users) # ? [{'id': 1, 'username': '@miller'}]
The find_many() method takes in the following arguments:
Argument |
Description |
Type |
Default |
Required |
|---|---|---|---|---|
|
The model class to retrieve documents from. |
|
|
|
|
Collection or a string of fields to select from the documents. |
|
|
|
|
Maximum number of documents to retrieve. |
|
|
|
|
Number of documents to skip before retrieving. |
|
|
|
|
Collection of |
|
|
|
|
Collection or a |
|
|
|
|
Collection of |
|
|
|
|
Collection of |
|
|
|
|
Boolean telling |
|
|
|
Tip
👍 Pro Tip: The distinction between the find_all() and find_many() methods lies in the fact that find_many() enables you to apply specific filters, whereas find_all() retrieves all the documents within the specified model.
3. find_one()
Here is an example showing you how you can use find_one() locate a single record in the database.
user = mysql_loom.find_one(
User,
filters=[Filter(column="username", value="@miller")],
select=["id", "username"],
)
print(user) # ? {'id': 1, 'username': '@miller'}
This method take the following as arguments
Argument |
Description |
Type |
Default |
Required |
|---|---|---|---|---|
|
The model class to retrieve instances from. |
|
|
|
|
|
|
|
|
|
Collection of |
|
|
|
|
Collection of |
|
|
|
|
Number of instances to skip before retrieving. |
|
|
4. find_by_pk()
Here is an example showing how you can use the find_by_pk() to locate a single record in the database.
user = mysql_loom.find_by_pk(User, pk=userId, select=["id", "username"])
print(user) # ? {'id': 1, 'username': '@miller'}
The method takes the following as arguments:
Argument |
Description |
Type |
Default |
Required |
|---|---|---|---|---|
|
The model class to retrieve instances from. |
|
|
|
|
The primary key value to use for retrieval. |
|
|
|
|
Collection column names to select from the instances. |
|
|
|
|
A Collection of |
|
|
|