-
Notifications
You must be signed in to change notification settings - Fork 4
/
db.ts
36 lines (29 loc) · 1016 Bytes
/
db.ts
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
import mongoose from "mongoose";
import config from "./config";
if (config.db.username == "" && config.db.password == "") {
mongoose.connect(
`mongodb://${config.db.host}:${config.db.port}/${config.db.database}`,
{ useNewUrlParser: true, useUnifiedTopology: true }
);
} else {
mongoose.connect(
`mongodb://${config.db.username}:${config.db.password}@${config.db.host}:${config.db.port}/${config.db.database}`,
{ useNewUrlParser: true, useUnifiedTopology: true }
);
}
mongoose.set("useFindAndModify", false);
const db = mongoose.connection;
db.on("error", function (e) {
console.log("Error with MongoDB: " + e);
process.exit(1);
});
db.once("open", function () {
console.log(`Successfully connected to MongoDB on port ${config.db.port}`);
});
export default mongoose;
export function toObjectId(s: string) {
return new mongoose.Schema.Types.ObjectId(s);
}
export function isValidID(ID: string) {
return mongoose.Types.ObjectId.isValid(ID);
}