Commit b1ec3ba7 authored by Wu Cheng-Han's avatar Wu Cheng-Han
Browse files

Refactor data processing to model definition

parent d9e19b60
......@@ -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
......
......@@ -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));
}
......
......@@ -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);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment