diff --git a/lib/models/index.js b/lib/models/index.js index 6d0fd3c..e83956e 100644 --- a/lib/models/index.js +++ b/lib/models/index.js @@ -27,6 +27,12 @@ function stripNullByte(value) { } sequelize.stripNullByte = stripNullByte; +function processData(data, _default, process) { + if (data === undefined) return data; + else return data === null ? _default : (process ? process(data) : data); +} +sequelize.processData = processData; + var db = {}; fs diff --git a/lib/models/note.js b/lib/models/note.js index 37d26ec..5c63dc1 100644 --- a/lib/models/note.js +++ b/lib/models/note.js @@ -53,18 +53,27 @@ module.exports = function (sequelize, DataTypes) { }, title: { type: DataTypes.TEXT, + get: function () { + return sequelize.processData(this.getDataValue('title'), ""); + }, set: function (value) { this.setDataValue('title', sequelize.stripNullByte(value)); } }, content: { type: DataTypes.TEXT, + get: function () { + return sequelize.processData(this.getDataValue('content'), ""); + }, set: function (value) { this.setDataValue('content', sequelize.stripNullByte(value)); } }, authorship: { type: DataTypes.TEXT, + get: function () { + return sequelize.processData(this.getDataValue('authorship'), [], JSON.parse); + }, set: function (value) { this.setDataValue('authorship', JSON.stringify(value)); } diff --git a/lib/models/revision.js b/lib/models/revision.js index adc651f..c7360fe 100644 --- a/lib/models/revision.js +++ b/lib/models/revision.js @@ -60,18 +60,27 @@ module.exports = function (sequelize, DataTypes) { }, patch: { type: DataTypes.TEXT, + get: function () { + return sequelize.processData(this.getDataValue('patch'), ""); + }, set: function (value) { this.setDataValue('patch', sequelize.stripNullByte(value)); } }, lastContent: { type: DataTypes.TEXT, + get: function () { + return sequelize.processData(this.getDataValue('lastContent'), ""); + }, set: function (value) { this.setDataValue('lastContent', sequelize.stripNullByte(value)); } }, content: { type: DataTypes.TEXT, + get: function () { + return sequelize.processData(this.getDataValue('content'), ""); + }, set: function (value) { this.setDataValue('content', sequelize.stripNullByte(value)); } @@ -81,6 +90,9 @@ module.exports = function (sequelize, DataTypes) { }, authorship: { type: DataTypes.TEXT, + get: function () { + return sequelize.processData(this.getDataValue('authorship'), [], JSON.parse); + }, set: function (value) { this.setDataValue('authorship', value ? JSON.stringify(value) : value); }