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

sg-db-connector

v1.0.0-beta.2

Published

`sg-db-connector` is a flexible, environment-driven npm package that facilitates easy connections to various databases like PostgreSQL, MySQL, MongoDB, and SQLite. This package follows SOLID principles and is designed to be simple, modular, and easy to in

Downloads

4

Readme

SG DB Connector

sg-db-connector is a flexible, environment-driven npm package that facilitates easy connections to various databases like PostgreSQL, MySQL, MongoDB, and SQLite. This package follows SOLID principles and is designed to be simple, modular, and easy to integrate into any Node.js project.

Features

  • Supports multiple databases (PostgreSQL, MySQL, MongoDB, and SQLite).
  • Configurable via environment variables.
  • Provides a clean and consistent interface for database connections.
  • Easy to integrate into any Node.js project.
  • Includes logging functionality using winston for monitoring and debugging.
  • Follows Clean Code and SOLID principles.

Table of Contents

Installation

To install the package, run the following command:

npm install sg-db-connector

Usage

Environment Configuration

Before using this package, you need to set up your environment variables in a .env file. This file should include URLs for the databases you want to connect to:

# PostgreSQL connection string
POSTGRES_URL=postgres://user:password@localhost:5432/mydatabase

# MySQL connection string
MYSQL_URL=mysql://user:password@localhost:3306/mydatabase

# MongoDB connection string
MONGODB_URL=mongodb://localhost:27017/mydatabase

# SQLite file path
SQLITE_URL=./mydatabase.db

Connecting to Databases

You can import the connection classes provided by this package and connect to different databases as needed.

import { PostgreSQLConnection } from "sg-db-connector/src/connections/postgresql.connection";
import { MySQLConnection } from "sg-db-connector/src/connections/mysql.connection";
import { MongoDBConnection } from "sg-db-connector/src/connections/mongodb.connection";
import { SQLiteConnection } from "sg-db-connector/src/connections/sqlite.connection";

// PostgreSQL
const postgresConnection = new PostgreSQLConnection();
await postgresConnection.connect();
await postgresConnection.disconnect();

// MySQL
const mysqlConnection = new MySQLConnection();
await mysqlConnection.connect();
await mysqlConnection.disconnect();

// MongoDB
const mongoConnection = new MongoDBConnection();
await mongoConnection.connect();
await mongoConnection.disconnect();

// SQLite
const sqliteConnection = new SQLiteConnection();
await sqliteConnection.connect();
await sqliteConnection.disconnect();

Logging

This package includes a built-in logger using the winston library. You can use it for logging messages, warnings, errors, and debug information.

Example:

import logger from "sg-db-connector/src/utils/logger";

logger.info("This is an info message");
logger.warn("This is a warning message");
logger.error("This is an error message");
logger.debug("This is a debug message");

Examples

Connecting to PostgreSQL:

import { PostgreSQLConnection } from "sg-db-connector/src/connections/postgresql.connection";

async function main() {
  const postgresConnection = new PostgreSQLConnection();
  await postgresConnection.connect();

  // Perform your database operations here

  await postgresConnection.disconnect();
}

main();

Connecting to MySQL:

import { MySQLConnection } from "sg-db-connector/src/connections/mysql.connection";

async function main() {
  const mysqlConnection = new MySQLConnection();
  await mysqlConnection.connect();

  // Perform your database operations here

  await mysqlConnection.disconnect();
}

main();

Author

SolGuruz® LLP

Developer

Rahul Rathod