-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateModels.js
229 lines (227 loc) · 7.49 KB
/
generateModels.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
let schemas = require('./Schema');
let fs = require('fs');
let rewrite = false;
let includeTypeInArray = false;
const useES6 = false;
if (!fs.existsSync(__dirname + '/../Models')) {
fs.mkdirSync(__dirname + '/../Models');
}
const getProps = (props, prefix = 'this') => {
let p = '';
for (let pr in props) {
if (props[pr].constructor == String) {
p += `${prefix}.${pr} = ${getDataTypeConstructor(props[pr])}; \n\t\t\t`;
} else if (props[pr].constructor == Object) {
p += `${prefix}.${pr} = ${getDataTypeConstructor(props[pr]['type'])}; \n\t\t\t`;
}
}
return p;
}
const getDataTypeConstructor = (type) => {
let DataTypeMapping = {
'string': 'String()',
'int': 'Number()',
'bool': 'Boolean()',
'date': 'new Date()',
'data': 'new ArrayBuffer(0)',
'float': 'Number()',
'double': 'Number()',
}
if (DataTypeMapping[type] != undefined) {
return `${DataTypeMapping[type]}`;
} else {
if (type.constructor == String && type.includes('[]')) {
type = type.replace('[]', '');
if (DataTypeMapping[type] != undefined) {
return `Array${includeTypeInArray? '<' + DataTypeMapping[type].replace(/new|[(?0)]/g,'') + '>':''}()`
} else {
return `Array${includeTypeInArray? '<' + type + '>':''}()`
}
} else {
return `new ${type}();`
}
}
}
const getImportStatements = (ob) => {
let importsReq = [];
let importStatement = '';
let nonImports = [
'string',
'int',
'bool',
'date',
'data',
'float',
'double',
]
if (ob.constructor == Object) {
for (let o in ob) {
let type;
if (ob[o].constructor == String) {
type = ob[o].replace('[]', '');
} else if (ob[o].constructor == Object) {
type = ob[o]['type'].replace('[]', '');
}
if (!nonImports.includes(type) && !importsReq.includes(type)) {
importsReq.push(type);
}
}
}
for (let i = 0; i < importsReq.length; i++) {
if (useES6) {
importStatement += `import ${importsReq[i]} from './${importsReq[i]}';\n`
} else {
importStatement += `const ${importsReq[i]} = require('./${importsReq[i]})\n`;
}
}
return importStatement;
}
const indexArray = [];
if (useES6) {
schemas.forEach((ob) => {
let data =
`import RealmModel from '../Realm/ModelES6';
${getImportStatements(ob.properties)}
export default class ${ob.name} extends RealmModel{
constructor(){
super();
this.presave = ${ob.name}.preSave;
this.postsave = ${ob.name}.postSave;
${getProps(ob.properties)}
}
static preSave(obj = new ${ob.name}(),next){
next();
}
static postSave(obj = new ${ob.name}()){}
static ModelName = '${ob.name}';
static parseJsonObject(jsonObject){
let ob = new ${ob.name}();
if(jsonObject.constructor == String){
ob = JSON.parse(jsonObject);
}else{
ob = jsonObject;
}
return ob;
}
static getAll(){
let found = super.all(${ob.name}.ModelName);
return found.map((ob)=>{
return this.parseJsonObject(ob);
})
}
static all(){
return super.all(${ob.name}.ModelName);
}
static find(query){
let found = super.find(${ob.name}.ModelName,query);
return found.map((ob)=>{
return this.parseJsonObject(ob);
})
}
static findOne(query){
let found = super.findOne(${ob.name}.ModelName,query);
return this.parseJsonObject(found);
}
static findByObjectId(objectId){
let found = super.findByObjectId(${ob.name}.ModelName,objectId);
return this.parseJsonObject(found);
}
save(){
this.presave(this,()=>{
super.save(${ob.name}.ModelName);
});
this.postsave(this);
}
}
`
if (fs.existsSync(__dirname + `/../Models/${ob.name}.js`)) {
if (rewrite) {
fs.writeFileSync(__dirname + `/../Models/${ob.name}.js`, data);
}
} else {
fs.writeFileSync(__dirname + `/../Models/${ob.name}.js`, data);
}
indexArray.push(ob.name);
})
} else {
schemas.forEach((ob) => {
let data = `const RealmModel = require('../Realm/Model');
${getImportStatements(ob.properties)}
function ${ob.name}(){
RealmModel.call(this);
this.presave = ${ob.name}.preSave;
this.postsave = ${ob.name}.postSave;
${getProps(ob.properties)}
}
${ob.name}.preSave = function(obj = new ${ob.name}(),next){
next();
}
${ob.name}.postSave = function(obj = new ${ob.name}()){}
${ob.name}.parseJsonObject = function(jsonObject){
let ob = new ${ob.name}();
let descriptor = Object();
for(let j in jsonObject){
descriptor[j] = {value:jsonObject[j],writable:true}
}
if(jsonObject && jsonObject.constructor == String){
Object.defineProperties(ob,descriptor);
}else{
Object.defineProperties(ob,descriptor);
}
return ob;
}
${ob.name}.getAll = function(){
let found = RealmModel.all(${ob.name}.name);
return found.map((ob)=>{
return this.parseJsonObject(ob);
})
}
${ob.name}.find = function(query){
let found = RealmModel.find(${ob.name}.name,query);
return found.map((ob)=>{
return this.parseJsonObject(ob);
})
}
${ob.name}.findOne = function(query){
let found = RealmModel.findOne(${ob.name}.name,query);
if(found){
return this.parseJsonObject(found);
}else{
return undefined;
}
}
${ob.name}.findObjectById = function(objectId){
let found = RealmModel.findByObjectId(${ob.name}.name,objectId);
if(found){
return this.parseJsonObject(found);
}else{
return undefined;
}
}
${ob.name}.prototype.save = function(){
this.presave(this,()=>{
RealmModel.prototype.save.call(this,${ob.name}.name);
});
this.postsave(this);
}
module.exports = ${ob.name}`;
if (fs.existsSync(__dirname + `/../Models/${ob.name}.js`)) {
if (rewrite) {
fs.writeFileSync(__dirname + `/../Models/${ob.name}.js`, data);
}
} else {
fs.writeFileSync(__dirname + `/../Models/${ob.name}.js`, data);
}
indexArray.push(ob.name);
})
}
let index = '';
indexArray.forEach((value) => {
if (useES6) {
index += `import ${value} from './${value}';\nexport {${value}}\n`;
} else {
index += `const ${value} = require('./${value}');\nmodule.exports.${value} = ${value}`
}
})
fs.writeFileSync(__dirname + '/../Models/index.js', index);
console.log('\x1b[32m', 'Models Generated at ' + __dirname + '/../Models');