Data management
Lenra Data system is based on Mongo.
There is two ways to handle data in Lenra:
- in the listeners: you can use the API to manage your data
- in the views: you can use the View component to adapt your application interface
@me
The @me
value is a special string that represents the current user. It can be used for document manipulation and queries.
To use it, you just need to set the @me
value to a field of the document as done in our templates (see the create a template from scratch guide to better understand how to use it).
API
To manage the data in your application, you can utilize our REST API. To make a basic API call within Listeners, you can use the "api" object that is passed as the third parameter. This object provides you with the server URL and your authentication token.
Set the authentication token in the header of your request:
Authorization: Bearer ${api.token}
CRUD
Create a document, give the content in the body
- POST `${api.url}/app/colls/${coll}/docs`
Read a specific document
- GET `${api.url}/app/colls/${coll}/docs/${id}`
Update a document, give the changes in the body
- PUT `${api.url}/app/colls/${coll}/docs/${doc._id}`
Delete a document
- DELETE `${api.url}/app/colls/${coll}/docs/${doc._id}`
Transactions
To handle multiple operations in one request, you can use the Mongo transactions.
You can start a transaction by creating it via the API:
- POST `${api.url}/app/colls/${coll}/transaction`
This will return a transaction token.
Then you can add operations (CRUD and/or advanced Mongo functions) to the transaction by using the transaction token instead of the API token in the header of your request:
Authorization: Bearer ${transactionToken}
When you are done, you can commit the transaction to execute all the operations:
- POST `${api.url}/app/colls/${coll}/transaction/commit`
Or you can abort the transaction to cancel all the operations:
- POST `${api.url}/app/colls/${coll}/transaction/abort`
You also need to use the transaction token in the header of those final requests.
Advanced Mongo functions
find
The MongoDB find function can find many documents in a collection filtered by the query filter.
Run find request, give find params in body like:
- query: the query filter
- projection: the projection map more details here
- POST `${api.url}/app/colls/${coll}/docs/find`
The projection allows you to filter the keys in the return object, by giving a projection map, all keys set to true will be returned, the default values are false, example:
We have in the users collection
{
"_id": "123456",
"name": "John",
"age": 20
}
updateMany
The MongoDB updateMany function udates all documents that match the specified filter for a collection.
- POST `${api.url}/app/colls/${coll}/updateMany`
filter
select all document to update (like find query, see query selectors)update
the modifications to apply see update operators
View
Lenra also allows the use of Mongo find to customize the view's results according to the requested data. Additionally, this behavior automatically updates the interface when a data point corresponding to the view query is changed.
Examples
The next example will call the filteredUsers
view with the user called Thomas
as input data:
{
"type": "view",
"name": "filteredUsers",
"find": {
"coll": "users",
"query": {
"name": "Thomas"
}
}
}
You can get only the names of the users by using this projection:
{
"type": "view",
"name": "userList",
"find": {
"coll": "users",
"projection": {"name": true}
}
}
Limitations
In order to manage realtime refresh of the view we had to implement the Mongo operators in our query-parser project. Since it takes time to implement all the operators (and that we also have lot of work on Lenra) we only have implemented a short list of them. We will implement the rest of them soon, but if you want to help you can look at the list of operator task.
Managed operators
Comparison
Operator | Description |
---|---|
$eq | Matches values that are equal to a specified value. |
$ne | Matches all values that are not equal to a specified value. |
$gt | Matches all values that are not equal to a specified value. |
$gte | Matches values that are greater than or equal to a specified value. |
$lt | Matches values that are less than a specified value. |
$lte | Matches values that are less than or equal to a specified value. |
$in | Matches any of the values specified in an array. |
$nin | Matches none of the values specified in an array. |
Logical
Operator | Description |
---|---|
$and | Joins query clauses with a logical AND returns all documents that match the conditions of both clauses. |
$or | Inverts the effect of a query expression and returns documents that do not match the query expression. |
$not | Joins query clauses with a logical NOR returns all documents that fail to match both clauses. |
$nor | Joins query clauses with a logical OR returns all documents that match the conditions of either clause. |
Element
Operator | Description |
---|---|
$exists | Matches documents that have the specified field. |
$type | Selects documents if a field is of the specified type. |
Array
Operator | Description |
---|---|
$all | Matches arrays that contain all elements specified in the query. |