-
Notifications
You must be signed in to change notification settings - Fork 1
/
createCollection.js
174 lines (158 loc) · 5.42 KB
/
createCollection.js
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
var path=require('path');
var fs=require('fs');
var async=require('async');
module.exports={
createCollection:function(collection,dirPath){
return new Promise((resolve,reject)=>{
let collectionName=collection.collectionName;
let columns=collection.columns;
let field='';//the schema actually :"
let createObject='{';
let updateString='';
let columnsName='["id",';
/**
columns[0]->name
columns[1]->type
columns[2]->required
columns[3]->Unique or Not
columns[4]->ref for objectId thing
**/
for(let i=0;i<columns.length;i++){
//columnsName
columnsName+='"'+columns[i][0]+'",'
//updateString
updateString+='doc.'+columns[i][0]+' = req.body.'+columns[i][0]+' ? req.body.'+columns[i][0]+' : doc.'+columns[i][0]+';';
//createObject
createObject+=columns[i][0];
createObject+=":req.body."+columns[i][0]+',';
//field
field+=columns[i][0]; //adding the name of the field
if(columns[i][1]=='ObjectId'){ //if objectID
if(columns[i][4].length==0){reject("Reference is not given ?");}
field+=':{type:Schema.Types.ObjectId,ref:"'+columns[i][4]+'"';
}
else{
field+=':{type:'+columns[i][1];
}
if(columns[i][2]=='Required'){ //if required
field+=',required:true';
}
if(columns[i][3]=="Unique"){ //if unique
field+=',unique:true';
}
field+='},';
}
field=field.slice(0,field.length-1); //removing the last ,
//reading the mongoose model template and write to the new file
var rw1=function(cb){
fs.readFile(path.join(__dirname,'templates','modelTemplate.js'),"utf8",(err,model)=>{
if(err){return cb(err,null);}
else{
model = model.replace(/{modelName}/, collectionName);
model = model.replace(/{schemaName}/g,collectionName+'Schema');
model = model.replace(/{fields}/, field);
var dirName="models";
//creating the models dir if not exist
if (!fs.existsSync(dirPath + '/' + dirName)){
//try and catch to handle the wrong paths issue
try{
fs.mkdirSync(dirPath + '/' + dirName);}
catch(e){
return cb(e,null);
}
}
//write to the model file
fs.writeFile(dirPath+'/'+dirName+'/'+collectionName+'.js', model, {mode:0777, encoding: 'utf8'}, function (err) {
if (err) {return cb(err,null);}//rejecting due to writing issue
else{return cb(null,true);}
});
}
});
};
//reading the router template and write to the new file
var rw2=function(cb){
fs.readFile(path.join(__dirname,'templates','routerMongoTemplate.js'),"utf8",(err,data)=>{
if(err){return cb(err,null);}
else{
columnsName=columnsName.slice(0,columnsName.length-1);
columnsName+=']';
createObject=createObject.slice(0,createObject.length-1);
createObject+='}';
data=data.replace(/{createObject}/g,createObject);
data=data.replace(/{updateString}/g,updateString);
data=data.replace(/{modelName}/g,collectionName);
data=data.replace(/{modelPath}/g,'"../models/'+collectionName+'.js"');
data=data.replace(/{columnsName}/g,columnsName);
var dirName="routes";
//creating the routes dir if not exist
if (!fs.existsSync(dirPath + '/' + dirName)){
//try and catch to handle the wrong paths issue
try{
fs.mkdirSync(dirPath + '/' + dirName);}
catch(e){
return cb(e,null);
}
}
//write to the model file
fs.writeFile(dirPath+'/'+dirName+'/'+collectionName+'.js', data, {mode:0777, encoding: 'utf8'}, function (err) {
if (err) {return cb(err,null);}//rejecting due to writing issue
else{return cb(null,true);}
});
}
});
};
//rw3
var rw3=function(cb){
fs.readFile(path.join(__dirname, 'templates','viewTemplate.ejs'),"utf8",(err,data)=>{
if(err){return cb(err,null);}
else{
data=data.replace(/{tableName}/g,collectionName);
var dirName="views";
if (!fs.existsSync(dirPath + '/' + dirName)){
try{
fs.mkdirSync(dirPath + '/' + dirName);}
catch(e){
return cb(e,null);
}
}
fs.writeFile(dirPath+'/'+dirName+'/'+collectionName+'s.ejs', data, {mode:0777, encoding: 'utf8'}, function (err) {
if (err) {return cb(err,null);}//rejecting due to writing issue
else{return cb(null,true);}
});
}
});
}
//rw4
var rw4=function(cb){
fs.readFile(path.join(__dirname, 'templates','viewTemplate2.ejs'),"utf8",(err,data)=>{
if(err){return cb(err,null);}
else{
data=data.replace(/{tableName}/g,collectionName);
var dirName="views";
if (!fs.existsSync(dirPath + '/' + dirName)){
try{
fs.mkdirSync(dirPath + '/' + dirName);}
catch(e){
return cb(e,null);
}
}
fs.writeFile(dirPath+'/'+dirName+'/'+collectionName+'.ejs', data, {mode:0777, encoding: 'utf8'}, function (err) {
if (err) {return cb(err,null);}//rejecting due to writing issue
else{return cb(null,true);}
});
}
});
}
//stack of async
stack=[];
stack.push(rw1);
stack.push(rw2);
stack.push(rw3);
stack.push(rw4);
async.parallel(stack,function(err,results){
if(err){reject(err);}
resolve(true);
});
});
}
};