-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbConnector.js
64 lines (54 loc) · 1.66 KB
/
dbConnector.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
"use strict";
/**
* @desc defines DB schemata & connects to mongoDB
*/
var mongoose = require('mongoose');
/* definition of publicationSchema */
//_id attribute will be created automatically
var publicationSchema = new mongoose.Schema({
title: String,
abstract: String,
author: String,
authorID: String,
publicationDate: Date,
widgets: [mongoose.Schema.Types.ObjectId]
});
/* definition of userSchema */
//_id attribute will be created automatically
var userSchema = new mongoose.Schema({
name: String,
email: String,
provider: String,
providerID: String
});
/**
* @desc definition of the widget-schema
*/
var widgetSchema = new mongoose.Schema({
publicationID: String,
caption: String,
fileType: String,
widgetType: {type: String, enum: ['map', 'timeseries']}
});
var publicationModel = mongoose.model('Publication', publicationSchema);
var userModel = mongoose.model('User', userSchema);
var widgetModel = mongoose.model('Widget', widgetSchema);
/**
* @desc function to connect the ODB driver to mongoDB
* @param dbPort: port on which mongoDB is listening
* @param dbName: name of the database to use
* @param callback: function with parameter 'error' (node style callback)
*/
exports.connect = function(dbAddress, dbName, callback) {
var uri = 'mongodb://' + dbAddress + '/' + dbName;
var options = {};
mongoose.connect(uri, options);
mongoose.connection.on('error', function() { callback('database connection error'); });
mongoose.connection.once('open', function() { callback(null); });
};
/* the database models, that can be accessed for queries etc */
exports.models = {
publications: publicationModel,
users: userModel,
widget: widgetModel
};