Skip to content

Commit 52650ed

Browse files
committed
outlining docs
1 parent 7bfcb8a commit 52650ed

15 files changed

Lines changed: 2868 additions & 9298 deletions

File tree

docs/Connections.md

Lines changed: 93 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,30 @@
22

33
Connection classes provide database-specific implementations for interacting with different SQL engines. Each connection class implements the `Connection` interface from the Inquire library, ensuring a consistent API across all supported databases.
44

5-
## Overview
5+
## Table of Contents
6+
7+
1. [Overview](#1-overview)
8+
2. [Mysql2Connection](#2-mysql2connection)
9+
3. [PGConnection](#3-pgconnection)
10+
4. [PGLiteConnection](#4-pgliteconnection)
11+
5. [BetterSqlite3Connection](#5-bettersqlite3connection)
12+
6. [Common Interface](#6-common-interface)
13+
7. [Database-Specific Features](#7-database-specific-features)
14+
15+
## 1. Overview
16+
17+
This section provides an overview of the available connection classes and their specific database implementations. Each connection class is designed to work with a particular database engine while maintaining a consistent interface.
618

719
Inquire supports multiple database engines through dedicated connection packages:
820

9-
- **Mysql2Connection** - MySQL via `@stackpress/inquire-mysql2`
10-
- **PGConnection** - PostgreSQL via `@stackpress/inquire-pg`
11-
- **PGLiteConnection** - PGLite via `@stackpress/inquire-pglite`
12-
- **BetterSqlite3Connection** - SQLite via `@stackpress/inquire-sqlite3`
21+
- **Mysql2Connection** - MySQL via `@stackpress/inquire-mysql2`
22+
- **PGConnection** - PostgreSQL via `@stackpress/inquire-pg`
23+
- **PGLiteConnection** - PGLite via `@stackpress/inquire-pglite`
24+
- **BetterSqlite3Connection** - SQLite via `@stackpress/inquire-sqlite3`
1325

14-
## Mysql2Connection
26+
## 2. Mysql2Connection
1527

16-
The `Mysql2Connection` class provides a connection interface for interacting with MySQL databases using the mysql2 library.
28+
The `Mysql2Connection` class provides a connection interface for interacting with MySQL databases using the mysql2 library. This connection class handles MySQL-specific data formatting and query execution patterns.
1729

1830
```typescript
1931
import mysql from 'mysql2/promise';
@@ -28,7 +40,7 @@ const resource = await mysql.createConnection({
2840
const engine = connect(resource);
2941
```
3042

31-
### Properties
43+
### 2.1. Properties
3244

3345
The following properties are available when using Mysql2Connection.
3446

@@ -37,9 +49,11 @@ The following properties are available when using Mysql2Connection.
3749
| `dialect` | `Dialect` | The SQL dialect used by the connection (Mysql) |
3850
| `lastId` | `number\|string\|undefined` | The last inserted ID from the database |
3951

40-
### Methods
52+
### 2.2. Methods
53+
54+
The following methods provide MySQL-specific functionality for query execution and data management.
4155

42-
#### Formatting Queries
56+
#### 2.2.1. Formatting Queries
4357

4458
The following example shows how the connection formats queries and values for MySQL.
4559

@@ -62,7 +76,7 @@ const formatted = connection.format({
6276

6377
A formatted `QueryObject` with values converted for MySQL compatibility.
6478

65-
#### Executing Queries
79+
#### 2.2.2. Executing Queries
6680

6781
The following example shows how to execute queries and get results.
6882

@@ -84,7 +98,7 @@ const users = await connection.query<User>({
8498

8599
A promise that resolves to an array of results typed as `R[]`.
86100

87-
#### Managing Transactions
101+
#### 2.2.3. Managing Transactions
88102

89103
The following example shows how to execute transactions with MySQL.
90104

@@ -106,9 +120,9 @@ const result = await connection.transaction(async (trx) => {
106120

107121
A promise that resolves to the return value of the callback function.
108122

109-
## PGConnection
123+
## 3. PGConnection
110124

111-
The `PGConnection` class provides a connection interface for interacting with PostgreSQL databases.
125+
The `PGConnection` class provides a connection interface for interacting with PostgreSQL databases. This connection class handles PostgreSQL-specific parameter placeholders and advanced features.
112126

113127
```typescript
114128
import { Client } from 'pg';
@@ -123,17 +137,19 @@ await client.connect();
123137
const engine = connect(client);
124138
```
125139

126-
### Properties
140+
### 3.1. Properties
127141

128142
The following properties are available when using PGConnection.
129143

130144
| Property | Type | Description |
131145
|----------|------|-------------|
132146
| `dialect` | `Dialect` | The SQL dialect used by the connection (Pgsql) |
133147

134-
### Methods
148+
### 3.2. Methods
135149

136-
#### Formatting Queries
150+
The following methods provide PostgreSQL-specific functionality for query execution and advanced database operations.
151+
152+
#### 3.2.1. Formatting Queries
137153

138154
The following example shows how the connection formats queries for PostgreSQL.
139155

@@ -156,7 +172,7 @@ const formatted = connection.format({
156172

157173
A formatted `QueryObject` with PostgreSQL-style parameter placeholders ($1, $2, etc.).
158174

159-
#### Executing Queries
175+
#### 3.2.2. Executing Queries
160176

161177
The following example shows how to execute queries and get results.
162178

@@ -178,7 +194,7 @@ const users = await connection.query<User>({
178194

179195
A promise that resolves to an array of results typed as `R[]`.
180196

181-
#### Managing Transactions
197+
#### 3.2.3. Managing Transactions
182198

183199
The following example shows how to execute transactions with PostgreSQL.
184200

@@ -200,9 +216,9 @@ const result = await connection.transaction(async (trx) => {
200216

201217
A promise that resolves to the return value of the callback function.
202218

203-
## PGLiteConnection
219+
## 4. PGLiteConnection
204220

205-
The `PGLiteConnection` class provides a connection interface for interacting with PGLite databases.
221+
The `PGLiteConnection` class provides a connection interface for interacting with PGLite databases. This connection class offers PostgreSQL compatibility in a lightweight, client-side environment.
206222

207223
```typescript
208224
import { PGlite } from '@electric-sql/pglite';
@@ -212,17 +228,19 @@ const db = new PGlite();
212228
const engine = connect(db);
213229
```
214230

215-
### Properties
231+
### 4.1. Properties
216232

217233
The following properties are available when using PGLiteConnection.
218234

219235
| Property | Type | Description |
220236
|----------|------|-------------|
221237
| `dialect` | `Dialect` | The SQL dialect used by the connection (Pgsql) |
222238

223-
### Methods
239+
### 4.2. Methods
240+
241+
The following methods provide PGLite-specific functionality optimized for client-side and edge environments.
224242

225-
#### Formatting Queries
243+
#### 4.2.1. Formatting Queries
226244

227245
The following example shows how the connection formats queries for PGLite.
228246

@@ -245,7 +263,7 @@ const formatted = connection.format({
245263

246264
A formatted `QueryObject` with PostgreSQL-style parameter placeholders.
247265

248-
#### Executing Queries
266+
#### 4.2.2. Executing Queries
249267

250268
The following example shows how to execute queries with PGLite.
251269

@@ -267,7 +285,7 @@ const users = await connection.query<User>({
267285

268286
A promise that resolves to an array of results typed as `R[]`.
269287

270-
#### Managing Transactions
288+
#### 4.2.3. Managing Transactions
271289

272290
The following example shows how to execute transactions with PGLite.
273291

@@ -289,9 +307,9 @@ const result = await connection.transaction(async (trx) => {
289307

290308
A promise that resolves to the return value of the callback function.
291309

292-
## BetterSqlite3Connection
310+
## 5. BetterSqlite3Connection
293311

294-
The `BetterSqlite3Connection` class provides a connection interface for interacting with SQLite databases using the better-sqlite3 library.
312+
The `BetterSqlite3Connection` class provides a connection interface for interacting with SQLite databases using the better-sqlite3 library. This connection class handles SQLite-specific data type conversions and synchronous operations.
295313

296314
```typescript
297315
import sqlite from 'better-sqlite3';
@@ -301,7 +319,7 @@ const db = sqlite(':memory:');
301319
const engine = connect(db);
302320
```
303321

304-
### Properties
322+
### 5.1. Properties
305323

306324
The following properties are available when using BetterSqlite3Connection.
307325

@@ -310,9 +328,11 @@ The following properties are available when using BetterSqlite3Connection.
310328
| `dialect` | `Dialect` | The SQL dialect used by the connection (Sqlite) |
311329
| `lastId` | `number\|string\|undefined` | The last inserted row ID from the database |
312330

313-
### Methods
331+
### 5.2. Methods
332+
333+
The following methods provide SQLite-specific functionality for efficient local database operations.
314334

315-
#### Formatting Queries
335+
#### 5.2.1. Formatting Queries
316336

317337
The following example shows how the connection formats queries for SQLite.
318338

@@ -336,7 +356,7 @@ const formatted = connection.format({
336356

337357
A formatted `QueryObject` with values converted for SQLite compatibility.
338358

339-
#### Executing Queries
359+
#### 5.2.2. Executing Queries
340360

341361
The following example shows how to execute queries with SQLite.
342362

@@ -358,7 +378,7 @@ const users = await connection.query<User>({
358378

359379
A promise that resolves to an array of results typed as `R[]`.
360380

361-
#### Managing Transactions
381+
#### 5.2.3. Managing Transactions
362382

363383
The following example shows how to execute transactions with SQLite.
364384

@@ -380,19 +400,21 @@ const result = await connection.transaction(async (trx) => {
380400

381401
A promise that resolves to the return value of the callback function.
382402

383-
## Common Interface
403+
## 6. Common Interface
384404

385-
All connection classes implement the same `Connection<R>` interface, providing:
405+
All connection classes implement the same `Connection<R>` interface, providing consistent functionality across different database engines. This unified interface ensures that switching between databases requires minimal code changes.
386406

387-
### Core Methods
407+
### 6.1. Core Methods
388408

389-
- **`format(request: QueryObject)`** - Formats queries and values for the specific database
390-
- **`query<R>(request: QueryObject)`** - Executes queries and returns typed results
391-
- **`raw<R>(request: QueryObject)`** - Executes queries and returns raw database results
392-
- **`resource()`** - Returns the underlying database connection resource
393-
- **`transaction<R>(callback: Transaction<R>)`** - Manages database transactions
409+
The following core methods are available across all connection implementations:
394410

395-
### Type Safety
411+
- **`format(request: QueryObject)`** - Formats queries and values for the specific database
412+
- **`query<R>(request: QueryObject)`** - Executes queries and returns typed results
413+
- **`raw<R>(request: QueryObject)`** - Executes queries and returns raw database results
414+
- **`resource()`** - Returns the underlying database connection resource
415+
- **`transaction<R>(callback: Transaction<R>)`** - Manages database transactions
416+
417+
### 6.2. Type Safety
396418

397419
All connection classes support TypeScript generics for type-safe operations:
398420

@@ -411,7 +433,7 @@ const users = await connection.query<User>({
411433
// users is typed as User[]
412434
```
413435

414-
### Error Handling
436+
### 6.3. Error Handling
415437

416438
All connections use consistent error handling through the `InquireException`:
417439

@@ -427,29 +449,39 @@ try {
427449
}
428450
```
429451

430-
## Database-Specific Features
452+
## 7. Database-Specific Features
453+
454+
Each connection class provides unique features tailored to its respective database engine. Understanding these differences helps in choosing the right database for your application needs.
455+
456+
### 7.1. MySQL Features
457+
458+
MySQL connections provide the following specific capabilities:
459+
460+
- **Auto-increment tracking**: Automatically tracks `lastId` for inserted records
461+
- **Date handling**: Converts JavaScript Date objects to ISO strings
462+
- **Transaction support**: Uses `BEGIN`, `COMMIT`, and `ROLLBACK`
463+
464+
### 7.2. PostgreSQL Features
431465

432-
### MySQL Features
466+
PostgreSQL connections offer advanced database functionality:
433467

434-
- **Auto-increment tracking**: Automatically tracks `lastId` for inserted records
435-
- **Date handling**: Converts JavaScript Date objects to ISO strings
436-
- **Transaction support**: Uses `BEGIN`, `COMMIT`, and `ROLLBACK`
468+
- **Parameter placeholders**: Converts `?` placeholders to `$1`, `$2`, etc.
469+
- **Strict parameter matching**: Validates that query placeholders match provided values
470+
- **Transaction support**: Uses `BEGIN`, `COMMIT`, and `ROLLBACK`
437471

438-
### PostgreSQL Features
472+
### 7.3. SQLite Features
439473

440-
- **Parameter placeholders**: Converts `?` placeholders to `$1`, `$2`, etc.
441-
- **Strict parameter matching**: Validates that query placeholders match provided values
442-
- **Transaction support**: Uses `BEGIN`, `COMMIT`, and `ROLLBACK`
474+
SQLite connections provide lightweight, local database capabilities:
443475

444-
### SQLite Features
476+
- **Boolean conversion**: Converts boolean values to numbers (0/1)
477+
- **Row ID tracking**: Automatically tracks `lastInsertRowid`
478+
- **Query optimization**: Uses `stmt.all()` for SELECT queries and `stmt.run()` for others
479+
- **Transaction support**: Uses `BEGIN TRANSACTION`, `COMMIT`, and `ROLLBACK`
445480

446-
- **Boolean conversion**: Converts boolean values to numbers (0/1)
447-
- **Row ID tracking**: Automatically tracks `lastInsertRowid`
448-
- **Query optimization**: Uses `stmt.all()` for SELECT queries and `stmt.run()` for others
449-
- **Transaction support**: Uses `BEGIN TRANSACTION`, `COMMIT`, and `ROLLBACK`
481+
### 7.4. PGLite Features
450482

451-
### PGLite Features
483+
PGLite connections combine PostgreSQL compatibility with client-side optimization:
452484

453-
- **PostgreSQL compatibility**: Uses PostgreSQL syntax and parameter placeholders
454-
- **Lightweight**: Optimized for client-side and edge environments
455-
- **Exec optimization**: Uses `exec()` for queries without parameters
485+
- **PostgreSQL compatibility**: Uses PostgreSQL syntax and parameter placeholders
486+
- **Lightweight**: Optimized for client-side and edge environments
487+
- **Exec optimization**: Uses `exec()` for queries without parameters

0 commit comments

Comments
 (0)