Skip to content

Remove schematype caster and casterConstructor properties in favor of embeddedSchemaType and Constructor #15513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Jul 7, 2025

Conversation

vkarpov15
Copy link
Collaborator

Summary

#15179 is a result of an issue that's frustrated me for several years: inconsistency in the caster and casterConstructor properties. In Mongoose 8, caster can be either a schema type instance or an embedded document constructor depending on document array vs primitive array, which is inconsistent and confusing. With this PR, we split it up into:

  1. embeddedSchemaType now stores the embedded schema type for arrays and document arrays. Likely should consider switching maps too, but maps for now use $__schemaType.
  2. Constructor now stores the embedded document constructor for subdocuments and document arrays.

This should improve internal maintainability, and make our TypeScript types more coherent. However, this will also unfortunately break anyone's code that relies on caster, which is why we're putting this in Mongoose 9.

Examples

@vkarpov15 vkarpov15 added this to the 9.0 milestone Jul 3, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors array and subdocument schema types by removing the old caster/casterConstructor properties and replacing them with embeddedSchemaType and Constructor, simplifying internal logic and improving TypeScript types.

  • Replace path.caster.instance checks and uses with path.embeddedSchemaType.instance
  • Migrate all casterConstructor references to the new Constructor property
  • Update tests and documentation to reflect the new property names

Reviewed Changes

Copilot reviewed 31 out of 32 changed files in this pull request and generated no comments.

Show a summary per file
File Description
test/schema.test.js Updated assertions from caster/casterConstructor to embeddedSchemaType/Constructor
test/schema.documentarray.test.js Switched .$embeddedSchemaType to .embeddedSchemaType
test/query.test.js Replaced .caster with .Constructor for bookHolder path
test/document.test.js Updated path.casterConstructor to path.Constructor
package.json Contains unresolved merge conflict markers
lib/types/documentArray/methods/... Replaced casterConstructor with Constructor in methods
lib/types/documentArray/index.js Updated internal array schema symbol to use embeddedSchemaType
lib/types/array/methods/index.js Redirected setter/getter calls from caster to embeddedSchemaType
lib/types/array/index.js Switched getter logic to use embeddedSchemaType
lib/schemaType.js Updated getEmbeddedSchemaType to return this.embeddedSchemaType
lib/schema/subdocument.js Removed this.caster, added this.Constructor assignments
lib/schema/documentArrayElement.js Changed constructor signature to (path, schema, options)
lib/schema/documentArray.js Reworked document array initialization to use embeddedSchemaType and Constructor
lib/schema/array.js Refactored SchemaArray to set embeddedSchemaType
lib/schema.js Updated childSchemas logic to reference Constructor and embeddedSchemaType
lib/helpers/... Various query, populate, plugin helpers updated from caster/casterConstructor to new properties
docs/migrating_to_9.md Added migration guide for casterembeddedSchemaType and Constructor
Comments suppressed due to low confidence (2)

package.json:57

  • Resolve the Git merge conflict markers by removing <<<<<<<, =======, and >>>>>>> and consolidating the correct dependency versions under a single block.
    "pug": "3.0.3",

lib/schema/documentArrayElement.js:21

  • [nitpick] Update the JSDoc or add parameter comments to document the newly introduced schema parameter for SchemaDocumentArrayElement.
function SchemaDocumentArrayElement(path, schema, options) {

@vkarpov15 vkarpov15 merged commit a7c0c7d into 9.0 Jul 7, 2025
65 checks passed
@hasezoey hasezoey deleted the vkarpov15/gh-15179 branch July 8, 2025 06:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants