UNPKG

acf-like-fields

Version:

ACF-like dynamic field manager for Node.js + MongoDB

25 lines (21 loc) 773 B
const mongoose = require("mongoose"); const FieldSchema = new mongoose.Schema({ name: String, label: String, type: String, // text, select, repeater, etc. options: [String], // for select, checkbox, radio required: Boolean, defaultValue: mongoose.Schema.Types.Mixed, repeaterFields: [this], // will throw error }, { _id: false }); // optional: no _id for embedded // FIX: Use function reference FieldSchema.add({ repeaterFields: [FieldSchema] // recursion }); const FieldGroupSchema = new mongoose.Schema({ title: String, location: String, // e.g., "post", "product" rules: [{ key: String, value: String }], fields: [FieldSchema], }, { timestamps: true }); module.exports = mongoose.model("FieldGroup", FieldGroupSchema);