Git Product home page Git Product logo

Comments (5)

8eecf0d2 avatar 8eecf0d2 commented on May 24, 2024

All done, seems to work as expected, will make the PR shortly, tested with:

const uuid = require('uuid')
const mongoose = require('mongoose')
const patchHistory = require('mongoose-patch-history').default

const SchemaString = new mongoose.Schema({
    _id: {
        type: String,
        default: uuid.v4
    },
    title: {
        type: String
    }
})
SchemaString.plugin(patchHistory, {
    mongoose,
    name: 'schemaStringHistory'
})
const ModelString = mongoose.model('schemaString', SchemaString)

const SchemaObjectId = new mongoose.Schema({
    title: {
        type: String
    }
})
SchemaObjectId.plugin(patchHistory, {
    mongoose,
    name: 'schemaObjectIdHistory'
})
const ModelObjectId = mongoose.model('schemaObjectId', SchemaObjectId)

let tempStringDoc = new ModelString({
    title: 'First Title'
})
let tempObjectIdDoc = new ModelObjectId({
    title: 'First Title'
})

tempStringDoc.save()
tempObjectIdDoc.save()

from mongoose-patch-history.

codepunkt avatar codepunkt commented on May 24, 2024

Hey @brod - thanks for your work!

I'm not sure i understand the issue. What is it exactly that throws the CastError?

from mongoose-patch-history.

8eecf0d2 avatar 8eecf0d2 commented on May 24, 2024

CastError is thrown at validation of the ref type in the PatchSchema.

ref: { type: Schema.Types.ObjectId, required: true, index: true }

ref: { type: Schema.Types.ObjectId, required: true, index: true }

The ref type needs to use the same type as the schema _id it's being used for.

// this works, since the default `_id` type will be an `ObjectId`
const mySchema = new mongoose.Schema({
  ...
})
mySchema.plugin(patchHistory, { mongoose, name: 'mySchemaPatches' })

// this doesn't, since the `_id` type is not an `ObjectId`
const mySchema = new mongoose.Schema({
  _id: {
    type: String,
    default: uuidFunc
  },
  ...
})
mySchema.plugin(patchHistory, { mongoose, name: 'mySchemaPatches' })

from mongoose-patch-history.

codepunkt avatar codepunkt commented on May 24, 2024

Thanks for the explanation and the PR update.

Haven't used non-ObjectId _id props so far. Do the documents that use an _id of type Number, for example, get any object id assigned in addition to the numeric _id?

from mongoose-patch-history.

8eecf0d2 avatar 8eecf0d2 commented on May 24, 2024

You're welcome, thanks for the merge!
From what I can tell, no, the _id prop is the only required source of uniqueness and neither mongoose or mongodb care that an ObjectId is missing.

Using a Number or String also has a reasonable impact on performance for the worse IIRC.

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.