CubicWeb Monthly news April-June 2024
April to June activity report
New response format for cubicweb-api
/rql route
Version 0.15.0 of cubicweb-api
changes the way result sets are serialized.
A result set was represented as an array of array of cells. We decided to wrap
this data into an object to be able to add metadata to each result set.
This new structure already allows us to store column names :
Before:
[
[123, "Alice's Adventures in Wonderland", "Lewis Carroll"],
[124, "The Jungle Book", "Rudyard Kipling"],
[125, "The Wizard of Oz", "L. Frank Baum"],
[126, "Peter Pan", "J.M. Barrie"],
[127, "The Secret Garden", "Frances Hodgson Burnett"]
]
Now:
{
"column_names": ["X", "TITLE", "AUTHOR"],
"rows": [
[123, "Alice's Adventures in Wonderland", "Lewis Carroll"],
[124, "The Jungle Book", "Rudyard Kipling"],
[125, "The Wizard of Oz", "L. Frank Baum"],
[126, "Peter Pan", "J.M. Barrie"],
[127, "The Secret Garden", "Frances Hodgson Burnett"]
],
}
New ResultSet class in @cubicweb/client
We made some changes to ResultSet manipulation in typescript.
Accessing column names
As seen above, the API now returns column names based on selection part of the
RQL. These are made available through the ResultSet.columnNames
property:
const rql = "Any X, LOGIN WHERE X is CWUser, X login LOGIN"
const resultSet = await client.execute(rql);
console.log(resultSet.columnNames) // Displays ["X", "LOGIN"]
Indexing
You can access a cell either with ResultSet.getCell(row, column)
or with
ResultSet.getRow(row).getCell(column)
where row
is an integer and column
is either an integer or the name of a column.
console.log(resultSet.getCell(0, "X")) // Displays 123
Iterating over a ResultSet
ResultSet is now an iterable class, which allows you to iterate over its rows or map them:
// resultSet can be iterated over
for (const [eid, login] of resultSet) {
// ...
}
// using Array.from allows to map rows of the ResultSet
const persons = Array.from(resultSet, ([eid, login]) => ({
eid,
login
})
ES modules distribution of @cubicweb/client
@cubicweb/client
is now distributed as an ESM only package. So long CommonJS !
Introducing a new @cubicweb/rql-generator
package (experimental)
We initially designed a @cubicweb/transaction-builder
JS package whose aim
was to help creating complex RQL queries and chaining them in transactions. We
soon discovered that the level of abstraction of such a library might be a
little too high and have finally decided to work on a new helpers library.
The @cubicweb/rql-generator
will provide utilities to help creating common
RQL queries with basic sorting, filtering and pagination features.
import {generateSelectEntitiesRQL} from "@cubicweb/rql-generator";
const queries = generateSelectEntitiesRQL(schema, "Blog", {where: {eid: [1, 2, 3]}})
// equivalent of `Any X, TITLE, DESCRIPTION WHERE X eid in (1, 2, 3), X title TITLE, X description DESCRIPTION`
More
This blog post does not cover all of what is happening in Cubicweb. It only lists the most impactful changes and releases. If you want to learn more, check out the development board or chat with us on Matrix.
You can also meet us every Tuesday at 14:00 (Europe/Paris) on Whereby for our weekly development meeting, where we review the development board then select a few subjects to do pair-programming on.
See you next month!