You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Connections.md
+93-61Lines changed: 93 additions & 61 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,18 +2,30 @@
2
2
3
3
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.
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.
6
18
7
19
Inquire supports multiple database engines through dedicated connection packages:
8
20
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`
13
25
14
-
## Mysql2Connection
26
+
## 2. Mysql2Connection
15
27
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.
A promise that resolves to the return value of the callback function.
108
122
109
-
## PGConnection
123
+
## 3. PGConnection
110
124
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.
112
126
113
127
```typescript
114
128
import { Client } from'pg';
@@ -123,17 +137,19 @@ await client.connect();
123
137
const engine =connect(client);
124
138
```
125
139
126
-
### Properties
140
+
### 3.1. Properties
127
141
128
142
The following properties are available when using PGConnection.
129
143
130
144
| Property | Type | Description |
131
145
|----------|------|-------------|
132
146
|`dialect`|`Dialect`| The SQL dialect used by the connection (Pgsql) |
133
147
134
-
### Methods
148
+
### 3.2. Methods
135
149
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
137
153
138
154
The following example shows how the connection formats queries for PostgreSQL.
A promise that resolves to the return value of the callback function.
202
218
203
-
## PGLiteConnection
219
+
## 4. PGLiteConnection
204
220
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.
206
222
207
223
```typescript
208
224
import { PGlite } from'@electric-sql/pglite';
@@ -212,17 +228,19 @@ const db = new PGlite();
212
228
const engine =connect(db);
213
229
```
214
230
215
-
### Properties
231
+
### 4.1. Properties
216
232
217
233
The following properties are available when using PGLiteConnection.
218
234
219
235
| Property | Type | Description |
220
236
|----------|------|-------------|
221
237
|`dialect`|`Dialect`| The SQL dialect used by the connection (Pgsql) |
222
238
223
-
### Methods
239
+
### 4.2. Methods
240
+
241
+
The following methods provide PGLite-specific functionality optimized for client-side and edge environments.
224
242
225
-
#### Formatting Queries
243
+
#### 4.2.1. Formatting Queries
226
244
227
245
The following example shows how the connection formats queries for PGLite.
A promise that resolves to the return value of the callback function.
291
309
292
-
## BetterSqlite3Connection
310
+
## 5. BetterSqlite3Connection
293
311
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.
295
313
296
314
```typescript
297
315
importsqlitefrom'better-sqlite3';
@@ -301,7 +319,7 @@ const db = sqlite(':memory:');
301
319
const engine =connect(db);
302
320
```
303
321
304
-
### Properties
322
+
### 5.1. Properties
305
323
306
324
The following properties are available when using BetterSqlite3Connection.
307
325
@@ -310,9 +328,11 @@ The following properties are available when using BetterSqlite3Connection.
310
328
|`dialect`|`Dialect`| The SQL dialect used by the connection (Sqlite) |
311
329
|`lastId`|`number\|string\|undefined`| The last inserted row ID from the database |
312
330
313
-
### Methods
331
+
### 5.2. Methods
332
+
333
+
The following methods provide SQLite-specific functionality for efficient local database operations.
314
334
315
-
#### Formatting Queries
335
+
#### 5.2.1. Formatting Queries
316
336
317
337
The following example shows how the connection formats queries for SQLite.
A promise that resolves to the return value of the callback function.
382
402
383
-
## Common Interface
403
+
## 6. Common Interface
384
404
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.
386
406
387
-
### Core Methods
407
+
### 6.1. Core Methods
388
408
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
All connections use consistent error handling through the `InquireException`:
417
439
@@ -427,29 +449,39 @@ try {
427
449
}
428
450
```
429
451
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`
0 commit comments