dbnext
v1.2.0
Published
File-based JSON DB with model + file upload support
Downloads
3
Maintainers
Readme
dbnext
A lightweight JSON-based file database with optional file uploads — perfect for CMS-like systems and editable sites using Next.js.
🚀 Features
- ✅ Simple JSON file-based storage (no DB required)
- 📂 File upload support using
formidable - 🧩 Optional schema modeling via JS model files
- 🔍 Nested path access like
db.get("hero.title") - 📝 Easy integration with Next.js API routes
- 🕒 Snapshot history & revert functionality
📦 Installation
npm install dbnext📁 Project Structure
your-app/
├── api/
│ └── content.js # Next.js API using dbnext + upload
├── models/
│ └── headerModel.js # Optional default model structure
├── public/
│ └── uploads/ # File uploads go here
├── data/
│ └── content.json # Stored JSON data
└── node_modules/🧠 How It Works
- Uses
fsto persist data into a.jsonfile - Allows nested updates via paths like
hero.title - Supports file uploads (e.g., images) and stores path
- Automatically creates file if not found
- Tracks snapshots for undo support
📘 Basic Usage
1. Initialize with model
const dbnext = require('dbnext');
const headerModel = require('./models/headerModel');
const db = new dbnext('data/content.json', headerModel);2. Update data
db.set('hero.title', 'Welcome to my portfolio');3. Access data
console.log(db.get('hero.title')); // → "Welcome to my portfolio"4. Push to array
db.push('projects', { name: 'Portfolio', link: 'https://...' });📤 File Upload Integration (API)
Use this in pages/api/content.js in a Next.js project:
import { IncomingForm } from 'formidable';
import fs from 'fs-extra';
import path from 'path';
import dbnext from 'dbnext';
import headerModel from '@/models/headerModel';
export const config = {
api: { bodyParser: false },
};
const uploadDir = path.join(process.cwd(), 'public/uploads');
fs.ensureDirSync(uploadDir);
const db = new dbnext('data/content.json', headerModel);
export default async function handler(req, res) {
if (req.method === 'GET') {
return res.status(200).json(db.get());
}
if (req.method === 'POST') {
const form = new IncomingForm({ uploadDir, keepExtensions: true });
form.parse(req, (err, fields, files) => {
if (err) return res.status(500).json({ error: 'Upload failed' });
const updated = {
...fields,
...(files?.image?.[0] && {
image: `/uploads/${path.basename(files.image[0].filepath)}`
}),
};
Object.entries(updated).forEach(([key, value]) => {
db.set(key, Array.isArray(value) ? value[0] : value);
});
res.status(200).json({ success: true, data: db.get() });
});
} else {
res.status(405).json({ error: 'Method not allowed' });
}
}📂 Example Model (headerModel.js)
module.exports = {
hero: {
title: '',
subtitle: '',
image: ''
},
about: {
bio: '',
image: ''
},
projects: [],
contact: {
email: '',
phone: ''
}
};🔁 Revert Support
db.set('hero.title', 'New Title', { track: true });
db.revert(); // undo last change🧪 Sample API Test
curl http://localhost:3000/api/contentcurl -X POST http://localhost:3000/api/content \
-F "hero.title=Updated" \
-F "image=@/path/to/image.png"🛡 License
MIT © Saurabh Singh
