-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb.js
178 lines (165 loc) · 7.08 KB
/
mongodb.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
//Importing mongodb and mongoose modules
var mongodb = require('mongodb');
var mongoose = require("mongoose");
//var url = 'mongodb://localhost:27017/urlShortener';
//var url = 'mongodb://theokeles:Ckdnsdacd#[email protected]:45669/mongolad';
//Protecting the credentials by setting env variables in Heroku, not exposing in public repo
var url = process.env.MONGOLAB_URI;
//Using global promise since mongoose promise is deprecated
mongoose.Promise = global.Promise;
console.log("Mongoose stuff");
//Making mongodb connection from mongoose ODM
var dbconn = mongoose.connect(url, function(error) {
if (error) {
return console.log("Error in connecting to mongodb ", error);
} else {
console.log("Connection successful to mongo db on local");
}
});
//Create a Schema(object) -- a predefined structure/format for the documents in the 'urlshortener' collection.
//Schema are the blueprint(class) for the documents in the collection.
var urlShortnerSchema = mongoose.Schema({
originalLongUrl: String,
urlCode: {
type: Number,
index: true
},
shortURL: String
});
//Counters collection keeps track of the latest count_sequences of different fields
//Schema for counters collection
var countersCollectionSchema = mongoose.Schema({
"_id": String,
"sequence_value": Number
});
//Models are constructors compiled from Schema definitions.
//Creating a model(object) for querying,creating and validating documents in 'UrlData' collection based on the specific schema(urlShortnerSchema).
var urlShortenerModel = dbconn.model('urldata', urlShortnerSchema);
//Model for counter collection
var countersCollectionModel = dbconn.model('counter', countersCollectionSchema);
//One time create&insert (initialize) counter collection with - sequence_value": 1000
//Code run on the time of require('mongodb.js') load
//Below code snippet must go inside callback else it is initilising everytime check??????
var counterData = {
"_id": "urlCode",
"sequence_value": 1000
};
var counterEntry = new countersCollectionModel(counterData);
//Promise returned on adding new counter doc
var counterPromise = counterEntry.save();
//Search if any document exists ????? count()? maybe
countersCollectionModel.find({ "sequence_value": 1000 }, function(error, docs) {
if (error) {
console.log("An error has occured", error);
} else {
if (docs.length === 0) {
counterPromise.then(function(docs) {
console.log("Initial counter doc with sequence_value " + docs + " saved to db");
});
}
}
});
//module.exports object contains those contents(functions and data) that we want other files to use
module.exports = {
//Check validity of short URL and redirect
checkIfShortUrlExist: function(shortUrl) {
var checkPromise = urlShortenerModel.findOne({ 'shortURL': shortUrl }).exec();
return checkPromise;
},
//Retrieve the next urlCode sequence value
getLatestUrlCodeSequence: function(sequenceName) {
//query({}).exec() returns a js Promise(mongoose default)
var counterPromise = countersCollectionModel.find({ _id: sequenceName }).exec();
/*counterPromise.then(function(sequence_doc) {
console.log("docs", sequence_doc);
return sequence_doc;
})
.then(function(sequence_doc) {
console.log('sequence_doc.sequence_value', sequence_doc.sequence_value);
sequence_doc.sequence_value = sequence_doc.sequence_value + 1;
urlCode = sequence_doc.sequence_value;
return sequence_doc.save();
})
.then(function(docs) {
console.log("The doc " + docs + " is saved");
return urlCode;
});*/
return counterPromise;
},
//Increment (Update) the counters collection sequenceField
incrementCounter: function() {
//Use update() to modify document in mongoose instead of save()
//~update({find query},{modify data query},options,callback)~
//update().exec() returns Promise
var counterUpdatePromise = countersCollectionModel.update({ "_id": "urlCode" }, { $inc: { sequence_value: 1 } }).exec();
return counterUpdatePromise;
},
//Save new documents(urlData) to collection
enterNewUrlData: function(newUrlData) {
//New instance of model to enter new document
var newEntry = new urlShortenerModel(newUrlData);
//Document entry into db
/*newEntry.save(function(error) {
if (error) {
return console.log("An error has occured: ", error);
} else {
console.log("Entry saved to collection in db");
}
});*/
//Mongoose save() function returns promise
var savePromise = newEntry.save();
return savePromise;
},
//Check for duplicate URL in db
checkDuplicateData: function(inputUrl) {
//console.log("Inside checkDuplicateData");
var isDuplicateUrl;
var query = urlShortenerModel.find({ 'originalLongUrl': inputUrl });
var queryPromise = query.exec();
/*promise.then(function(doc) {
console.log("Inside promise then()");
isDuplicateUrl = true;
if (doc.length == 0) {
isDuplicateUrl = false;
} else {
isDuplicateUrl = true;
}
console.log("--isDuplicateUrl-- ", isDuplicateUrl);
});
return isDuplicateUrl;*/
return queryPromise;
}
/*checkDuplicateData: function(inputUrl) {
urlShortenerModel.find({
'originalLongUrl': inputUrl
}, function(error, docs) {
if (error) {
return console.log('An error has occured', error);
} else {
console.log("Queried docs ", docs);
console.log("docs.length : ", docs.length);
if (docs.length !== 0) {
var isDuplicate = true;
console.log("There is a duplicate url in the db");
} else {
console.log('Valid url for entry to db');
var newUrlData = {
originalLongUrl: inputUrl,
urlCode: 0
};
var newEntry = new urlShortenerModel(newUrlData);
//Document entry into db
newEntry.save(function(error) {
if (error) {
return console.log("An error has occured: ", error);
} else {
console.log("Entry saved to collection in db");
}
});
}
}
});
}*/
};
//Model is used for querying,creating,retrieving documents.
//On enetring a document not following the Schema, a validation error will be thrown