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 field does not work anymore in MySQL 5.7+ #92

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ const options = {
continuationKey: 'userId',
belongsToUserOptions: undefined,
metaDataFields: undefined,
metaDataContinuationKey: 'metaData'
metaDataContinuationKey: 'metaData',
documentFieldType: 'postgres'
};
```

Expand Down Expand Up @@ -218,6 +219,7 @@ const options = {
| [belongsToUserOptions] | Object | undefined | The options used for belongsTo between userModel and Revision model |
| [metaDataFields] | Object | undefined | The keys that will be provided in the meta data object. { key: isRequired (boolean)} format. Can be used to privovide additional fields - other associations, dates, etc to the Revision model |
| [metaDataContinuationKey] | String | 'metaData' | The continuation-local-storage key that contains the meta data object, from where the metaDataFields are extracted. |
| [documentFieldType] | String | ['legacy', 'postgres', 'mysql'] | Changes the type of field 'document' what will be created. 'legacy' produces a `TEXT`, 'postgres' a `JSONB` and 'mysql' a `JSON` field. |

## Limitations

Expand Down
20 changes: 15 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ exports.init = (sequelize, optionsArg) => {
continuationKey: 'userId',
metaDataFields: null,
metaDataContinuationKey: 'metaData',
mysql: false,
documentFieldType: 'postgres',
};

let ns = null;
Expand All @@ -59,6 +59,11 @@ exports.init = (sequelize, optionsArg) => {
helpers.toUnderscored(defaultOptions.defaultAttributes);
}

// keep the compatibility with old project
if (optsArg.mysql && !optsArg.documentFieldType) {
optsArg.documentFieldType = 'legacy';
}

const options = _.defaults(optsArg, defaultOptions);

// if (optionsArg.defaultAttributes) {
Expand Down Expand Up @@ -375,7 +380,7 @@ exports.init = (sequelize, optionsArg) => {

let document = currentVersion;

if (options.mysql) {
if (options.documentFieldType === 'legacy') {
document = JSON.stringify(document);
}

Expand Down Expand Up @@ -448,7 +453,7 @@ exports.init = (sequelize, optionsArg) => {
document = difference;
let diff = o || n ? jsdiff.diffChars(o, n) : [];

if (options.mysql) {
if (options.documentFieldType === 'legacy') {
document = JSON.stringify(document);
diff = JSON.stringify(diff);
}
Expand Down Expand Up @@ -601,8 +606,10 @@ exports.init = (sequelize, optionsArg) => {
operation: Sequelize.STRING(7),
};

if (options.mysql) {
if (options.documentFieldType === 'legacy') {
attributes.document.type = Sequelize.TEXT('MEDIUMTEXT');
} else if (options.documentFieldType === 'mysql') {
attributes.document.type = Sequelize.JSON;
}

attributes[options.defaultAttributes.documentId] = {
Expand Down Expand Up @@ -666,9 +673,12 @@ exports.init = (sequelize, optionsArg) => {
},
};

if (options.mysql) {
if (options.documentFieldType === 'legacy') {
attributes.document.type = Sequelize.TEXT('MEDIUMTEXT');
attributes.diff.type = Sequelize.TEXT('MEDIUMTEXT');
} else if (options.documentFieldType === 'mysql') {
attributes.document.type = Sequelize.JSON;
attributes.diff.type = Sequelize.JSON;
}

if (options.UUID) {
Expand Down