Crate ciboulette2pg[][src]

Expand description

Introduction

Ciboulette2Pg is an library that execute Ciboulette requests as Postgres queries and convert back the result to Ciboulette responses.

It strive to always execute Ciboulette requests as a single Postgres query reducing latency.

It support sparse fields, sorting and including related objects.

High level operations

Query structure

When building a query, this library will make heavy use of CTEs. It allows the query to reference itself, having multiple sub-queries that perform different tasks.

All the query return data in the same way.

KeyDescription
idThe identifier of the resource, in TEXT format.
typeThe type of the resource, in TEXT format.
If this row handles a relationship, this will be the relationship chain (i.e. peoples.articles.comments)
dataOptional, JSON packed data object containing the attributes of the resource
related_idThe id of the resource it relates to
related_typeThe type of the resource it relates to. If it relates to another relationship of the main data, it should contains the relationship chain

The first CTE will be the one applying the action of the request. (i.e. For a create request, the first CTE will perfom the INSERT). Depending on the request type, a second CTE will be used to select the data modified by the first CTE. After that, all the required (either included or required for sorting) relationships will be used. Finally, depending on the sorting requirement, a final main CTE will be inserted to sort the main data.

Query Response

At the end of the query, all the necessary will be UNIONed together to form the response.

All the CTE have all the same commons keys as describe below, some have additional columns for sorting or linking but these won’t be included in the response.

One can think of the response like the following schema in which different CTE contributing to the final reponse beeing built.

                                                   ┌────────┐
                                                   │ Origin │
                                                   │ Table  │
                     ┌───┬─────────────────┬───────┴──────┬─┴─────────┬────────────┐
       Columns   ──► │ id│type             │     data     │related_id │related_type│
                     ├───┼─────────────────┼─────┬────────┴────┬──────┼────────────┤
                     │   │                 │     │cte_peoples_0│      │            │
     Action CTE  ──► │   │                 │     └────────┬────┘      │            │
                     │ 01│peoples          │   {<json>}   │   NULL    │ NULL       │
                     ├───┼─────────────────┼─────┬────────┴─────┬─────┼────────────┤
                     │   │                 │     │cte_articles_1│     │            │
                     │   │                 │     └────────┬─────┘     │            │
    Relationship ──► │ 42│articles         │   {<json>}   │    01     │ peoples    │
     "articles"      │ 43│articles         │   {<json>}   │    01     │ peoples    │
                     │ 44│articles         │   {<json>}   │    01     │ peoples    │
                     ├───┼─────────────────┼──┬───────────┴────────┬──┼────────────┤
                     │   │                 │  │cte_favorite_color_2│  │            │
    Relationship ──► │   │                 │  └───────────┬────────┘  │            │
  "favorite_color"   │ 61│favorite_color   │   {<json>}   │    01     │ peoples    │
                     ├───┼─────────────────┼──┬───────────┴───────────┼────────────┤
                     │   │                 │  │cte_articles_comments_3│            │
    Relationship ──► │   │                 │  └───────────┬───────────┤            │
"articles.comments"  │ 37│articles.comments│   {<json>}   │    42     │ articles   │
                     │ 30│articles.comments│   {<json>}   │    42     │ articles   │
                     │ 31│articles.comments│   {<json>}   │    43     │ articles   │
                     │ 32│articles.comments│   {<json>}   │    43     │ articles   │
                     │ 33│articles.comments│   {<json>}   │    44     │ articles   │
                     │ 35│articles.comments│   {<json>}   │    44     │ articles   │
                     └───┴─────────────────┴──────────────┴───────────┴────────────┘

The Postgres response can be deserialized with Ciboulette2PgRow.

To then build the ciboulette response, one can convert the Ciboulette2PgRow to Ciboulette response element.

Structs

Ciboulette2PgArguments

A list of parameters to send along side the query to database

Ciboulette2PgBuilder

Ciboulette to Postgres Query builder

Ciboulette2PgRow

Row returned by a query

Ciboulette2PgSafeIdent

An identifier that is safe to be wrapped in quote

Ciboulette2PgTable

A Postgres table

Ciboulette2PgTableStore

Store of the available tables

Enums

Ciboulette2PgError

An error throwable by this library

Ciboulette2PgId

Type of table id

Ciboulette2PgSafeIdentSelector

An selector for Ciboulette2PgSafeIdent

Ciboulette2PgValue

An SQLx compatible value that’ll contains the parameters of the queries