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

Function applySchemaTypeTransforms

lib/document.js:4649–4688  ·  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

4647 */
4648
4649function applySchemaTypeTransforms(self, json) {
4650 const schema = self.$__schema;
4651 const paths = schema.pathsWithTransforms();
4652 const cur = self._doc;
4653
4654 if (!cur || !paths) {
4655 return json;
4656 }
4657
4658 for (const path of paths) {
4659 const schematype = schema.paths[path];
4660 const topLevelTransformFunction = schematype.options.transform ?? schematype.constructor?.defaultOptions?.transform;
4661 const embeddedSchemaTypeTransformFunction = schematype.embeddedSchemaType?.options?.transform
4662 ?? schematype.embeddedSchemaType?.constructor?.defaultOptions?.transform;
4663 if (typeof topLevelTransformFunction === 'function') {
4664 const val = self.$get(path);
4665 if (val === undefined) {
4666 continue;
4667 }
4668 const transformedValue = topLevelTransformFunction.call(self, val);
4669 throwErrorIfPromise(path, transformedValue);
4670 utils.setValue(path, transformedValue, json);
4671 } else if (typeof embeddedSchemaTypeTransformFunction === 'function') {
4672 const val = self.$get(path);
4673 if (val === undefined) {
4674 continue;
4675 }
4676 const vals = [].concat(val);
4677 for (let i = 0; i < vals.length; ++i) {
4678 const transformedValue = embeddedSchemaTypeTransformFunction.call(self, vals[i]);
4679 vals[i] = transformedValue;
4680 throwErrorIfPromise(path, transformedValue);
4681 }
4682
4683 json[path] = vals;
4684 }
4685 }
4686
4687 return json;
4688}
4689
4690function throwErrorIfPromise(path, transformedValue) {
4691 if (isPromise(transformedValue)) {

Callers 1

document.jsFile · 0.85

Calls 1

throwErrorIfPromiseFunction · 0.85

Tested by

no test coverage detected