gatsby-pingbot/lib/schema.ts

28 lines
450 B
TypeScript
Raw Normal View History

2021-09-16 20:47:50 +00:00
import { model, models, Schema } from 'mongoose'
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-17 19:38:34 +00:00
schema.path('url').validate(async function(value) {
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-09-17 19:38:34 +00:00
const URLModel = model<URL>('url', schema)
2021-09-15 19:39:07 +00:00
export = URLModel