Skip to content

Instantly share code, notes, and snippets.

@GSchutz
Last active April 28, 2022 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GSchutz/100358d4a614b563da29e72c5f8cd5e9 to your computer and use it in GitHub Desktop.
Save GSchutz/100358d4a614b563da29e72c5f8cd5e9 to your computer and use it in GitHub Desktop.
/**
* Method for create recursively (tree) structure in self referenced mongoose
* Model
*
* @param {constructor} model A mongoose.model instance for schema
* validation
* @param {Object} obj The plain object to be inserted
* @param {String} field The field of the obj/Model
*
* @return {model} An instance of the Model 'model'
*/
function recursive_reference(model, obj, field) {
if(obj[field] && obj[field].length) {
var newObjs = []
obj[field].forEach(function(d) {
newObjs.push( recursive_reference(model, d, field) );
});
obj[field] = newObjs
}
return new model(obj)
}
/**
* Define the mongoose schema and model
*/
var mongoose = require('mongoose');
var FolderSchema = new mongoose.Schema({
label: {type: String},
// set the self reference
folders: [this],
});
var FolderModel = mongoose.model('folders', FolderSchema);
FolderSchema.pre('save', function(next) {
if (this.isNew) {
recursive_reference(FolderModel, this, "folders")
}
next();
});
module.exports = FolderModel;
// example of data
{
"_id": "588e34b4c00784891be9354a",
"label": "Root Folder",
"folders": [{
"_id": "57b23504dd49e2265835041e",
"label": "My Folder 1",
"folders": []
},{
"_id": "57a9eddc5d9221a11a75fd9b",
"label": "My Folder 2",
"folders": []
},{
"_id": "580937552de886fa6e9e4992",
"label": "My folder 3",
"folders": [{
"_id": "55a43eb8ac57a491230028fb",
"label": "My Folder 3.1",
"folders": []
}]
}]
}
// example of data without the recursive_reference no _id is created on
// subfolders, because they are interpreted as raw data;
{
"_id": "588e34b4c00784891be9354a",
"label": "Root Folder",
"folders": [{
"label": "My Folder 1",
"folders": []
},{
"label": "My Folder 2",
"folders": []
},{
"label": "My folder 3",
"folders": [{
"label": "My Folder 3.1",
"folders": []
}]
}]
}
@ilomon10
Copy link

ilomon10 commented Apr 9, 2020

how to apply it in Typescript language.
ever tried someting like this line

...

var FolderSchema = new mongoose.Schema({
    label: {type: String},
    // set the self reference
+   folders: [this],
});

...

error will appear

'this' implicitly has type 'any' because it does not have a type annotation.

@etomarat
Copy link

how to apply it in Typescript language. ever tried someting like this line

...

var FolderSchema = new mongoose.Schema({
    label: {type: String},
    // set the self reference
+   folders: [this],
});

...

error will appear

'this' implicitly has type 'any' because it does not have a type annotation.

const FolderSchema = new mongoose.Schema({
      label: {type: String},
      folders: [this],
});

FolderSchema.add({
      folders: [FolderSchema],
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment