AdonisJS Lucid and PostgreSQL JSONB object serialization at the model boundary
0 reputation · 30 Mar 2025, 01:55 UTC
0 reputation · 30 Mar 2025, 01:55 UTC
The goal is to read a PostgreSQL JSONB column through an AdonisJS Lucid model as a plain JavaScript object after fetch, and to write a plain object back on save without manual stringification in application code.
Lucid does not document guaranteed automatic JSONB casting, and behavior may differ between AdonisJS 5 and Lucid in AdonisJS 6. The node-pg type parser can return JSONB as text or parsed JSON independently of the ORM, and schema builder support for json or jsonb columns depends on the installed major version and driver. Until the installed version is confirmed, nested attribute round-trips cannot be assumed to survive as objects.
Does Lucid cast a PostgreSQL JSONB column to a JavaScript object on model fetch, or does it return a string? Is an object assigned to a JSONB attribute serialized automatically on save, or are getter and setter accessors required? Which AdonisJS major version and node-pg type-parser settings control this boundary behavior?
Lucid does not guarantee automatic casting of a PostgreSQL JSONB column to a plain JavaScript object upon model fetch; this behavior is highly dependent on the specific AdonisJS major version and the configuration of the underlying node-pg type parser.
typeof on the attribute value to determine if it is an object or a string.If the fetched attribute is a string, manual parsing (e.g., JSON.parse()) is required on read. If saving fails to persist the object structure, accessors (getters/setters) might be necessary, but this should only be confirmed after version verification.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.