🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

@inceptools/db

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@inceptools/db

IncepTools DB Service - A modular database adapter for MongoDB, SQL (Sequelize), and Redis.

1.0.2
latest
Version published
Maintainers
0
Created

@inceptools/db

A modular database adapter for MongoDB, SQL (Sequelize), and Redis.

NPM Version Package License NPM Downloads

Overview

@inceptools/db is a powerful database adapter that provides seamless integration with multiple databases, including:

  • MongoDB (via Mongoose)
  • SQL (via Sequelize)
  • Redis

It simplifies database management by offering a unified API for different database types, allowing for efficient and scalable application development.

Features

  • Unified Interface: Work with MongoDB, SQL databases, and Redis using a consistent API.
  • Multiple Connections: Connect to multiple databases of different types simultaneously.
  • Type Safety: Full TypeScript support with proper type inference.
  • Migration Support: Built-in migration tools for MongoDB and SQL databases.
  • Logging: Configurable logging for all database operations.

Installation

npm install @inceptools/db

or

yarn add @inceptools/db

Quick Start

import { DBService, SUPPORTED_DBS } from "@inceptools/db";
import mongoose from "mongoose";
import { Sequelize, DataTypes } from "sequelize";

// Define your database schemas/models
const db = {
	mongodb: {
		users: new mongoose.Schema({
			name: String,
			email: String,
		}),
	},
	postgres: {
		contacts: (sequelize: Sequelize) => {
			return sequelize.define("Contact", {
				name: {
					type: DataTypes.STRING,
					allowNull: false,
				},
				email: {
					type: DataTypes.STRING,
					allowNull: false,
				},
			});
		},
	},
};

// Configure your database connections
const config = {
	mongodb: {
		type: SUPPORTED_DBS.MONGO_DB,
		connectionString: "mongodb://localhost:27017/myapp",
		models: db.mongodb,
	},
	postgres: {
		type: SUPPORTED_DBS.SQL,
		connectionString: "postgresql://user:password@localhost:5432/myapp",
		models: db.postgres,
		configOptions: {
			dialect: "postgres",
		},
	},
	redis: {
		type: SUPPORTED_DBS.REDIS,
		connectionString: "redis://localhost:6379",
	},
};

// Create a database service instance
const dbService = new DBService(config);

// Connect to all databases
await dbService.connect();

// Use your database models
const users = await dbService.mongodb.users.find();
const contacts = await dbService.postgres.contacts.findAll();
await dbService.redis.set("key", "value");

// Close all connections when done
await dbService.closeConnection();

Individual Database Usage

MongoDB

import { MongoService, SUPPORTED_DBS } from "@inceptools/db";
import mongoose from "mongoose";

// Define your MongoDB schemas
const schemas = {
	users: new mongoose.Schema({
		name: String,
		email: String,
		createdAt: { type: Date, default: Date.now },
	}),
};

// Create a MongoDB service instance
const mongoService = new MongoService({
	type: SUPPORTED_DBS.MONGO_DB,
	connectionString: "mongodb://localhost:27017/myapp",
	models: schemas,
});

// Connect to MongoDB
await mongoService.connect();

// Use your models
const newUser = await mongoService.users.create({
	name: "John Doe",
	email: "john@example.com",
});

const users = await mongoService.users.find();

// Close the connection when done
await mongoService.closeConnection();

Migrations

MongoDB Migrations

// Create a migration service
const mongoService = dbService.mongodb;
const migrationService = mongoService.migrationService;

// Initialize migrations
await migrationService.init();

// Generate a new migration
await migrationService.generateMigration("add-user-roles");

// Apply pending migrations
await migrationService.migrate();

SQL Migrations

// Create a migration service
const sqlService = dbService.postgres;
const migrationService = sqlService.migrationService;

// Initialize migrations
await migrationService.init();

// Generate a new migration
await migrationService.generateMigration("add-order-status");

// Apply pending migrations
await migrationService.migrate();

Support

@inceptools/db is an open-source project under the MIT license. If you find this package useful, consider supporting it by contributing or reporting issues on GitHub.

Stay in Touch

License

@inceptools/db is MIT licensed.

FAQs

Package last updated on 11 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts