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

easy-database-connector

v1.0.6

Published

A flexible database connector service with MSSQL support, pagination, caching, and encryption

Downloads

479

Readme

Easy Database Connector

A flexible and robust database connector service with built-in support for MSSQL, pagination, caching, encryption, and transactions.

Features

  • 🔒 Built-in encryption support for sensitive data
  • 🚀 Connection pooling for optimal performance
  • 📄 Pagination support with cache
  • 💾 Redis caching integration
  • 🔄 Transaction support
  • 🔌 Bulk operations
  • 📦 TypeScript support
  • 🛡️ Type safety

Installation

npm install easy-database-connector

Quick Start

import { query, execute, queryWithPagination, transaction } from 'easy-database-connector';

// Basic query
const users = await query<User>({
    sql: 'SELECT * FROM users WHERE active = @p0',
    parameters: [true]
});

// Paginated query with caching
const pagedUsers = await queryWithPagination<User>({
    sql: 'SELECT * FROM users',
    parameters: [],
    page: 1,
    pageSize: 10,
    orderBy: 'name ASC',
    cache: {
        key: 'users:page1',
        ttl: 300 // 5 minutes
    }
});

// Encrypted data
await execute({
    sql: 'INSERT INTO secure_data (data) VALUES (@p0)',
    parameters: ['sensitive information'],
    encryption: {
        open: true,
        data: ['0']
    }
});

// Transaction example
await transaction(async (trx) => {
    await execute({
        sql: 'INSERT INTO users (name) VALUES (@p0)',
        parameters: ['John'],
        transaction: trx
    });

    await execute({
        sql: 'INSERT INTO logs (action) VALUES (@p0)',
        parameters: ['user_created'],
        transaction: trx
    });
});

Configuration

Create a .env file:

# Database Configuration
DB_TYPE=mssql
DB_HOST=localhost
DB_USER=your_user
DB_PASSWORD=your_password
DB_DATABASE=your_database
DB_PORT=1433
DB_ENCRYPT=true

# For Encrypted Columns
MSSQL_SYNNETRIC_KEY_NAME=your_key_name
MSSQL_CERTIFICATE_NAME=your_cert_name

# Redis Configuration
REDIS_ENABLED=true
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=your_redis_password
REDIS_TTL=3600

API Reference

Query Functions

query<T>(input: ExecuteInput): Promise<T[]>

Execute a SELECT query with optional caching.

const users = await query<User>({
    sql: 'SELECT * FROM users',
    parameters: [],
    cache: {
        key: 'all-users',
        ttl: 3600
    }
});

queryWithPagination<T>(input: ExecuteInput): Promise<QueryResult<T>>

Execute a paginated query with total count.

const result = await queryWithPagination<User>({
    sql: 'SELECT * FROM users',
    page: 1,
    pageSize: 10,
    orderBy: 'created_at DESC'
});

execute(input: ExecuteInput): Promise<unknown[]>

Execute INSERT, UPDATE, DELETE queries or bulk operations.

// Bulk insert
await execute({
    sql: 'INSERT INTO users',
    parameters: users,
    bulk: {
        columns: [
            ['name', mssql.NVarChar(100)],
            ['email', mssql.NVarChar(100)]
        ],
        batchSize: 1000
    }
});

transaction<T>(callback: (trx: Transaction) => Promise<T>): Promise<T>

Execute multiple queries in a transaction.

await transaction(async (trx) => {
    await execute({
        sql: 'DELETE FROM users WHERE id = @p0',
        parameters: [1],
        transaction: trx
    });
});

Types

ExecuteInput

interface ExecuteOptions {
    sql: string;
    parameters?: SqlValue[];
    encryption?: {
        open: boolean;
        data: string[];
    };
    bulk?: {
        columns: ColumnType[];
        batchSize?: number;
    };
    page?: number;
    pageSize?: number;
    orderBy?: string;
    cache?: {
        key: string;
        ttl?: number;
    };
    transaction?: mssql.Transaction;
}

QueryResult

interface QueryResult<T> {
    data: T[];
    pagination: {
        page: number;
        pageSize: number;
        total: number;
        totalPages: number;
    };
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License

Support

For support, please open an issue in the GitHub repository.