-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.mjs
66 lines (54 loc) · 2.01 KB
/
db.mjs
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
import mongoose from "mongoose";
import slug from 'mongoose-slug-updater';
import passportLocalMongoose from 'passport-local-mongoose';
// is the environment variable, NODE_ENV, set to PRODUCTION?
import fs from 'fs';
import path from 'path';
import url from 'url';
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
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/finalproj';
}
const mongooseOpts = {
useNewUrlParser: true,
useUnifiedTopology: true
};
mongoose.connect(dbconf, mongooseOpts).then(console.log('Connected')).catch(err => console.log(err));
mongoose.plugin(slug);
const User = new mongoose.Schema({
kitchenposts:[{type: mongoose.Schema.Types.ObjectId, ref: 'Kitchen'}],
brandposts:[{type: mongoose.Schema.Types.ObjectId, ref: 'Brand'}],
cscore:Number
})
//Enable passport plugin
User.plugin(passportLocalMongoose);
const KitchenSchema = new mongoose.Schema({
user:[{type: mongoose.Schema.Types.ObjectId, ref: 'User'}],
name:String,
Location:String,
Brands:[{type: mongoose.Schema.Types.ObjectId, ref: 'Brand'}],
slug: {type: String, slug: 'name', unique: true},
tScore:Number
},{timestamps: true})
const BrandSchema = new mongoose.Schema({
user:[{type: mongoose.Schema.Types.ObjectId, ref: 'User'}],
name:String,
Locations:[{type:String}],
description: {type: String, required: false},
slug: {type: String, slug: 'name', unique: true},
tscore:Number
},{timestamps: true})
mongoose.model('user', User);
mongoose.model('Kitchen', KitchenSchema);
mongoose.model('Brand', BrandSchema);