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

Function applySchemaTypeTransforms

lib/document.js:4552–4591  ·  view source on GitHub ↗

* Applies schema type transforms to `json`. * * @param {Document} self * @param {object} json * @return {object} `json` * @api private

(self, json)

Source from the content-addressed store, hash-verified

4550 */
4551
4552function applySchemaTypeTransforms(self, json) {
4553 const schema = self.$__schema;
4554 const paths = schema.pathsWithTransforms();
4555 const cur = self._doc;
4556
4557 if (!cur || !paths) {
4558 return json;
4559 }
4560
4561 for (const path of paths) {
4562 const schematype = schema.paths[path];
4563 const topLevelTransformFunction = schematype.options.transform ?? schematype.constructor?.defaultOptions?.transform;
4564 const embeddedSchemaTypeTransformFunction = schematype.embeddedSchemaType?.options?.transform
4565 ?? schematype.embeddedSchemaType?.constructor?.defaultOptions?.transform;
4566 if (typeof topLevelTransformFunction === 'function') {
4567 const val = self.$get(path);
4568 if (val === undefined) {
4569 continue;
4570 }
4571 const transformedValue = topLevelTransformFunction.call(self, val);
4572 throwErrorIfPromise(path, transformedValue);
4573 utils.setValue(path, transformedValue, json);
4574 } else if (typeof embeddedSchemaTypeTransformFunction === 'function') {
4575 const val = self.$get(path);
4576 if (val === undefined) {
4577 continue;
4578 }
4579 const vals = [].concat(val);
4580 for (let i = 0; i < vals.length; ++i) {
4581 const transformedValue = embeddedSchemaTypeTransformFunction.call(self, vals[i]);
4582 vals[i] = transformedValue;
4583 throwErrorIfPromise(path, transformedValue);
4584 }
4585
4586 json[path] = vals;
4587 }
4588 }
4589
4590 return json;
4591}
4592
4593function throwErrorIfPromise(path, transformedValue) {
4594 if (isPromise(transformedValue)) {

Callers 1

document.jsFile · 0.85

Calls 1

throwErrorIfPromiseFunction · 0.85

Tested by

no test coverage detected