seegno / bookshelf-json-columns

Parse JSON columns with Bookshelf.js

Home Page:http://seegno.github.io/bookshelf-json-columns

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why is parse on fetch limited to sqlite?

pandapaul opened this issue · comments

https://github.com/seegno/bookshelf-json-columns/blob/master/src/index.js#L52 limits parsing on fetch to only sqlite and sqlite3. Why is this? Allowing parse on fetch with mysql as the client seems to work great for me, but I might be misunderstanding how to enable parse on fetch in some other way.

Is there another way I should be enabling parse on fetch with mysql as the client?

Thanks very much,
Paul

Hi @pandapaul.

This plugin was designed to fix the JSON columns issue with PostgreSQL, and only later introduced tests for SQLite. If I remember correctly we believed that there was no need to use this plugin with MySQL as the client handles all these issues itself.

However, can you provide a snippet of your code and schema to figure out if we should introduce MySQL on the test suite?

Oh interesting. The knex migration I use for creating this table looks approximately like this:

exports.up = knex => knex.schema.createTable('aTable', (table) => {
  table.uuid('id').primary().notNullable()
  table.json('someJson')
})

And here's what the bookshelf model looks like:

module.exports = bookshelf.Model.extend({
  tableName: 'aTable',
  uuid: true,
}, {
  jsonColumns: ['someJson'],
})

With mysql as the client, table.json() simply creates a TEXT column, so I'd love to parse that on fetches. Changing that parseOnFetch logic to include mysql works great to accomplish this, but maybe I'm missing something that would allow me to accomplish this without the plugin?

Perhaps the real problem is that table.json() creates a TEXT column: knex/knex#1036

I'll attempt to adjust my schema and let you know how it goes.

I think it's the same problem here knex/knex#1800.

Agreed. Changing the schema to use table.specificType() to force the column to be typed JSON doesn't help. Results in the same errors as above, which lines up with what is reported in the knex issue you mentioned. I think this validates the use of this plugin for mysql clients on both save and fetch.

Splendid. Thanks @ricardogama and @abelsoares