mirror of
https://github.com/MedzikUser/gatsby-pingbot
synced 2024-08-15 03:20:27 +00:00
25 lines
479 B
TypeScript
25 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
|