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; 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 = {}; var db = {};
fs fs

View file

@ -53,18 +53,27 @@ module.exports = function (sequelize, DataTypes) {
}, },
title: { title: {
type: DataTypes.TEXT, type: DataTypes.TEXT,
get: function () {
return sequelize.processData(this.getDataValue('title'), "");
},
set: function (value) { set: function (value) {
this.setDataValue('title', sequelize.stripNullByte(value)); this.setDataValue('title', sequelize.stripNullByte(value));
} }
}, },
content: { content: {
type: DataTypes.TEXT, type: DataTypes.TEXT,
get: function () {
return sequelize.processData(this.getDataValue('content'), "");
},
set: function (value) { set: function (value) {
this.setDataValue('content', sequelize.stripNullByte(value)); this.setDataValue('content', sequelize.stripNullByte(value));
} }
}, },
authorship: { authorship: {
type: DataTypes.TEXT, type: DataTypes.TEXT,
get: function () {
return sequelize.processData(this.getDataValue('authorship'), [], JSON.parse);
},
set: function (value) { set: function (value) {
this.setDataValue('authorship', JSON.stringify(value)); this.setDataValue('authorship', JSON.stringify(value));
} }

View file

@ -60,18 +60,27 @@ module.exports = function (sequelize, DataTypes) {
}, },
patch: { patch: {
type: DataTypes.TEXT, type: DataTypes.TEXT,
get: function () {
return sequelize.processData(this.getDataValue('patch'), "");
},
set: function (value) { set: function (value) {
this.setDataValue('patch', sequelize.stripNullByte(value)); this.setDataValue('patch', sequelize.stripNullByte(value));
} }
}, },
lastContent: { lastContent: {
type: DataTypes.TEXT, type: DataTypes.TEXT,
get: function () {
return sequelize.processData(this.getDataValue('lastContent'), "");
},
set: function (value) { set: function (value) {
this.setDataValue('lastContent', sequelize.stripNullByte(value)); this.setDataValue('lastContent', sequelize.stripNullByte(value));
} }
}, },
content: { content: {
type: DataTypes.TEXT, type: DataTypes.TEXT,
get: function () {
return sequelize.processData(this.getDataValue('content'), "");
},
set: function (value) { set: function (value) {
this.setDataValue('content', sequelize.stripNullByte(value)); this.setDataValue('content', sequelize.stripNullByte(value));
} }
@ -81,6 +90,9 @@ module.exports = function (sequelize, DataTypes) {
}, },
authorship: { authorship: {
type: DataTypes.TEXT, type: DataTypes.TEXT,
get: function () {
return sequelize.processData(this.getDataValue('authorship'), [], JSON.parse);
},
set: function (value) { set: function (value) {
this.setDataValue('authorship', value ? JSON.stringify(value) : value); this.setDataValue('authorship', value ? JSON.stringify(value) : value);
} }