-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebPushSubscription.ts
More file actions
34 lines (30 loc) · 1.13 KB
/
WebPushSubscription.ts
File metadata and controls
34 lines (30 loc) · 1.13 KB
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
import mongoose, { Schema, Document } from 'mongoose'
export interface IWebPushSubscription extends Document {
clerkUserId?: string
endpoint: string
keys: {
p256dh: string
auth: string
}
userAgent?: string
userRole: string
userEmail?: string
userName?: string
createdAt: Date
updatedAt: Date
}
const webPushSubscriptionSchema = new Schema<IWebPushSubscription>({
clerkUserId: { type: String, required: false },
endpoint: { type: String, required: true },
keys: {
p256dh: { type: String, required: true },
auth: { type: String, required: true }
},
userAgent: { type: String },
userRole: { type: String, required: true, default: 'user' },
userEmail: { type: String },
userName: { type: String }
}, { timestamps: true })
webPushSubscriptionSchema.index({ endpoint: 1 }, { unique: true })
webPushSubscriptionSchema.index({ clerkUserId: 1 }, { unique: true, sparse: true }) // Sparse allows nulls, but ensures one subscription per user
export default mongoose.models.WebPushSubscription || mongoose.model<IWebPushSubscription>('WebPushSubscription', webPushSubscriptionSchema, 'web-push-subscriptions')