Skip to content

stackpress/inquire

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ’¬ Inquire

NPM Package Tests Status Coverage Status Commits License

Super lightweight generic typed SQL query builder, SQL dialects and composite engine. Schema builder, but no ORM. Bring your own database library.

What is Inquire?

Inquire is a powerful yet lightweight SQL query builder that provides a unified interface for working with multiple database engines. Unlike traditional ORMs, Inquire focuses on query building and execution while letting you bring your own database connection library. This approach gives you the flexibility to use your preferred database driver while benefiting from a consistent, type-safe query building experience.

Key Features

  • πŸͺΆ Lightweight: No ORM overhead - just pure query building
  • πŸ”§ Generic Typed: Full TypeScript support with generic types for enhanced type safety
  • πŸ—„οΈ Multi-Database: Same expressive query builder pattern for all SQL engines
  • πŸ”„ Unified Interface: Consistent API across different database engines
  • πŸ“ Schema Builder: Create and modify database schemas programmatically
  • πŸ”— Template Strings: Support for type-safe template string query building
  • ⚑ Transaction Support: Common transaction pattern across all engines
  • 🎯 Dialect Agnostic: Query builders work with any supported SQL dialect

Supported Databases

Inquire supports a wide range of database engines through dedicated connection packages:

  • MySQL - via @stackpress/inquire-mysql2 (Node MySQL2)
  • PostgreSQL - via @stackpress/inquire-pg (Node PostGres pg)
  • SQLite - via @stackpress/inquire-sqlite3 (Better SQLite3)
  • PGLite - via @stackpress/inquire-pglite (PGLite)
  • CockroachDB - Compatible with PostgreSQL adapter
  • NeonDB - Compatible with PostgreSQL adapter
  • Vercel Postgres - Compatible with PostgreSQL adapter
  • Supabase - Compatible with PostgreSQL adapter

Installation

Install the core library:

npm install @stackpress/inquire

Then install the appropriate database adapter:

# For MySQL
npm install @stackpress/inquire-mysql2 mysql2

# For PostgreSQL
npm install @stackpress/inquire-pg pg

# For SQLite
npm install @stackpress/inquire-sqlite3 better-sqlite3

# For PGLite
npm install @stackpress/inquire-pglite @electric-sql/pglite

Quick Start

MySQL Connection

import mysql from 'mysql2/promise';
import connect from '@stackpress/inquire-mysql2';

// Create the raw database connection
const resource = await mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'inquire',
});

// Map the resource to the Inquire engine
const engine = connect(resource);

PostgreSQL Connection

import { Client, Pool } from 'pg';
import connect from '@stackpress/inquire-pg';

// Using a Pool
const pool = new Pool({
  database: 'inquire',
  user: 'postgres'
});
const connection = await pool.connect();

// Or using a Client
const client = new Client({
  database: 'inquire',
  user: 'postgres'
});
await client.connect();

// Map the resource to the Inquire engine
const engine = connect(connection); // or connect(client)

SQLite Connection

import sqlite from 'better-sqlite3';
import connect from '@stackpress/inquire-sqlite3';

// Create the raw database connection
const resource = sqlite(':memory:');

// Map the resource to the Inquire engine
const engine = connect(resource);

Basic Usage

Once you have an engine instance, you can start building and executing queries:

// Create a table
await engine.create('users')
  .addField('id', { type: 'INTEGER', autoIncrement: true })
  .addField('name', { type: 'VARCHAR', length: 255 })
  .addField('email', { type: 'VARCHAR', length: 255 })
  .addPrimaryKey('id');

// Insert data
await engine
  .insert('users')
  .values({ name: 'John Doe', email: 'john@example.com' });

// Select data
const users = await engine
  .select('*')
  .from('users')
  .where('name = ?', ['John Doe']);

console.log(users);

// Update data
await engine
  .update('users')
  .set({ email: 'john.doe@example.com' })
  .where('id = ?', [1]);

// Delete data
await engine
  .delete('users')
  .where('id = ?', [1]);

Query Builders

Inquire provides comprehensive query builders for all common SQL operations:

  • Create - Create tables and schemas
  • Alter - Modify existing tables
  • Select - Query data with joins, conditions, and aggregations
  • Insert - Insert single or multiple records
  • Update - Update existing records
  • Delete - Delete records with conditions

Template String Queries

For complex queries, you can use type-safe template strings:

type User = {
  id: number;
  name: string;
  email: string;
};

const userId = 123;
const results = await engine.sql<User>`
  SELECT u.*, p.title 
  FROM users u 
  LEFT JOIN posts p ON u.id = p.user_id 
  WHERE u.id = ${userId}
`;
// results is typed as User[]

Transactions

Execute multiple queries in a transaction:

const result = await engine.transaction(async (trx) => {
  await trx.insert('users').values({ name: 'Alice' });
  await trx.insert('posts').values({ title: 'Hello World', user_id: 1 });
  return 'success';
});

Type Safety

Inquire is designed with TypeScript in mind, providing full type safety:

type User = {
  id: number;
  name: string;
  email: string;
};

// Type-safe queries
const users = await engine.select<User>('*').from('users');
// users is now typed as User[]

const user = await engine.select<User>('*')
  .from('users')
  .where('id = ?', [1])
  .limit(1);
// user is typed as User[]

API Documentation

For detailed API documentation, see:

Examples

Check out the examples directory for complete working examples with different database engines:

About

Strong typed SQL

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •