MCPcopy Create free account
hub / github.com/Automattic/mongoose / MongooseDocumentArray

Function MongooseDocumentArray

lib/types/documentArray/index.js:30–107  ·  view source on GitHub ↗

* DocumentArray constructor * * @param {Array} values * @param {string} path the path to this array * @param {Document} doc parent document * @api private * @return {MongooseDocumentArray} * @inherits MongooseArray * @see https://bit.ly/f6CnZU

(values, path, doc, schematype)

Source from the content-addressed store, hash-verified

28 */
29
30function MongooseDocumentArray(values, path, doc, schematype) {
31 const __array = [];
32
33 const internals = {
34 [arrayAtomicsSymbol]: {},
35 [arrayAtomicsBackupSymbol]: void 0,
36 [arrayPathSymbol]: path,
37 [arraySchemaSymbol]: void 0,
38 [arrayParentSymbol]: void 0
39 };
40
41 if (Array.isArray(values)) {
42 if (values[arrayPathSymbol] === path &&
43 values[arrayParentSymbol] === doc) {
44 internals[arrayAtomicsSymbol] = Object.assign({}, values[arrayAtomicsSymbol]);
45 }
46 values.forEach(v => {
47 _basePush.call(__array, v);
48 });
49 }
50 internals[arrayPathSymbol] = path;
51 internals.__array = __array;
52
53 if (doc?.$__) {
54 internals[arrayParentSymbol] = doc;
55 internals[arraySchemaSymbol] = doc.$__schema.path(path);
56
57 // `schema.path()` doesn't drill into nested arrays properly yet, see
58 // gh-6398, gh-6602. This is a workaround because nested arrays are
59 // always plain non-document arrays, so once you get to a document array
60 // nesting is done. Matryoshka code.
61 while (internals[arraySchemaSymbol] != null &&
62 internals[arraySchemaSymbol].$isMongooseArray &&
63 !internals[arraySchemaSymbol].$isMongooseDocumentArray) {
64 internals[arraySchemaSymbol] = internals[arraySchemaSymbol].embeddedSchemaType;
65 }
66 }
67
68 const proxy = new Proxy(__array, {
69 get: function(target, prop) {
70 if (prop === 'isMongooseArray' ||
71 prop === 'isMongooseArrayProxy' ||
72 prop === 'isMongooseDocumentArray' ||
73 prop === 'isMongooseDocumentArrayProxy') {
74 return true;
75 }
76 if (Object.hasOwn(internals, prop)) {
77 return internals[prop];
78 }
79 if (Object.hasOwn(DocumentArrayMethods, prop)) {
80 return DocumentArrayMethods[prop];
81 }
82 if (schematype?.virtuals && Object.hasOwn(schematype.virtuals, prop)) {
83 return schematype.virtuals[prop].applyGetters(undefined, target);
84 }
85 if (Object.hasOwn(ArrayMethods, prop)) {
86 return ArrayMethods[prop];
87 }

Callers

nothing calls this directly

Calls 2

testMethod · 0.80
applySettersMethod · 0.80

Tested by

no test coverage detected