acf-like-fields
Version:
ACF-like dynamic field manager for Node.js + MongoDB
235 lines (178 loc) • 4.75 kB
Markdown
# 🧩 ACF-like Field Manager (Node.js + MongoDB)
A flexible and dynamic field system inspired by WordPress ACF (Advanced Custom Fields), built for **Node.js**, **Express**, and **MongoDB**.
This library lets you define custom field groups (like text, select, repeater, etc.) and attach them to any resource (products, posts, users, etc.), with support for nested and repeatable fields.
## ✨ Features
- 📁 Define field groups for any resource type (`product`, `post`, `user`, etc.)
- 🛠 Supports multiple field types: `text`, `select`, `radio`, `checkbox`, `repeater`
- 🔁 Repeater fields with nested fields (`repeaterFields`)
- 📦 Save dynamic field values for each resource
- 🧱 Extensible architecture for custom validations, conditionals, etc.
## 🚀 Installation
```bash
npm install acf-like-fields
```
Then mount the routes in your Express app:
```js
const acfRoutes = require("acf-like-fields");
app.use("/acf", acfRoutes);
```
## 📚 API Overview
### 📌 1. Create Field Group
**Endpoint:**
`POST /acf/field-groups`
Create a group of custom fields for a resource type.
#### ✅ Request Example:
```json
{
"title": "Product Meta Fields",
"location": "product",
"rules": [{ "key": "type", "value": "physical" }],
"fields": [
{
"name": "warranty",
"label": "Warranty",
"type": "text",
"required": true,
"defaultValue": "6 months"
},
{
"name": "colors",
"label": "Available Colors",
"type": "select",
"options": ["Red", "Green", "Blue"]
},
{
"name": "features",
"label": "Features",
"type": "repeater",
"repeaterFields": [
{
"name": "title",
"label": "Feature Title",
"type": "text"
},
{
"name": "description",
"label": "Feature Description",
"type": "text"
}
]
}
]
}
```
#### 🟢 Response:
```json
{
"message": "Field group created successfully",
"fieldGroup": { ... }
}
```
### 📌 2. Get Field Groups by Resource Type
**Endpoint:**
`GET /acf/field-groups/:resourceType`
Fetch field groups defined for a specific resource type.
#### 🟢 Response Example:
```json
[
{
"_id": "64f...",
"title": "Product Meta Fields",
"location": "product",
"fields": [
{
"name": "warranty",
"type": "text",
"label": "Warranty",
"defaultValue": "6 months"
},
...
]
}
]
```
### 📌 3. Save Field Values for a Resource
**Endpoint:**
`POST /acf/field-values/:resourceType/:resourceId`
Save custom field values for a specific resource (e.g. product, post).
#### 📝 Example:
```
POST /acf/field-values/product/6651e6241f8aab1234567890
```
#### ✅ Payload:
```json
{
"values": {
"warranty": "12 months",
"colors": "Red",
"features": [
{
"title": "Shockproof",
"description": "Drop-tested from 2m"
},
{
"title": "Waterproof",
"description": "IP67 Certified"
}
]
}
}
```
#### 🟢 Response:
```json
{
"message": "Field values saved",
"fieldValue": { ... }
}
```
### 📌 4. Get Field Values by Resource
**Endpoint:**
`GET /acf/field-values/:resourceType/:resourceId`
Fetch previously stored field values for a given resource.
#### 📝 Example:
```
GET /acf/field-values/product/6651e6241f8aab1234567890
```
#### 🟢 Response:
```json
{
"resourceType": "product",
"resourceId": "6651e6241f8aab1234567890",
"values": {
"warranty": "12 months",
"colors": "Red",
"features": [
{
"title": "Shockproof",
"description": "Drop-tested from 2m"
}
]
}
}
```
## 🧩 Field Types Supported
| Type | Description |
|-----------|----------------------------------|
| `text` | Simple text input |
| `select` | Dropdown with predefined options |
| `radio` | Radio buttons |
| `checkbox`| Single or multiple selection |
| `repeater`| Repeatable group of fields |
## 🛠 Planned Features
- 📋 Conditional logic for fields (e.g., show field if X is selected)
- ✅ Field validations (required, min/max, etc.)
- 🧩 Custom field type plugins
## 🤝 Contributing
We welcome PRs and issues! If you find a bug or want to suggest improvements, feel free to open an issue or contribute to the project.
## 📄 License
MIT License