monastery 3.0.18 → 3.0.19
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.
- package/changelog.md +2 -0
- package/lib/model-crud.js +52 -29
- package/lib/util.js +1 -2
- package/package.json +1 -1
package/changelog.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [3.0.19](https://github.com/boycce/monastery/compare/3.0.18...3.0.19) (2024-05-05)
|
|
6
|
+
|
|
5
7
|
### [3.0.18](https://github.com/boycce/monastery/compare/3.0.16...3.0.18) (2024-05-02)
|
|
6
8
|
|
|
7
9
|
### [3.0.16](https://github.com/boycce/monastery/compare/3.0.15...3.0.16) (2024-05-02)
|
package/lib/model-crud.js
CHANGED
|
@@ -579,7 +579,7 @@ Model.prototype._processAfterFind = function (data, projection={}, afterFindCont
|
|
|
579
579
|
let callbackSeries = []
|
|
580
580
|
let models = this.manager.models
|
|
581
581
|
let parent = util.toArray(data).map(o => ({ dataRef: o, dataPath: '', dataFieldName: '', modelName: this.name }))
|
|
582
|
-
let modelFields = this._recurseAndFindModels(
|
|
582
|
+
let modelFields = this._recurseAndFindModels(data).concat(parent)
|
|
583
583
|
|
|
584
584
|
// Loop found model/deep-model data objects, and populate missing default-fields and call afterFind on each
|
|
585
585
|
for (let item of modelFields) {
|
|
@@ -612,12 +612,12 @@ Model.prototype._processAfterFind = function (data, projection={}, afterFindCont
|
|
|
612
612
|
return util.runSeries.call(this, callbackSeries, 'afterFind').then(() => data)
|
|
613
613
|
}
|
|
614
614
|
|
|
615
|
-
Model.prototype._recurseAndFindModels = function (
|
|
615
|
+
Model.prototype._recurseAndFindModels = function (dataArr, dataParentPath='', modelPaths) {
|
|
616
616
|
/**
|
|
617
617
|
* Returns a flattened list of models fields
|
|
618
|
-
* @param {string} parentPath
|
|
619
|
-
* @param {object} schemaFields - schema fields object
|
|
620
618
|
* @param {object|array} dataArr
|
|
619
|
+
* @param {string} <dataParentPath>
|
|
620
|
+
* @this Model
|
|
621
621
|
* @return [{
|
|
622
622
|
* dataRef: { *fields here* },
|
|
623
623
|
* dataPath: 'usersNewCompany',
|
|
@@ -626,44 +626,67 @@ Model.prototype._recurseAndFindModels = function (parentPath, schemaFields, data
|
|
|
626
626
|
* },..]
|
|
627
627
|
*/
|
|
628
628
|
let out = []
|
|
629
|
+
let dataParentPath2 = dataParentPath.replace(/\.[0-9]+(\.|$)/g, '$1')
|
|
630
|
+
|
|
631
|
+
// Get all model fields
|
|
632
|
+
if (!modelPaths) {
|
|
633
|
+
modelPaths = Object.keys(this.fieldsFlattened).reduce((acc, k) => {
|
|
634
|
+
if (this.fieldsFlattened[k].model) {
|
|
635
|
+
acc.push({ k: k, k2: k.replace(/\.[0-9]+(\.|$)/g, '$1') }) // e.g. [{k: 'pets.0.dog', 'k2': 'pets.dog'}, ...]
|
|
636
|
+
}
|
|
637
|
+
return acc
|
|
638
|
+
}, [])
|
|
639
|
+
}
|
|
640
|
+
|
|
629
641
|
for (let data of util.toArray(dataArr)) {
|
|
630
|
-
|
|
631
|
-
if (!data
|
|
632
|
-
const
|
|
633
|
-
|
|
642
|
+
for (let key in data) {
|
|
643
|
+
if (!data[key]) continue
|
|
644
|
+
const dataPath = dataParentPath ? `${dataParentPath}.${key}` : key
|
|
645
|
+
const dataPath2 = dataParentPath2 ? `${dataParentPath2}.${key}` : key
|
|
646
|
+
|
|
647
|
+
// modelPath has to contain data path
|
|
648
|
+
const modelPath = modelPaths.find(o => o.k2.match(new RegExp('^' + dataPath2 + '(\\.|$)')))
|
|
649
|
+
if (!modelPath) continue
|
|
650
|
+
|
|
651
|
+
// Exact path match
|
|
652
|
+
const pathMatch = modelPath.k2 == dataPath2
|
|
634
653
|
|
|
635
|
-
//
|
|
636
|
-
if (util.
|
|
637
|
-
out = [...out, ...this._recurseAndFindModels(newParentPath, field, data[fieldName])]
|
|
654
|
+
// Ignore id values
|
|
655
|
+
if (util.isId(data[key])) continue
|
|
638
656
|
|
|
657
|
+
// Recurse through sub-document fields
|
|
658
|
+
if (!pathMatch && util.isSubdocument(data[key])) {
|
|
659
|
+
out = [...out, ...this._recurseAndFindModels(data[key], dataPath, modelPaths)]
|
|
660
|
+
|
|
639
661
|
// Recurse through array of sub-documents
|
|
640
|
-
} else if (
|
|
641
|
-
for (let i=0, l=data[
|
|
642
|
-
out = [...out, ...this._recurseAndFindModels(
|
|
662
|
+
} else if (!pathMatch && util.isSubdocument((data[key]||{})[0])) {
|
|
663
|
+
for (let i=0, l=data[key].length; i<l; i++) {
|
|
664
|
+
out = [...out, ...this._recurseAndFindModels(data[key][i], dataPath + '.' + i, modelPaths)]
|
|
643
665
|
}
|
|
644
666
|
|
|
645
|
-
//
|
|
646
|
-
} else if (
|
|
667
|
+
// Array of data models (schema field can be either a single or array of models, due to custom $lookup's)
|
|
668
|
+
} else if (pathMatch && util.isObject(data[key])) {
|
|
647
669
|
out.push({
|
|
648
|
-
dataRef: data[
|
|
649
|
-
dataPath:
|
|
650
|
-
dataFieldName:
|
|
651
|
-
modelName:
|
|
670
|
+
dataRef: data[key],
|
|
671
|
+
dataPath: dataPath,
|
|
672
|
+
dataFieldName: key,
|
|
673
|
+
modelName: this.fieldsFlattened[modelPath.k].model,
|
|
652
674
|
})
|
|
653
|
-
|
|
675
|
+
|
|
654
676
|
// Array of data models (schema field can be either a single or array of models, due to custom $lookup's)
|
|
655
|
-
} else if (util.
|
|
656
|
-
for (let i=0, l=data[
|
|
677
|
+
} else if (pathMatch && util.isObject(data[key][0])) {
|
|
678
|
+
for (let i=0, l=data[key].length; i<l; i++) {
|
|
657
679
|
out.push({
|
|
658
|
-
dataRef: data[
|
|
659
|
-
dataPath:
|
|
660
|
-
dataFieldName:
|
|
661
|
-
modelName:
|
|
680
|
+
dataRef: data[key][i],
|
|
681
|
+
dataPath: dataPath + '.' + i,
|
|
682
|
+
dataFieldName: key,
|
|
683
|
+
modelName: this.fieldsFlattened[modelPath.k].model,
|
|
662
684
|
})
|
|
663
685
|
}
|
|
664
|
-
}
|
|
665
|
-
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
666
688
|
}
|
|
689
|
+
|
|
667
690
|
return out
|
|
668
691
|
}
|
|
669
692
|
|
package/lib/util.js
CHANGED
|
@@ -110,8 +110,7 @@ module.exports = {
|
|
|
110
110
|
isId: function(value) {
|
|
111
111
|
// Called from db.isId
|
|
112
112
|
// True if value is a MongoDB ObjectId() or a valid id string
|
|
113
|
-
if (
|
|
114
|
-
else if (value && this.isString(value) && ObjectId.isValid(value)) return true
|
|
113
|
+
if (ObjectId.isValid(value) && typeof value !== 'number') return true
|
|
115
114
|
else return false
|
|
116
115
|
},
|
|
117
116
|
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "monastery",
|
|
3
3
|
"description": "⛪ A simple, straightforward MongoDB ODM",
|
|
4
4
|
"author": "Ricky Boyce",
|
|
5
|
-
"version": "3.0.
|
|
5
|
+
"version": "3.0.19",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:boycce/monastery",
|
|
8
8
|
"homepage": "https://boycce.github.io/monastery/",
|