Comments (12)
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.
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.
Basically I don't understand how to save the Person -> Organization reference correctly.
from mongoose-patch-history.
- Do you have a more complete example where i can also see the model connections being made and the patches being defined?
- Before changing the ORG on the person, what do the person and organization collections/documents look like?
from mongoose-patch-history.
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.
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.
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.
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.
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.
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.
@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.
Nice change! 🎉
from mongoose-patch-history.
Related Issues (20)
- mongoose 6.4.4 not work HOT 4
- Close version with latest changes HOT 2
- Patches are not deleted if I use deleteMany({}) HOT 1
- An in-range update of mongoose is breaking the build 🚨 HOT 28
- Cannot read property '$__' of undefined HOT 1
- An in-range update of fast-json-patch is breaking the build 🚨 HOT 4
- Usage of old mongoose index practice HOT 2
- Is there an option to track author of changes HOT 5
- An in-range update of mongoose is breaking the build 🚨 HOT 45
- Bluebird promises module is a run-time dependency!
- Is this repo still active? HOT 9
- Breaking change in new released version v1.4.0: MongoError: unknown top level operator: $push HOT 6
- Cannot perform an `add` operation at the desired path HOT 6
- findOneAndUpdate with options { new: true, rawResult: true } throws Error HOT 6
- Bug: With the plugin, updateOne hangs, if nothing was found HOT 8
- Timeout upon saving a new document HOT 7
- Adding lean() causes TypeError: document.data is not a function HOT 9
- Not capturing delete events. HOT 7
- @types/mongoose-patch-history HOT 4
- Make name of internal data() method configurable
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongoose-patch-history.