10
10
*/
11
11
const mongoose = require ( 'mongoose' ) ;
12
12
const bcrypt = require ( 'bcryptjs' ) ;
13
- const mongoURI = 'mongodb+srv://Daniel:[email protected] /Cluster0?retryWrites=true&w=majority' ;
14
- const URI = process . env . NODE_ENV === 'production' ?
mongoURI :
'mongodb+srv://Daniel:[email protected] /Cluster0?retryWrites=true&w=majority' ;
13
+ require ( 'dotenv' ) . config ( ) ;
14
+ const mongoURI = process . env . MONGO_DB_NEW ;
15
+ const URI =
16
+ process . env . NODE_ENV === 'production' ? mongoURI : process . env . MONGO_DB_NEW ;
15
17
16
- const SALT_WORK_FACTOR = 14 ;
18
+ const SALT_WORK_FACTOR = 10 ;
17
19
// connect to mongo db
18
20
mongoose
19
- . connect ( URI ,
20
- {
21
- // options for the connect method to parse the URI
22
- useNewUrlParser : true ,
23
- useUnifiedTopology : true ,
24
- useCreateIndex : true ,
25
- // stop deprecation warning for findOneAndUpdate and findOneAndDelete queries
26
- useFindAndModify : false ,
27
- // sets the name of the DB that our collections are part of
28
- dbName : 'ReacType' ,
29
- } )
21
+ . connect ( URI , {
22
+ // options for the connect method to parse the URI
23
+ useNewUrlParser : true ,
24
+ useUnifiedTopology : true ,
25
+ useCreateIndex : true ,
26
+ // stop deprecation warning for findOneAndUpdate and findOneAndDelete queries
27
+ useFindAndModify : false ,
28
+ // sets the name of the DB that our collections are part of
29
+ dbName : 'ReacType'
30
+ } )
30
31
. then ( ( ) => console . log ( 'Connected to Mongo DB.' ) )
31
32
. catch ( err => console . log ( err ) ) ;
32
33
@@ -35,7 +36,7 @@ const { Schema } = mongoose;
35
36
const userSchema = new Schema ( {
36
37
username : { type : String , required : true , unique : true } ,
37
38
email : { type : String , required : false , unique : true } ,
38
- password : { type : String , required : true } ,
39
+ password : { type : String , required : true }
39
40
} ) ;
40
41
41
42
// mongoose middleware that will run before the save to
@@ -51,7 +52,7 @@ userSchema.pre('save', function cb(next) {
51
52
log : `bcrypt password hashing error: ${ err } ` ,
52
53
message : {
53
54
err : 'bcrypt hash error: check server logs for details.'
54
- } ,
55
+ }
55
56
} ) ;
56
57
}
57
58
this . password = hash ;
@@ -63,36 +64,44 @@ const commentsSchema = new Schema({
63
64
username : { type : String , required : true } ,
64
65
text : { type : String , required : true } ,
65
66
projectId : { type : Schema . Types . ObjectId , required : true } ,
66
- createdAt : { type : Date , default : Date . now } ,
67
+ createdAt : { type : Date , default : Date . now }
67
68
} ) ;
68
69
69
70
const sessionSchema = new Schema ( {
70
71
cookieId : { type : String , required : true , unique : true } ,
71
- createdAt : { type : Date , default : Date . now } ,
72
+ createdAt : { type : Date , default : Date . now }
72
73
} ) ;
73
74
74
- const projectSchema = new Schema ( {
75
- name : String ,
76
- likes : { type : Number , default : 0 } ,
77
- published : { type : Boolean , default : false } ,
78
- project : { type : Object , required : true } ,
79
- userId : {
80
- type : Schema . Types . ObjectId ,
81
- ref : 'Users' ,
75
+ const projectSchema = new Schema (
76
+ {
77
+ name : String ,
78
+ likes : { type : Number , default : 0 } ,
79
+ published : { type : Boolean , default : false } ,
80
+ project : { type : Object , required : true } ,
81
+ userId : {
82
+ type : Schema . Types . ObjectId ,
83
+ ref : 'Users'
84
+ } ,
85
+ username : { type : String , required : true } ,
86
+ createdAt : { type : Date , default : Date . now } ,
87
+ comments : [
88
+ {
89
+ type : Schema . Types . ObjectId ,
90
+ ref : 'Comments'
91
+ }
92
+ ]
82
93
} ,
83
- username : { type : String , required : true } ,
84
- createdAt : { type : Date , default : Date . now } ,
85
- comments : [ {
86
- type : Schema . Types . ObjectId ,
87
- ref : 'Comments' ,
88
- } ] ,
89
- } , { minimize : true } ) ;
94
+ { minimize : true }
95
+ ) ;
90
96
91
97
const Users = mongoose . model ( 'Users' , userSchema ) ;
92
98
const Comments = mongoose . model ( 'Comments' , commentsSchema ) ;
93
99
const Sessions = mongoose . model ( 'Sessions' , sessionSchema ) ;
94
100
const Projects = mongoose . model ( 'Projects' , projectSchema ) ;
95
101
96
102
module . exports = {
97
- Users, Comments, Sessions, Projects,
103
+ Users,
104
+ Comments,
105
+ Sessions,
106
+ Projects
98
107
} ;
0 commit comments