├── LICENSE ├── README.md ├── package-lock.json ├── package.json ├── providers └── JsonableProvider.js └── src └── Traits └── Jsonable.js /LICENSE: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2017 Chris Watson 5 | 6 | Everyone is permitted to copy and distribute verbatim or modified 7 | copies of this license document, and changing it is allowed as long 8 | as the name is changed. 9 | 10 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 | 13 | 0. You just DO WHAT THE FUCK YOU WANT TO. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # adonis-jsonable 2 | 3 | The `Jsonable` trait solves a major problem that you may run into if you plan on using Postgres' JSON type in your Lucid models. Currently [knex.js][1], which Adonis uses to handle database queries, requires that JSON be stringified before being sent to the database. 4 | 5 | This means that if you want to store an array or a JSON object in the database you have to call `JSON.stringify` on it each time. Unfortunately that also means that when persisting a model to the database with something like this 6 | 7 | ```js 8 | let user = User.create({ name: "Bob", petnames: JSON.stringify(["Rex", "Francis", "Snuffles"]) }) 9 | ``` 10 | 11 | `user.petnames` is going to be the stringified array, even though in the database it is seen as an array. This is not ideal, especially if you're creating an API and planning on sending the newly created model to a user. 12 | 13 | ## What this Trait does 14 | 15 | The `Jsonable` trait solves this issue by allowing you to specify a list of `jsonFields` in your Model which will be automatically stringified before the model is saved, and then after the model is saved those same fields will be converted back to their original state. 16 | 17 | ## Example Usage 18 | 19 | First install this package 20 | 21 | ```bash 22 | adonis install adonis-jsonable 23 | # or 24 | npm install --save adonis-jsonable 25 | # or 26 | yarn add adonis-jsonable 27 | ``` 28 | 29 | Add the JsonableProvider to your `start/app.js` file 30 | 31 | ```js 32 | const providers = [ 33 | ... 34 | 'adonis-jsonable/providers/JsonableProvider', 35 | ] 36 | ``` 37 | 38 | Then load the provider into your model and tell it what attributes should be `Jsonable` 39 | 40 | ```js 41 | 'use strict' 42 | 43 | const Model = use('Model') 44 | 45 | class User extends Model { 46 | 47 | get jsonFields () { 48 | return [ 'pets' ] 49 | } 50 | 51 | static boot () { 52 | super.boot() 53 | this.addTrait('@provider:Jsonable') 54 | } 55 | 56 | } 57 | 58 | module.exports = User 59 | 60 | ``` 61 | 62 | If you don't want to create a `jsonFields` getter you can also add the fields you want to be `Jsonable` as the second parameter in `this.addTrait` 63 | 64 | ```js 65 | this.addTrait('@provider:Jsonable', [ 'pets' ]) 66 | ``` 67 | 68 | ## Changelog 69 | 70 | ### v0.1.1 71 | - Removed `console.log` 72 | 73 | ### v0.1.0 74 | - Initial release 75 | 76 | ## License 77 | 78 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 79 | Version 2, December 2004 80 | 81 | Copyright (C) 2017 Chris Watson 82 | 83 | Everyone is permitted to copy and distribute verbatim or modified 84 | copies of this license document, and changing it is allowed as long 85 | as the name is changed. 86 | 87 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 88 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 89 | 90 | 0. You just DO WHAT THE FUCK YOU WANT TO. 91 | 92 | [1]: http://knexjs.org 93 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "adonis-jsonable", 3 | "version": "0.2.1", 4 | "lockfileVersion": 1 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "adonis-jsonable", 3 | "version": "0.2.1", 4 | "description": "Help Adonis handle Postgres JSON types better", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/watzon/adonis-jsonable.git" 12 | }, 13 | "keywords": [ 14 | "json", 15 | "adonis", 16 | "adonisjs", 17 | "postgres", 18 | "postgresql", 19 | "pgsql" 20 | ], 21 | "author": "Chris Watzon ", 22 | "license": "MIT", 23 | "peerDependencies": { 24 | "@adonisjs/fold": "^4.0.5" 25 | }, 26 | "bugs": { 27 | "url": "https://github.com/watzon/adonis-jsonable/issues" 28 | }, 29 | "homepage": "https://github.com/watzon/adonis-jsonable#readme" 30 | } 31 | -------------------------------------------------------------------------------- /providers/JsonableProvider.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const { ServiceProvider } = require('@adonisjs/fold') 4 | 5 | class JsonableProvider extends ServiceProvider { 6 | register () { 7 | const Jsonable = require('../src/Traits/Jsonable') 8 | this.app.bind('Adonis/Traits/Jsonable', () => new Jsonable) 9 | this.app.alias('Adonis/Traits/Jsonable', 'Jsonable') 10 | } 11 | 12 | boot () { 13 | 14 | } 15 | } 16 | 17 | module.exports = JsonableProvider -------------------------------------------------------------------------------- /src/Traits/Jsonable.js: -------------------------------------------------------------------------------- 1 | class Jsonable { 2 | 3 | constructor () { 4 | this.fields = [] 5 | } 6 | 7 | register (Model, options) { 8 | this.fields = (options.length > 0 ? options : null) || Model.prototype.jsonFields || [] 9 | 10 | Model.addHook('beforeSave', this._beforeSave.bind(this)) 11 | Model.addHook('afterSave', this._afterSave.bind(this)) 12 | Model.addHook('afterFind', this._afterFind.bind(this)) 13 | Model.addHook('afterFetch', this._afterFetch.bind(this)) 14 | Model.addHook('afterPaginate', this._afterFetch.bind(this)) 15 | } 16 | 17 | _beforeSave (instance) { 18 | for (let field of this.fields) { 19 | if (instance[field]) { 20 | instance[field] = JSON.stringify(instance[field]) 21 | } 22 | } 23 | } 24 | 25 | _afterSave (instance) { 26 | for (let field of this.fields) { 27 | if (instance[field] && typeof instance[field] === 'string') { 28 | instance[field] = JSON.parse(instance[field]) 29 | } 30 | } 31 | } 32 | 33 | _afterFind (instance) { 34 | for (let field of this.fields) { 35 | if (instance[field] && typeof instance[field] === 'string') { 36 | instance[field] = JSON.parse(instance[field]) 37 | } 38 | } 39 | } 40 | 41 | _afterFetch (instances) { 42 | for (let instance of instances) { 43 | for (let field of this.fields) { 44 | if (instance[field] && typeof instance[field] === 'string') { 45 | instance[field] = JSON.parse(instance[field]) 46 | } 47 | } 48 | } 49 | } 50 | } 51 | 52 | module.exports = Jsonable 53 | --------------------------------------------------------------------------------