-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.js
44 lines (38 loc) · 1.01 KB
/
Database.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
"use strict";
const { Schema } = require("mongoose");
//Schema definition
const filesSchema = new Schema({
filename: String,
uploadDate: { type: Date, default: Date.now() },
contentType: String,
length: Number,
});
class Database {
constructor(connection) {
this.db = connection;
this.db.on(
"error",
console.error.bind(console, "MongoDB connection error:")
);
try {
this.File = this.db.model("files");
} catch (error) {
console.log("Error Caught: ", error)
this.File = this.db.model("files", filesSchema);
}
}
async createFile(path) {
let file = new this.File(path);
let entry = await file.save();
return entry;
}
async getFile(id) {
let entry = await this.File.findOne({ _id: id });
return entry;
}
async deleteFile(id) {
let entry = await this.File.deleteOne({ _id: id });
return entry;
}
}
module.exports = Database;