diff --git a/core/database-connection-factory.js b/core/database-connection-factory.js index 76cebb316..a60cf8400 100644 --- a/core/database-connection-factory.js +++ b/core/database-connection-factory.js @@ -8,6 +8,7 @@ const STORAGE_DIR = path.join(__base, 'storage'); if (!fs.existsSync(STORAGE_DIR)) { fs.mkdirSync(STORAGE_DIR); } +Object.assign(Sequelize, require('./database-connection-types')(Sequelize)) class DatabaseConnectionFactory { constructor() { diff --git a/core/database-types.js b/core/database-types.js new file mode 100644 index 000000000..f851fa489 --- /dev/null +++ b/core/database-types.js @@ -0,0 +1,12 @@ +module.exports = (Sequelize) => ({ + JSONTYPE: (fieldName) => ({ + type: Sequelize.STRING, + defaultValue: '{}', + get: function get() { + return JSON.parse(this.getDataValue('syncState')) + }, + set: function set(val) { + this.setDataValue('syncState', JSON.stringify(val)); + }, + }), +}) diff --git a/core/models/account/category.js b/core/models/account/category.js index 39f416829..cede313b4 100644 --- a/core/models/account/category.js +++ b/core/models/account/category.js @@ -2,16 +2,7 @@ module.exports = (sequelize, Sequelize) => { const Category = sequelize.define('Category', { name: Sequelize.STRING, role: Sequelize.STRING, - syncState: { - type: Sequelize.STRING, - defaultValue: '{}', - get: function get() { - return JSON.parse(this.getDataValue('syncState')) - }, - set: function set(val) { - this.setDataValue('syncState', JSON.stringify(val)); - }, - }, + syncState: Sequelize.JSONTYPE('syncState'), }, { classMethods: { associate: ({Message}) => {