gatsby-pingbot/lib/schema.ts

26 lines
479 B
TypeScript

import { model, models, Schema } from 'mongoose'
const { MONGODB_COLLECTION } = process.env
interface URL {
url: string
}
const schema = new Schema<URL>({
url: {
type: String,
required: true,
unique: true
}
})
schema.path(MONGODB_COLLECTION).validate(async function(value) {
const count = await models.URL.countDocuments({ url: value })
return !count
}, 'URL already exists')
const URLModel = model<URL>(MONGODB_COLLECTION, schema)
export = URLModel