MCPcopy
hub / github.com/Automattic/mongoose / _insertMany

Function _insertMany

lib/model.js:3045–3281  ·  view source on GitHub ↗
(arr, options)

Source from the content-addressed store, hash-verified

3043};
3044
3045async function _insertMany(arr, options) {
3046 options = options || {};
3047 const preFilter = buildMiddlewareFilter(options, 'pre');
3048 const postFilter = buildMiddlewareFilter(options, 'post');
3049
3050 try {
3051 [arr] = await this._middleware.execPre('insertMany', this, [arr], { filter: preFilter });
3052 } catch (error) {
3053 await this._middleware.execPost('insertMany', this, [arr], { error, filter: postFilter });
3054 }
3055 const ThisModel = this;
3056 const limit = options.limit || 1000;
3057 const rawResult = !!options.rawResult;
3058 const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
3059 const throwOnValidationError = typeof options.throwOnValidationError === 'boolean' ? options.throwOnValidationError : false;
3060 const lean = !!options.lean;
3061
3062 const asyncLocalStorage = this.db.base.transactionAsyncLocalStorage?.getStore();
3063 if ((!options || !Object.hasOwn(options, 'session')) && asyncLocalStorage?.session != null) {
3064 options = { ...options, session: asyncLocalStorage.session };
3065 }
3066
3067 if (!Array.isArray(arr)) {
3068 arr = [arr];
3069 }
3070
3071 const validationErrors = [];
3072 const validationErrorsToOriginalOrder = new Map();
3073 const results = ordered ? null : new Array(arr.length);
3074 async function validateDoc(doc, index) {
3075 // If option `lean` is set to true bypass validation and hydration
3076 if (lean) {
3077 return doc;
3078 }
3079 let createdNewDoc = false;
3080 if (!(doc instanceof ThisModel)) {
3081 if (doc != null && typeof doc !== 'object') {
3082 throw new ObjectParameterError(doc, 'arr.' + index, 'insertMany');
3083 }
3084 doc = new ThisModel(doc);
3085 createdNewDoc = true;
3086 }
3087
3088 if (options.session != null) {
3089 doc.$session(options.session);
3090 }
3091 return doc.$validate(createdNewDoc ? { _skipParallelValidateCheck: true } : null)
3092 .then(() => doc)
3093 .catch(error => {
3094 if (ordered === false) {
3095 error.index = index;
3096 validationErrors.push(error);
3097 validationErrorsToOriginalOrder.set(error, index);
3098 results[index] = error;
3099 return;
3100 }
3101 throw error;
3102 });

Callers

nothing calls this directly

Calls 8

buildMiddlewareFilterFunction · 0.85
parallelLimitFunction · 0.85
_setIsNewFunction · 0.85
getMethod · 0.80
toObjectMethod · 0.80
populateMethod · 0.80
insertManyMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…