Vincit / objection.js

An SQL-friendly ORM for Node.js
https://vincit.github.io/objection.js
MIT License
7.26k stars 637 forks source link

No JSON-Parsing on joinRelated #1530

Closed eopo closed 5 years ago

eopo commented 5 years ago

Hello,

I'm trying to fetch a model with a relation (via join). This related Model contains a json field I set in jsonAttributes. If I query the related Model itself, this field get's returned as Object. If I query it via joinRelated, I just get it as a string. PMAC or bug?

Message.js

class Message extends Model {
    static get tableName () {
        return 'messages';
    }

    static get relationMappings () {
        return {
            alias: {
                relation: Model.BelongsToOneRelation,
                modelClass: Alias,
                join: {
                    from: 'messages.alias_id',
                    to: 'capcodes.id'
                }
            }
        }
    }
    ...
}

Alias.js

class Alias extends Model {
    static get tableName () {
        return 'capcodes';
    }

    static get jsonAttributes () {
        return ['pluginconf'];
    }

    static get relationMappings () {
        const { Message } = require('./Message');
        return {
            messages: {
                relation: Model.HasManyRelation,
                modelClass: Message,
                join: {
                    from: 'capcodes.id',
                    to: 'messages.alias_id'
                }
            }
        }
    }
    ...
}

Real return

{
  id: 204,
  address: '...',
  message: '...',
  source: 'LOCAL',
  timestamp: 1571914155,
  alias_id: 204,
  changed: null,
  alias: '...',
  agency: '...',
  icon: '...',
  color: '#000000',
  pluginconf: '{"Discord":{},"Filter":{},"Gotify":{},"MessageRepeat":{},"MicrosoftTeams":{},"Prowl":{},"Pushover":{},"RegexReplace":{},"SMTP":{"someTestKey":"someTestValue","someOtherKey":"someOtherValue"},"Shell":{},"Slack":{},"Telegram":{},"Twitter":{},"Template":{}}',
  ignore: 0,
  pluginData: {}
}

Expected result:

{
  id: 204,
  address: '...',
  message: '...',
  source: 'LOCAL',
  timestamp: 1571914155,
  alias_id: 204,
  changed: null,
  alias: '...',
  agency: '...',
  icon: '...',
  color: '#000000',
  pluginconf: {
    Discord: {},
    Filter: {},
    Gotify: {},
    MessageRepeat: {},
    MicrosoftTeams: {},
    Prowl: {},
    Pushover: {},
    RegexReplace: {},
    SMTP: {
      someTestKey: 'someTestValue',
      someOtherKey: 'someOtherValue'
    },
    Shell: {},
    Slack: {},
    Telegram: {},
    Twitter: {},
    Template: {}
  },
  ignore: 0,
  pluginData: {}
}

Result of a direct query to Alias:

{
  id: 204,
  address: '...',
  alias: '...',
  agency: '...',
  icon: '...',
  color: '#000000',
  pluginconf: {
    Discord: {},
    Filter: {},
    MicrosoftTeams: {},
    Pushover: {},
    RegexReplace: {},
    SMTP: {
        someTestKey: 'someTestValue',
        someOtherKey: 'someOtherValue'
    },
    Slack: {},
    Telegram: {},
    Template: {},
    Twitter: {}
  },
  ignore: 0
}
koskimas commented 5 years ago

Use joinEager.

eopo commented 5 years ago

Puts the alias object into a sub-object. Not wanted here. Theoretically more useful, yes, but in this case not.