-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdb.js
82 lines (72 loc) · 2.17 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
80
81
82
const fs = require("fs");
const path = require("path");
const url = require("url");
const mongoose = require("mongoose");
const mongooseSlugPlugin = require("mongoose-slug-plugin");
/**
* The following are schemas for MongoDB.
*/
const AccountSchema = new mongoose.Schema(
{
name: { type: String, required: true },
password: { type: String, unique: true, required: true },
email: { type: String, unique: true, required: true },
role: {
type: String,
required: true,
default: "user",
enum: ["user", "admin"],
},
},
{ timestamps: true }
);
const ReportSchema = new mongoose.Schema(
{
account: { type: mongoose.Schema.Types.ObjectId, ref: "Account" },
score: { type: Number },
description: { type: String },
starred: { type: Boolean, default: false },
},
{ timestamps: true }
);
const PostSchema = new mongoose.Schema(
{
account: { type: mongoose.Schema.Types.ObjectId, ref: "Account" },
title: { type: String, required: true },
content: { type: String },
type: {
type: String,
default: "regular",
enum: ["regular", "announcement"],
},
},
{ timestamps: true }
);
const ReplySchema = new mongoose.Schema(
{
account: { type: mongoose.Schema.Types.ObjectId, ref: "Account" },
post: { type: mongoose.Schema.Types.ObjectId, ref: "Post" },
content: { type: String },
},
{ timestamps: true }
);
AccountSchema.plugin(mongooseSlugPlugin, { tmpl: "<%=name%>" });
mongoose.model("Account", AccountSchema);
mongoose.model("Report", ReportSchema);
mongoose.model("Post", PostSchema);
mongoose.model("Reply", ReplySchema);
let dbconf;
if (process.env.NODE_ENV === "PRODUCTION") {
// if we're in PRODUCTION mode, then read the configration from a file
// use blocking file io to do this...
const fn = path.join(__dirname, "config.json");
const data = fs.readFileSync(fn);
// our configuration file will be in json, so parse it and set the
// conenction string appropriately!
const conf = JSON.parse(data);
dbconf = conf.dbconf;
} else {
// if we're not in PRODUCTION mode, then use
dbconf = "mongodb://127.0.0.1:27017/depression-diagnosis-js";
}
mongoose.connect(dbconf);