mongoose 6.6.2 → 6.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,63 @@
1
+ 'use strict';
2
+ const schemaMerge = require('../schema/merge');
3
+ const specialProperties = require('../../helpers/specialProperties');
4
+ const isBsonType = require('../../helpers/isBsonType');
5
+ const ObjectId = require('../../types/objectid');
6
+ const isObject = require('../../helpers/isObject');
7
+ /**
8
+ * Merges `from` into `to` without overwriting existing properties.
9
+ *
10
+ * @param {Object} to
11
+ * @param {Object} from
12
+ * @param {String} [path]
13
+ * @api private
14
+ */
15
+
16
+ module.exports = function mergeDiscriminatorSchema(to, from, path) {
17
+ const keys = Object.keys(from);
18
+ let i = 0;
19
+ const len = keys.length;
20
+ let key;
21
+
22
+ path = path || '';
23
+
24
+ while (i < len) {
25
+ key = keys[i++];
26
+ if (key === 'discriminators' || key === 'base' || key === '_applyDiscriminators') {
27
+ continue;
28
+ }
29
+ if (path === 'tree' && from != null && from.instanceOfSchema) {
30
+ continue;
31
+ }
32
+ if (specialProperties.has(key)) {
33
+ continue;
34
+ }
35
+ if (to[key] == null) {
36
+ to[key] = from[key];
37
+ } else if (isObject(from[key])) {
38
+ if (!isObject(to[key])) {
39
+ to[key] = {};
40
+ }
41
+ if (from[key] != null) {
42
+ // Skip merging schemas if we're creating a discriminator schema and
43
+ // base schema has a given path as a single nested but discriminator schema
44
+ // has the path as a document array, or vice versa (gh-9534)
45
+ if ((from[key].$isSingleNested && to[key].$isMongooseDocumentArray) ||
46
+ (from[key].$isMongooseDocumentArray && to[key].$isSingleNested)) {
47
+ continue;
48
+ } else if (from[key].instanceOfSchema) {
49
+ if (to[key].instanceOfSchema) {
50
+ schemaMerge(to[key], from[key].clone(), true);
51
+ } else {
52
+ to[key] = from[key].clone();
53
+ }
54
+ continue;
55
+ } else if (isBsonType(from[key], 'ObjectID')) {
56
+ to[key] = new ObjectId(from[key]);
57
+ continue;
58
+ }
59
+ }
60
+ mergeDiscriminatorSchema(to[key], from[key], path ? path + '.' + key : key);
61
+ }
62
+ }
63
+ };
@@ -4,6 +4,7 @@ const Mixed = require('../../schema/mixed');
4
4
  const defineKey = require('../document/compile').defineKey;
5
5
  const get = require('../get');
6
6
  const utils = require('../../utils');
7
+ const mergeDiscriminatorSchema = require('../../helpers/discriminator/mergeDiscriminatorSchema');
7
8
 
8
9
  const CUSTOMIZABLE_DISCRIMINATOR_OPTIONS = {
9
10
  toJSON: true,
@@ -108,8 +109,7 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
108
109
  }
109
110
  }
110
111
 
111
- utils.merge(schema, baseSchema, {
112
- isDiscriminatorSchemaMerge: true,
112
+ mergeDiscriminatorSchema(schema, baseSchema, {
113
113
  omit: { discriminators: true, base: true, _applyDiscriminators: true },
114
114
  omitNested: conflictingPaths.reduce((cur, path) => {
115
115
  cur['tree.' + path] = true;
@@ -27,16 +27,16 @@ module.exports = function setupTimestamps(schema, timestamps) {
27
27
 
28
28
  schema.$timestamps = { createdAt: createdAt, updatedAt: updatedAt };
29
29
 
30
- if (updatedAt && !schema.paths[updatedAt]) {
31
- schemaAdditions[updatedAt] = Date;
32
- }
33
-
34
30
  if (createdAt && !schema.paths[createdAt]) {
35
31
  const baseImmutableCreatedAt = schema.base.get('timestamps.createdAt.immutable');
36
32
  const immutable = baseImmutableCreatedAt != null ? baseImmutableCreatedAt : true;
37
33
  schemaAdditions[createdAt] = { [schema.options.typeKey || 'type']: Date, immutable };
38
34
  }
39
35
 
36
+ if (updatedAt && !schema.paths[updatedAt]) {
37
+ schemaAdditions[updatedAt] = Date;
38
+ }
39
+
40
40
  schema.add(schemaAdditions);
41
41
 
42
42
  schema.pre('save', function timestampsPreSave(next) {
package/lib/query.js CHANGED
@@ -2402,6 +2402,10 @@ Query.prototype.merge = function(source) {
2402
2402
 
2403
2403
  utils.merge(this._mongooseOptions, source._mongooseOptions);
2404
2404
 
2405
+ return this;
2406
+ } else if (this.model != null && source instanceof this.model.base.Types.ObjectId) {
2407
+ utils.merge(this._conditions, { _id: source }, opts);
2408
+
2405
2409
  return this;
2406
2410
  }
2407
2411
 
@@ -2951,11 +2955,14 @@ Query.prototype.distinct = function(field, conditions, callback) {
2951
2955
  * // equivalent
2952
2956
  * query.sort('field -test');
2953
2957
  *
2958
+ * // also possible is to use a array with array key-value pairs
2959
+ * query.sort([['field', 'asc']]);
2960
+ *
2954
2961
  * #### Note:
2955
2962
  *
2956
2963
  * Cannot be used with `distinct()`
2957
2964
  *
2958
- * @param {Object|String} arg
2965
+ * @param {Object|String|Array<Array<(string | number)>>} arg
2959
2966
  * @return {Query} this
2960
2967
  * @see cursor.sort https://docs.mongodb.org/manual/reference/method/cursor.sort/
2961
2968
  * @api public
Binary file
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "6.6.2",
4
+ "version": "6.6.3",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -53,7 +53,7 @@
53
53
  "mkdirp": "^1.0.4",
54
54
  "mocha": "10.0.0",
55
55
  "moment": "2.x",
56
- "mongodb-memory-server": "8.9.1",
56
+ "mongodb-memory-server": "8.9.3",
57
57
  "ncp": "^2.0.0",
58
58
  "nyc": "15.1.0",
59
59
  "pug": "3.0.2",
package/types/index.d.ts CHANGED
@@ -162,19 +162,20 @@ declare module 'mongoose' {
162
162
  : M
163
163
  : M;
164
164
 
165
- export type DiscriminatorSchema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, T> = T extends Schema<infer T1, infer T2, infer T3, infer T4, infer T5>
166
- ? Schema<Omit<DocType, keyof T1> & T1, DiscriminatorModel<T2, M>, T3 | TInstanceMethods, T4 | TQueryHelpers, T5 | TVirtuals>
167
- : Schema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals>;
165
+ export type DiscriminatorSchema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods, DisSchema> =
166
+ DisSchema extends Schema<infer DisSchemaEDocType, infer DisSchemaM, infer DisSchemaInstanceMethods, infer DisSchemaQueryhelpers, infer DisSchemaVirtuals, infer DisSchemaStatics>
167
+ ? Schema<Omit<DocType, keyof DisSchemaEDocType> & DisSchemaEDocType, DiscriminatorModel<DisSchemaM, M>, DisSchemaInstanceMethods | TInstanceMethods, DisSchemaQueryhelpers | TQueryHelpers, DisSchemaVirtuals | TVirtuals, DisSchemaStatics & TStaticMethods>
168
+ : Schema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods>;
168
169
 
169
170
  type QueryResultType<T> = T extends Query<infer ResultType, any> ? ResultType : never;
170
171
 
171
172
  type PluginFunction<
172
173
  DocType,
173
- M = Model<DocType, any, any, any>,
174
- TInstanceMethods = {},
175
- TQueryHelpers = {},
176
- TVirtuals = {},
177
- TStaticMethods = {}> = (schema: Schema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods>, opts?: any) => void;
174
+ M,
175
+ TInstanceMethods,
176
+ TQueryHelpers,
177
+ TVirtuals,
178
+ TStaticMethods> = (schema: Schema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods>, opts?: any) => void;
178
179
 
179
180
  export class Schema<
180
181
  EnforcedDocType = any,
@@ -207,7 +208,7 @@ declare module 'mongoose' {
207
208
  /** Returns a copy of this schema */
208
209
  clone<T = this>(): T;
209
210
 
210
- discriminator<T = Schema>(name: string, schema: T): DiscriminatorSchema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, T>;
211
+ discriminator<DisSchema = Schema>(name: string, schema: DisSchema): DiscriminatorSchema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods, DisSchema>;
211
212
 
212
213
  /** Returns a new schema that has the picked `paths` from this schema. */
213
214
  pick<T = this>(paths: string[], options?: SchemaOptions): T;
@@ -263,7 +264,7 @@ declare module 'mongoose' {
263
264
  pathType(path: string): string;
264
265
 
265
266
  /** Registers a plugin for this schema. */
266
- plugin<PFunc extends PluginFunction<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods>, POptions extends Parameters<PFunc>[1] = Parameters<PFunc>[1]>(fn: PFunc, opts?: POptions): this;
267
+ plugin<PFunc extends PluginFunction<DocType, M, any, any, any, any>, POptions extends Parameters<PFunc>[1] = Parameters<PFunc>[1]>(fn: PFunc, opts?: POptions): this;
267
268
 
268
269
  /** Defines a post hook for the model. */
269
270
  post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PostMiddlewareFunction<T, T>): this;
package/types/query.d.ts CHANGED
@@ -601,7 +601,7 @@ declare module 'mongoose' {
601
601
  snapshot(val?: boolean): this;
602
602
 
603
603
  /** Sets the sort order. If an object is passed, values allowed are `asc`, `desc`, `ascending`, `descending`, `1`, and `-1`. */
604
- sort(arg?: string | { [key: string]: SortOrder | { $meta: 'textScore' } } | undefined | null): this;
604
+ sort(arg?: string | { [key: string]: SortOrder | { $meta: 'textScore' } } | [string, SortOrder][] | undefined | null): this;
605
605
 
606
606
  /** Sets the tailable option (for use with capped collections). */
607
607
  tailable(bool?: boolean, opts?: {
@@ -100,7 +100,7 @@ declare module 'mongoose' {
100
100
  * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will
101
101
  * build an index on this path when the model is compiled.
102
102
  */
103
- index?: boolean | number | IndexOptions | '2d' | '2dsphere' | 'hashed' | 'text';
103
+ index?: boolean | IndexDirection;
104
104
 
105
105
  /**
106
106
  * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose