Skip to content

Commit

Permalink
fix(mariadb): do not automatically parse JSON fields by checking meta
Browse files Browse the repository at this point in the history
  • Loading branch information
lesion committed Feb 23, 2023
1 parent 3b9eb23 commit ea2dee4
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/core/src/dialects/mariadb/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,19 @@ export class MariaDbQuery extends AbstractQuery {
return;
}

for (const _field of Object.keys(this.model.fieldRawAttributesMap)) {
const meta = rows.meta;
for (const [i, _field] of Object.keys(this.model.fieldRawAttributesMap).entries()) {
const modelField = this.model.fieldRawAttributesMap[_field];
if (modelField.type instanceof DataTypes.JSON) {
// Value is returned as String, not JSON
rows = rows.map(row => {
// JSON fields for MariaDB server 10.5.2+ already results in JSON format, skip JSON.parse
// this is due to this https://jira.mariadb.org/browse/MDEV-17832 and how mysql2 connector interacts with MariaDB and JSON fields
if (row[modelField.fieldName] && typeof row[modelField.fieldName] === 'string' && !this.connection.info.hasMinVersion(10, 5, 2)) {
// JSON fields for MariaDB server 10.5.2+ already results in JSON format so we can skip JSON.parse
// In this case the column type field will be MYSQL_TYPE_STRING, but the extended type will indicate 'json'
if (
row[modelField.fieldName]
&& typeof row[modelField.fieldName] === 'string'
&& (!meta[i] || meta[i].dataTypeFormat !== 'json')
) {
row[modelField.fieldName] = JSON.parse(row[modelField.fieldName]);
}

Expand Down

0 comments on commit ea2dee4

Please sign in to comment.