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

queryforge

Package Overview
Dependencies
Maintainers
0
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

queryforge

A powerful, type-safe SQL query builder for Node.js with support for MySQL, PostgreSQL, and SQLite. Features fluent API, transaction management, and connection pooling.

0.1.3
latest
Source
npm
Version published
Weekly downloads
7
-30%
Maintainers
0
Weekly downloads
 
Created
Source

QueryForge

npm version License: MIT TypeScript Build Status

A powerful, type-safe SQL query builder primarily designed for TypeScript, with support for MySQL, PostgreSQL, and SQLite.

Features

  • 🔒 Type Safety: Full TypeScript integration with type definitions
  • 🎯 Fluent API: Easy to read and write query chains
  • 🔄 Multi-Database Support: MySQL, PostgreSQL, and SQLite
  • 🛡️ Security: SQL injection protection with parameterized queries
  • 🎭 Transaction Management: ACID compliant operations
  • 🔌 Connection Pool: Automatic connection management

Installation

npm install queryforge

JavaScript vs TypeScript Usage

QueryForge is primarily designed for TypeScript, but you can use it in JavaScript projects in two ways:

const { QueryForge } = require('queryforge');

const qf = new QueryForge({
  type: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: '',
  database: 'test_db'
});

// Use normally
async function example() {
  await qf.connect();
  const users = await qf
    .table('users')
    .select('*')
    .execute();
}

2. Using TypeScript with JavaScript

// @ts-check
const { QueryForge } = require('queryforge');

/** @type {import('queryforge').DatabaseConfig} */
const config = {
  type: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: '',
  database: 'test_db'
};

// Now you get TypeScript type checking in JS

Quick Start (TypeScript)

import { QueryForge } from 'queryforge';

// Database configuration
const config = {
  type: 'mysql', // or 'postgres', 'sqlite'
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: '',
  database: 'test_db'
};

// Create QueryForge instance
const qf = new QueryForge(config, { logging: true });

// Basic Usage Examples
async function examples() {
  // Connect to database
  await qf.connect();

  try {
    // SELECT example
    const users = await qf
      .table('users')
      .select('id', 'name', 'email')
      .where('age', '>', 18)
      .orderBy('name', 'ASC')
      .limit(10)
      .execute();

    // INSERT example
    const newUser = await qf
      .table('users')
      .insert({
        name: 'John Doe',
        email: 'john@example.com',
        age: 25
      })
      .execute();

    // UPDATE example
    await qf
      .table('users')
      .update({ status: 'active' })
      .where('id', '=', 1)
      .execute();

    // DELETE example
    await qf
      .table('users')
      .delete()
      .where('status', '=', 'inactive')
      .execute();

    // Transaction example
    await qf.beginTransaction();
    try {
      await qf
        .table('orders')
        .insert({
          user_id: 1,
          total: 100
        })
        .execute();

      await qf
        .table('users')
        .update({ order_count: qf.raw('order_count + 1') })
        .where('id', '=', 1)
        .execute();

      await qf.commit();
    } catch (error) {
      await qf.rollback();
      throw error;
    }
  } catch (error) {
    console.error('Error:', error);
  } finally {
    await qf.disconnect();
  }
}

Advanced Examples

Complex SELECT Queries

const result = await qf
  .table('users as u')
  .select('u.id', 'u.name', 'o.total as order_total')
  .join('orders as o', 'u.id', '=', 'o.user_id')
  .where('u.status', '=', 'active')
  .andWhere('o.created_at', '>', '2024-01-01')
  .groupBy('u.id')
  .having('COUNT(o.id)', '>', 5)
  .orderBy('u.name', 'ASC')
  .limit(10)
  .offset(0)
  .execute();

Bulk Insert Operations

const users = [
  { name: 'Alice', email: 'alice@example.com' },
  { name: 'Bob', email: 'bob@example.com' }
];

await qf
  .table('users')
  .insertMany(users)
  .execute();

Contributing

  • Fork the repository
  • Create your feature branch (git checkout -b feature/amazing-feature)
  • Commit your changes (git commit -m 'feat: Add amazing feature')
  • Push to the branch (git push origin feature/amazing-feature)
  • Open a Pull Request

License

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

Keywords

sql

FAQs

Package last updated on 10 Jan 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