Last active
August 25, 2020 14:20
-
-
Save BMartinos/d24dab6cb6d5b77600a5aeb990c0d40f to your computer and use it in GitHub Desktop.
Mongoose Patch History $push bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Promise, { join } from 'bluebird' | |
import mongoose, { Schema } from 'mongoose' | |
import patchHistory from 'mongoose-patch-history' | |
mongoose.Promise = Promise | |
const PostSchema = new Schema( | |
{ | |
title: String, | |
tags: { type: [String], default: void 0 }, | |
}, | |
{ timestamps: true } | |
) | |
PostSchema.plugin(patchHistory, { | |
mongoose, | |
name: 'postPatches', | |
transforms: [name => name.toLowerCase(), () => 'post_history'], | |
includes: { | |
version: { type: Number, from: '__v' }, | |
reason: { type: String, from: '__reason' }, | |
user: { type: Object, from: '__user' }, | |
}, | |
}) | |
PostSchema.virtual('user').set(function (user) { | |
this.__user = user | |
}) | |
PostSchema.virtual('reason').set(function (reason) { | |
this.__reason = reason | |
}) | |
let Post = mongoose.model('Post', PostSchema) | |
mongoose | |
.connect('mongodb://localhost/mongoose-patch-history', { | |
useNewUrlParser: true, | |
useCreateIndex: true, | |
useUnifiedTopology: true, | |
useFindAndModify: false, | |
}) | |
.then(() => { | |
join( | |
Post.deleteMany({}), | |
Post.Patches.deleteMany({}) | |
) | |
.then(() => () => { | |
console.log('all done...') | |
}) | |
}) | |
Post.create({ title: 'tagged1', tags: ['match'] }) | |
.then(post => | |
Post.updateMany( | |
{ _id: post._id }, | |
{ $push: { tags: 'match2' } }, | |
{ timestamps: false } | |
) | |
) | |
.then(() => Post.find({ title: 'tagged1' })) | |
.then(posts => | |
posts[0].patches.find({ ref: posts[0]._id }).sort({ _id: 1 }) | |
) | |
.then(patches => { | |
console.log(patches[1].ops) | |
}) | |
.then(() => () => { | |
console.log("all completed fine") | |
}) | |
.catch((err) => { | |
console.log("Error: ", err) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"scripts": { | |
"compile": "rm -rf lib/ && babel -d lib/ index.js", | |
"start": "npm run compile && node lib/index.js" | |
}, | |
"dependencies": { | |
"babel-cli": "^6.26.0", | |
"babel-preset-es2015": "^6.22.0", | |
"bluebird": "^3.7.2", | |
"mongoose": "^5.6.2", | |
"mongoose-patch-history": "^1.4.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above example taken from the repository test script does not yield the same result as in the tests.
This specific example tests the $push operation of MongoDB (v4.2) which is currently broken in the new version (v1.4.0)
Steps to reproduce:
npm i
npm start
Error being returned: