-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseActions.js
executable file
·131 lines (114 loc) · 3.85 KB
/
DatabaseActions.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
var mongo = require( "mongodb" )
var mongoClient = mongo.MongoClient
var dbLocation = "mongodb://localhost/egDB"
var dbColl = "egColl"
//Http status codes
var ut = require( './UtilityFunctions.js' )
var HTTP_STATUS = ut.http_codes()
module.exports = {
/**
* Overwrites the default database location.
* @param {STRING} location
*/
SetLocation: function ( location ) {
dbLocation = location
},
/**
* Overwrites the default collection of the database.
* @param {STRING} collection
*/
SetCollection: function ( collection ) {
dbColl = collection
},
/**
* Opens a connection to the database and finds one document with the matching key.
* @param { JSON OBJECT } k - contains a key used to identify unqiue rows.
* @param { FUNCTION } response - takes http code to track success and output (row(s)) from
* the database.
*/
read: function ( k, response ) {
mongoClient.connect( dbLocation, function( err, db ) {
if ( err ) {
response( HTTP_STATUS.ISE, "ERROR: Database error." )
} else {
var collection = db.collection( dbColl )
collection.findOne( { key : k }, function( err, res ) {
if ( err ) {
response( HTTP_STATUS.ISE, "ERROR: Database error." )
} else {
if( res != null ) response( HTTP_STATUS.OK, { key: res.key, value: res.value } )
else response( HTTP_STATUS.BAD_REQUEST, "ERROR: No entry with that key was found.")
}
})
}
db.close()
})
},
/**
* Opens a connection to the database and either creates a new entry or updates an existing entry with
* new entry information.
* @param { JSON OBJECT } kv - contains a key and a value forming a document entry ready for inserting.
* @param { FUNCTION } response - takes http code to track success and output (row(s)) from
* the database.
*/
insert: function ( kv, response ) {
mongoClient.connect( dbLocation, function( err, db ) {
if ( err ) {
response( HTTP_STATUS.ISE, "ERROR: Database error." )
} else {
var collection = db.collection( dbColl )
collection.findOne( {key: kv.key, }, function ( err, res ){
if ( err ) {
response( HTTP_STATUS.ISE, "ERROR: Database error." )
} else {
// Looked to see if Key exists.
if( res == null ) {
collection.save( kv, function ( err, res ) {
if ( err ) {
response( HTTP_STATUS.ISE , "ERROR: Database error." )
} else {
// Key didn't exists therefore we save and return complete.
response( HTTP_STATUS.CREATED, { key: kv.key, value: kv.value } )
}
})
} else {
collection.update( { key : kv.key }, kv, function ( err, res ) {
if ( err ) {
response( HTTP_STATUS.ISE.status, "ERROR: Database error." )
} else {
// Key already exists.
response( HTTP_STATUS.OK, { key: kv.key, value: kv.value } )
}
})
}
db.close()
}
})
}
})
},
/**
* Opens a connection to the database and deletes the row with the matching key.
* @param { JSON OBJECT } k - contains a key used to identify unqiue rows.
* @param { FUNCTION } response - takes http code to track success and output (row(s)) from
* the database.
*/
delete: function ( k, response ) {
mongoClient.connect( dbLocation, function( err, db ) {
if ( err ) {
response( HTTP_STATUS.ISE, "ERROR: Database error." )
} else {
var collection = db.collection( dbColl )
collection.remove( { key : k }, function( err, res ) {
if ( err ) {
response( HTTP_STATUS.ISE.status, "ERROR: Database error." )
} else {
if ( res.result.n != 0 ) response( HTTP_STATUS.NO_CONTENT, null )
else response( HTTP_STATUS.BAD_REQUEST, "ERROR: No key value pair with that key to delete.")
}
})
}
db.close()
})
},
}