1- # sqlite3-queries
1+ < h1 align = " center " > sqlite3-queries</ h1 >
22
3- > A type-safe and promise-based query client for node sqlite3.
3+ <p align =" center " >A type-safe and promise-based query client for node sqlite3</p >
4+
5+ <p align =" center " >
6+ <img src =" https://img.shields.io/npm/v/sqlite3-queries?color=orange&label=version " >
7+ <img src =" https://img.shields.io/github/license/alex8088/sqlite3-queries?color=blue " alt =" license " />
8+ </p >
9+
10+ ## Features
11+
12+ - SQL-based
13+ - Promise-based
14+ - Type-safe
15+ - Support automatic migration
416
517## Install
618
@@ -10,15 +22,14 @@ npm i sqlite3 sqlite3-queries
1022
1123## Usage
1224
13- ### Opening Database
25+ #### Opening Database
1426
1527- Open an anonymous in-memory database
1628
1729``` ts
1830import { Dbo } from ' sqlite3-queries'
1931
2032const dbo = new Dbo ()
21-
2233await dbo .open ()
2334```
2435
@@ -29,26 +40,25 @@ import path from 'node:path'
2940import { Dbo } from ' sqlite3-queries'
3041
3142const dbo = new Dbo (path .join (__dirname , ' /tmp/database.db' ))
32-
3343await dbo .open ()
3444```
3545
3646- Open a verbose database for debugging
3747
3848``` ts
3949import { Dbo } from ' sqlite3-queries'
40- const dbo = new Dbo (' :memory:' , { verbose: true })
4150
51+ const dbo = new Dbo (' :memory:' , { verbose: true })
4252await dbo .open ()
4353```
4454
45- ### Closing Database
55+ #### Closing Database
4656
4757``` ts
4858await dbo .close ()
4959```
5060
51- ### Tracing and Logging
61+ #### Tracing and Logging
5262
5363``` js
5464import { Dbo } from ' sqlite3-queries'
@@ -63,7 +73,7 @@ const dbo = new Dbo(Dbo.IN_MEMORY_PATH, {
6373await dbo .open ()
6474```
6575
66- ### Automatic Migration
76+ #### Automatic Migration
6777
6878``` js
6979import path from ' node:path'
@@ -148,21 +158,21 @@ declare class DbContext {
148158
149159Promise-based APIs for sqlite3.
150160
151- ### ` Open `
161+ #### ` Open `
152162
153- ** Type:** ` (mode?: number) => Promise<void> `
163+ Type: ` (mode?: number) => Promise<void> `
154164
155165Open the database. The ` mode ` is one or more of ` Dbo.OPEN_READONLY ` , ` Dbo.OPEN_READWRITE ` , ` Dbo.OPEN_CREATE ` , ` Dbo.OPEN_FULLMUTEX ` , ` Dbo.OPEN_URI ` , ` Dbo.OPEN_SHAREDCACHE ` , ` Dbo.OPEN_PRIVATECACHE ` . Default: ` OPEN_READWRITE | OPEN_CREATE | OPEN_FULLMUTEX ` .
156166
157- ### ` Close `
167+ #### ` Close `
158168
159- ** Type:** ` () => Promise<void> `
169+ Type: ` () => Promise<void> `
160170
161171Close the database.
162172
163- ### ` Run `
173+ #### ` Run `
164174
165- ** Type:** ` (sql: string, params?: SqlQueryParam | (string | number)[]) => Promise<RunResult> `
175+ Type: ` (sql: string, params?: SqlQueryParam | (string | number)[]) => Promise<RunResult> `
166176
167177Runs the SQL query with the specified parameters.
168178
@@ -180,9 +190,9 @@ const sql = `UPDATE user SET name = $name WHERE id = $id`
180190const result = await dbo .run (sql , { $id: 0 , $name: ' Evie Le' })
181191```
182192
183- ### ` Get `
193+ #### ` Get `
184194
185- ** Type:** ` <T extends Record<string, any>>(sql: string, params?: SqlQueryParam | (string | number)[]) => Promise<T | undefined> `
195+ Type: ` <T extends Record<string, any>>(sql: string, params?: SqlQueryParam | (string | number)[]) => Promise<T | undefined> `
186196
187197Runs the SQL query with the specified parameters and returns a subsequent result row. If data is not found, ` undefined ` is returned.
188198
@@ -193,9 +203,9 @@ const result = await dbo.get<{ id: number; name: string }>(sql, [0])
193203// Output: {id: 0, name: 'Evie Le'}
194204```
195205
196- ### ` All `
206+ #### ` All `
197207
198- ** Type:** ` <T extends Record<string, any>>(sql: string, params?: SqlQueryParam | (string | number)[]) => Promise<T[]> `
208+ Type: ` <T extends Record<string, any>>(sql: string, params?: SqlQueryParam | (string | number)[]) => Promise<T[]> `
199209
200210Runs the SQL query with the specified parameters and returns all result rows afterwards.
201211
@@ -206,21 +216,21 @@ const result = await dbo.all<{ id: number; name: string }>(sql)
206216// Output: [{id: 0, name: 'Evie Le'}]
207217```
208218
209- ### ` Exec `
219+ #### ` Exec `
210220
211- ** Type:** ` (sql: string) => Promise<void> `
221+ Type: ` (sql: string) => Promise<void> `
212222
213223Runs all SQL queries in the supplied string.
214224
215- ### ` Prepare `
225+ #### ` Prepare `
216226
217- ** Type:** ` (sql: string, runCallback: (stmt: Statement) => void) => void `
227+ Type: ` (sql: string, runCallback: (stmt: Statement) => void) => void `
218228
219229Prepares the SQL statement and run the callback with the statement object.
220230
221- ### ` Transaction `
231+ #### ` Transaction `
222232
223- ** Type:** ` (transactions: () => void) => Promise<void> `
233+ Type: ` (transactions: () => void) => Promise<void> `
224234
225235Start a transaction explicitly. A transaction is the propagation of one or more changes to the database. For example, if you are creating, updating, or deleting a record from the table, then you are performing transaction on the table. It is important to control transactions to ensure data integrity and to handle database errors.
226236
@@ -235,21 +245,21 @@ await dbo.transaction(() => {
235245})
236246```
237247
238- ### ` LoadExtension `
248+ #### ` LoadExtension `
239249
240- ** Type:** ` (fileName: string) => Promise<void> `
250+ Type: ` (fileName: string) => Promise<void> `
241251
242252Loads a compiled SQLite extension into the database connection object.
243253
244- ### ` Vacuum `
254+ #### ` Vacuum `
245255
246- ** Type:** ` () => Promise<void> `
256+ Type: ` () => Promise<void> `
247257
248258Rebuild the database file, repacking it into a minimal amount of disk space.
249259
250- ### ` Pragma `
260+ #### ` Pragma `
251261
252- ** Type:** ` <T extends Record<string, any>>(flag: string, value?: string | number): Promise<T | undefined> `
262+ Type: ` <T extends Record<string, any>>(flag: string, value?: string | number): Promise<T | undefined> `
253263
254264Executes the PRAGMA command to modify the operation of the SQLite library or to query the library for internal (non-table) data.
255265
@@ -261,15 +271,15 @@ dbo.pragma('cache_size')
261271dbo .pragma (' cache_size' , 1000 * 1024 )
262272```
263273
264- ### ` Escape `
274+ #### ` Escape `
265275
266- ** Type:** ` (str: string) => string `
276+ Type: ` (str: string) => string `
267277
268278Escape the ` / ` , ` % ` , and ` _ ` characters of query parameters.
269279
270- ### ` toSqlQueryParam `
280+ #### ` toSqlQueryParam `
271281
272- ** Type:** ` (param: Record<string, string | number>) => SqlQueryParam `
282+ Type: ` (param: Record<string, string | number>) => SqlQueryParam `
273283
274284Convert object parameters to sql query parameters. All object keys will be prefixed with ` $ ` .
275285
0 commit comments