|
8 | 8 |
|
9 | 9 | Super lightweight generic typed SQL query builder, SQL dialects and composite engine. Schema builder, but no ORM. Bring your own database library. |
10 | 10 |
|
11 | | - - Node MySQL2 |
12 | | - - Better SQLite3 |
13 | | - - Node PostGres (pg) |
14 | | - - PGLite |
15 | | - - CockroachDB |
16 | | - - NeonDB |
17 | | - - Vercel Postgres |
| 11 | +## What is Inquire? |
18 | 12 |
|
19 | | -## Install |
| 13 | +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. |
| 14 | + |
| 15 | +## Key Features |
| 16 | + |
| 17 | +- **🪶 Lightweight**: No ORM overhead - just pure query building |
| 18 | +- **🔧 Generic Typed**: Full TypeScript support with generic types for enhanced type safety |
| 19 | +- **🗄️ Multi-Database**: Same expressive query builder pattern for all SQL engines |
| 20 | +- **🔄 Unified Interface**: Consistent API across different database engines |
| 21 | +- **📝 Schema Builder**: Create and modify database schemas programmatically |
| 22 | +- **🔗 Template Strings**: Support for type-safe template string query building |
| 23 | +- **⚡ Transaction Support**: Common transaction pattern across all engines |
| 24 | +- **🎯 Dialect Agnostic**: Query builders work with any supported SQL dialect |
| 25 | + |
| 26 | +## Supported Databases |
| 27 | + |
| 28 | +Inquire supports a wide range of database engines through dedicated connection packages: |
| 29 | + |
| 30 | +- **MySQL** - via `@stackpress/inquire-mysql2` (Node MySQL2) |
| 31 | +- **PostgreSQL** - via `@stackpress/inquire-pg` (Node PostGres pg) |
| 32 | +- **SQLite** - via `@stackpress/inquire-sqlite3` (Better SQLite3) |
| 33 | +- **PGLite** - via `@stackpress/inquire-pglite` (PGLite) |
| 34 | +- **CockroachDB** - Compatible with PostgreSQL adapter |
| 35 | +- **NeonDB** - Compatible with PostgreSQL adapter |
| 36 | +- **Vercel Postgres** - Compatible with PostgreSQL adapter |
| 37 | +- **Supabase** - Compatible with PostgreSQL adapter |
| 38 | + |
| 39 | +## Installation |
| 40 | + |
| 41 | +Install the core library: |
20 | 42 |
|
21 | 43 | ```bash |
22 | | -$ npm i @stackpress/inquire |
| 44 | +npm install @stackpress/inquire |
23 | 45 | ``` |
24 | 46 |
|
25 | | -## MySQL Connection |
| 47 | +Then install the appropriate database adapter: |
| 48 | + |
| 49 | +```bash |
| 50 | +# For MySQL |
| 51 | +npm install @stackpress/inquire-mysql2 mysql2 |
| 52 | + |
| 53 | +# For PostgreSQL |
| 54 | +npm install @stackpress/inquire-pg pg |
| 55 | + |
| 56 | +# For SQLite |
| 57 | +npm install @stackpress/inquire-sqlite3 better-sqlite3 |
| 58 | + |
| 59 | +# For PGLite |
| 60 | +npm install @stackpress/inquire-pglite @electric-sql/pglite |
| 61 | +``` |
26 | 62 |
|
27 | | -```js |
| 63 | +## Quick Start |
| 64 | + |
| 65 | +### MySQL Connection |
| 66 | + |
| 67 | +```typescript |
28 | 68 | import mysql from 'mysql2/promise'; |
29 | 69 | import connect from '@stackpress/inquire-mysql2'; |
30 | 70 |
|
31 | | -//this is the raw resource, anything you want |
| 71 | +// Create the raw database connection |
32 | 72 | const resource = await mysql.createConnection({ |
33 | 73 | host: 'localhost', |
34 | 74 | user: 'root', |
35 | 75 | database: 'inquire', |
36 | 76 | }); |
37 | | -//this maps the resource to the engine |
| 77 | + |
| 78 | +// Map the resource to the Inquire engine |
38 | 79 | const engine = connect(resource); |
39 | 80 | ``` |
40 | 81 |
|
41 | | -## PostGreSQL Connection |
| 82 | +### PostgreSQL Connection |
42 | 83 |
|
43 | | -```js |
| 84 | +```typescript |
44 | 85 | import { Client, Pool } from 'pg'; |
45 | 86 | import connect from '@stackpress/inquire-pg'; |
46 | 87 |
|
47 | | -//this is the raw resource, anything you want |
48 | | -const connection = usePool |
49 | | - ? await (async () => { |
50 | | - const pool = new Pool({ |
51 | | - database: 'inquire', |
52 | | - user: 'postgres' |
53 | | - }); |
54 | | - return await pool.connect(); |
55 | | - })() |
56 | | - : await (async () => { |
57 | | - const resource = new Client({ |
58 | | - database: 'inquire', |
59 | | - user: 'postgres' |
60 | | - }); |
61 | | - await resource.connect(); |
62 | | - return resource; |
63 | | - })(); |
64 | | - |
65 | | -//this maps the resource to the engine |
66 | | -const engine = connect(connection); |
| 88 | +// Using a Pool |
| 89 | +const pool = new Pool({ |
| 90 | + database: 'inquire', |
| 91 | + user: 'postgres' |
| 92 | +}); |
| 93 | +const connection = await pool.connect(); |
| 94 | + |
| 95 | +// Or using a Client |
| 96 | +const client = new Client({ |
| 97 | + database: 'inquire', |
| 98 | + user: 'postgres' |
| 99 | +}); |
| 100 | +await client.connect(); |
| 101 | + |
| 102 | +// Map the resource to the Inquire engine |
| 103 | +const engine = connect(connection); // or connect(client) |
67 | 104 | ``` |
68 | 105 |
|
69 | | -## SQLite Connection |
| 106 | +### SQLite Connection |
70 | 107 |
|
71 | | -```js |
| 108 | +```typescript |
72 | 109 | import sqlite from 'better-sqlite3'; |
73 | 110 | import connect from '@stackpress/inquire-sqlite3'; |
74 | 111 |
|
75 | | -//this is the raw resource, anything you want |
| 112 | +// Create the raw database connection |
76 | 113 | const resource = sqlite(':memory:'); |
77 | | -//this maps the resource to the engine |
| 114 | + |
| 115 | +// Map the resource to the Inquire engine |
78 | 116 | const engine = connect(resource); |
79 | 117 | ``` |
80 | 118 |
|
81 | | -## Usage |
| 119 | +## Basic Usage |
| 120 | + |
| 121 | +Once you have an engine instance, you can start building and executing queries: |
82 | 122 |
|
83 | | -```js |
84 | | -const create = engine.create('profile') |
85 | | - .addField('id', { type: 'INTEGER' }) |
| 123 | +```typescript |
| 124 | +// Create a table |
| 125 | +await engine.create('users') |
| 126 | + .addField('id', { type: 'INTEGER', autoIncrement: true }) |
86 | 127 | .addField('name', { type: 'VARCHAR', length: 255 }) |
| 128 | + .addField('email', { type: 'VARCHAR', length: 255 }) |
87 | 129 | .addPrimaryKey('id'); |
88 | | -console.log(create.query()); |
89 | | -console.log(await create); |
90 | | - |
91 | | -const insert = engine |
92 | | - .insert('profile') |
93 | | - .values({ id: '1', name: 'John Doe' }); |
94 | | -console.log(insert.query()); |
95 | | -console.log(JSON.stringify(await insert, null, 2)); |
96 | | - |
97 | | -const select = engine.select('*').from('profile'); |
98 | | -console.log(select.query()); |
99 | | -console.log(JSON.stringify(await select, null, 2)); |
100 | | - |
101 | | -const update = engine |
102 | | - .update('profile') |
103 | | - .set({ name: 'Jane Doe' }) |
104 | | - .where('id = ?', [ '1' ]); |
105 | | -console.log(update.query()); |
106 | | -console.log(JSON.stringify(await update, null, 2)); |
107 | | -console.log(JSON.stringify(await select, null, 2)); |
108 | | - |
109 | | -const remove = engine |
110 | | - .delete('profile') |
111 | | - .where('id = ?', [ '1' ]); |
112 | | -console.log(remove.query()); |
113 | | -console.log(JSON.stringify(await remove, null, 2)); |
114 | | -console.log(JSON.stringify(await select, null, 2)); |
115 | | - |
116 | | -if (usePool && connection instanceof Client === false) { |
117 | | - connection.release(); |
118 | | -} |
119 | | - |
120 | | -if (connection instanceof Client) { |
121 | | - await connection.end(); |
122 | | -} |
| 130 | + |
| 131 | +// Insert data |
| 132 | +await engine |
| 133 | + .insert('users') |
| 134 | + .values({ name: 'John Doe', email: 'john@example.com' }); |
| 135 | + |
| 136 | +// Select data |
| 137 | +const users = await engine |
| 138 | + .select('*') |
| 139 | + .from('users') |
| 140 | + .where('name = ?', ['John Doe']); |
| 141 | + |
| 142 | +console.log(users); |
| 143 | + |
| 144 | +// Update data |
| 145 | +await engine |
| 146 | + .update('users') |
| 147 | + .set({ email: 'john.doe@example.com' }) |
| 148 | + .where('id = ?', [1]); |
| 149 | + |
| 150 | +// Delete data |
| 151 | +await engine |
| 152 | + .delete('users') |
| 153 | + .where('id = ?', [1]); |
123 | 154 | ``` |
124 | 155 |
|
125 | | -See [examples](https://github.com/stackpress/inquire/tree/main/examples) |
126 | | -for other ways to use. |
| 156 | +## Query Builders |
| 157 | + |
| 158 | +Inquire provides comprehensive query builders for all common SQL operations: |
| 159 | + |
| 160 | +- **[Create](./docs/builders/Create.md)** - Create tables and schemas |
| 161 | +- **[Alter](./docs/builders/Alter.md)** - Modify existing tables |
| 162 | +- **[Select](./docs/builders/Select.md)** - Query data with joins, conditions, and aggregations |
| 163 | +- **[Insert](./docs/builders/Insert.md)** - Insert single or multiple records |
| 164 | +- **[Update](./docs/builders/Update.md)** - Update existing records |
| 165 | +- **[Delete](./docs/builders/Delete.md)** - Delete records with conditions |
| 166 | + |
| 167 | +## Template String Queries |
| 168 | + |
| 169 | +For complex queries, you can use type-safe template strings: |
| 170 | + |
| 171 | +```typescript |
| 172 | +type User = { |
| 173 | + id: number; |
| 174 | + name: string; |
| 175 | + email: string; |
| 176 | +}; |
| 177 | + |
| 178 | +const userId = 123; |
| 179 | +const results = await engine.sql<User>` |
| 180 | + SELECT u.*, p.title |
| 181 | + FROM users u |
| 182 | + LEFT JOIN posts p ON u.id = p.user_id |
| 183 | + WHERE u.id = ${userId} |
| 184 | +`; |
| 185 | +// results is typed as User[] |
| 186 | +``` |
| 187 | + |
| 188 | +## Transactions |
| 189 | + |
| 190 | +Execute multiple queries in a transaction: |
| 191 | + |
| 192 | +```typescript |
| 193 | +const result = await engine.transaction(async (trx) => { |
| 194 | + await trx.insert('users').values({ name: 'Alice' }); |
| 195 | + await trx.insert('posts').values({ title: 'Hello World', user_id: 1 }); |
| 196 | + return 'success'; |
| 197 | +}); |
| 198 | +``` |
| 199 | + |
| 200 | +## Type Safety |
| 201 | + |
| 202 | +Inquire is designed with TypeScript in mind, providing full type safety: |
| 203 | + |
| 204 | +```typescript |
| 205 | +type User = { |
| 206 | + id: number; |
| 207 | + name: string; |
| 208 | + email: string; |
| 209 | +}; |
| 210 | + |
| 211 | +// Type-safe queries |
| 212 | +const users = await engine.select<User>('*').from('users'); |
| 213 | +// users is now typed as User[] |
| 214 | + |
| 215 | +const user = await engine.select<User>('*') |
| 216 | + .from('users') |
| 217 | + .where('id = ?', [1]) |
| 218 | + .limit(1); |
| 219 | +// user is typed as User[] |
| 220 | +``` |
| 221 | + |
| 222 | +## API Documentation |
| 223 | + |
| 224 | +For detailed API documentation, see: |
| 225 | + |
| 226 | +- **[Engine](./docs/Engine.md)** - Core engine class and methods |
| 227 | +- **[Connection Classes](./docs/Connections.md)** - Database-specific connection implementations |
| 228 | +- **[Query Builders](./docs/builders/README.md)** - Detailed documentation for all query builders |
| 229 | +- **[SQL Dialects](./docs/dialects/README.md)** - Detailed documentation for all SQL dialects |
| 230 | +- **[Examples](./docs/Examples.md)** - Comprehensive usage examples |
| 231 | + |
| 232 | +## Examples |
| 233 | + |
| 234 | +Check out the [examples directory](./examples) for complete working examples with different database engines: |
| 235 | + |
| 236 | +- [MySQL Example](./examples/with-mysql2) |
| 237 | +- [PostgreSQL Example](./examples/with-pg) |
| 238 | +- [SQLite Example](./examples/with-sqlite3) |
| 239 | +- [PGLite Example](./examples/with-pglite) |
0 commit comments