forked from christopher-pott/CATSReferenceCollectionsDb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_mongo.js
64 lines (55 loc) · 1.91 KB
/
db_mongo.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
/**
* Mongo database for caching corpus responses
*/
var databaseUrl = "cats";
var collections = ["samples", "artworks", "users", "vocabs"];
var db = require("./node_modules/mongojs").connect(databaseUrl, collections);
var logger = require("./logging");
/* Create a text index on all samples to enable
* mongos full text search capability
*/
db.samples.ensureIndex(
{ "$**": "text" },
{ name: "TextIndex" }
);
/* Add a unique constraint to the vocabs and users, so we will only insert the
* default data (at startup) if they don't already exist
*/
db.vocabs.ensureIndex(
{ "type": 1 },
{ unique: true }
);
db.users.ensureIndex(
{ "username": 1 },
{ unique: true }
);
/* Copy over the default vocabs to catsdb upon startup (if they are not present)
*/
var v = require('./vocabs');
var defaults = v.defaultVocabs();
for (var i=0; i < defaults.length; i++){
db.vocabs.insert(defaults[i], function(err, doc) {
if (err || !doc){
/*if the error is 'duplicate' - it just means the vocab is already initialised*/
if(err && err.code != 11000) {/*11000 == mongo duplicate key error : don't log this*/
logger.error("Default vocab not added : " + err);
}
} else {
logger.info(doc.type + " default vocabs inserted successfully");
}
});
}
/* Setup a default user on startup (if not present). Password is "admin" and should be changed upon first use.
*/
var default_admin = {username: '[email protected]', password : '$2a$10$j4GD2P.isxPBgMCcEiFrPOBbRl4uTpeG.qQKe.trtnNj1M1yrF.te', role : 'admin'};
db.users.insert(default_admin, function(err, doc) {
if (err || !doc){
if(err && err.code != 11000) {
logger.error("Default admin not added : " + err);
}
} else {
logger.info(doc.type + " default admin inserted successfully");
}
});
//Public API
module.exports = db;