-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
193 lines (138 loc) · 5.32 KB
/
main.js
File metadata and controls
193 lines (138 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const mongoose = require("mongoose");
//create model that allows easy nested object insertions
const createModel = function(fields,name){
try{
const newSchema = new mongoose.Schema(fields,{strict:false});
const newModel = mongoose.model(name,newSchema);
return newModel;
}catch(err){
throw err;
}
}
//helper fucntion
const makeFilterString = function(docuField,path){
if(!docuField || !path || path.length <= 0){
throw new Error("Invalid or no arguments provided");
}
let filterString = `${docuField}`;
path.forEach((index) =>{
filterString+=`.${index}.${docuField}`;
});
return filterString;
}
//add nested object using path property on parent object
//you locate thr parent object we'll insert with the path
//need a query string builder , need to write a fuction for that
//TODO add callbak function
const addNestedDocuByPath = function(config,fn){ //make argument into an object
const {path,modelName,docu,id,docuField} = config;
const fields = ["modelName","path","id",'docu',"docuField"];
for(const key of fields){
if(!config.hasOwnProperty(key)){
throw new Error(`${key} field not indicated`);
}
if(config[key] === undefined || !config[key] || config[key] === "" ){
throw new Error(`no value for '${key}' key specified`);
}
}
modelName.findById(id,function(err,returned){
if(err){
return fn(err, null);
}
const filterString = makeFilterString(docuField,path); //query string to locate desired object and do insertion
const newPath = path.slice();
// console.log(returned)
let parentObject = returned[docuField];
newPath.forEach((key) => {
parentObject = parentObject[key][docuField];
});
docu.path=[...path,parentObject.length]; //
docu.extended_family=[];
modelName.findByIdAndUpdate(id,{"$push":{[filterString]:docu }},{strict:false},(err,response) => {
if(err){
return fn(err,null);
}
return fn(null,response);
})
}).select(docuField);// select first level of nesting then use it to locate desired document
}
//find by path
const findNestedDocuByPath = function(config,fn){
const {path,modelName,id,docuField} = config;
const fields = ["modelName","path","id","docuField"];
for(const key of fields){
if(!config.hasOwnProperty(key)){
throw new Error(`${key} field not provided`);
}
if(config[key] === undefined || !config[key] || config[key] === ""){
throw new Error(`no value for '${key}' key specified`);
}
}
modelName.findById(id,function(err,returned){
if(err){
return fn(err,null);
}
// console.log(returned)
let parentObject = returned[docuField];
path.forEach((key,index) => {
if(index === path.length - 1){
parentObject = parentObject[key];
}else{
parentObject = parentObject[key][docuField];
}
});
return fn(null,parentObject);
}).select(docuField);
}
const addNestedDocuById = function(options,fn){
const fields = ["model","id",'docu',"docuField"];
for(const key of fields){
if(!options.hasOwnProperty(key)){
throw new Error(`${key} field not indicated`);
}
if(options[key] === undefined || !options[key] || options[key] === "" ) {
throw new Error(`no value for '${key}' key specified`);
}
}
const {model,id,docu,docuField} = options;
model.findById(id,function(err,returned){
if(err){
return fn(err,null);
}else{
if(!returned){
return fn(new Error(`No document found with id ${id}`));
}
if(returned.hasOwnProperty(docuField)){
const NestedArrayLength = returned[docuField].length; //to see how many nested objects in the the first level
docu.path = [];
docu[docuField] = [];
//add head document id to each nested docu parentId:returned._id
docu.path.push(NestedArrayLength);
model.findByIdAndUpdate(id,{"$push":{[docuField]:docu }},(err,response) => {
if(err){
return fn(err,null);
}
return fn(null,response);
})
}else{
docu.path = [];
docu[docuField] = [];
docu.path.push(0);
model.findByIdAndUpdate(id,{[docuField]:[docu]},{strict:false},(err,response) => {
if(err){
return fn(err,null);
}
fn(null, response);
})
}
}
});
}
module.exports = {
"createModel":createModel,
"addNestedDocuById":addNestedDocuById,
"makeFilterString":makeFilterString,
"addNestedDocuByPath":addNestedDocuByPath,
"findNestedDocuByPath":findNestedDocuByPath
}
//crate add documents to be nested in specified model before nesting it