不要怂,就是干,撸起袖子干!

Commit 63d4aaa3 by u9r52sld Committed by Sushant

fix(json): access included data with attributes (#9662)

1 parent c0038718
...@@ -1496,6 +1496,10 @@ class QueryGenerator { ...@@ -1496,6 +1496,10 @@ class QueryGenerator {
let prefix; let prefix;
if (verbatim === true) { if (verbatim === true) {
prefix = attr; prefix = attr;
} else if (/#>>|->>/.test(attr)) {
prefix = `(${this.quoteIdentifier(includeAs.internalAs)}.${attr.replace(/\(|\)/g, '')})`;
} else if (/json_extract/.test(attr)) {
prefix = `json_extract(${this.quoteIdentifier(includeAs.internalAs)}.${attr.replace(/\(|\)|json_extract/g, '')})`;
} else { } else {
prefix = `${this.quoteIdentifier(includeAs.internalAs)}.${this.quoteIdentifier(attr)}`; prefix = `${this.quoteIdentifier(includeAs.internalAs)}.${this.quoteIdentifier(attr)}`;
} }
......
...@@ -17,6 +17,9 @@ describe('model', () => { ...@@ -17,6 +17,9 @@ describe('model', () => {
emergency_contact: DataTypes.JSON, emergency_contact: DataTypes.JSON,
emergencyContact: DataTypes.JSON emergencyContact: DataTypes.JSON
}); });
this.Order = this.sequelize.define('Order');
this.Order.belongsTo(this.User);
return this.sequelize.sync({ force: true }); return this.sequelize.sync({ force: true });
}); });
...@@ -255,6 +258,65 @@ describe('model', () => { ...@@ -255,6 +258,65 @@ describe('model', () => {
}); });
}); });
} }
it('should be able retrieve json value with nested include', function() {
return this.User.create({
emergency_contact: {
name: 'kate'
}
}).then(user => {
return this.Order.create({ UserId: user.id });
}).then(() => {
return this.Order.findAll({
attributes: ['id'],
include: [{
model: this.User,
attributes: [
[this.sequelize.json('emergency_contact.name'), 'katesName']
]
}]
});
}).then(orders => {
expect(orders[0].User.getDataValue('katesName')).to.equal('kate');
});
});
});
}
if (current.dialect.supports.JSONB) {
describe('jsonb', () => {
beforeEach(function() {
this.User = this.sequelize.define('User', {
username: DataTypes.STRING,
emergency_contact: DataTypes.JSONB,
});
this.Order = this.sequelize.define('Order');
this.Order.belongsTo(this.User);
return this.sequelize.sync({ force: true });
});
it('should be able retrieve json value with nested include', function() {
return this.User.create({
emergency_contact: {
name: 'kate'
}
}).then(user => {
return this.Order.create({ UserId: user.id });
}).then(() => {
return this.Order.findAll({
attributes: ['id'],
include: [{
model: this.User,
attributes: [
[this.sequelize.json('emergency_contact.name'), 'katesName']
]
}]
});
}).then(orders => {
expect(orders[0].User.getDataValue('katesName')).to.equal('kate');
});
});
}); });
} }
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!