const mongoose = require("mongoose"); const jsonHelper = require("../utils/jsonHelper"); function createSingletonPageModel(modelName, collectionName, dataFileName) { const schema = new mongoose.Schema( {}, { strict: false, timestamps: true, collection: collectionName, }, ); schema.statics.getSingle = async function getSingle() { let doc = await this.findOne(); if (!doc) { const defaultData = jsonHelper.readJsonFile(dataFileName) || {}; doc = await this.create(defaultData); } return doc; }; return mongoose.model(modelName, schema); } module.exports = createSingletonPageModel;