@adinet/indigodb
v1.0.2
Published
ORM for PostgreSQL and MongoDB with real-time support
Downloads
161
Maintainers
Readme
IndigoDB
IndigoDB is a lightweight ORM for Node.js, supporting both PostgreSQL and MongoDB with real-time updates through WebSockets. Inspired by Firebase's real-time database functionality, IndigoDB simplifies API creation by abstracting database operations and providing built-in support for real-time data synchronization.
Features
- Dual Database Support: Works seamlessly with PostgreSQL and MongoDB.
- Real-Time Updates: Synchronizes database changes in real-time using WebSockets.
- Simple API: Developers can focus on logic without worrying about database or WebSocket configuration.
- TypeScript Support: Fully typed for improved developer experience.
- Schema-Based Models: Define models and let IndigoDB handle the rest (CRUD, real-time updates, etc.).
Installation
Backend (Node.js)
To use IndigoDB in your Node.js backend project:
npm install indigodb
Frontend (Angular) (Optional for Real-Time Frontend Integration)
Coming soon...
Getting Started
Backend Setup
Initialize IndigoDB: Use the
initialize
function to configure your database and WebSocket server.import { initialize, defineModel, DataTypes } from "indigodb"; // Initialize the ORM await initialize({ databaseType: "postgresql", // or 'mongodb' host: "localhost", port: 5432, // MongoDB users should provide `connectionString` instead user: "db_user", password: "db_password", database: "db_name", websocketPort: 8080, // WebSocket server port });
Define a Model: Define your models with schemas.
interface User { id: number; name: string; email: string; } const UserModel = defineModel<User>("users", { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, name: { type: DataTypes.STRING }, email: { type: DataTypes.STRING, unique: true }, });
Perform CRUD Operations: Use the model to interact with your database.
// Create a new user const user = await UserModel.create({ name: "John Doe", email: "[email protected]", }); // Find all users const users = await UserModel.findAll(); // Update a user const updatedUser = await UserModel.update(user.id, { name: "Jane Doe" }); // Delete a user await UserModel.delete(user.id);
Real-Time Frontend Integration
IndigoDB provides built-in real-time updates through WebSockets. Any changes in the database (insert, update, delete) are sent to connected clients.
Connect to WebSocket: On the frontend, connect to the WebSocket server:
const socket = new WebSocket("ws://localhost:8080"); socket.onmessage = (event) => { const message = JSON.parse(event.data); console.log("Real-time update:", message); };
Receive Updates: Handle real-time updates in your frontend application.
API Reference
Backend
initialize(config: Config): Promise<void>
Initializes the ORM with database and WebSocket configurations.
config
:databaseType
:'postgresql'
or'mongodb'
host
,port
,user
,password
,database
(PostgreSQL)connectionString
(MongoDB)websocketPort
: Port for the WebSocket server
defineModel<T>(name: string, schema: ModelSchema): Model<T>
Defines a model with the given schema.
name
: Name of the database table/collection.schema
: Object defining the model schema, including types and constraints.
Model Methods
create(data: Partial<T>): Promise<T>
Creates a new record in the database.findAll(criteria?: Partial<T>): Promise<T[]>
Retrieves all records matching the criteria.findById(id: any): Promise<T | null>
Retrieves a record by its ID.update(id: any, data: Partial<T>): Promise<T | null>
Updates a record by its ID.delete(id: any): Promise<T | null>
Deletes a record by its ID.
Supported Data Types
IndigoDB supports the following data types:
| IndigoDB Type | PostgreSQL Type | MongoDB Type |
| ------------- | --------------- | ------------ |
| INTEGER
| INTEGER
| Number
|
| STRING
| VARCHAR(255)
| String
|
| FLOAT
| REAL
| Number
|
| BOOLEAN
| BOOLEAN
| Boolean
|
| DATE
| TIMESTAMP
| Date
|
| TEXT
| TEXT
| String
|
| JSON
| JSON
| Object
|
Real-Time Updates
IndigoDB uses PostgreSQL triggers and MongoDB change streams to send real-time updates to connected WebSocket clients.
PostgreSQL Setup
IndigoDB automatically creates triggers for your models to notify changes via pg_notify
.
MongoDB Setup
Ensure MongoDB is running as a replica set to enable change streams.
Testing
To run tests, make sure you have jest
and the required database instances running. Use the following command:
npm test
Roadmap
- Add support for other databases (e.g., MySQL, SQLite).
- Provide a frontend library for Angular with full TypeScript support.
- Improve real-time capabilities with custom WebSocket events.
Contributing
We welcome contributions! Please follow these steps:
- Fork the repository.
- Create a new branch (
feature/my-new-feature
). - Commit your changes (
git commit -m 'Add my feature'
). - Push to the branch (
git push origin feature/my-new-feature
). - Open a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.