History for Mongo using Mongoose History

Skip to first unread message

Christopher Britz

Mar 21, 2014, 3:33:53 PM3/21/14
to keyst...@googlegroups.com
So I am trying to get versioning down for posts and other mongoose models, so I am wondering how  you are creating your mongoose models under your list objects.  Once I understand this then I can add the mongoose_history to the each model dynamically, so all models that are created via new keystone.List() will have history.  Then once I get that to work I would like to set an option to include or not include history in mongo collection for each keystone list.  Any help is appreciated, and of course when I finish I will do a pull request.


Mar 24, 2014, 11:58:11 PM3/24/14
to keyst...@googlegroups.com
Hey Christopher,

Sounds like a great feature - cheers.

Lists create a mongoose schema when they are initialised, which is the available as myList.schema

(see line 39 of lib/list.js)

So you could work with that fairly easily.

If you want direct access to mongoose itself, it is available on the Keystone instance, so you could create a new Schema like this:

var mySchema = new keystone.mongoose.Schema()

Christopher Britz

Mar 25, 2014, 3:29:00 PM3/25/14
to keyst...@googlegroups.com
So I am kinda stuck here and wonder if you might be able to help.  I decided to get this working from within one of the schemas that you create with the yeoman generator, so that I can have use the adminui to save models easier. After I get this working I will add it to the list.js file within keystone repo. So within the posts model i have:

var keystone = require('keystone'),
    mongooseHistory = require('mongoose-history'),
Types = keystone.Field.Types;

var Post = new keystone.List('Post', {
map: { name: 'title' },
autokey: { path: 'slug', from: 'title', unique: true }

title: { type: String, required: true },
slug: { type: String, index: true },
state: { type: Types.Select, options: 'draft, published, archived', default: 'draft', index: true },
author: { type: Types.Relationship, ref: 'User', index: true },
publishedDate: { type: Types.Date, index: true },
image: { type: Types.CloudinaryImage },
content: {
brief: { type: Types.Html, wysiwyg: true, height: 150 },
extended: { type: Types.Html, wysiwyg: true, height: 400 }
categories: { type: Types.Relationship, ref: 'PostCategory', many: true }

Post.schema.virtual('content.full').get(function() {
return this.content.extended || this.content.brief;


Post.defaultColumns = 'title, state|20%, author|20%, publishedDate|20%';

Which should get the mongoose schema from the Post list object and add mongoose history as a plugin.  Now when I go to the AdminUI and try to save/update an existing post the application just hangs.  No error or anything.  I have a couple of guesses here, but dont know how far fetched I am.  First, this could be becuase this is a GET request and its not returning anything from the save on <modelName>_history collection because its not returning correctly using a callback.  Second, this could be because of other Model.pre('save'...) methods which its getting stuck on.  Any suggestions?

Christopher Britz

Mar 26, 2014, 2:04:03 PM3/26/14
to keyst...@googlegroups.com
So it appears to be an issue with having multiple mongoose versions in your project.  For example, I had two, one in keystone and one in mongoose-history.  Got it working with an exception of making the mongoose schema _id option in new <model>_history collection to false.  I dont know why it needs to be false, but that is what the author of mongoose-history is doing.  I set it to true and now everything is working as expected.  Going to add to my fork of keystone now. 

Christopher Britz

Mar 27, 2014, 2:12:26 PM3/27/14
to keyst...@googlegroups.com
Got it working now. In pull request #264


Nov 26, 2018, 9:16:50 AM11/26/18
to Keystone JS
Hello, Christopher.

I have just now hit the exact same snag on my Keystone.js project. Not sure how you set the "mongoose schema _id option in new <model>_history collection" to false, but my keystone.js application updates failed once I used the List.schema.plugin(require('mongoose-history')) code. I really could use your help.
Reply all
Reply to author
0 new messages