Filter and sort on properties stored in a JSON column
autoquery() turns the query string of a grid into filters for columns. When the data lives in one JSON (or JSONB) column, 0.0.20 has the same for its properties: autoqueryjson($, column, schema, defsort, maxlimit). The schema is the allowlist, a comma separated list of property or property:Type (the type defaults to string), so a client can never filter, sort or read a property that you did not declare.
The builder does not write SQL. It names each field in a neutral way, json:column:property:type, in fields, in the filters and in sort, and the database adapter (PostgreSQL, MySQL, SQLite...) translates that name into its own syntax. That keeps the action free of any dialect. The column name is checked, and an invalid one throws.
The example driver only echoes the options that it receives, which is what an adapter reads.
NEWDB('echo', function(options, callback) {
callback(null, options);
});exports.install = function() {
ROUTE('GET /people/', people);
};
const SCHEMA = 'id,name,email,age:Number,dtcreated:Date,tags:[string]';
async function people($) {
try {
const options = await DB().list('echo/people').autoqueryjson($, 'data', SCHEMA, 'dtcreated_desc', 50).promise();
$.json(options);
} catch (e) {
$.invalid(e);
}
}The query string uses the names of the properties: ?fields=id,name chooses what to read, ?age=>18 filters, ?sort=name_desc sorts and ?page=2&limit=500 pages (the limit never goes above maxlimit). Without fields, every property of the schema is read.