Add support of saving authors and authorship

This commit is contained in:
Wu Cheng-Han 2016-07-30 11:21:38 +08:00
parent 44fd0a617b
commit 2f117a22cd
6 changed files with 240 additions and 3 deletions

43
lib/models/author.js Normal file
View file

@ -0,0 +1,43 @@
"use strict";
// external modules
var Sequelize = require("sequelize");
// core
var logger = require("../logger.js");
module.exports = function (sequelize, DataTypes) {
var Author = sequelize.define("Author", {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
color: {
type: DataTypes.STRING
}
}, {
indexes: [
{
unique: true,
fields: ['noteId', 'userId']
}
],
classMethods: {
associate: function (models) {
Author.belongsTo(models.Note, {
foreignKey: "noteId",
as: "note",
constraints: false
});
Author.belongsTo(models.User, {
foreignKey: "userId",
as: "user",
constraints: false
});
}
}
});
return Author;
};

View file

@ -51,6 +51,9 @@ module.exports = function (sequelize, DataTypes) {
content: {
type: DataTypes.TEXT
},
authorship: {
type: DataTypes.TEXT
},
lastchangeAt: {
type: DataTypes.DATE
},
@ -74,6 +77,11 @@ module.exports = function (sequelize, DataTypes) {
foreignKey: "noteId",
constraints: false
});
Note.hasMany(models.Author, {
foreignKey: "noteId",
as: "authors",
constraints: false
});
},
checkFileExist: function (filePath) {
try {

View file

@ -30,6 +30,9 @@ module.exports = function (sequelize, DataTypes) {
},
length: {
type: DataTypes.INTEGER
},
authorship: {
type: DataTypes.TEXT
}
}, {
classMethods: {