-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdb.js
292 lines (166 loc) · 6.65 KB
/
db.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* @file Manages the database connection and schemas for entity types.
*
*/
// See if a database is set in config if not, for now set it to MongoDB.
if (!iris.config.dbEngine) {
iris.config.dbEngine = "mongodb";
}
var fs = require('fs');
//Connect to database
iris.invokeHook("hook_db_connect__" + iris.config.dbEngine, "root", iris.config, null).then(function () {
iris.dbPopulate(true);
iris.status.ready = true;
console.log("Ready on port " + iris.config.port + ".");
iris.log("info", "Server started");
});
iris.dbPopulate = function (firstTime) {
iris.fieldTypes = {};
iris.entityTypes = {};
if (iris.entityTypes) {
Object.keys(iris.entityTypes).forEach(function (entityType) {
delete iris.entityTypes[entityType];
});
}
var dbSchema = {};
var glob = require("glob");
var merge = require("merge");
// Get field types
Object.keys(iris.modules).forEach(function (moduleName) {
var modulePath = iris.modules[moduleName].path;
var fields = glob.sync(modulePath + "/**/*.iris.field");
fields.forEach(function (fieldPath) {
try {
var field = fs.readFileSync(fieldPath, "utf8");
field = JSON.parse(field);
if (!iris.fieldTypes[field.name]) {
iris.fieldTypes[field.name] = field;
} else {
// Merge field's properties
var newObject = merge.recursive(true, iris.fieldTypes[field.name], field);
iris.fieldTypes[field.name] = newObject;
}
} catch (e) {
iris.log("error", e);
}
});
});
// Loop over all enabled modules and check for schema files
Object.keys(iris.modules).forEach(function (moduleName) {
try {
fs.readdirSync(iris.modules[moduleName].path + "/schema").forEach(function (schemafile) {
schemafile = schemafile.toLowerCase().replace(".json", "");
//Check if schema already exists for entity type, if not, add it
if (!dbSchema[schemafile]) {
dbSchema[schemafile] = {};
}
var file = JSON.parse(fs.readFileSync(iris.modules[moduleName].path + "/schema/" + schemafile + ".json"));
dbSchema[schemafile] = merge.recursive(true, file, dbSchema[schemafile]);
});
} catch (e) {
// Catch errors if the file could be found (such as JSON errors)
if (e.code !== "ENOENT") {
iris.log("error", "Could not parse schema file in module " + moduleName);
iris.log("error", e);
}
}
});
// See if site config has added any schema or schemafields
fs.readdirSync(iris.sitePath + "/configurations/entity").forEach(function (schemafile) {
var schemaName = schemafile.toLowerCase().replace(".json", "");
var file;
try {
file = JSON.parse(fs.readFileSync(iris.sitePath + "/configurations/entity/" + schemafile, "UTF8"));
} catch (e) {
iris.log("error", schemaName + " failed db schema insertion valid JSON");
iris.log("error", e);
return false;
}
if (!dbSchema[schemaName]) {
dbSchema[schemaName] = {};
}
Object.keys(file).forEach(function (field) {
dbSchema[schemaName][field] = file[field];
});
});
Object.keys(dbSchema).forEach(function (schema) {
// Make JSON copy of complete schema and save to non mongoosed object for reference
iris.entityTypes[schema] = JSON.parse(JSON.stringify(dbSchema[schema]));
// Sneaky shortcut way of saving of fieldtypes into the entityType list
var stringySchema = JSON.stringify(iris.entityTypes[schema]);
Object.keys(iris.fieldTypes).forEach(function (fieldType) {
try {
fieldType = iris.fieldTypes[fieldType];
var name = fieldType.name;
var type = fieldType.type;
var search = `"fieldType":"${name}",`;
var replace = search + `"fieldTypeType":"${type}",`;
stringySchema = stringySchema.split(search).join(replace);
} catch (e) {
iris.log("error", e);
}
});
iris.entityTypes[schema] = JSON.parse(stringySchema);
});
var schemaCounter = 0;
var schemaLoaded = function () {
schemaCounter += 1;
if (schemaCounter === Object.keys(iris.entityTypes).length) {
process.emit("dbReady", firstTime);
}
};
Object.keys(iris.entityTypes).forEach(function (entityType) {
if (!iris.entityTypes[entityType].fields) {
iris.entityTypes[entityType].fields = {};
}
iris.entityTypes[entityType].fields.path = {
fieldType: 'Textfield',
fieldTypeType: 'String',
label: 'path',
fixed: true, // A fixed field isn't shown on the schema edit page as it can't be edited/deleted
weight: 1111, // JSON doesn't support infinity - ugh
machineName: 'path',
permissions: ["anonymous", "authenticated"]
};
iris.invokeHook("hook_db_schema__" + iris.config.dbEngine, "root", {
schema: entityType,
schemaConfig: JSON.parse(JSON.stringify(iris.entityTypes[entityType]))
}).then(function () {
//Create permissions for this entity type
iris.modules.auth.globals.registerPermission("can create " + entityType, "entity");
iris.modules.auth.globals.registerPermission("can edit any " + entityType, "entity");
iris.modules.auth.globals.registerPermission("can edit own " + entityType, "entity");
iris.modules.auth.globals.registerPermission("can view any " + entityType, "entity");
iris.modules.auth.globals.registerPermission("can view own " + entityType, "entity");
iris.modules.auth.globals.registerPermission("can delete any " + entityType, "entity");
iris.modules.auth.globals.registerPermission("can delete own " + entityType, "entity");
iris.modules.auth.globals.registerPermission("can fetch " + entityType, "entity", "Can use the API to <b>fetch</b> entities.");
iris.modules.auth.globals.registerPermission("can delete schema " + entityType, "entity", "Delete the entire schema. <strong>This includes the data</strong>.");
schemaLoaded();
});
});
};
/**
* Function for registering a DB schema directly in code
* isContent boolean variable determines whether the entity type appears on a content page
*/
iris.dbSchemaRegister = function (name, fields, content = false) {
return new Promise(function (resolve, reject) {
var schema = {
entityTypeName: name,
fields: fields
};
if (!content) {
schema.systemOnly = true;
}
iris.entityTypes[name] = schema;
iris.invokeHook("hook_db_schema__" + iris.config.dbEngine, "root", {
schema: name,
schemaConfig: JSON.parse(JSON.stringify(schema))
}).then(function () {
resolve();
}, function (fail) {
reject(fail);
});
});
};