-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
79 lines (65 loc) · 1.92 KB
/
db.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
const mongoose = require("mongoose");
const mongodb = require("mongodb");
const { type } = require("jquery");
const dbAddress = process.env.DATABASE || "localhost";
const dbCollectionName = process.env.DATABASE_COLLECTION || "elfinconnect";
let filesCollection;
let gridfsBucket;
const connectDB = async () => {
try {
await mongoose.connect(`mongodb://${dbAddress}/${dbCollectionName}`);
console.log("DB Connected!");
} catch (err) {
console.log("DB Connection Error: ", err);
process.exit(1);
}
gridfsBucket = new mongodb.GridFSBucket(mongoose.connection.db, {
bucketName: "downloads",
});
filesCollection = mongoose.connection.db.collection("downloads.files");
};
// Define the Device schema and model
const deviceSchema = new mongoose.Schema({
hostName: String,
macAddress: String,
lastSeenDate: Date,
view: { type: mongoose.Schema.Types.ObjectId, ref: "View" },
});
const Device = mongoose.model("Device", deviceSchema);
// Define the User schema and model
const userSchema = new mongoose.Schema({
uuid: String,
username: String,
password: String,
admin: { type: Boolean, default: false },
allDevices: [{ type: mongoose.Schema.Types.ObjectId, ref: "Device" }],
});
const User = mongoose.model("User", userSchema);
const viewComponentSchema = new mongoose.Schema({
id: String,
name: String,
type: String,
style: mongoose.Schema.Types.Mixed,
order: Number,
modbus: mongoose.Schema.Types.Mixed,
extra: mongoose.Schema.Types.Mixed,
});
const layoutSchema = new mongoose.Schema({
type: String,
});
const ViewComponent = mongoose.model("ViewComponent", viewComponentSchema);
const viewSchema = new mongoose.Schema({
layout: layoutSchema,
components: [viewComponentSchema],
});
const View = mongoose.model("View", viewSchema);
// Export the database connection and models
module.exports = {
connectDB,
filesCollection: () => filesCollection,
gridfsBucket: () => gridfsBucket,
Device,
User,
View,
ViewComponent,
};