Registry / database / adonis-jsonable

adonis-jsonable

JSON →
library0.2.1jsnpmunverified

Adonis-jsonable is a trait for AdonisJS Lucid models (v4.x) that automatically stringifies and parses PostgreSQL JSON fields. It solves the issue where knex.js requires JSON stringification before saving, but after saving the in-memory model retains the stringified value instead of the original object. Version 0.2.1, last updated in 2017, no active maintenance. Use it by defining jsonFields on your model or passing an array to this.addTrait. Only supports Adonis 4 with @adonisjs/fold peer dependency and the old provider system.

npm install adonis-jsonable
INSTALL
IMPORT
SIG · ADONIS-JSONABLE
A
adonis-jsonable
databasejavascriptv0.2.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

JsonableProvider
import 'adonis-jsonable/providers/JsonableProvider'
import { JsonableProvider } from 'adonis-jsonable'
The provider is imported as a string path in the providers array, not as a module export. It's registered in start/app.js.
this.addTrait
class User extends Model { static boot () { super.boot(); this.addTrait('@provider:Jsonable') } }
this.addTrait('adonis-jsonable')
The trait must be referenced with '@provider:Jsonable' namespace, not the package name.
jsonFields
class User extends Model { get jsonFields () { return ['pets'] } }
static get jsonFields() { return ['pets'] }
In older Adonis models, jsonFields is defined as a getter on the instance, not a static getter. Some newer docs may use static, but this package expects instance getter.

Registers the Jsonable provider, defines a User model with jsonFields and boot method, and shows automatic JSON stringify/parse on create.

// start/app.js const providers = [ '@adonisjs/framework/providers/AppProvider', 'adonis-jsonable/providers/JsonableProvider' ] // app/Models/User.js 'use strict' const Model = use('Model') class User extends Model { get jsonFields () { return ['pets', 'preferences'] } static boot () { super.boot() this.addTrait('@provider:Jsonable') } } module.exports = User // Usage const User = use('App/Models/User') const user = await User.create({ name: 'Bob', pets: ['Rex', 'Francis'] }) console.log(user.pets) // ['Rex', 'Francis'] (original array, not stringified)
Debug
Known issues
deprecatedPackage uses old Adonis 4 provider system and @adonisjs/fold.
fix
Migrate to Adonis 5+ and use @adonisjs/lucid built-in JSON support or a modern alternative.
affects: >=0.0.0
gotchaThe trait only runs on boot, so jsonFields must be defined before save operations.
fix
Define jsonFields as an instance getter, not static.
affects: <0.2.0
gotchaThe trait does not handle nested JSON or deeply nested object mutations; it only stringifies the top-level field.
fix
Manually handle deep mutations or use a alternative like @adonisjs/lucid migrations.
affects: >=0.0.0
breakingv0.1.0 removed console.log, but there might be unnoticed side effects.
fix
Upgrade to v0.2.1.
affects: <0.1.1
Errors
Common errors & fixes
TypeError: Cannot read property 'jsonFields' of undefined
JsonableProvider not registered in providers array.
fix
Add 'adonis-jsonable/providers/JsonableProvider' to your providers list in start/app.js.
Error: Cannot find module 'adonis-jsonable/providers/JsonableProvider'
Package not installed or path wrong.
fix
Run `npm install --save adonis-jsonable` and ensure the string path is correct.
ReferenceError: jsonFields is not defined
jsonFields not defined on the model.
fix
Add a getter `get jsonFields() { return ['fieldName'] }` inside your model class.
Upgrade
Version history
0.2.1latest on npm
Audit
Dependencies
@adonisjs/foldrequiredPeer dependency required by the provider system
Agent activity
34 hits · last 30 days
node
30
OpenAI (training)
1
Resources
adonis-jsonable — npm install adonis-jsonable · libregistry