2021-09-16 20:47:50 +00:00
|
|
|
import { model, models, Schema } from 'mongoose'
|
|
|
|
|
2021-10-03 12:55:49 +00:00
|
|
|
import type { URLSchema } from '../types/mongo'
|
2021-09-15 19:39:07 +00:00
|
|
|
|
2021-10-03 12:55:49 +00:00
|
|
|
const schema = new Schema<URLSchema>({
|
2021-09-15 19:39:07 +00:00
|
|
|
url: {
|
|
|
|
type: String,
|
|
|
|
required: true,
|
|
|
|
unique: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-10-03 12:55:49 +00:00
|
|
|
schema.path('url').validate(async function(value: string) {
|
2021-09-17 19:38:34 +00:00
|
|
|
if (models.url) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
const count = await models.url.countDocuments({ url: value })
|
2021-09-15 19:39:07 +00:00
|
|
|
|
|
|
|
return !count
|
|
|
|
}, 'URL already exists')
|
|
|
|
|
2021-10-03 12:55:49 +00:00
|
|
|
const URLModel = model<URLSchema>('url', schema)
|
2021-09-15 19:39:07 +00:00
|
|
|
|
|
|
|
export = URLModel
|