npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@adinet/indigodb

v1.0.2

Published

ORM for PostgreSQL and MongoDB with real-time support

Downloads

161

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

  1. 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
    });
  2. 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 },
    });
  3. 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.

  1. 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);
    };
  2. 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

  1. create(data: Partial<T>): Promise<T> Creates a new record in the database.

  2. findAll(criteria?: Partial<T>): Promise<T[]> Retrieves all records matching the criteria.

  3. findById(id: any): Promise<T | null> Retrieves a record by its ID.

  4. update(id: any, data: Partial<T>): Promise<T | null> Updates a record by its ID.

  5. 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:

  1. Fork the repository.
  2. Create a new branch (feature/my-new-feature).
  3. Commit your changes (git commit -m 'Add my feature').
  4. Push to the branch (git push origin feature/my-new-feature).
  5. Open a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.