revision.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. module.exports = function(app, models) {
  2. var mongoose = require('mongoose')
  3. , ObjectId = mongoose.Schema.Types.ObjectId
  4. , revisionSchema;
  5. revisionSchema = new mongoose.Schema({
  6. path: { type: String, required: true },
  7. body: { type: String, required: true },
  8. format: { type: String, default: 'markdown' },
  9. author: { type: ObjectId, ref: 'User' },
  10. createdAt: { type: Date, default: Date.now }
  11. });
  12. revisionSchema.statics.findLatestRevision = function(path, cb) {
  13. this.find({path: path})
  14. .sort({'createdAt': -1})
  15. .limit(1)
  16. .exec(function(err, data) {
  17. cb(err, data.shift());
  18. });
  19. };
  20. revisionSchema.statics.findRevisionList = function(path, options, cb) {
  21. this.find({path: path})
  22. .sort({'createdAt': -1})
  23. .populate('author')
  24. .exec(function(err, data) {
  25. cb(err, data);
  26. });
  27. };
  28. revisionSchema.statics.updateRevisionListByPath = function(path, updateData, options, cb) {
  29. this.update({path: path}, {$set: updateData}, {multi: true}, function(err, data) {
  30. cb(err, data);
  31. });
  32. };
  33. revisionSchema.statics.findRevision = function(id, cb) {
  34. this.findById(id)
  35. .populate('author')
  36. .exec(function(err, data) {
  37. cb(err, data);
  38. });
  39. };
  40. revisionSchema.statics.prepareRevision = function(pageData, body, user, options) {
  41. var Revision = this;
  42. if (!options) {
  43. options = {};
  44. }
  45. var format = options.format || 'markdown';
  46. if (!user._id) {
  47. throw new Error('Error: user should have _id');
  48. }
  49. var newRevision = new Revision();
  50. newRevision.path = pageData.path;
  51. newRevision.body = body;
  52. newRevision.format = format;
  53. newRevision.author = user._id;
  54. newRevision.createdAt = Date.now();
  55. return newRevision;
  56. };
  57. revisionSchema.statics.updatePath = function(pathName) {
  58. };
  59. models.Revision = mongoose.model('Revision', revisionSchema);
  60. return models.Revision;
  61. };