Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4df6527
WIP adding Schema.create() for better TypeScript typings
vkarpov15 Jun 18, 2025
91d803d
WIP Schema.create() re: #14954
vkarpov15 Jun 18, 2025
b581bf8
fix remaining tests re: #14954
vkarpov15 Jun 19, 2025
f23215a
fix lint
vkarpov15 Jun 19, 2025
47cef1d
Merge branch '9.0' into vkarpov15/schema-create
vkarpov15 Jul 3, 2025
bd8a7b0
Merge branch '9.0' into vkarpov15/schema-create
vkarpov15 Jul 3, 2025
024e5a7
fix tests
vkarpov15 Jul 3, 2025
b02e3e7
address comments
vkarpov15 Jul 3, 2025
f6a68a3
address comments
vkarpov15 Jul 3, 2025
f2ed2f0
types: correct inference for timestamps with methods
vkarpov15 Jul 3, 2025
ab9cbc6
Merge branch '9.0' into vkarpov15/schema-create
vkarpov15 Jul 4, 2025
0a18377
Merge branch '9.0' into vkarpov15/schema-create
vkarpov15 Jul 7, 2025
ae2e9e7
types: add InferRawDocTypeFromSchema
vkarpov15 Jul 7, 2025
c69ee03
Merge branch '9.0' into vkarpov15/schema-create
vkarpov15 Jul 7, 2025
da055be
fix lint
vkarpov15 Jul 7, 2025
ee120cf
types: WIP inferHydratedDocTypeFromSchema
vkarpov15 Jul 7, 2025
9d721e2
types: complete testing inferHydratedDocTypeFromSchema with new Schem…
vkarpov15 Jul 8, 2025
0949ada
Merge branch '9.0' into vkarpov15/schema-create
vkarpov15 Jul 11, 2025
31299fc
Merge branch 'vkarpov15/schema-create' of github.com:Automattic/mongo…
vkarpov15 Jul 11, 2025
8e4b6a6
test: fix tests
vkarpov15 Jul 11, 2025
42a8689
Merge branch '9.0' into vkarpov15/schema-create
vkarpov15 Jul 23, 2025
f63f223
clean up most type issues from merge
vkarpov15 Oct 3, 2025
7eba5ff
fix last tests
vkarpov15 Oct 5, 2025
5dc43e0
types: remove a bunch of unnecessary function overrides and simplify …
vkarpov15 Oct 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,27 @@ Schema.prototype.paths;

Schema.prototype.tree;

/**
* Creates a new schema with the given definition and options. Equivalent to `new Schema(definition, options)`.
*
* `Schema.create()` is primarily useful for automatic schema type inference in TypeScript.
*
* #### Example:
*
* const schema = Schema.create({ name: String }, { toObject: { virtuals: true } });
* // Equivalent:
* const schema2 = new Schema({ name: String }, { toObject: { virtuals: true } });
*
* @return {Schema} the new schema
* @api public
* @memberOf Schema
* @static
*/

Schema.create = function create(definition, options) {
return new Schema(definition, options);
};

/**
* Returns a deep copy of the schema
*
Expand Down
4 changes: 2 additions & 2 deletions test/types/inferrawdoctype.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InferRawDocType } from 'mongoose';
import { InferRawDocType, Types } from 'mongoose';
import { expectType, expectError } from 'tsd';

function gh14839() {
Expand All @@ -21,5 +21,5 @@ function gh14839() {
};

type UserType = InferRawDocType< typeof schemaDefinition>;
expectType<{ email: string, password: string, dateOfBirth: Date }>({} as UserType);
expectType<{ email: string, password: string, dateOfBirth: Date } & { _id: Types.ObjectId }>({} as UserType);
}
Loading
Loading