Lenra Docs
Register on Lenra
  • Home
  • Getting started
    Open/Close
  • Guides
    Open/Close
  • Features
    Open/Close
  • References
    Open/Close
  • Contribute

    Data management

    Lenra Data system is based on Mongo.

    There is two ways to handle data in Lenra:

    @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:

    - 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`
    

    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

    OperatorDescription
    $eqMatches values that are equal to a specified value.
    $neMatches all values that are not equal to a specified value.
    $gtMatches all values that are not equal to a specified value.
    $gteMatches values that are greater than or equal to a specified value.
    $ltMatches values that are less than a specified value.
    $lteMatches values that are less than or equal to a specified value.
    $inMatches any of the values specified in an array.
    $ninMatches none of the values specified in an array.

    Logical

    OperatorDescription
    $andJoins query clauses with a logical AND returns all documents that match the conditions of both clauses.
    $orInverts the effect of a query expression and returns documents that do not match the query expression.
    $notJoins query clauses with a logical NOR returns all documents that fail to match both clauses.
    $norJoins query clauses with a logical OR returns all documents that match the conditions of either clause.

    Element

    OperatorDescription
    $existsMatches documents that have the specified field.
    $typeSelects documents if a field is of the specified type.

    Array

    OperatorDescription
    $allMatches arrays that contain all elements specified in the query.