Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json objects returned as strings for "memory" adapter #192

Open
raffaele-clevermind opened this issue Jul 10, 2018 · 0 comments
Open

Json objects returned as strings for "memory" adapter #192

raffaele-clevermind opened this issue Jul 10, 2018 · 0 comments

Comments

@raffaele-clevermind
Copy link

raffaele-clevermind commented Jul 10, 2018

When using the memory adapter and saving json objects, like shown in the example below, the objects are converted into strings but when returned from "find" method the values are not converted back:

const caminte = require('caminte'),
    Schema = caminte.Schema,
    config = {
         driver     : "memory",
    };

let schema = new Schema(config.driver, config);
let users = schema.define( 'user ', {
  id: { type: schema.Integer},
  name: { type: schema.String},
  data: { type: schema.Json}
}, { primaryKeys: [ 'id' ] } );


users.create({
  id: 1,
  name: 'raffaele',
  data: {age: 22}
}, () => {
  users.find({where: {}}, function(err, users){
     console.log('users:', users); // the data object is a string
  });
});

From what i see this depends on the fact that the memory driver is handled in the AbstractClass class as a "SQL" adapter, which means that the json values are stringified:

AbstractClass._forDB = function (data) {
    var res = {}, YesSQL = ['mysql', 'sqlite', 'sqlite3', 'firebird', 'memory'];
    Object.keys(data).forEach(function (propName) {
        if ((this.whatTypeName(propName) || '').toString().toLowerCase() === 'json'
            || data[propName] instanceof Array) {
            if (YesSQL.indexOf(this.schema.adapter.name || this.schema.name) !== -1) {
                res[propName] = JSON.stringify(data[propName]);
            } else {
                res[propName] = data[propName];
            }
        } else {
            res[propName] = data[propName];
        }
    }.bind(this));
    return res;
};

But the memory adapter doesn't then parse the values when returning them from methods like "find".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant