added docs for TinyBuilder::BuildsQueries

This commit is contained in:
silverqx
2022-07-29 08:42:45 +02:00
parent 1ed1b11d4e
commit a3d06fdd6f

View File

@@ -17,6 +17,7 @@ keywords: [c++ orm, orm, getting started, tinyorm]
- [Default Attribute Values](#default-attribute-values)
- [Retrieving Models](#retrieving-models)
- [Containers](#containers)
- [Chunking Results](#chunking-results)
- [Advanced Subqueries](#advanced-subqueries)
- [Retrieving Single Models / Aggregates](#retrieving-single-models)
- [Retrieving Or Creating Models](#retrieving-or-creating-models)
@@ -352,6 +353,34 @@ As we have seen, TinyORM methods like `all` and `get` retrieve multiple records
An `all` method is defined on the `Orm::Tiny::Model<Derived, AllRelations...>` class and `get` method is defined on the `Orm::Tiny::Builder`, may be also referred as `TinyBuilder`, and on the `Orm::Query::Builder` alias `QueryBuilder`.
:::
### Chunking Results
Your application may run out of memory if you attempt to load tens of thousands of TinyORM records via the `all` or `get` methods. Instead of using these methods, the `chunk` method may be used to process large numbers of models more efficiently.
The `chunk` method will retrieve a subset of TinyORM models, passing them to a lambda expression for processing. Since only the current chunk of TinyORM models is retrieved at a time, the `chunk` method will provide significantly reduced memory usage when working with a large number of models:
Flight::chunk(200, [](QVector<Flight> &&flights, const int /*unused*/)
{
for (auto &&flight : flights) {
//
}
return true;
});
The first argument passed to the `chunk` method is the number of records you wish to receive per "chunk". The lambda expression passed as the second argument will be invoked for each chunk that is retrieved from the database. A database query will be executed to retrieve each chunk of records passed to the lambda expression.
If you are filtering the results of the `chunk` method based on a column that you will also be updating while iterating over the results, you should use the `chunkById` method. Using the `chunk` method in these scenarios could lead to unexpected and inconsistent results. Internally, the `chunkById` method will always retrieve models with an `id` column greater than the last model in the previous chunk:
Flight::whereEq("departed", true)
->chunkById(200, [](QVector<Flight> &&flights, const int /*unused*/)
{
for (auto &&flight : flights)
flight.update({{"departed", false}});
return true;
});
### Advanced Subqueries
#### Subquery Selects