@datanimbus/mongoose 6.13.13 → 9.13.2

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.
Files changed (302) hide show
  1. package/README.md +80 -70
  2. package/eslint.config.mjs +202 -0
  3. package/index.js +2 -1
  4. package/lib/aggregate.js +276 -174
  5. package/lib/cast/bigint.js +46 -0
  6. package/lib/cast/boolean.js +3 -3
  7. package/lib/cast/decimal128.js +4 -1
  8. package/lib/cast/double.js +56 -0
  9. package/lib/cast/int32.js +42 -0
  10. package/lib/cast/number.js +6 -6
  11. package/lib/cast/objectid.js +3 -3
  12. package/lib/cast/string.js +3 -3
  13. package/lib/cast/uuid.js +35 -0
  14. package/lib/cast.js +100 -92
  15. package/lib/collection.js +31 -22
  16. package/lib/connection.js +792 -507
  17. package/lib/constants.js +73 -0
  18. package/lib/cursor/{AggregationCursor.js → aggregationCursor.js} +205 -52
  19. package/lib/cursor/changeStream.js +218 -0
  20. package/lib/cursor/queryCursor.js +676 -0
  21. package/lib/defaults/schemaDefaultsCache.js +62 -0
  22. package/lib/document.js +2419 -1306
  23. package/lib/drivers/node-mongodb-native/bulkWriteResult.js +5 -0
  24. package/lib/drivers/node-mongodb-native/collection.js +101 -135
  25. package/lib/drivers/node-mongodb-native/connection.js +386 -28
  26. package/lib/drivers/node-mongodb-native/index.js +3 -5
  27. package/lib/error/browserMissingSchema.js +5 -4
  28. package/lib/error/bulkSaveIncompleteError.js +44 -0
  29. package/lib/error/cast.js +30 -21
  30. package/lib/error/createCollectionsError.js +26 -0
  31. package/lib/error/divergentArray.js +11 -9
  32. package/lib/error/eachAsyncMultiError.js +2 -2
  33. package/lib/error/index.js +38 -29
  34. package/lib/error/invalidSchemaOption.js +32 -0
  35. package/lib/error/messages.js +4 -3
  36. package/lib/error/missingSchema.js +8 -6
  37. package/lib/error/notFound.js +7 -5
  38. package/lib/error/objectExpected.js +9 -8
  39. package/lib/error/objectParameter.js +12 -11
  40. package/lib/error/overwriteModel.js +7 -6
  41. package/lib/error/parallelSave.js +11 -8
  42. package/lib/error/parallelValidate.js +9 -7
  43. package/lib/error/serverSelection.js +1 -0
  44. package/lib/error/setOptionError.js +11 -9
  45. package/lib/error/strict.js +12 -10
  46. package/lib/error/strictPopulate.js +11 -9
  47. package/lib/error/syncIndexes.js +2 -2
  48. package/lib/error/validation.js +11 -17
  49. package/lib/error/validator.js +9 -8
  50. package/lib/error/version.js +12 -10
  51. package/lib/filters/operators.js +8 -0
  52. package/lib/helpers/aggregate/prepareDiscriminatorPipeline.js +4 -4
  53. package/lib/helpers/buildMiddlewareFilter.js +42 -0
  54. package/lib/helpers/clone.js +121 -35
  55. package/lib/helpers/common.js +7 -7
  56. package/lib/helpers/cursor/eachAsync.js +14 -11
  57. package/lib/helpers/cursor/waitForInitialization.js +17 -0
  58. package/lib/helpers/discriminator/applyEmbeddedDiscriminators.js +36 -0
  59. package/lib/helpers/discriminator/areDiscriminatorValuesEqual.js +1 -1
  60. package/lib/helpers/discriminator/getConstructor.js +9 -6
  61. package/lib/helpers/discriminator/getDiscriminatorByKey.js +1 -1
  62. package/lib/helpers/discriminator/getDiscriminatorByValue.js +1 -1
  63. package/lib/helpers/discriminator/getDiscriminatorValueFromFilter.js +30 -0
  64. package/lib/helpers/discriminator/getSchemaDiscriminatorByValue.js +1 -1
  65. package/lib/helpers/discriminator/mergeDiscriminatorSchema.js +41 -9
  66. package/lib/helpers/document/applyDefaults.js +82 -14
  67. package/lib/helpers/document/applyTimestamps.js +106 -0
  68. package/lib/helpers/document/applyVirtuals.js +147 -0
  69. package/lib/helpers/document/cleanModifiedSubpaths.js +14 -4
  70. package/lib/helpers/document/compile.js +20 -15
  71. package/lib/helpers/document/getDeepestSubdocumentForPath.js +38 -0
  72. package/lib/helpers/document/getEmbeddedDiscriminatorPath.js +6 -3
  73. package/lib/helpers/document/handleSpreadDoc.js +2 -1
  74. package/lib/helpers/document/isInPathsToSave.js +24 -0
  75. package/lib/helpers/document/toObjectRecursive.js +49 -0
  76. package/lib/helpers/get.js +1 -1
  77. package/lib/helpers/getDefaultBulkwriteResult.js +11 -20
  78. package/lib/helpers/getInheritedProperty.js +28 -0
  79. package/lib/helpers/getOwnProperty.js +19 -0
  80. package/lib/helpers/indexes/applySchemaCollation.js +1 -1
  81. package/lib/helpers/indexes/decorateDiscriminatorIndexOptions.js +1 -1
  82. package/lib/helpers/indexes/getRelatedIndexes.js +8 -4
  83. package/lib/helpers/indexes/isDefaultIdIndex.js +1 -1
  84. package/lib/helpers/indexes/isIndexEqual.js +13 -12
  85. package/lib/helpers/indexes/isIndexSpecEqual.js +32 -0
  86. package/lib/helpers/indexes/isTimeseriesIndex.js +16 -0
  87. package/lib/helpers/isBsonType.js +3 -6
  88. package/lib/helpers/isMongooseDocument.js +24 -0
  89. package/lib/helpers/isMongooseObject.js +6 -4
  90. package/lib/helpers/isObject.js +2 -2
  91. package/lib/helpers/isPOJO.js +12 -0
  92. package/lib/helpers/isSimpleValidator.js +2 -2
  93. package/lib/helpers/minimize.js +41 -0
  94. package/lib/helpers/model/applyDefaultsToPOJO.js +2 -2
  95. package/lib/helpers/model/applyHooks.js +72 -54
  96. package/lib/helpers/model/applyMethods.js +3 -3
  97. package/lib/helpers/model/applyStaticHooks.js +20 -50
  98. package/lib/helpers/model/castBulkWrite.js +278 -172
  99. package/lib/helpers/model/decorateBulkWriteResult.js +8 -0
  100. package/lib/helpers/model/discriminator.js +77 -21
  101. package/lib/helpers/omitUndefined.js +20 -0
  102. package/lib/helpers/parallelLimit.js +61 -34
  103. package/lib/helpers/path/setDottedPath.js +1 -1
  104. package/lib/helpers/pathTrie.js +167 -0
  105. package/lib/helpers/pluralize.js +5 -4
  106. package/lib/helpers/populate/assignRawDocsToIdStructure.js +12 -7
  107. package/lib/helpers/populate/assignVals.js +46 -21
  108. package/lib/helpers/populate/createPopulateQueryFilter.js +13 -5
  109. package/lib/helpers/populate/getModelsMapForPopulate.js +250 -108
  110. package/lib/helpers/populate/getSchemaTypes.js +27 -30
  111. package/lib/helpers/populate/getVirtual.js +31 -0
  112. package/lib/helpers/populate/markArraySubdocsPopulated.js +6 -4
  113. package/lib/helpers/populate/modelNamesFromRefPath.js +11 -5
  114. package/lib/helpers/populate/removeDeselectedForeignField.js +3 -2
  115. package/lib/helpers/populate/setInheritedReadOptions.js +21 -0
  116. package/lib/helpers/populate/setPopulatedVirtualValue.js +33 -0
  117. package/lib/helpers/populate/splitPopulateQuery.js +81 -0
  118. package/lib/helpers/printJestWarning.js +1 -1
  119. package/lib/helpers/processConnectionOptions.js +7 -2
  120. package/lib/helpers/projection/applyProjection.js +8 -2
  121. package/lib/helpers/projection/hasIncludedChildren.js +1 -1
  122. package/lib/helpers/projection/isExclusive.js +8 -6
  123. package/lib/helpers/projection/isInclusive.js +2 -1
  124. package/lib/helpers/projection/isPathExcluded.js +3 -3
  125. package/lib/helpers/projection/isPathSelectedInclusive.js +1 -11
  126. package/lib/helpers/projection/isSubpath.js +1 -1
  127. package/lib/helpers/projection/parseProjection.js +9 -4
  128. package/lib/helpers/query/applyGlobalOption.js +9 -9
  129. package/lib/helpers/query/cast$expr.js +52 -23
  130. package/lib/helpers/query/castFilterPath.js +22 -22
  131. package/lib/helpers/query/castUpdate.js +207 -79
  132. package/lib/helpers/query/cloneMongooseOptions.js +44 -0
  133. package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +27 -13
  134. package/lib/helpers/query/handleImmutable.js +18 -2
  135. package/lib/helpers/query/handleReadPreferenceAliases.js +23 -0
  136. package/lib/helpers/query/sanitizeFilter.js +3 -2
  137. package/lib/helpers/query/selectPopulatedFields.js +13 -0
  138. package/lib/helpers/query/validOps.js +1 -22
  139. package/lib/helpers/schema/applyPlugins.js +2 -2
  140. package/lib/helpers/schema/applyReadConcern.js +20 -0
  141. package/lib/helpers/schema/applyWriteConcern.js +12 -3
  142. package/lib/helpers/schema/cleanPositionalOperators.js +5 -3
  143. package/lib/helpers/schema/getIndexes.js +20 -13
  144. package/lib/helpers/schema/getKeysInSchemaOrder.js +13 -16
  145. package/lib/helpers/schema/getPath.js +11 -3
  146. package/lib/helpers/schema/getSubdocumentStrictValue.js +32 -0
  147. package/lib/helpers/schema/handleIdOption.js +1 -1
  148. package/lib/helpers/schema/idGetter.js +7 -2
  149. package/lib/helpers/schema/merge.js +9 -1
  150. package/lib/helpers/schematype/getConditionalHandler.js +23 -0
  151. package/lib/helpers/schematype/handleImmutable.js +1 -1
  152. package/lib/helpers/setDefaultsOnInsert.js +85 -43
  153. package/lib/helpers/symbols.js +4 -4
  154. package/lib/helpers/timestamps/setDocumentTimestamps.js +2 -2
  155. package/lib/helpers/timestamps/setupTimestamps.js +69 -37
  156. package/lib/helpers/update/applyTimestampsToChildren.js +1 -1
  157. package/lib/helpers/update/applyTimestampsToUpdate.js +53 -37
  158. package/lib/helpers/update/castArrayFilters.js +68 -29
  159. package/lib/helpers/update/decorateUpdateWithVersionKey.js +35 -0
  160. package/lib/helpers/update/modifiedPaths.js +2 -2
  161. package/lib/helpers/update/removeUnusedArrayFilters.js +11 -2
  162. package/lib/helpers/updateValidators.js +87 -128
  163. package/lib/index.js +6 -1320
  164. package/lib/internal.js +7 -1
  165. package/lib/model.js +2587 -2618
  166. package/lib/modifiedPathsSnapshot.js +9 -0
  167. package/lib/mongoose.js +1456 -0
  168. package/lib/options/saveOptions.js +2 -0
  169. package/lib/options/{SchemaArrayOptions.js → schemaArrayOptions.js} +16 -5
  170. package/lib/options/{SchemaBufferOptions.js → schemaBufferOptions.js} +2 -2
  171. package/lib/options/{SchemaDateOptions.js → schemaDateOptions.js} +1 -1
  172. package/lib/options/{SchemaDocumentArrayOptions.js → schemaDocumentArrayOptions.js} +24 -1
  173. package/lib/options/{SchemaMapOptions.js → schemaMapOptions.js} +1 -1
  174. package/lib/options/{SchemaNumberOptions.js → schemaNumberOptions.js} +5 -5
  175. package/lib/options/{SchemaObjectIdOptions.js → schemaObjectIdOptions.js} +4 -4
  176. package/lib/options/{SchemaStringOptions.js → schemaStringOptions.js} +10 -10
  177. package/lib/options/schemaSubdocumentOptions.js +89 -0
  178. package/lib/options/{SchemaTypeOptions.js → schemaTypeOptions.js} +28 -14
  179. package/lib/options/schemaUnionOptions.js +32 -0
  180. package/lib/options/{VirtualOptions.js → virtualOptions.js} +13 -13
  181. package/lib/options.js +4 -1
  182. package/lib/plugins/index.js +0 -2
  183. package/lib/plugins/saveSubdocs.js +85 -50
  184. package/lib/plugins/sharding.js +43 -15
  185. package/lib/plugins/trackTransaction.js +46 -34
  186. package/lib/query.js +1844 -1909
  187. package/lib/{queryhelpers.js → queryHelpers.js} +184 -81
  188. package/lib/schema/array.js +229 -169
  189. package/lib/schema/bigint.js +281 -0
  190. package/lib/schema/boolean.js +90 -26
  191. package/lib/schema/buffer.js +119 -50
  192. package/lib/schema/date.js +102 -38
  193. package/lib/schema/decimal128.js +88 -37
  194. package/lib/schema/{documentarray.js → documentArray.js} +259 -151
  195. package/lib/schema/documentArrayElement.js +180 -0
  196. package/lib/schema/double.js +244 -0
  197. package/lib/schema/index.js +12 -19
  198. package/lib/schema/int32.js +288 -0
  199. package/lib/schema/map.js +129 -20
  200. package/lib/schema/mixed.js +40 -26
  201. package/lib/schema/number.js +128 -51
  202. package/lib/schema/{objectid.js → objectId.js} +89 -47
  203. package/lib/schema/operators/exists.js +1 -1
  204. package/lib/schema/operators/geospatial.js +2 -2
  205. package/lib/schema/operators/text.js +4 -4
  206. package/lib/schema/string.js +125 -66
  207. package/lib/schema/subdocument.js +443 -0
  208. package/lib/schema/symbols.js +2 -0
  209. package/lib/schema/union.js +167 -0
  210. package/lib/schema/uuid.js +90 -120
  211. package/lib/schema.js +1267 -388
  212. package/lib/{schematype.js → schemaType.js} +481 -246
  213. package/lib/standardSchema/convertErrorToIssues.js +20 -0
  214. package/lib/{statemachine.js → stateMachine.js} +66 -17
  215. package/lib/tracing.js +38 -0
  216. package/lib/types/array/index.js +13 -10
  217. package/lib/types/array/methods/index.js +243 -63
  218. package/lib/types/{ArraySubdocument.js → arraySubdocument.js} +35 -14
  219. package/lib/types/buffer.js +31 -14
  220. package/lib/types/decimal128.js +1 -1
  221. package/lib/types/{DocumentArray → documentArray}/index.js +13 -13
  222. package/lib/types/{DocumentArray → documentArray}/methods/index.js +197 -31
  223. package/lib/types/double.js +13 -0
  224. package/lib/types/index.js +5 -2
  225. package/lib/types/map.js +62 -25
  226. package/lib/types/objectid.js +2 -2
  227. package/lib/types/subdocument.js +79 -58
  228. package/lib/types/uuid.js +13 -0
  229. package/lib/utils.js +201 -155
  230. package/lib/{validoptions.js → validOptions.js} +10 -2
  231. package/lib/{virtualtype.js → virtualType.js} +47 -18
  232. package/package.json +75 -92
  233. package/types/aggregate.d.ts +31 -7
  234. package/types/augmentations.d.ts +2 -2
  235. package/types/collection.d.ts +21 -16
  236. package/types/connection.d.ts +82 -27
  237. package/types/cursor.d.ts +15 -10
  238. package/types/document.d.ts +137 -56
  239. package/types/error.d.ts +11 -6
  240. package/types/expressions.d.ts +33 -2
  241. package/types/index.d.ts +805 -190
  242. package/types/indexes.d.ts +85 -13
  243. package/types/inferhydrateddoctype.d.ts +151 -0
  244. package/types/inferrawdoctype.d.ts +201 -0
  245. package/types/inferschematype.d.ts +287 -128
  246. package/types/middlewares.d.ts +50 -7
  247. package/types/models.d.ts +1555 -268
  248. package/types/mongooseoptions.d.ts +56 -7
  249. package/types/pipelinestage.d.ts +42 -3
  250. package/types/populate.d.ts +63 -1
  251. package/types/query.d.ts +592 -255
  252. package/types/schemaoptions.d.ts +78 -12
  253. package/types/schematypes.d.ts +254 -65
  254. package/types/session.d.ts +0 -4
  255. package/types/tracing.d.ts +10 -0
  256. package/types/types.d.ts +25 -8
  257. package/types/utility.d.ts +329 -30
  258. package/types/validation.d.ts +19 -14
  259. package/types/virtuals.d.ts +10 -10
  260. package/.eslintrc.js +0 -232
  261. package/.mocharc.yml +0 -4
  262. package/browser.js +0 -8
  263. package/lgtm.yml +0 -12
  264. package/lib/browser.js +0 -158
  265. package/lib/browserDocument.js +0 -101
  266. package/lib/cursor/ChangeStream.js +0 -154
  267. package/lib/cursor/QueryCursor.js +0 -545
  268. package/lib/document_provider.js +0 -30
  269. package/lib/drivers/browser/ReadPreference.js +0 -7
  270. package/lib/drivers/browser/binary.js +0 -14
  271. package/lib/drivers/browser/decimal128.js +0 -7
  272. package/lib/drivers/browser/index.js +0 -16
  273. package/lib/drivers/browser/objectid.js +0 -29
  274. package/lib/drivers/node-mongodb-native/ReadPreference.js +0 -47
  275. package/lib/drivers/node-mongodb-native/binary.js +0 -10
  276. package/lib/drivers/node-mongodb-native/decimal128.js +0 -7
  277. package/lib/drivers/node-mongodb-native/objectid.js +0 -16
  278. package/lib/error/disconnected.js +0 -33
  279. package/lib/helpers/printStrictQueryWarning.js +0 -11
  280. package/lib/helpers/promiseOrCallback.js +0 -55
  281. package/lib/helpers/query/applyQueryMiddleware.js +0 -79
  282. package/lib/helpers/query/completeMany.js +0 -52
  283. package/lib/helpers/query/wrapThunk.js +0 -36
  284. package/lib/options/SchemaSubdocumentOptions.js +0 -42
  285. package/lib/options/removeOptions.js +0 -14
  286. package/lib/plugins/removeSubdocs.js +0 -31
  287. package/lib/plugins/validateBeforeSave.js +0 -45
  288. package/lib/promise_provider.js +0 -49
  289. package/lib/schema/DocumentArrayElement.js +0 -100
  290. package/lib/schema/SubdocumentPath.js +0 -369
  291. package/scripts/build-browser.js +0 -18
  292. package/scripts/create-tarball.js +0 -7
  293. package/scripts/generateSearch.js +0 -154
  294. package/scripts/tsc-diagnostics-check.js +0 -15
  295. package/tools/auth.js +0 -31
  296. package/tools/repl.js +0 -35
  297. package/tools/sharded.js +0 -46
  298. package/tsconfig.json +0 -9
  299. /package/lib/{connectionstate.js → connectionState.js} +0 -0
  300. /package/lib/helpers/populate/{SkipPopulateValue.js → skipPopulateValue.js} +0 -0
  301. /package/lib/options/{PopulateOptions.js → populateOptions.js} +0 -0
  302. /package/lib/types/{DocumentArray → documentArray}/isMongooseDocumentArray.js +0 -0
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ const ValidationError = require('../error/validation');
4
+
5
+ module.exports = function convertErrorToIssues(error) {
6
+ if (error instanceof ValidationError) {
7
+ return Object.keys(error.errors).map(path => {
8
+ const err = error.errors[path];
9
+ return {
10
+ message: err.message,
11
+ path: path.split('.').map(part => {
12
+ const num = +part;
13
+ return Number.isInteger(num) && String(num) === part ? num : part;
14
+ })
15
+ };
16
+ });
17
+ }
18
+
19
+ return [{ message: error.message }];
20
+ };
@@ -25,8 +25,7 @@ const StateMachine = module.exports = exports = function StateMachine() {
25
25
  * methods named after each state. These transition methods
26
26
  * place their path argument into the given state.
27
27
  *
28
- * @param {String} state
29
- * @param {String} [state]
28
+ * @param {...string} state one or more state names
30
29
  * @return {Function} subclass constructor
31
30
  * @api private
32
31
  */
@@ -35,12 +34,12 @@ StateMachine.ctor = function() {
35
34
  const states = [...arguments];
36
35
 
37
36
  const ctor = function() {
38
- StateMachine.apply(this, arguments);
39
37
  this.paths = {};
40
38
  this.states = {};
41
39
  };
42
40
 
43
41
  ctor.prototype = new StateMachine();
42
+ ctor.prototype.constructor = ctor;
44
43
 
45
44
  ctor.prototype.stateNames = states;
46
45
 
@@ -65,12 +64,43 @@ StateMachine.ctor = function() {
65
64
  */
66
65
 
67
66
  StateMachine.prototype._changeState = function _changeState(path, nextState) {
68
- const prevBucket = this.states[this.paths[path]];
69
- if (prevBucket) delete prevBucket[path];
67
+ const prevState = this.paths[path];
68
+ if (prevState === nextState) {
69
+ return;
70
+ }
71
+ if (prevState !== undefined) {
72
+ const prevBucket = this.states[prevState];
73
+ if (prevBucket) delete prevBucket[path];
74
+ }
70
75
 
71
76
  this.paths[path] = nextState;
72
- this.states[nextState] = this.states[nextState] || {};
73
- this.states[nextState][path] = true;
77
+ let bucket = this.states[nextState];
78
+ if (bucket == null) {
79
+ bucket = this.states[nextState] = {};
80
+ }
81
+ bucket[path] = true;
82
+ };
83
+
84
+ /*!
85
+ * ignore
86
+ */
87
+
88
+ StateMachine.prototype.clearAllExcept = function clearAllExcept(state) {
89
+ // State buckets are created lazily, so `states[state]` may not exist yet.
90
+ const bucket = this.states[state];
91
+ const keys = bucket == null ? [] : Object.keys(bucket);
92
+ if (keys.length === 0) {
93
+ this.paths = {};
94
+ this.states = {};
95
+ return;
96
+ }
97
+ this.paths = {};
98
+ for (const path of keys) {
99
+ this.paths[path] = state;
100
+ }
101
+ this.states = {
102
+ [state]: this.states[state]
103
+ };
74
104
  };
75
105
 
76
106
  /*!
@@ -82,13 +112,14 @@ StateMachine.prototype.clear = function clear(state) {
82
112
  return;
83
113
  }
84
114
  const keys = Object.keys(this.states[state]);
115
+ if (keys.length === 0) {
116
+ return;
117
+ }
118
+ this.states[state] = {};
85
119
  let i = keys.length;
86
- let path;
87
120
 
88
121
  while (i--) {
89
- path = keys[i];
90
- delete this.states[state][path];
91
- delete this.paths[path];
122
+ delete this.paths[keys[i]];
92
123
  }
93
124
  };
94
125
 
@@ -121,7 +152,7 @@ StateMachine.prototype.getStatePaths = function getStatePaths(state) {
121
152
  * Checks to see if at least one path is in the states passed in via `arguments`
122
153
  * e.g., this.some('required', 'inited')
123
154
  *
124
- * @param {String} state that we want to check for.
155
+ * @param {string} state that we want to check for.
125
156
  * @api private
126
157
  */
127
158
 
@@ -140,7 +171,7 @@ StateMachine.prototype.some = function some() {
140
171
  * This function builds the functions that get assigned to `forEach` and `map`,
141
172
  * since both of those methods share a lot of the same logic.
142
173
  *
143
- * @param {String} iterMethod is either 'forEach' or 'map'
174
+ * @param {string} iterMethod is either 'forEach' or 'map'
144
175
  * @return {Function}
145
176
  * @api private
146
177
  */
@@ -175,8 +206,7 @@ StateMachine.prototype._iter = function _iter(iterMethod) {
175
206
  * this.forEach(state1, state2, fn); // iterates over all paths in state1 or state2
176
207
  * this.forEach(fn); // iterates over all paths in all states
177
208
  *
178
- * @param {String} [state]
179
- * @param {String} [state]
209
+ * @param {...string} [state] one or more state names to filter by
180
210
  * @param {Function} callback
181
211
  * @api private
182
212
  */
@@ -194,8 +224,7 @@ StateMachine.prototype.forEach = function forEach() {
194
224
  * this.forEach(state1, state2, fn); // iterates over all paths in state1 or state2
195
225
  * this.forEach(fn); // iterates over all paths in all states
196
226
  *
197
- * @param {String} [state]
198
- * @param {String} [state]
227
+ * @param {...string} [state] one or more state names to filter by
199
228
  * @param {Function} callback
200
229
  * @return {Array}
201
230
  * @api private
@@ -205,3 +234,23 @@ StateMachine.prototype.map = function map() {
205
234
  this.map = this._iter('map');
206
235
  return this.map.apply(this, arguments);
207
236
  };
237
+
238
+ /**
239
+ * Returns a copy of this state machine
240
+ *
241
+ * @param {Function} callback
242
+ * @return {StateMachine}
243
+ * @api private
244
+ */
245
+
246
+ StateMachine.prototype.clone = function clone() {
247
+ const result = new this.constructor();
248
+ result.paths = { ...this.paths };
249
+ for (const state of this.stateNames) {
250
+ if (!(state in this.states)) {
251
+ continue;
252
+ }
253
+ result.states[state] = this.states[state] == null ? this.states[state] : { ...this.states[state] };
254
+ }
255
+ return result;
256
+ };
package/lib/tracing.js ADDED
@@ -0,0 +1,38 @@
1
+ 'use strict';
2
+
3
+ let dc;
4
+ try {
5
+ dc = (typeof process !== 'undefined' && 'getBuiltinModule' in process)
6
+ ? process.getBuiltinModule('node:diagnostics_channel')
7
+ : require('node:diagnostics_channel');
8
+ } catch {
9
+ // diagnostics_channel not available
10
+ }
11
+
12
+ const hasTracingChannel = !!dc && typeof dc.tracingChannel === 'function';
13
+
14
+ function shouldTrace(channel) {
15
+ // Node 18 and Bun don't expose hasSubscribers on TracingChannel
16
+ // so undefined means it may or may not have subscribers, so we'll trace anyway
17
+ return !!channel && channel.hasSubscribers !== false;
18
+ }
19
+
20
+ function createTracedChannel(name) {
21
+ const ch = hasTracingChannel ? dc.tracingChannel(name) : undefined;
22
+
23
+ function trace(fn, contextFactory) {
24
+ if (!shouldTrace(ch)) {
25
+ return fn();
26
+ }
27
+
28
+ const traced = ch.tracePromise(fn, contextFactory());
29
+ return traced;
30
+ }
31
+
32
+ return { channel: ch, trace };
33
+ }
34
+
35
+ module.exports = {
36
+ createTracedChannel,
37
+ cursorNextChannel: createTracedChannel('mongoose:cursor:next')
38
+ };
@@ -4,7 +4,6 @@
4
4
 
5
5
  'use strict';
6
6
 
7
- const Document = require('../../document');
8
7
  const mongooseArrayMethods = require('./methods');
9
8
 
10
9
  const arrayAtomicsSymbol = require('../../helpers/symbols').arrayAtomicsSymbol;
@@ -21,7 +20,7 @@ const arraySchemaSymbol = require('../../helpers/symbols').arraySchemaSymbol;
21
20
  * _Values always have to be passed to the constructor to initialize, otherwise `MongooseArray#push` will mark the array as modified._
22
21
  *
23
22
  * @param {Array} values
24
- * @param {String} path
23
+ * @param {string} path
25
24
  * @param {Document} doc parent document
26
25
  * @api private
27
26
  * @inherits Array https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
@@ -73,31 +72,35 @@ function MongooseArray(values, path, doc, schematype) {
73
72
  internals[arrayAtomicsSymbol] = values[arrayAtomicsSymbol];
74
73
  }
75
74
 
76
- // Because doc comes from the context of another function, doc === global
77
- // can happen if there was a null somewhere up the chain (see #3020)
78
- // RB Jun 17, 2015 updated to check for presence of expected paths instead
79
- // to make more proof against unusual node environments
80
- if (doc != null && doc instanceof Document) {
75
+ if (doc?.$__) {
81
76
  internals[arrayParentSymbol] = doc;
82
77
  internals[arraySchemaSymbol] = schematype || doc.schema.path(path);
83
78
  }
84
79
 
85
80
  const proxy = new Proxy(__array, {
86
81
  get: function(target, prop) {
87
- if (internals.hasOwnProperty(prop)) {
82
+ if (Object.hasOwn(internals, prop)) {
88
83
  return internals[prop];
89
84
  }
90
- if (mongooseArrayMethods.hasOwnProperty(prop)) {
85
+ if (Object.hasOwn(mongooseArrayMethods, prop)) {
91
86
  return mongooseArrayMethods[prop];
92
87
  }
88
+ if (schematype?.virtuals && Object.hasOwn(schematype.virtuals, prop)) {
89
+ return schematype.virtuals[prop].applyGetters(undefined, target);
90
+ }
91
+ if (typeof prop === 'string' && numberRE.test(prop) && schematype?.embeddedSchemaType != null) {
92
+ return schematype.embeddedSchemaType.applyGetters(__array[prop], doc);
93
+ }
93
94
 
94
95
  return __array[prop];
95
96
  },
96
97
  set: function(target, prop, value) {
97
98
  if (typeof prop === 'string' && numberRE.test(prop)) {
98
99
  mongooseArrayMethods.set.call(proxy, prop, value, false);
99
- } else if (internals.hasOwnProperty(prop)) {
100
+ } else if (Object.hasOwn(internals, prop)) {
100
101
  internals[prop] = value;
102
+ } else if (schematype?.virtuals && Object.hasOwn(schematype.virtuals, prop)) {
103
+ schematype.virtuals[prop].applySetters(value, target);
101
104
  } else {
102
105
  __array[prop] = value;
103
106
  }