mirror of
https://github.com/MedzikUser/gatsby-pingbot
synced 2024-08-15 03:20:27 +00:00
25 lines
486 B
TypeScript
25 lines
486 B
TypeScript
import { model, models, Schema } from 'mongoose'
|
|
|
|
import type { URLSchema } from '../types/mongo'
|
|
|
|
const schema = new Schema<URLSchema>({
|
|
url: {
|
|
type: String,
|
|
required: true,
|
|
unique: true
|
|
}
|
|
})
|
|
|
|
schema.path('url').validate(async function(value: string) {
|
|
if (models.url) {
|
|
return true
|
|
}
|
|
|
|
const count = await models.url.countDocuments({ url: value })
|
|
|
|
return !count
|
|
}, 'URL already exists')
|
|
|
|
const URLModel = model<URLSchema>('url', schema)
|
|
|
|
export = URLModel
|