2021-09-16 20:47:50 +00:00
|
|
|
import { model, models, Schema } from 'mongoose'
|
|
|
|
|
|
|
|
const { MONGODB_COLLECTION } = process.env
|
2021-09-15 19:39:07 +00:00
|
|
|
|
|
|
|
interface URL {
|
|
|
|
url: string
|
|
|
|
}
|
|
|
|
|
|
|
|
const schema = new Schema<URL>({
|
|
|
|
url: {
|
|
|
|
type: String,
|
|
|
|
required: true,
|
|
|
|
unique: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-09-16 20:47:50 +00:00
|
|
|
schema.path(MONGODB_COLLECTION).validate(async function(value) {
|
2021-09-15 19:39:07 +00:00
|
|
|
const count = await models.URL.countDocuments({ url: value })
|
|
|
|
|
|
|
|
return !count
|
|
|
|
}, 'URL already exists')
|
|
|
|
|
2021-09-16 20:47:50 +00:00
|
|
|
const URLModel = model<URL>(MONGODB_COLLECTION, schema)
|
2021-09-15 19:39:07 +00:00
|
|
|
|
|
|
|
export = URLModel
|