Refactor data processing to model definition

This commit is contained in:
Wu Cheng-Han 2017-01-02 11:05:36 +08:00
parent d9e19b6029
commit b1ec3ba748
3 changed files with 27 additions and 0 deletions

View file

@ -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

View file

@ -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));
}

View file

@ -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);
}