-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
65 lines (59 loc) · 1.42 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
const mongoose = require('mongoose');
const validator = require('validator');
const MONGO_URI = 'mongodb://127.0.0.1:27017/userPosts';
// const MONGO_URI = 'mongodb+srv://adhildspot:[email protected]/?retryWrites=true&w=majority';
//'mongodb://127.0.0.1:27017/adhildspot/userPostApp';
mongoose.connect(MONGO_URI, {
})
.then(() => {
console.log('Connected to MongoDB');
})
.catch((error) => {
console.error('Error connecting to MongoDB:', error);
});
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
mobileNo: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
lowercase: true,
validate: {
validator: validator.isEmail,
message: 'Invalid email address',
},
},
createdAt: {
type: Date,
default: Date.now,
},
});
const postSchema = new mongoose.Schema({
postTitle: {
type: String,
required: true,
},
postContent: {
type: String,
required: true,
},
createdAt: {
type: Date,
default: Date.now,
},
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Users',
required: true,
},
});
const users = mongoose.model("users", userSchema);
const posts = mongoose.model("userPosts", postSchema);
module.exports = { users, posts };