Git Product home page Git Product logo

Comments (12)

mrchess avatar mrchess commented on May 23, 2024 1

Fwiw I think I'm just gonna change my:

  organization: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Organization',
  },

to

  organization: {
    type: String,
    ref: 'Organization',
  },

And use virtuals to populate. I don't really see a danger in doing this from a mongoose perspective. Please let me know if you see one.

from mongoose-patch-history.

codepunkt avatar codepunkt commented on May 23, 2024 1

Thanks for helping me out! 👍

I need to think about this and play around with it when i find the time so i'm keeping this issue open. If ObjectId refs really can't be compared with jsonpatch, this should at least be documented in the readme!

from mongoose-patch-history.

mrchess avatar mrchess commented on May 23, 2024

Basically I don't understand how to save the Person -> Organization reference correctly.

from mongoose-patch-history.

codepunkt avatar codepunkt commented on May 23, 2024

@mrchess

  1. Do you have a more complete example where i can also see the model connections being made and the patches being defined?
  2. Before changing the ORG on the person, what do the person and organization collections/documents look like?

from mongoose-patch-history.

mrchess avatar mrchess commented on May 23, 2024

Here is a simple example.

const mongoose = require('mongoose');
const patchHistory = require('mongoose-patch-history');
const Promise = require('bluebird');
mongoose.Promise = Promise;

// Organization Model
const OrganizationSchema = new mongoose.Schema({
  name: {
    type: String,
  },
});
const Organization = mongoose.model('Organization', OrganizationSchema);

// Person Model
const PersonSchema = new mongoose.Schema({
  organization: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Organization',
  },
  name: {
    type: String,
  },
});

PersonSchema.plugin(patchHistory.default, {
  mongoose,
  name: 'roomPatches',
});

const Person = mongoose.model('Person', PersonSchema);

//////////////
// The Test //
//////////////
const uri = 'mongodb://localhost:27017/mongoose_patch_history_test';

let workOrg;
let homeOrg;
let person;
mongoose.connect(uri)
  // Setup
  .then(() => Organization.create({ name: 'Home '}))
  .then((o) => (homeOrg = o))
  .then(() => Organization.create({ name: 'Work '}))
  .then((o) => (workOrg = o))
  .then(() => Person.create({ name: 'Bob', organization: homeOrg._id }))
  .then((p) => (person = p))
  // Test
  .then(() => person.patches.find({ ref: person.id }))
  .then((patches) => {
    console.log(JSON.stringify(patches,null,2));
    // [
    //   {
    //     "_id": "589d7124c2d0b1aa30dcfb14",
    //     "ref": "589d7124c2d0b1aa30dcfb13",
    //     "__v": 0,
    //     "ops": [
    //       {
    //         "op": "add",
    //         "path": "/name",
    //         "value": "Bob"
    //       },
    //       {
    //         "op": "add",
    //         "path": "/organization",
    //         "value": "589d7124c2d0b1aa30dcfb11"
    //       }
    //     ],
    //     "date": "2017-02-10T07:52:04.201Z"
    //   }
    // ]
  })
  // Switch the org
  .then(() => {
    person.organization = workOrg._id;
    return person.save();
  })
  .then(() => person.patches.find({ ref: person.id }))
  .then((patches) => {
    console.log(JSON.stringify(patches,null,2));
    // [
    //   {
    //     "_id": "589d7195101d63abfae70aee",
    //     "ref": "589d7195101d63abfae70aed",
    //     "__v": 0,
    //     "ops": [
    //       {
    //         "op": "add",
    //         "path": "/name",
    //         "value": "Bob"
    //       },
    //       {
    //         "op": "add",
    //         "path": "/organization",
    //         "value": "589d7195101d63abfae70aeb"
    //       }
    //     ],
    //     "date": "2017-02-10T07:53:57.053Z"
    //   },
    //   {
    //     "_id": "589d7195101d63abfae70aef",
    //     "ref": "589d7195101d63abfae70aed",
    //     "__v": 0,
    //     "ops": [
    //       {
    //         "op": "replace",
    //         "path": "/organization/id/11",
    //         "value": 236
    //       }
    //     ],
    //     "date": "2017-02-10T07:53:57.070Z"
    //   }
    // ]
  });

Here is an example case. See how in the second patch the operation path/value is kind of weird?

"ops": [
  {
    "op": "replace",
    "path": "/organization/id/11",
    "value": 236
  }

Shouldn't it be:

"ops": [
  {
    "op": "replace",
    "path": "/organization", // <--- This path
    "value": THE_WORK_ORG_ID // <--- A Mongo Object ID string
  }

from mongoose-patch-history.

mrchess avatar mrchess commented on May 23, 2024

Quickly adding my version as well:

  "dependencies": {
    "bluebird": "^3.4.7",
    "mongoose": "^4.8.1",
    "mongoose-patch-history": "^1.1.5"
  }

from mongoose-patch-history.

codepunkt avatar codepunkt commented on May 23, 2024

That seems weird indeed.

There is this line in the plugin:
const ops = jsonpatch.compare(this.isNew ? {} : this._original, this.data())
Can you see what is compared here? this._original vs this.data()

from mongoose-patch-history.

mrchess avatar mrchess commented on May 23, 2024

Interesting it yields this:

console.log(this._original, this.data());
// { name: 'Bob', organization: 589e966e0167f20340a26d44 } { name: 'Bob', organization: 589e966e0167f20340a26d45 }
const ops = jsonpatch.compare(this.isNew ? {} : this._original, this.data())
console.log(ops);
//[ { op: 'replace', path: '/organization/id/11', value: 69 } ]

I'm trying to debug it.

from mongoose-patch-history.

mrchess avatar mrchess commented on May 23, 2024

I think it is because they are mongoose ObjectId objects.

console.log(typeof this._original.organization, typeof this.data().organization);
// object object
console.log(this._original.organization.constructor.name);
// ObjectID

from mongoose-patch-history.

mrchess avatar mrchess commented on May 23, 2024

So yeah, it must be a problem with the jsonpatch.compare not being able to resolve these mongoose objects. Any suggestions? I may try to just change them to strings or something... the "ref" keys.

Have you yourself been using ObjectID refs in your collections?

from mongoose-patch-history.

codepunkt avatar codepunkt commented on May 23, 2024

@mrchess Actually, the comparison to generate JSON patches should be done on JSON data - which the object returned from .data() is not. I added in a toJSON method that converts BSON to JSON (and therefore ObjectId references to strings) before comparison.

Tested with your example, should work. Published on npm as v1.1.6! Enjoy! :)

from mongoose-patch-history.

mrchess avatar mrchess commented on May 23, 2024

Nice change! 🎉

from mongoose-patch-history.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.