codemeasandwich / mongoose-model-stream

a mongoose model generator with a change stream
Apache License 2.0
3 stars 0 forks source link

Race condition leading to changes being merged into one #2

Closed codemeasandwich closed 4 years ago

codemeasandwich commented 4 years ago

I have found that doing multiple separate changes in one go. May not show up as distinct changes

Example

const personSchema = new Schema({ name: String, job:Schema.Types.ObjectId },{ timestamps: true });
const jobSchema = new Schema({ role: String });

const Person = mongoose.moduleStream('person', personSchema)
const Job = mongoose.module('job', jobSchema)

Promise.all([Person.create({name:"bob"}), Job.create({role:"baker"})])
             .then(function([person,job]) {
  person.job = job._id
  person.save()
}).then(console.log)
  .catch(console.error)

This can lead to

{
    "_id" : ObjectId("5e6c96d728321a7249ace214"),
    "patchs" : [ 
        {
            "op" : "add",
            "path" : "/",
            "value" : {
                "_id" : "5e6c96d728321a7249ace212",
                "name": "bob", 
                "job" : "5e6c96d728321a7249ace213",
                "createdAt" : "2020-03-14T08:33:27.846Z",
                "updatedAt" : "2020-03-14T08:33:27.985Z",
                "__v" : 0
            }
        }
    ],
    "target" : ObjectId("5e6c96d728321a7249ace212"),
    "createdAt" : ISODate("2020-03-14T08:33:27.932Z")
}

Where this should be 2 adds

codemeasandwich commented 4 years ago

Fixed: https://github.com/codemeasandwich/mongoose-model-stream/commit/43ea3c16ef4afead26ec12fd477638e697cc5a89