import { model, models, Schema } from 'mongoose' const { MONGODB_COLLECTION } = process.env interface URL { url: string } const schema = new Schema({ 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(MONGODB_COLLECTION, schema) export = URLModel