Unified DB Layer

A cross-platform unified database layer that automatically detects your runtime environment and uses the appropriate storage backend with a familiar Prisma-like API.
✨ Features
- 🌍 Cross-platform: Works seamlessly in Browser, Node.js, Electron, and React Native
- 🔍 Auto-detection: Automatically selects the best storage backend for your environment
- 📊 Prisma-like API: Familiar and intuitive database operations with method chaining
- 📴 Offline-first: No network dependencies, works entirely offline
- ⚡ Zero-config: Works out of the box with sensible defaults
- 🎯 TypeScript: Full type support included
- 🔒 Data validation: Built-in validation with custom rules
- 🪝 Hooks system: Extensible middleware for custom logic
- 📦 Lightweight: Minimal bundle size with tree-shaking support
- 🔄 Import/Export: Easy data backup and migration
🚀 Quick Start
Installation
npm install unified-db-layer
For React Native projects, also install:
npm install @react-native-async-storage/async-storage
Basic Usage
import { UnifiedDB } from "unified-db-layer";
const db = new UnifiedDB({
models: {
User: {
fields: {
id: { type: "string", primaryKey: true },
name: { type: "string", required: true },
email: { type: "string", required: true },
age: { type: "number", default: 0 },
createdAt: { type: "date", default: "now" },
},
},
Post: {
fields: {
id: { type: "string", primaryKey: true },
title: { type: "string", required: true },
content: { type: "string", required: true },
authorId: { type: "string", required: true },
published: { type: "boolean", default: false },
},
},
},
});
await db.init();
const user = await db.user.create({
name: "John Doe",
email: "john@example.com",
age: 30,
});
const users = await db.user.findMany({
where: { age: { gte: 18 } },
orderBy: { name: "asc" },
take: 10,
});
const post = await db.post.create({
title: "Hello World",
content: "This is my first post!",
authorId: user.id,
published: true,
});
🌐 Platform Support
| Browser | IndexedDB (preferred) | ~50MB+ | Until cleared |
| Browser | localStorage (fallback) | ~5-10MB | Until cleared |
| Node.js | File System | Disk space | Permanent |
| Electron | File System | Disk space | Permanent |
| React Native | AsyncStorage | Platform dependent | Until uninstalled |
The library automatically detects your environment and chooses the best available storage backend.
📚 Documentation
🎯 Use Cases
- Offline-first applications - Work without internet connectivity
- Cross-platform development - Same API across web, mobile, and desktop
- Rapid prototyping - Quick setup without external database dependencies
- Local data caching - Store API responses and user preferences
- Development and testing - Mock databases for testing environments
🔧 Advanced Features
Query Builder
const results = await db.user
.where("age", ">", 18)
.where("email", "contains", "@company.com")
.orderBy("createdAt", "desc")
.limit(50)
.findMany();
Hooks and Middleware
db.addHook("beforeCreate", (modelName, data) => {
data.createdBy = getCurrentUserId();
return data;
});
db.addHook("afterUpdate", (modelName, data) => {
console.log(`Updated ${modelName}:`, data.id);
});
Data Import/Export
const backup = await db.export();
await db.import(backup, { clearFirst: true });
🤝 Contributing
We welcome contributions! Please see our Contributing Guide for details.
Development Setup
git clone https://github.com/yourusername/unified-db-layer.git
cd unified-db-layer
npm install
npm test
npm run build
npm run lint
Running Tests
npm test
npm run test:watch
npm run test:coverage
📋 Requirements
- Node.js: >= 14.0.0
- Browsers: Modern browsers with ES2018+ support
- React Native: >= 0.60.0 (with AsyncStorage)
🐛 Issues and Support
📈 Roadmap
🏆 Acknowledgments
- Inspired by Prisma for the API design
- Built with Rollup for optimal bundling
- Tested with Jest for reliability
📄 License
MIT © Your Name
Made with ❤️ for the JavaScript community