-
Notifications
You must be signed in to change notification settings - Fork 1
/
schemas.js
58 lines (50 loc) · 1.23 KB
/
schemas.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
import mongoose from "mongoose";
// 카운터 스키마 설정
const counterSchema = mongoose.Schema({
_id: { type: String, required: true },
seq: { type: Number, default: 0 },
});
const counter = mongoose.model("counter", counterSchema);
// 채팅방 스키마 설정
const roomSchema = new mongoose.Schema({
roomId: { type: Number, unique: true },
participants: [String],
postId: Number,
postType: Number,
buyer: String,
seller: String,
buyer_enter: Date,
seller_enter: Date,
buyer_out: Date,
seller_out: Date,
});
// 채팅 스키마 설정
const chatSchema = new mongoose.Schema({
content: String,
roomId: Number,
username: String,
time: Date,
notificationId: String,
});
roomSchema.pre("save", async function (next) {
if (this.isNew) {
var doc = this;
try {
const counterDoc = await counter.findByIdAndUpdate(
{ _id: "roomId" },
{ $inc: { seq: 1 } },
{ new: true, upsert: true }
);
doc.roomId = counterDoc.seq;
next();
} catch (error) {
return next(error);
}
} else {
next();
}
});
// 스키마 생성
const chat = mongoose.model("chat", chatSchema);
const chatroom = mongoose.model("chatroom", roomSchema);
export { chat, chatroom };