From 2f117a22cd21fa4fb67a3b2d687485795f86d030 Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Sat, 30 Jul 2016 11:21:38 +0800 Subject: Add support of saving authors and authorship --- lib/models/author.js | 43 +++++++++++++++++++++++++++++++++++++++++++ lib/models/note.js | 8 ++++++++ lib/models/revision.js | 3 +++ 3 files changed, 54 insertions(+) create mode 100644 lib/models/author.js (limited to 'lib/models') diff --git a/lib/models/author.js b/lib/models/author.js new file mode 100644 index 00000000..0b0f149d --- /dev/null +++ b/lib/models/author.js @@ -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; +}; \ No newline at end of file diff --git a/lib/models/note.js b/lib/models/note.js index f9a8ec61..5ee5c7de 100644 --- a/lib/models/note.js +++ b/lib/models/note.js @@ -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 { diff --git a/lib/models/revision.js b/lib/models/revision.js index bb89782f..f525ea55 100644 --- a/lib/models/revision.js +++ b/lib/models/revision.js @@ -30,6 +30,9 @@ module.exports = function (sequelize, DataTypes) { }, length: { type: DataTypes.INTEGER + }, + authorship: { + type: DataTypes.TEXT } }, { classMethods: { -- cgit v1.2.3