--- url: /reference/angular.md --- [sqlocal](../index.md) / angular # angular ## Functions * [useReactiveQuery](functions/useReactiveQuery.md) --- --- url: /api/batch.md --- # batch Execute a batch of SQL queries against the database in an atomic way. ## Usage Access or destructure `batch` from the `SQLocal` client. ```javascript import { SQLocal } from 'sqlocal'; const { batch } = new SQLocal('database.sqlite3'); ``` ::: tip NOTE If you are using the [Kysely Query Builder](/kysely/setup) or [Drizzle ORM](/drizzle/setup) for type-safe queries, you will initialize the client with a child class of `SQLocal`. See the corresponding setup page. Usage is the same otherwise. ::: The `batch` function takes a group of SQL queries, passes them to the database all together, and executes them inside a transaction. If any of the queries fail, `batch` will throw an error, and the transaction will be rolled back automatically. If all queries succeed, the transaction will be committed and the results from each query will be returned. Provide a function to `batch` that returns an array of SQL queries constructed using the `sql` tagged template function passed to it. This `sql` tag function works similarly to the [`sql` tag function used for single queries](sql.md), but the queries passed to `batch` should not be individually `await`ed. Await the call to `batch`, and each query will be executed against the database in order. ```javascript const senderId = 1; const receiverId = 2; const coins = 4856; // Ensures that these 3 queries either all succeed // or get rolled back await batch((sql) => [ sql`INSERT INTO transfer (senderId, receiverId, coins) VALUES (${senderId}, ${receiverId}, ${coins})`, sql`UPDATE player SET coins = coins - ${coins} WHERE id = ${senderId}`, sql`UPDATE player SET coins = coins + ${coins} WHERE id = ${receiverId}`, ]); // Results from queries will also be returned as // items in an array, one item per query const [players, transfers] = await batch((sql) => [ sql`SELECT * FROM player WHERE id = ${senderId} OR id = ${receiverId}`, sql`SELECT * FROM transfer WHERE senderId = ${senderId}`, ]); ``` `batch` ensures atomicity and isolation for the queries it executes, but if you also need to execute other logic between queries, you should use the [`transaction` method](transaction.md). --- --- url: /reference/index/classes/SQLiteKvvfsDriver.md --- [sqlocal](../../index.md) / [index](../index.md) / SQLiteKvvfsDriver # Class: SQLiteKvvfsDriver Defined in: [src/drivers/sqlite-kvvfs-driver.ts:13](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-kvvfs-driver.ts#L13) A SQLocal driver that implements the interface needed for interacting with SQLite databases in localStorage or sessionStorage. ## Extends * [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md) ## Implements * [`SQLocalDriver`](../interfaces/SQLocalDriver.md) ## Constructors ### Constructor ```ts new SQLiteKvvfsDriver(storageType, sqlite3InitModule?): SQLiteKvvfsDriver; ``` Defined in: [src/drivers/sqlite-kvvfs-driver.ts:19](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-kvvfs-driver.ts#L19) #### Parameters | Parameter | Type | | ------ | ------ | | `storageType` | `"local"` | `"session"` | | `sqlite3InitModule?` | [`Sqlite3InitModule`](../type-aliases/Sqlite3InitModule.md) | #### Returns `SQLiteKvvfsDriver` #### Overrides [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`constructor`](SQLiteMemoryDriver.md#constructor) ## Properties ### storageType ```ts readonly storageType: "local" | "session"; ``` Defined in: [src/drivers/sqlite-kvvfs-driver.ts:20](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-kvvfs-driver.ts#L20) #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`storageType`](../interfaces/SQLocalDriver.md#storagetype) #### Inherited from [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`storageType`](SQLiteMemoryDriver.md#storagetype) ## Methods ### clear() ```ts clear(): Promise; ``` Defined in: [src/drivers/sqlite-kvvfs-driver.ts:83](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-kvvfs-driver.ts#L83) #### Returns `Promise`<`void`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`clear`](../interfaces/SQLocalDriver.md#clear) #### Overrides [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`clear`](SQLiteMemoryDriver.md#clear) *** ### createFunction() ```ts createFunction(fn): Promise; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:136](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L136) #### Parameters | Parameter | Type | | ------ | ------ | | `fn` | [`UserFunction`](../type-aliases/UserFunction.md) | #### Returns `Promise`<`void`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`createFunction`](../interfaces/SQLocalDriver.md#createfunction) #### Inherited from [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`createFunction`](SQLiteMemoryDriver.md#createfunction) *** ### destroy() ```ts destroy(): Promise; ``` Defined in: [src/drivers/sqlite-kvvfs-driver.ts:89](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-kvvfs-driver.ts#L89) #### Returns `Promise`<`void`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`destroy`](../interfaces/SQLocalDriver.md#destroy) #### Overrides [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`destroy`](SQLiteMemoryDriver.md#destroy) *** ### exec() ```ts exec(statement): Promise; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:65](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L65) #### Parameters | Parameter | Type | | ------ | ------ | | `statement` | [`DriverStatement`](../type-aliases/DriverStatement.md) | #### Returns `Promise`<[`RawResultData`](../type-aliases/RawResultData.md)> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`exec`](../interfaces/SQLocalDriver.md#exec) #### Inherited from [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`exec`](SQLiteMemoryDriver.md#exec) *** ### execBatch() ```ts execBatch(statements, method?): Promise; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:71](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L71) #### Parameters | Parameter | Type | Default value | | ------ | ------ | ------ | | `statements` | [`DriverStatement`](../type-aliases/DriverStatement.md)\[] | `undefined` | | `method` | `"transaction"` | `"savepoint"` | `'transaction'` | #### Returns `Promise`<[`RawResultData`](../type-aliases/RawResultData.md)\[]> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`execBatch`](../interfaces/SQLocalDriver.md#execbatch) #### Inherited from [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`execBatch`](SQLiteMemoryDriver.md#execbatch) *** ### export() ```ts export(): Promise<{ data: ArrayBuffer | Uint8Array; name: string; }>; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:195](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L195) #### Returns `Promise`<{ `data`: `ArrayBuffer` | `Uint8Array`<`ArrayBuffer`>; `name`: `string`; }> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`export`](../interfaces/SQLocalDriver.md#export) #### Inherited from [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`export`](SQLiteMemoryDriver.md#export) *** ### getDatabaseSizeBytes() ```ts getDatabaseSizeBytes(): Promise; ``` Defined in: [src/drivers/sqlite-kvvfs-driver.ts:61](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-kvvfs-driver.ts#L61) #### Returns `Promise`<`number`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`getDatabaseSizeBytes`](../interfaces/SQLocalDriver.md#getdatabasesizebytes) #### Overrides [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`getDatabaseSizeBytes`](SQLiteMemoryDriver.md#getdatabasesizebytes) *** ### import() ```ts import(database): Promise; ``` Defined in: [src/drivers/sqlite-kvvfs-driver.ts:67](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-kvvfs-driver.ts#L67) #### Parameters | Parameter | Type | | ------ | ------ | | `database` | | `ArrayBuffer` | `Uint8Array`<`ArrayBuffer`> | `ReadableStream`<`Uint8Array`<`ArrayBuffer`>> | #### Returns `Promise`<`void`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`import`](../interfaces/SQLocalDriver.md#import) #### Overrides [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`import`](SQLiteMemoryDriver.md#import) *** ### init() ```ts init(config): Promise; ``` Defined in: [src/drivers/sqlite-kvvfs-driver.ts:26](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-kvvfs-driver.ts#L26) #### Parameters | Parameter | Type | | ------ | ------ | | `config` | [`DriverConfig`](../type-aliases/DriverConfig.md) | #### Returns `Promise`<`void`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`init`](../interfaces/SQLocalDriver.md#init) #### Overrides [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`init`](SQLiteMemoryDriver.md#init) *** ### isDatabasePersisted() ```ts isDatabasePersisted(): Promise; ``` Defined in: [src/drivers/sqlite-kvvfs-driver.ts:57](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-kvvfs-driver.ts#L57) #### Returns `Promise`<`boolean`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`isDatabasePersisted`](../interfaces/SQLocalDriver.md#isdatabasepersisted) #### Overrides [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`isDatabasePersisted`](SQLiteMemoryDriver.md#isdatabasepersisted) *** ### onWrite() ```ts onWrite(callback): () => void; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:57](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L57) #### Parameters | Parameter | Type | | ------ | ------ | | `callback` | (`change`) => `void` | #### Returns () => `void` #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`onWrite`](../interfaces/SQLocalDriver.md#onwrite) #### Inherited from [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`onWrite`](SQLiteMemoryDriver.md#onwrite) --- --- url: /reference/index/classes/SQLiteMemoryDriver.md --- [sqlocal](../../index.md) / [index](../index.md) / SQLiteMemoryDriver # Class: SQLiteMemoryDriver Defined in: [src/drivers/sqlite-memory-driver.ts:20](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L20) A SQLocal driver that implements the interface needed for interacting with SQLite databases in memory. ## Extended by * [`SQLiteOpfsDriver`](SQLiteOpfsDriver.md) * [`SQLiteKvvfsDriver`](SQLiteKvvfsDriver.md) ## Implements * [`SQLocalDriver`](../interfaces/SQLocalDriver.md) ## Constructors ### Constructor ```ts new SQLiteMemoryDriver(sqlite3InitModule?): SQLiteMemoryDriver; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:30](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L30) #### Parameters | Parameter | Type | | ------ | ------ | | `sqlite3InitModule?` | [`Sqlite3InitModule`](../type-aliases/Sqlite3InitModule.md) | #### Returns `SQLiteMemoryDriver` ## Properties ### storageType ```ts readonly storageType: Sqlite3StorageType = 'memory'; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:28](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L28) #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`storageType`](../interfaces/SQLocalDriver.md#storagetype) ## Methods ### clear() ```ts clear(): Promise; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:209](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L209) #### Returns `Promise`<`void`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`clear`](../interfaces/SQLocalDriver.md#clear) *** ### createFunction() ```ts createFunction(fn): Promise; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:136](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L136) #### Parameters | Parameter | Type | | ------ | ------ | | `fn` | [`UserFunction`](../type-aliases/UserFunction.md) | #### Returns `Promise`<`void`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`createFunction`](../interfaces/SQLocalDriver.md#createfunction) *** ### destroy() ```ts destroy(): Promise; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:211](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L211) #### Returns `Promise`<`void`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`destroy`](../interfaces/SQLocalDriver.md#destroy) *** ### exec() ```ts exec(statement): Promise; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:65](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L65) #### Parameters | Parameter | Type | | ------ | ------ | | `statement` | [`DriverStatement`](../type-aliases/DriverStatement.md) | #### Returns `Promise`<[`RawResultData`](../type-aliases/RawResultData.md)> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`exec`](../interfaces/SQLocalDriver.md#exec) *** ### execBatch() ```ts execBatch(statements, method?): Promise; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:71](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L71) #### Parameters | Parameter | Type | Default value | | ------ | ------ | ------ | | `statements` | [`DriverStatement`](../type-aliases/DriverStatement.md)\[] | `undefined` | | `method` | `"transaction"` | `"savepoint"` | `'transaction'` | #### Returns `Promise`<[`RawResultData`](../type-aliases/RawResultData.md)\[]> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`execBatch`](../interfaces/SQLocalDriver.md#execbatch) *** ### export() ```ts export(): Promise<{ data: ArrayBuffer | Uint8Array; name: string; }>; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:195](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L195) #### Returns `Promise`<{ `data`: `ArrayBuffer` | `Uint8Array`<`ArrayBuffer`>; `name`: `string`; }> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`export`](../interfaces/SQLocalDriver.md#export) *** ### getDatabaseSizeBytes() ```ts getDatabaseSizeBytes(): Promise; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:121](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L121) #### Returns `Promise`<`number`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`getDatabaseSizeBytes`](../interfaces/SQLocalDriver.md#getdatabasesizebytes) *** ### import() ```ts import(database): Promise; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:169](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L169) #### Parameters | Parameter | Type | | ------ | ------ | | `database` | | `ArrayBuffer` | `Uint8Array`<`ArrayBuffer`> | `ReadableStream`<`Uint8Array`<`ArrayBuffer`>> | #### Returns `Promise`<`void`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`import`](../interfaces/SQLocalDriver.md#import) *** ### init() ```ts init(config): Promise; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:34](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L34) #### Parameters | Parameter | Type | | ------ | ------ | | `config` | [`DriverConfig`](../type-aliases/DriverConfig.md) | #### Returns `Promise`<`void`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`init`](../interfaces/SQLocalDriver.md#init) *** ### isDatabasePersisted() ```ts isDatabasePersisted(): Promise; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:117](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L117) #### Returns `Promise`<`boolean`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`isDatabasePersisted`](../interfaces/SQLocalDriver.md#isdatabasepersisted) *** ### onWrite() ```ts onWrite(callback): () => void; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:57](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L57) #### Parameters | Parameter | Type | | ------ | ------ | | `callback` | (`change`) => `void` | #### Returns () => `void` #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`onWrite`](../interfaces/SQLocalDriver.md#onwrite) --- --- url: /reference/index/classes/SQLiteOpfsDriver.md --- [sqlocal](../../index.md) / [index](../index.md) / SQLiteOpfsDriver # Class: SQLiteOpfsDriver Defined in: [src/drivers/sqlite-opfs-driver.ts:15](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-opfs-driver.ts#L15) A SQLocal driver that implements the interface needed for interacting with SQLite databases in the origin private file system. ## Extends * [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md) ## Implements * [`SQLocalDriver`](../interfaces/SQLocalDriver.md) ## Constructors ### Constructor ```ts new SQLiteOpfsDriver(sqlite3InitModule?): SQLiteOpfsDriver; ``` Defined in: [src/drivers/sqlite-opfs-driver.ts:21](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-opfs-driver.ts#L21) #### Parameters | Parameter | Type | | ------ | ------ | | `sqlite3InitModule?` | [`Sqlite3InitModule`](../type-aliases/Sqlite3InitModule.md) | #### Returns `SQLiteOpfsDriver` #### Overrides [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`constructor`](SQLiteMemoryDriver.md#constructor) ## Properties ### storageType ```ts readonly storageType: Sqlite3StorageType = 'opfs'; ``` Defined in: [src/drivers/sqlite-opfs-driver.ts:19](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-opfs-driver.ts#L19) #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`storageType`](../interfaces/SQLocalDriver.md#storagetype) #### Overrides [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`storageType`](SQLiteMemoryDriver.md#storagetype) ## Methods ### clear() ```ts clear(): Promise; ``` Defined in: [src/drivers/sqlite-opfs-driver.ts:103](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-opfs-driver.ts#L103) #### Returns `Promise`<`void`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`clear`](../interfaces/SQLocalDriver.md#clear) #### Overrides [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`clear`](SQLiteMemoryDriver.md#clear) *** ### createFunction() ```ts createFunction(fn): Promise; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:136](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L136) #### Parameters | Parameter | Type | | ------ | ------ | | `fn` | [`UserFunction`](../type-aliases/UserFunction.md) | #### Returns `Promise`<`void`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`createFunction`](../interfaces/SQLocalDriver.md#createfunction) #### Inherited from [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`createFunction`](SQLiteMemoryDriver.md#createfunction) *** ### destroy() ```ts destroy(): Promise; ``` Defined in: [src/drivers/sqlite-opfs-driver.ts:125](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-opfs-driver.ts#L125) #### Returns `Promise`<`void`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`destroy`](../interfaces/SQLocalDriver.md#destroy) #### Overrides [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`destroy`](SQLiteMemoryDriver.md#destroy) *** ### exec() ```ts exec(statement): Promise; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:65](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L65) #### Parameters | Parameter | Type | | ------ | ------ | | `statement` | [`DriverStatement`](../type-aliases/DriverStatement.md) | #### Returns `Promise`<[`RawResultData`](../type-aliases/RawResultData.md)> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`exec`](../interfaces/SQLocalDriver.md#exec) #### Inherited from [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`exec`](SQLiteMemoryDriver.md#exec) *** ### execBatch() ```ts execBatch(statements, method?): Promise; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:71](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L71) #### Parameters | Parameter | Type | Default value | | ------ | ------ | ------ | | `statements` | [`DriverStatement`](../type-aliases/DriverStatement.md)\[] | `undefined` | | `method` | `"transaction"` | `"savepoint"` | `'transaction'` | #### Returns `Promise`<[`RawResultData`](../type-aliases/RawResultData.md)\[]> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`execBatch`](../interfaces/SQLocalDriver.md#execbatch) #### Inherited from [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`execBatch`](SQLiteMemoryDriver.md#execbatch) *** ### export() ```ts export(): Promise<{ data: ArrayBuffer | Uint8Array; name: string; }>; ``` Defined in: [src/drivers/sqlite-opfs-driver.ts:76](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-opfs-driver.ts#L76) #### Returns `Promise`<{ `data`: `ArrayBuffer` | `Uint8Array`<`ArrayBuffer`>; `name`: `string`; }> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`export`](../interfaces/SQLocalDriver.md#export) #### Overrides [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`export`](SQLiteMemoryDriver.md#export) *** ### getDatabaseSizeBytes() ```ts getDatabaseSizeBytes(): Promise; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:121](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L121) #### Returns `Promise`<`number`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`getDatabaseSizeBytes`](../interfaces/SQLocalDriver.md#getdatabasesizebytes) #### Inherited from [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`getDatabaseSizeBytes`](SQLiteMemoryDriver.md#getdatabasesizebytes) *** ### import() ```ts import(database): Promise; ``` Defined in: [src/drivers/sqlite-opfs-driver.ts:60](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-opfs-driver.ts#L60) #### Parameters | Parameter | Type | | ------ | ------ | | `database` | | `ArrayBuffer` | `Uint8Array`<`ArrayBuffer`> | `ReadableStream`<`Uint8Array`<`ArrayBuffer`>> | #### Returns `Promise`<`void`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`import`](../interfaces/SQLocalDriver.md#import) #### Overrides [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`import`](SQLiteMemoryDriver.md#import) *** ### init() ```ts init(config): Promise; ``` Defined in: [src/drivers/sqlite-opfs-driver.ts:25](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-opfs-driver.ts#L25) #### Parameters | Parameter | Type | | ------ | ------ | | `config` | [`DriverConfig`](../type-aliases/DriverConfig.md) | #### Returns `Promise`<`void`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`init`](../interfaces/SQLocalDriver.md#init) #### Overrides [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`init`](SQLiteMemoryDriver.md#init) *** ### isDatabasePersisted() ```ts isDatabasePersisted(): Promise; ``` Defined in: [src/drivers/sqlite-opfs-driver.ts:56](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-opfs-driver.ts#L56) #### Returns `Promise`<`boolean`> #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`isDatabasePersisted`](../interfaces/SQLocalDriver.md#isdatabasepersisted) #### Overrides [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`isDatabasePersisted`](SQLiteMemoryDriver.md#isdatabasepersisted) *** ### onWrite() ```ts onWrite(callback): () => void; ``` Defined in: [src/drivers/sqlite-memory-driver.ts:57](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drivers/sqlite-memory-driver.ts#L57) #### Parameters | Parameter | Type | | ------ | ------ | | `callback` | (`change`) => `void` | #### Returns () => `void` #### Implementation of [`SQLocalDriver`](../interfaces/SQLocalDriver.md).[`onWrite`](../interfaces/SQLocalDriver.md#onwrite) #### Inherited from [`SQLiteMemoryDriver`](SQLiteMemoryDriver.md).[`onWrite`](SQLiteMemoryDriver.md#onwrite) --- --- url: /reference/index/classes/SQLocal.md --- [sqlocal](../../index.md) / [index](../index.md) / SQLocal # Class: SQLocal Defined in: [src/client.ts:54](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L54) This class is your entry point for connecting to and interacting with an on-device SQLite database in the browser. ## See ## Extended by * [`SQLocalDrizzle`](../../drizzle/classes/SQLocalDrizzle.md) * [`SQLocalKysely`](../../kysely/classes/SQLocalKysely.md) ## Constructors ### Constructor ```ts new SQLocal(databasePath): SQLocal; ``` Defined in: [src/client.ts:75](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L75) #### Parameters | Parameter | Type | | ------ | ------ | | `databasePath` | [`DatabasePath`](../type-aliases/DatabasePath.md) | #### Returns `SQLocal` ### Constructor ```ts new SQLocal(config): SQLocal; ``` Defined in: [src/client.ts:76](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L76) #### Parameters | Parameter | Type | | ------ | ------ | | `config` | [`ClientConfig`](../type-aliases/ClientConfig.md) | #### Returns `SQLocal` ## Methods ### batch() ```ts batch(passStatements): Promise; ``` Defined in: [src/client.ts:315](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L315) Execute a batch of SQL queries against the database in an atomic way. #### Type Parameters | Type Parameter | | ------ | | `Result` *extends* `Record`<`string`, `any`> | #### Parameters | Parameter | Type | | ------ | ------ | | `passStatements` | (`sql`) => [`Statement`](../type-aliases/Statement.md)\[] | #### Returns `Promise`<`Result`\[]\[]> #### See *** ### createAggregateFunction() ```ts createAggregateFunction(funcName, func): Promise; ``` Defined in: [src/client.ts:628](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L628) Create a SQL function that can be called from queries to combine multiple rows into a single result row. #### Parameters | Parameter | Type | | ------ | ------ | | `funcName` | `string` | | `func` | { `final`: (...`args`) => `any`; `step`: (...`args`) => `void`; } | | `func.final` | (...`args`) => `any` | | `func.step` | (...`args`) => `void` | #### Returns `Promise`<`void`> #### See *** ### createCallbackFunction() ```ts createCallbackFunction(funcName, func): Promise; ``` Defined in: [src/client.ts:604](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L604) Create a SQL function that can be called from queries to trigger a JavaScript callback. #### Parameters | Parameter | Type | | ------ | ------ | | `funcName` | `string` | | `func` | (...`args`) => `void` | #### Returns `Promise`<`void`> #### See *** ### createScalarFunction() ```ts createScalarFunction(funcName, func): Promise; ``` Defined in: [src/client.ts:616](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L616) Create a SQL function that can be called from queries to transform column values or to filter rows. #### Parameters | Parameter | Type | | ------ | ------ | | `funcName` | `string` | | `func` | (...`args`) => `any` | #### Returns `Promise`<`void`> #### See *** ### createWindowFunction() ```ts createWindowFunction(funcName, func): Promise; ``` Defined in: [src/client.ts:640](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L640) Create a SQL function that can be called from queries to perform calculations for rows using data from related rows. #### Parameters | Parameter | Type | | ------ | ------ | | `funcName` | `string` | | `func` | { `final`: (...`args`) => `any`; `inverse`: (...`args`) => `void`; `step`: (...`args`) => `void`; `value`: (...`args`) => `any`; } | | `func.final` | (...`args`) => `any` | | `func.inverse` | (...`args`) => `void` | | `func.step` | (...`args`) => `void` | | `func.value` | (...`args`) => `any` | #### Returns `Promise`<`void`> #### See *** ### deleteDatabaseFile() ```ts deleteDatabaseFile(beforeUnlock?, destroy?): Promise; ``` Defined in: [src/client.ts:732](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L732) Delete the SQLite database file. #### Parameters | Parameter | Type | Default value | | ------ | ------ | ------ | | `beforeUnlock?` | () => `void` | `Promise`<`void`> | `undefined` | | `destroy?` | `boolean` | `false` | #### Returns `Promise`<`void`> #### See *** ### destroy() ```ts destroy(skipOptimize?): Promise; ``` Defined in: [src/client.ts:780](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L780) Disconnect this SQLocal client from the database and terminate its worker thread. #### Parameters | Parameter | Type | Default value | | ------ | ------ | ------ | | `skipOptimize` | `boolean` | `false` | #### Returns `Promise`<`void`> #### See *** ### getDatabaseFile() ```ts getDatabaseFile(): Promise; ``` Defined in: [src/client.ts:666](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L666) Access the SQLite database file so that it can be uploaded to the server or allowed to be downloaded by the user. #### Returns `Promise`<`File`> #### See *** ### getDatabaseInfo() ```ts getDatabaseInfo(): Promise; ``` Defined in: [src/client.ts:651](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L651) Retrieve information about the SQLite database file. #### Returns `Promise`<[`DatabaseInfo`](../type-aliases/DatabaseInfo.md)> #### See *** ### overwriteDatabaseFile() ```ts overwriteDatabaseFile(databaseFile, beforeUnlock?): Promise; ``` Defined in: [src/client.ts:682](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L682) Replace the contents of the SQLite database file. #### Parameters | Parameter | Type | | ------ | ------ | | `databaseFile` | | `ArrayBuffer` | `Uint8Array`<`ArrayBuffer`> | `ReadableStream`<`Uint8Array`<`ArrayBuffer`>> | `File` | `Blob` | | `beforeUnlock?` | () => `void` | `Promise`<`void`> | #### Returns `Promise`<`void`> #### See *** ### reactiveQuery() ```ts reactiveQuery(passStatement): ReactiveQuery; ``` Defined in: [src/client.ts:450](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L450) Subscribe to a SQL query and receive the latest results whenever the read tables change. #### Type Parameters | Type Parameter | | ------ | | `Result` *extends* `Record`<`string`, `any`> | #### Parameters | Parameter | Type | | ------ | ------ | | `passStatement` | [`StatementInput`](../type-aliases/StatementInput.md)<`Result`> | #### Returns [`ReactiveQuery`](../type-aliases/ReactiveQuery.md)<`Result`> #### See *** ### sql() ```ts sql(queryTemplate, ...params): Promise; ``` Defined in: [src/client.ts:298](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L298) Execute SQL queries against the database. #### Type Parameters | Type Parameter | | ------ | | `Result` *extends* `Record`<`string`, `any`> | #### Parameters | Parameter | Type | | ------ | ------ | | `queryTemplate` | `string` | `TemplateStringsArray` | | ...`params` | `unknown`\[] | #### Returns `Promise`<`Result`\[]> #### See *** ### transaction() ```ts transaction(transaction): Promise; ``` Defined in: [src/client.ts:407](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L407) Execute SQL transactions against the database. #### Type Parameters | Type Parameter | | ------ | | `Result` | #### Parameters | Parameter | Type | | ------ | ------ | | `transaction` | (`tx`) => `Promise`<`Result`> | #### Returns `Promise`<`Result`> #### See --- --- url: /reference/drizzle/classes/SQLocalDrizzle.md --- [sqlocal](../../index.md) / [drizzle](../index.md) / SQLocalDrizzle # Class: SQLocalDrizzle Defined in: [src/drizzle/client.ts:9](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drizzle/client.ts#L9) A subclass of the `SQLocal` client that provides additional methods for using SQLocal as a driver for Drizzle ORM. ## See ## Extends * [`SQLocal`](../../index/classes/SQLocal.md) ## Constructors ### Constructor ```ts new SQLocalDrizzle(databasePath): SQLocalDrizzle; ``` Defined in: [src/client.ts:75](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L75) #### Parameters | Parameter | Type | | ------ | ------ | | `databasePath` | [`DatabasePath`](../../index/type-aliases/DatabasePath.md) | #### Returns `SQLocalDrizzle` #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`constructor`](../../index/classes/SQLocal.md#constructor) ### Constructor ```ts new SQLocalDrizzle(config): SQLocalDrizzle; ``` Defined in: [src/client.ts:76](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L76) #### Parameters | Parameter | Type | | ------ | ------ | | `config` | [`ClientConfig`](../../index/type-aliases/ClientConfig.md) | #### Returns `SQLocalDrizzle` #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`constructor`](../../index/classes/SQLocal.md#constructor) ## Methods ### batch() ```ts batch(passStatements): Promise; ``` Defined in: [src/client.ts:315](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L315) Execute a batch of SQL queries against the database in an atomic way. #### Type Parameters | Type Parameter | | ------ | | `Result` *extends* `Record`<`string`, `any`> | #### Parameters | Parameter | Type | | ------ | ------ | | `passStatements` | (`sql`) => [`Statement`](../../index/type-aliases/Statement.md)\[] | #### Returns `Promise`<`Result`\[]\[]> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`batch`](../../index/classes/SQLocal.md#batch) *** ### batchDriver() ```ts batchDriver(queries): Promise; ``` Defined in: [src/drizzle/client.ts:39](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drizzle/client.ts#L39) A driver function that Drizzle can use to make batch queries to databases through SQLocal. #### Parameters | Parameter | Type | | ------ | ------ | | `queries` | { `method`: [`Sqlite3Method`](../../index/type-aliases/Sqlite3Method.md); `params`: `unknown`\[]; `sql`: `string`; }\[] | #### Returns `Promise`<[`RawResultData`](../../index/type-aliases/RawResultData.md)\[]> #### See *** ### createAggregateFunction() ```ts createAggregateFunction(funcName, func): Promise; ``` Defined in: [src/client.ts:628](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L628) Create a SQL function that can be called from queries to combine multiple rows into a single result row. #### Parameters | Parameter | Type | | ------ | ------ | | `funcName` | `string` | | `func` | { `final`: (...`args`) => `any`; `step`: (...`args`) => `void`; } | | `func.final` | (...`args`) => `any` | | `func.step` | (...`args`) => `void` | #### Returns `Promise`<`void`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`createAggregateFunction`](../../index/classes/SQLocal.md#createaggregatefunction) *** ### createCallbackFunction() ```ts createCallbackFunction(funcName, func): Promise; ``` Defined in: [src/client.ts:604](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L604) Create a SQL function that can be called from queries to trigger a JavaScript callback. #### Parameters | Parameter | Type | | ------ | ------ | | `funcName` | `string` | | `func` | (...`args`) => `void` | #### Returns `Promise`<`void`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`createCallbackFunction`](../../index/classes/SQLocal.md#createcallbackfunction) *** ### createScalarFunction() ```ts createScalarFunction(funcName, func): Promise; ``` Defined in: [src/client.ts:616](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L616) Create a SQL function that can be called from queries to transform column values or to filter rows. #### Parameters | Parameter | Type | | ------ | ------ | | `funcName` | `string` | | `func` | (...`args`) => `any` | #### Returns `Promise`<`void`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`createScalarFunction`](../../index/classes/SQLocal.md#createscalarfunction) *** ### createWindowFunction() ```ts createWindowFunction(funcName, func): Promise; ``` Defined in: [src/client.ts:640](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L640) Create a SQL function that can be called from queries to perform calculations for rows using data from related rows. #### Parameters | Parameter | Type | | ------ | ------ | | `funcName` | `string` | | `func` | { `final`: (...`args`) => `any`; `inverse`: (...`args`) => `void`; `step`: (...`args`) => `void`; `value`: (...`args`) => `any`; } | | `func.final` | (...`args`) => `any` | | `func.inverse` | (...`args`) => `void` | | `func.step` | (...`args`) => `void` | | `func.value` | (...`args`) => `any` | #### Returns `Promise`<`void`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`createWindowFunction`](../../index/classes/SQLocal.md#createwindowfunction) *** ### deleteDatabaseFile() ```ts deleteDatabaseFile(beforeUnlock?, destroy?): Promise; ``` Defined in: [src/client.ts:732](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L732) Delete the SQLite database file. #### Parameters | Parameter | Type | Default value | | ------ | ------ | ------ | | `beforeUnlock?` | () => `void` | `Promise`<`void`> | `undefined` | | `destroy?` | `boolean` | `false` | #### Returns `Promise`<`void`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`deleteDatabaseFile`](../../index/classes/SQLocal.md#deletedatabasefile) *** ### destroy() ```ts destroy(skipOptimize?): Promise; ``` Defined in: [src/client.ts:780](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L780) Disconnect this SQLocal client from the database and terminate its worker thread. #### Parameters | Parameter | Type | Default value | | ------ | ------ | ------ | | `skipOptimize` | `boolean` | `false` | #### Returns `Promise`<`void`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`destroy`](../../index/classes/SQLocal.md#destroy) *** ### driver() ```ts driver( sql, params, method): Promise; ``` Defined in: [src/drizzle/client.ts:15](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/drizzle/client.ts#L15) A driver function that Drizzle can use to query databases through SQLocal. #### Parameters | Parameter | Type | | ------ | ------ | | `sql` | `string` | | `params` | `unknown`\[] | | `method` | [`Sqlite3Method`](../../index/type-aliases/Sqlite3Method.md) | #### Returns `Promise`<[`RawResultData`](../../index/type-aliases/RawResultData.md)> #### See *** ### getDatabaseFile() ```ts getDatabaseFile(): Promise; ``` Defined in: [src/client.ts:666](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L666) Access the SQLite database file so that it can be uploaded to the server or allowed to be downloaded by the user. #### Returns `Promise`<`File`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`getDatabaseFile`](../../index/classes/SQLocal.md#getdatabasefile) *** ### getDatabaseInfo() ```ts getDatabaseInfo(): Promise; ``` Defined in: [src/client.ts:651](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L651) Retrieve information about the SQLite database file. #### Returns `Promise`<[`DatabaseInfo`](../../index/type-aliases/DatabaseInfo.md)> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`getDatabaseInfo`](../../index/classes/SQLocal.md#getdatabaseinfo) *** ### overwriteDatabaseFile() ```ts overwriteDatabaseFile(databaseFile, beforeUnlock?): Promise; ``` Defined in: [src/client.ts:682](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L682) Replace the contents of the SQLite database file. #### Parameters | Parameter | Type | | ------ | ------ | | `databaseFile` | | `ArrayBuffer` | `Uint8Array`<`ArrayBuffer`> | `ReadableStream`<`Uint8Array`<`ArrayBuffer`>> | `File` | `Blob` | | `beforeUnlock?` | () => `void` | `Promise`<`void`> | #### Returns `Promise`<`void`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`overwriteDatabaseFile`](../../index/classes/SQLocal.md#overwritedatabasefile) *** ### reactiveQuery() ```ts reactiveQuery(passStatement): ReactiveQuery; ``` Defined in: [src/client.ts:450](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L450) Subscribe to a SQL query and receive the latest results whenever the read tables change. #### Type Parameters | Type Parameter | | ------ | | `Result` *extends* `Record`<`string`, `any`> | #### Parameters | Parameter | Type | | ------ | ------ | | `passStatement` | [`StatementInput`](../../index/type-aliases/StatementInput.md)<`Result`> | #### Returns [`ReactiveQuery`](../../index/type-aliases/ReactiveQuery.md)<`Result`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`reactiveQuery`](../../index/classes/SQLocal.md#reactivequery) *** ### sql() ```ts sql(queryTemplate, ...params): Promise; ``` Defined in: [src/client.ts:298](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L298) Execute SQL queries against the database. #### Type Parameters | Type Parameter | | ------ | | `Result` *extends* `Record`<`string`, `any`> | #### Parameters | Parameter | Type | | ------ | ------ | | `queryTemplate` | `string` | `TemplateStringsArray` | | ...`params` | `unknown`\[] | #### Returns `Promise`<`Result`\[]> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`sql`](../../index/classes/SQLocal.md#sql) *** ### transaction() ```ts transaction(transaction): Promise; ``` Defined in: [src/client.ts:407](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L407) Execute SQL transactions against the database. #### Type Parameters | Type Parameter | | ------ | | `Result` | #### Parameters | Parameter | Type | | ------ | ------ | | `transaction` | (`tx`) => `Promise`<`Result`> | #### Returns `Promise`<`Result`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`transaction`](../../index/classes/SQLocal.md#transaction) --- --- url: /reference/kysely/classes/SQLocalKysely.md --- [sqlocal](../../index.md) / [kysely](../index.md) / SQLocalKysely # Class: SQLocalKysely Defined in: [src/kysely/client.ts:18](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/kysely/client.ts#L18) A subclass of the `SQLocal` client that provides an additional property for using SQLocal as a dialect for the Kysely query builder. ## See ## Extends * [`SQLocal`](../../index/classes/SQLocal.md) ## Constructors ### Constructor ```ts new SQLocalKysely(databasePath): SQLocalKysely; ``` Defined in: [src/client.ts:75](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L75) #### Parameters | Parameter | Type | | ------ | ------ | | `databasePath` | [`DatabasePath`](../../index/type-aliases/DatabasePath.md) | #### Returns `SQLocalKysely` #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`constructor`](../../index/classes/SQLocal.md#constructor) ### Constructor ```ts new SQLocalKysely(config): SQLocalKysely; ``` Defined in: [src/client.ts:76](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L76) #### Parameters | Parameter | Type | | ------ | ------ | | `config` | [`ClientConfig`](../../index/type-aliases/ClientConfig.md) | #### Returns `SQLocalKysely` #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`constructor`](../../index/classes/SQLocal.md#constructor) ## Properties ### dialect ```ts dialect: Dialect; ``` Defined in: [src/kysely/client.ts:24](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/kysely/client.ts#L24) A Kysely dialect that implements the interface needed for Kysely to interact with databases through SQLocal. #### See ## Methods ### batch() ```ts batch(passStatements): Promise; ``` Defined in: [src/client.ts:315](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L315) Execute a batch of SQL queries against the database in an atomic way. #### Type Parameters | Type Parameter | | ------ | | `Result` *extends* `Record`<`string`, `any`> | #### Parameters | Parameter | Type | | ------ | ------ | | `passStatements` | (`sql`) => [`Statement`](../../index/type-aliases/Statement.md)\[] | #### Returns `Promise`<`Result`\[]\[]> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`batch`](../../index/classes/SQLocal.md#batch) *** ### createAggregateFunction() ```ts createAggregateFunction(funcName, func): Promise; ``` Defined in: [src/client.ts:628](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L628) Create a SQL function that can be called from queries to combine multiple rows into a single result row. #### Parameters | Parameter | Type | | ------ | ------ | | `funcName` | `string` | | `func` | { `final`: (...`args`) => `any`; `step`: (...`args`) => `void`; } | | `func.final` | (...`args`) => `any` | | `func.step` | (...`args`) => `void` | #### Returns `Promise`<`void`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`createAggregateFunction`](../../index/classes/SQLocal.md#createaggregatefunction) *** ### createCallbackFunction() ```ts createCallbackFunction(funcName, func): Promise; ``` Defined in: [src/client.ts:604](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L604) Create a SQL function that can be called from queries to trigger a JavaScript callback. #### Parameters | Parameter | Type | | ------ | ------ | | `funcName` | `string` | | `func` | (...`args`) => `void` | #### Returns `Promise`<`void`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`createCallbackFunction`](../../index/classes/SQLocal.md#createcallbackfunction) *** ### createScalarFunction() ```ts createScalarFunction(funcName, func): Promise; ``` Defined in: [src/client.ts:616](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L616) Create a SQL function that can be called from queries to transform column values or to filter rows. #### Parameters | Parameter | Type | | ------ | ------ | | `funcName` | `string` | | `func` | (...`args`) => `any` | #### Returns `Promise`<`void`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`createScalarFunction`](../../index/classes/SQLocal.md#createscalarfunction) *** ### createWindowFunction() ```ts createWindowFunction(funcName, func): Promise; ``` Defined in: [src/client.ts:640](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L640) Create a SQL function that can be called from queries to perform calculations for rows using data from related rows. #### Parameters | Parameter | Type | | ------ | ------ | | `funcName` | `string` | | `func` | { `final`: (...`args`) => `any`; `inverse`: (...`args`) => `void`; `step`: (...`args`) => `void`; `value`: (...`args`) => `any`; } | | `func.final` | (...`args`) => `any` | | `func.inverse` | (...`args`) => `void` | | `func.step` | (...`args`) => `void` | | `func.value` | (...`args`) => `any` | #### Returns `Promise`<`void`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`createWindowFunction`](../../index/classes/SQLocal.md#createwindowfunction) *** ### deleteDatabaseFile() ```ts deleteDatabaseFile(beforeUnlock?, destroy?): Promise; ``` Defined in: [src/client.ts:732](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L732) Delete the SQLite database file. #### Parameters | Parameter | Type | Default value | | ------ | ------ | ------ | | `beforeUnlock?` | () => `void` | `Promise`<`void`> | `undefined` | | `destroy?` | `boolean` | `false` | #### Returns `Promise`<`void`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`deleteDatabaseFile`](../../index/classes/SQLocal.md#deletedatabasefile) *** ### destroy() ```ts destroy(skipOptimize?): Promise; ``` Defined in: [src/client.ts:780](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L780) Disconnect this SQLocal client from the database and terminate its worker thread. #### Parameters | Parameter | Type | Default value | | ------ | ------ | ------ | | `skipOptimize` | `boolean` | `false` | #### Returns `Promise`<`void`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`destroy`](../../index/classes/SQLocal.md#destroy) *** ### getDatabaseFile() ```ts getDatabaseFile(): Promise; ``` Defined in: [src/client.ts:666](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L666) Access the SQLite database file so that it can be uploaded to the server or allowed to be downloaded by the user. #### Returns `Promise`<`File`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`getDatabaseFile`](../../index/classes/SQLocal.md#getdatabasefile) *** ### getDatabaseInfo() ```ts getDatabaseInfo(): Promise; ``` Defined in: [src/client.ts:651](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L651) Retrieve information about the SQLite database file. #### Returns `Promise`<[`DatabaseInfo`](../../index/type-aliases/DatabaseInfo.md)> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`getDatabaseInfo`](../../index/classes/SQLocal.md#getdatabaseinfo) *** ### overwriteDatabaseFile() ```ts overwriteDatabaseFile(databaseFile, beforeUnlock?): Promise; ``` Defined in: [src/client.ts:682](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L682) Replace the contents of the SQLite database file. #### Parameters | Parameter | Type | | ------ | ------ | | `databaseFile` | | `ArrayBuffer` | `Uint8Array`<`ArrayBuffer`> | `ReadableStream`<`Uint8Array`<`ArrayBuffer`>> | `File` | `Blob` | | `beforeUnlock?` | () => `void` | `Promise`<`void`> | #### Returns `Promise`<`void`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`overwriteDatabaseFile`](../../index/classes/SQLocal.md#overwritedatabasefile) *** ### reactiveQuery() ```ts reactiveQuery(passStatement): ReactiveQuery; ``` Defined in: [src/client.ts:450](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L450) Subscribe to a SQL query and receive the latest results whenever the read tables change. #### Type Parameters | Type Parameter | | ------ | | `Result` *extends* `Record`<`string`, `any`> | #### Parameters | Parameter | Type | | ------ | ------ | | `passStatement` | [`StatementInput`](../../index/type-aliases/StatementInput.md)<`Result`> | #### Returns [`ReactiveQuery`](../../index/type-aliases/ReactiveQuery.md)<`Result`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`reactiveQuery`](../../index/classes/SQLocal.md#reactivequery) *** ### sql() ```ts sql(queryTemplate, ...params): Promise; ``` Defined in: [src/client.ts:298](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L298) Execute SQL queries against the database. #### Type Parameters | Type Parameter | | ------ | | `Result` *extends* `Record`<`string`, `any`> | #### Parameters | Parameter | Type | | ------ | ------ | | `queryTemplate` | `string` | `TemplateStringsArray` | | ...`params` | `unknown`\[] | #### Returns `Promise`<`Result`\[]> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`sql`](../../index/classes/SQLocal.md#sql) *** ### transaction() ```ts transaction(transaction): Promise; ``` Defined in: [src/client.ts:407](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/client.ts#L407) Execute SQL transactions against the database. #### Type Parameters | Type Parameter | | ------ | | `Result` | #### Parameters | Parameter | Type | | ------ | ------ | | `transaction` | (`tx`) => `Promise`<`Result`> | #### Returns `Promise`<`Result`> #### See #### Inherited from [`SQLocal`](../../index/classes/SQLocal.md).[`transaction`](../../index/classes/SQLocal.md#transaction) --- --- url: /reference/index/classes/SQLocalProcessor.md --- [sqlocal](../../index.md) / [index](../index.md) / SQLocalProcessor # Class: SQLocalProcessor Defined in: [src/processor.ts:37](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/processor.ts#L37) The `SQLocal` client exchanges messages with a paired instance of `SQLocalProcessor` to interact with databases. ## See ## Constructors ### Constructor ```ts new SQLocalProcessor(driver): SQLocalProcessor; ``` Defined in: [src/processor.ts:57](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/processor.ts#L57) #### Parameters | Parameter | Type | | ------ | ------ | | `driver` | [`SQLocalDriver`](../interfaces/SQLocalDriver.md) | #### Returns `SQLocalProcessor` ## Properties ### onmessage? ```ts optional onmessage?: (message, transfer) => void; ``` Defined in: [src/processor.ts:55](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/processor.ts#L55) After an `InputMessage` has been processed, the resulting `OutputMessage` is emitted to the function passed to `onmessage`. #### Parameters | Parameter | Type | | ------ | ------ | | `message` | `OutputMessage` | | `transfer` | `Transferable`\[] | #### Returns `void` ## Methods ### postMessage() ```ts postMessage(event, _transfer?): Promise; ``` Defined in: [src/processor.ts:141](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/processor.ts#L141) To interact with a database, an `InputMessage` is passed to `postMessage` for processing. #### Parameters | Parameter | Type | | ------ | ------ | | `event` | `InputMessage` | `MessageEvent`<`InputMessage`> | | `_transfer?` | `Transferable` | #### Returns `Promise`<`void`> --- --- url: /api/createaggregatefunction.md --- # createAggregateFunction Create a SQL function that can be called from queries to combine multiple rows into a single result row. ## Usage Access or destructure `createAggregateFunction` from the `SQLocal` client. ```javascript import { SQLocal } from 'sqlocal'; const { createAggregateFunction } = new SQLocal('database.sqlite3'); ``` ::: tip NOTE If you are using the [Kysely Query Builder](/kysely/setup) or [Drizzle ORM](/drizzle/setup) for type-safe queries, you will initialize the client with a child class of `SQLocal`. See the corresponding setup page. Usage is the same otherwise. ::: This method takes a string to name a custom SQL function as its first argument and an object containing two callbacks (`step` and `final`) as its second argument. After running `createAggregateFunction`, the aggregate function that you defined can be called from subsequent SQL queries. Arguments passed to the function in the SQL query will be passed to the JavaScript `step` callback. The `step` callback will run for every row in the SQL query. After every row is processed, the `final` callback will run, and its return value will be passed back to SQLite to use to complete the query. This can be used to combine rows together in a query based on some custom logic. For example, the below aggregate function can be used to find the most common value for a column, such as the most common category used in a table of tasks. ```javascript const values = new Map(); await createAggregateFunction('mostCommon', { step: (value) => { const valueCount = values.get(value) ?? 0; values.set(value, valueCount + 1); }, final: () => { const valueEntries = Array.from(values.entries()); const sortedEntries = valueEntries.sort((a, b) => b[1] - a[1]); const mostCommonValue = sortedEntries[0][0]; values.clear(); return mostCommonValue; }, }); await sql`SELECT mostCommon(category) AS mostCommonCategory FROM tasks`; ``` Aggregate functions can also be used in a query's `HAVING` clause to filter groups of rows. Here, we use the `mostCommon` function that we created in the previous example to find which days of the week have "Cleaning" as the most common category of task. ```javascript await sql` SELECT dayOfWeek FROM tasks GROUP BY dayOfWeek HAVING mostCommon(category) = 'Cleaning' `; ``` ::: tip NOTE Each function that you create will be connection-specific. If you create more than one connection using additional `SQLocal` instances but want to use the same function in queries sent over the other connections as well, you will need to create the function on each instance. ::: --- --- url: /api/createcallbackfunction.md --- # createCallbackFunction Create a SQL function that can be called from queries to trigger a JavaScript callback. ## Usage Access or destructure `createCallbackFunction` from the `SQLocal` client. ```javascript import { SQLocal } from 'sqlocal'; const { createCallbackFunction } = new SQLocal('database.sqlite3'); ``` ::: tip NOTE If you are using the [Kysely Query Builder](/kysely/setup) or [Drizzle ORM](/drizzle/setup) for type-safe queries, you will initialize the client with a child class of `SQLocal`. See the corresponding setup page. Usage is the same otherwise. ::: This method takes a string to name a custom SQL function as its first argument and a callback function as its second argument which the SQL function will call. After running `createCallbackFunction`, the function that you defined can be called from subsequent SQL queries. Arguments passed to the function in the SQL query will be passed to the JavaScript callback. A good use-case for this is making SQL triggers that notify your application when certain mutations are made in the database. For example, let's create a `logInsert` callback function that takes a table name and a record name to log a message. ```javascript await createCallbackFunction('logInsert', (tableName, recordName) => { console.log(`New ${tableName} record created with name: ${recordName}`); }); ``` Then, we can create a temporary trigger that calls `logInsert` whenever we insert a row into our `groceries` table. ```javascript await sql` CREATE TEMP TRIGGER logGroceriesInsert AFTER INSERT ON groceries BEGIN SELECT logInsert('groceries', new.name); END `; ``` Now, a message will be automatically logged whenever a query on the same connection inserts into the `groceries` table. ```javascript await sql`INSERT INTO groceries (name) VALUES ('bread')`; ``` ```log New groceries record created with name: bread ``` ::: tip NOTE Each function that you create will be connection-specific. If you create more than one connection using additional `SQLocal` instances but want to use the same function in queries sent over the other connections as well, you will need to create the function on each instance. ::: --- --- url: /api/createscalarfunction.md --- # createScalarFunction Create a SQL function that can be called from queries to transform column values or to filter rows. ## Usage Access or destructure `createScalarFunction` from the `SQLocal` client. ```javascript import { SQLocal } from 'sqlocal'; const { createScalarFunction } = new SQLocal('database.sqlite3'); ``` ::: tip NOTE If you are using the [Kysely Query Builder](/kysely/setup) or [Drizzle ORM](/drizzle/setup) for type-safe queries, you will initialize the client with a child class of `SQLocal`. See the corresponding setup page. Usage is the same otherwise. ::: This method takes a string to name a custom SQL function as its first argument and a callback function as its second argument which the SQL function will call. After running `createScalarFunction`, the function that you defined can be called from subsequent SQL queries. Arguments passed to the function in the SQL query will be passed to the JavaScript callback, and the return value of the callback will be passed back to SQLite to use to complete the query. This can be used to perform custom transformations on column values in a query. For example, you could define a function that converts temperatures from Celsius to Fahrenheit. ```javascript await createScalarFunction('toFahrenheit', (celsius) => { return celsius * (9 / 5) + 32; }); await sql`SELECT celsius, toFahrenheit(celsius) AS fahrenheit FROM temperatures`; ``` Scalar functions can also be used in a query's `WHERE` clause to filter rows. ```javascript await createScalarFunction('isEven', (num) => num % 2 === 0); await sql`SELECT num FROM nums WHERE isEven(num)`; ``` ::: tip NOTE Each function that you create will be connection-specific. If you create more than one connection using additional `SQLocal` instances but want to use the same function in queries sent over the other connections as well, you will need to create the function on each instance. ::: --- --- url: /api/createwindowfunction.md --- # createWindowFunction Create a SQL function that can be called from queries to perform calculations for rows using data from related rows. ## Usage Access or destructure `createWindowFunction` from the `SQLocal` client. ```javascript import { SQLocal } from 'sqlocal'; const { createWindowFunction } = new SQLocal('database.sqlite3'); ``` ::: tip NOTE If you are using the [Kysely Query Builder](/kysely/setup) or [Drizzle ORM](/drizzle/setup) for type-safe queries, you will initialize the client with a child class of `SQLocal`. See the corresponding setup page. Usage is the same otherwise. ::: This method takes a string to name a custom SQL function as its first argument and an object containing four callbacks (`step`, `inverse`, `value`, and `final`) as its second argument. After running `createWindowFunction`, the function that you defined can be called from subsequent SQL queries as a window aggregate function with an `OVER` clause or as a regular aggregate function if no `OVER` clause is used. Window aggregate functions will return a value for each row in the result set, and they can access values from other rows to calculate that value. | Callback | Description | | --------- | ----------- | | `step` | Used by both window aggregate and regular aggregate function implementations. It is invoked to add a row to the current window. The function arguments, if any, corresponding to the row being added are passed to the implementation of `step`. | | `final` | Used by both window aggregate and regular aggregate function implementations. It is invoked to return the current value of the aggregate (determined by the contents of the current window), and to free any resources allocated by earlier calls to `step`. | | `value` | Only used for window aggregate functions. The presence of this method is what distinguishes a window aggregate function from a regular aggregate function. This method is invoked to return the current value of the aggregate. Unlike `final`, the implementation should not delete any context. | | `inverse` | Only used for window aggregate functions. It is invoked to remove the oldest presently aggregated result of `step` from the current window. The function arguments, if any, are those passed to `step` for the row being removed. | For example, the below window function will use the preceding, current, and next rows in the result set, calculate the difference between the highest and lowest values of the three, and return that difference in the current result row. ```javascript let values = []; const calcRange = (isFinal) => { if (values.length === 0) return null; const min = values[0]; const max = values[values.length - 1]; if (isFinal) values = []; return max - min; }; await createWindowFunction('range', { step: (value) => { if (typeof value !== 'number') return; const idx = values.findIndex((v) => v > value); if (idx === -1) { values.push(value); } else { values.splice(idx, 0, value); } }, inverse: (value) => { if (typeof value !== 'number') return; const idx = values.indexOf(value); if (idx !== -1) values.splice(idx, 1); }, value: () => calcRange(false), final: () => calcRange(true), }); await sql` SELECT value, range(value) OVER ( ORDER BY sampleIndex ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING ) AS rangeOver3 FROM tracking `; ``` The same function can also be used as a regular aggregate function if called without an `OVER` clause. In this case, only 1 result row will be returned from the query, and it will contain the range over all rows that the query matched. ```javascript await sql`SELECT range(value) AS range FROM tracking`; ``` To define SQL functions that only need to work as regular aggregate functions, use [`createAggregateFunction`](./createaggregatefunction.md). ::: tip NOTE Each function that you create will be connection-specific. If you create more than one connection using additional `SQLocal` instances but want to use the same function in queries sent over the other connections as well, you will need to create the function on each instance. ::: --- --- url: /api/deletedatabasefile.md --- # deleteDatabaseFile Delete the SQLite database file. ## Usage Access or destructure `deleteDatabaseFile` from the `SQLocal` client. ```javascript import { SQLocal } from 'sqlocal'; const { deleteDatabaseFile } = new SQLocal('database.sqlite3'); ``` ::: tip NOTE If you are using the [Kysely Query Builder](/kysely/setup) or [Drizzle ORM](/drizzle/setup) for type-safe queries, you will initialize the client with a child class of `SQLocal`. See the corresponding setup page. Usage is the same otherwise. ::: The `deleteDatabaseFile` method returns a `Promise` to delete the `SQLocal` instance's associated database file and [temporary files](https://www.sqlite.org/tempfiles.html). After this is done, the `SQLocal` client will reinitialize (unless `true` is passed as the second argument), and any subsequent mutation queries will create a new database file. ```javascript await deleteDatabaseFile(); ``` The method also accepts an optional first argument: a callback function to run after the database is deleted but before connections from other SQLocal client instances are allowed to access the new database, a good time to run migrations. ```javascript await deleteDatabaseFile(async () => { // Run your migrations }); ``` The optional second argument can be passed `true` to immediately [destroy](./destroy.md) the SQLocal client instance after deleting the database, preventing the database file from being re-created. If there are other SQLocal client instances connected to the same database, though, this will not disconnect or destroy them. ```javascript await deleteDatabaseFile(undefined, true); ``` Since calling `deleteDatabaseFile` will reset all connections to the database file, the configured `onInit` statements and `onConnect` hook (see [Options](../guide/setup.md#options)) will re-run on any SQLocal clients connected to the database when it is cleared. The client that initiated the deletion will have its `onConnect` hook run first, before the method's callback, and the other clients' `onConnect` hooks will run after the callback. --- --- url: /api/destroy.md --- # destroy Disconnect a SQLocal client from the database and terminate its worker thread. ## Usage Access or destructure `destroy` from the `SQLocal` client. ```javascript import { SQLocal } from 'sqlocal'; const { destroy } = new SQLocal('database.sqlite3'); ``` ::: tip NOTE If you are using the [Kysely Query Builder](/kysely/setup) or [Drizzle ORM](/drizzle/setup) for type-safe queries, you will initialize the client with a child class of `SQLocal`. See the corresponding setup page. Usage is the same otherwise. ::: The `destroy` method will return a `Promise` to close the connection to the SQLite database file and terminate the web worker that the `SQLocal` client uses internally to run queries. By default, it will also execute [`PRAGMA optimize`](https://www.sqlite.org/pragma.html#pragma_optimize) on the database before closing the connection. This can be skipped by passing `true` to the method call. Call `destroy` if you want to clean up a `SQLocal` instance because you are finished querying its associated database for the remainder of the session. **Avoid** calling `destroy` after each query and then initializing a new `SQLocal` instance for the next query. ```javascript await destroy(); ``` ::: warning Once the `destroy` method is called on a `SQLocal` instance, any subsequent attempts to make queries through that instance will throw an error. You will need to initialize a new instance of `SQLocal` to make new queries. ::: --- --- url: /reference/drizzle.md --- [sqlocal](../index.md) / drizzle # drizzle ## Classes * [SQLocalDrizzle](classes/SQLocalDrizzle.md) ## Functions * [drizzle](functions/drizzle.md) --- --- url: /drizzle/setup.md --- # Drizzle ORM Setup SQLocal provides a driver for [Drizzle ORM](https://orm.drizzle.team/) so that you can use it to make fully typed queries against databases in your TypeScript codebase. ## Install Install the Drizzle ORM package alongside SQLocal in your application using your package manager. ::: code-group ```sh [npm] npm install sqlocal drizzle-orm ``` ```sh [yarn] yarn add sqlocal drizzle-orm ``` ```sh [pnpm] pnpm install sqlocal drizzle-orm ``` ::: ## Initialize SQLocal provides the Drizzle ORM driver from a child class of `SQLocal` called `SQLocalDrizzle` imported from `sqlocal/drizzle`. This class has all the same methods as `SQLocal` but adds `driver` and `batchDriver` which you pass to the `drizzle` instance. ```typescript import { SQLocalDrizzle } from 'sqlocal/drizzle'; import { drizzle } from 'drizzle-orm/sqlite-proxy'; const { driver, batchDriver } = new SQLocalDrizzle('database.sqlite3'); export const db = drizzle(driver, batchDriver); ``` Now, any queries you run through this Drizzle instance will be executed against the database passed to `SQLocalDrizzle`. ## Define Schema Define your schema using the functions that Drizzle ORM provides. You will need to import the table definitions where you will be making queries. See the [Drizzle documentation](https://orm.drizzle.team/docs/sql-schema-declaration). ```typescript import { sqliteTable, int, text } from 'drizzle-orm/sqlite-core'; export const groceries = sqliteTable('groceries', { id: int('id').primaryKey({ autoIncrement: true }), name: text('name').notNull(), }); ``` ## Make Queries Import your Drizzle instance to start making type-safe queries. ```typescript const data = await db .select({ name: groceries.name, quantity: groceries.quantity }) .from(groceries) .orderBy(groceries.name) .all(); console.log(data); ``` See the [Drizzle documentation](https://orm.drizzle.team/docs/crud) for more examples. ## Transactions [Drizzle's `transaction` method](https://orm.drizzle.team/docs/transactions) cannot isolate transactions from outside queries. It is recommended to use the `transaction` method of `SQLocalDrizzle` instead. See the [`transaction` documentation](../api/transaction.md#drizzle). --- --- url: /reference/vite/functions/default.md --- [sqlocal](../../index.md) / [vite](../index.md) / default # Function: default() ```ts function default(config?): Plugin; ``` Defined in: [src/vite/index.ts:23](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/vite/index.ts#L23) A Vite plugin that tweaks some Vite settings for building apps that use SQLocal. ## Parameters | Parameter | Type | | ------ | ------ | | `config` | [`VitePluginConfig`](../type-aliases/VitePluginConfig.md) | ## Returns `Plugin`<`UserConfig`> ## See --- --- url: /reference/drizzle/functions/drizzle.md --- [sqlocal](../../index.md) / [drizzle](../index.md) / drizzle # Function: drizzle() ## See ## Call Signature ```ts function drizzle(callback, config?): SqliteRemoteDatabase; ``` Defined in: node\_modules/drizzle-orm/sqlite-proxy/driver.d.ts:23 ### Type Parameters | Type Parameter | Default type | | ------ | ------ | | `TSchema` *extends* `Record`<`string`, `unknown`> | `Record`<`string`, `never`> | ### Parameters | Parameter | Type | | ------ | ------ | | `callback` | `AsyncRemoteCallback` | | `config?` | `DrizzleConfig`<`TSchema`> | ### Returns `SqliteRemoteDatabase`<`TSchema`> ## Call Signature ```ts function drizzle( callback, batchCallback?, config?): SqliteRemoteDatabase; ``` Defined in: node\_modules/drizzle-orm/sqlite-proxy/driver.d.ts:24 ### Type Parameters | Type Parameter | Default type | | ------ | ------ | | `TSchema` *extends* `Record`<`string`, `unknown`> | `Record`<`string`, `never`> | ### Parameters | Parameter | Type | | ------ | ------ | | `callback` | `AsyncRemoteCallback` | | `batchCallback?` | `AsyncBatchRemoteCallback` | | `config?` | `DrizzleConfig`<`TSchema`> | ### Returns `SqliteRemoteDatabase`<`TSchema`> --- --- url: /reference/angular/functions/useReactiveQuery.md --- [sqlocal](../../index.md) / [angular](../index.md) / useReactiveQuery # Function: useReactiveQuery() ```ts function useReactiveQuery(db, query): { data: Signal; error: Signal; status: Signal; }; ``` Defined in: [src/angular/index.ts:9](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/angular/index.ts#L9) A signal function for using reactive SQL queries in Angular components. ## Type Parameters | Type Parameter | | ------ | | `Result` *extends* `Record`<`string`, `any`> | ## Parameters | Parameter | Type | | ------ | ------ | | `db` | | [`SQLocal`](../../index/classes/SQLocal.md) | `Signal`<[`SQLocal`](../../index/classes/SQLocal.md)> | | `query` | | [`StatementInput`](../../index/type-aliases/StatementInput.md)<`Result`> | `Signal`<[`StatementInput`](../../index/type-aliases/StatementInput.md)<`Result`>> | ## Returns ```ts { data: Signal; error: Signal; status: Signal; } ``` | Name | Type | Defined in | | ------ | ------ | ------ | | `data` | `Signal`<`Result`\[]> | [src/angular/index.ts:13](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/angular/index.ts#L13) | | `error` | `Signal`<`Error` | `undefined`> | [src/angular/index.ts:14](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/angular/index.ts#L14) | | `status` | `Signal`<[`ReactiveQueryStatus`](../../index/type-aliases/ReactiveQueryStatus.md)> | [src/angular/index.ts:15](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/angular/index.ts#L15) | ## See --- --- url: /reference/react/functions/useReactiveQuery.md --- [sqlocal](../../index.md) / [react](../index.md) / useReactiveQuery # Function: useReactiveQuery() ```ts function useReactiveQuery(db, query): { data: Result[]; error: Error | undefined; setDb: Dispatch>; setQuery: Dispatch>>; status: ReactiveQueryStatus; }; ``` Defined in: [src/react/index.ts:16](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/react/index.ts#L16) A hook for using reactive SQL queries in React components. ## Type Parameters | Type Parameter | | ------ | | `Result` *extends* `Record`<`string`, `any`> | ## Parameters | Parameter | Type | | ------ | ------ | | `db` | [`SQLocal`](../../index/classes/SQLocal.md) | | `query` | [`StatementInput`](../../index/type-aliases/StatementInput.md)<`Result`> | ## Returns ```ts { data: Result[]; error: Error | undefined; setDb: Dispatch>; setQuery: Dispatch>>; status: ReactiveQueryStatus; } ``` | Name | Type | Defined in | | ------ | ------ | ------ | | `data` | `Result`\[] | [src/react/index.ts:20](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/react/index.ts#L20) | | `error` | `Error` | `undefined` | [src/react/index.ts:21](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/react/index.ts#L21) | | `setDb` | `Dispatch`<`SetStateAction`<[`SQLocal`](../../index/classes/SQLocal.md)>> | [src/react/index.ts:23](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/react/index.ts#L23) | | `setQuery` | `Dispatch`<`SetStateAction`<[`StatementInput`](../../index/type-aliases/StatementInput.md)<`Result`>>> | [src/react/index.ts:24](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/react/index.ts#L24) | | `status` | [`ReactiveQueryStatus`](../../index/type-aliases/ReactiveQueryStatus.md) | [src/react/index.ts:22](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/react/index.ts#L22) | ## See --- --- url: /reference/vue/functions/useReactiveQuery.md --- [sqlocal](../../index.md) / [vue](../index.md) / useReactiveQuery # Function: useReactiveQuery() ```ts function useReactiveQuery(db, query): { data: Readonly>>; error: Readonly>; status: Readonly>; }; ``` Defined in: [src/vue/index.ts:17](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/vue/index.ts#L17) A composable for using reactive SQL queries in Vue components. ## Type Parameters | Type Parameter | | ------ | | `Result` *extends* `Record`<`string`, `any`> | ## Parameters | Parameter | Type | | ------ | ------ | | `db` | | [`SQLocal`](../../index/classes/SQLocal.md) | `Ref`<[`SQLocal`](../../index/classes/SQLocal.md), [`SQLocal`](../../index/classes/SQLocal.md)> | | `query` | | [`StatementInput`](../../index/type-aliases/StatementInput.md)<`Result`> | `Ref`<[`StatementInput`](../../index/type-aliases/StatementInput.md)<`Result`>, [`StatementInput`](../../index/type-aliases/StatementInput.md)<`Result`>> | ## Returns ```ts { data: Readonly>>; error: Readonly>; status: Readonly>; } ``` | Name | Type | Defined in | | ------ | ------ | ------ | | `data` | `Readonly`<`Ref`<`DeepReadonly`<`Result`\[]>>> | [src/vue/index.ts:21](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/vue/index.ts#L21) | | `error` | `Readonly`<`Ref`<`Error` | `undefined`>> | [src/vue/index.ts:22](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/vue/index.ts#L22) | | `status` | `Readonly`<`Ref`<[`ReactiveQueryStatus`](../../index/type-aliases/ReactiveQueryStatus.md)>> | [src/vue/index.ts:23](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/vue/index.ts#L23) | ## See --- --- url: /api/getdatabasefile.md --- # getDatabaseFile Access the SQLite database file so that it can be uploaded to the server or allowed to be downloaded by the user. ## Usage Access or destructure `getDatabaseFile` from the `SQLocal` client. ```javascript import { SQLocal } from 'sqlocal'; const { getDatabaseFile } = new SQLocal('database.sqlite3'); ``` ::: tip NOTE If you are using the [Kysely Query Builder](/kysely/setup) or [Drizzle ORM](/drizzle/setup) for type-safe queries, you will initialize the client with a child class of `SQLocal`. See the corresponding setup page. Usage is the same otherwise. ::: The `getDatabaseFile` method takes no arguments. It will return a `Promise` for a [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File) object. That object can then be used to upload or download the database file of the `SQLocal` instance. For example, you can upload the database file to your server. ```javascript const databaseFile = await getDatabaseFile(); const formData = new FormData(); formData.set('databaseFile', databaseFile); await fetch('https://example.com/upload', { method: 'POST', body: formData, }); ``` Or, you can use it to make the database file available to the user for download. ```javascript const databaseFile = await getDatabaseFile(); const fileUrl = URL.createObjectURL(databaseFile); const a = document.createElement('a'); a.href = fileUrl; a.download = 'database.sqlite3'; a.click(); a.remove(); URL.revokeObjectURL(fileUrl); ``` --- --- url: /api/getdatabaseinfo.md --- # getDatabaseInfo Retrieve information about the SQLite database file. ## Usage Access or destructure `getDatabaseInfo` from the `SQLocal` client. ```javascript import { SQLocal } from 'sqlocal'; const { getDatabaseInfo } = new SQLocal('database.sqlite3'); ``` ::: tip NOTE If you are using the [Kysely Query Builder](/kysely/setup) or [Drizzle ORM](/drizzle/setup) for type-safe queries, you will initialize the client with a child class of `SQLocal`. See the corresponding setup page. Usage is the same otherwise. ::: The `getDatabaseInfo` method takes no arguments. It will return a `Promise` for an object that contains information about the database file being used by the `SQLocal` instance. ```javascript const databaseInfo = await getDatabaseInfo(); ``` The returned object contains the following properties: * **`databasePath`** (`string`) - The name of the database file. This will be identical to the value passed to the `SQLocal` constructor at initialization. * **`databaseSizeBytes`** (`number`) - An integer representing the current file size of the database in bytes. * **`storageType`** (`'memory' | 'local' | 'session' | 'opfs'`) - A string indicating whether the database is saved in the origin private file system, browser storage, or in memory. If set to use OPFS, the database will fall back to being saved in memory if the OPFS cannot be used, such as when the browser does not support it. * **`persisted`** (`boolean`) - This is `true` if the database is saved in the origin private file system *and* the application has used [`navigator.storage.persist()`](https://developer.mozilla.org/en-US/docs/Web/API/StorageManager/persist) to instruct the browser not to automatically evict the site's storage. If the `SQLocal` instance failed to initialize a database connection, these properties may be `undefined`. --- --- url: /reference/index.md --- [sqlocal](../index.md) / index # index ## Classes * [SQLiteKvvfsDriver](classes/SQLiteKvvfsDriver.md) * [SQLiteMemoryDriver](classes/SQLiteMemoryDriver.md) * [SQLiteOpfsDriver](classes/SQLiteOpfsDriver.md) * [SQLocal](classes/SQLocal.md) * [SQLocalProcessor](classes/SQLocalProcessor.md) ## Interfaces * [SQLocalDriver](interfaces/SQLocalDriver.md) ## Type Aliases * [AggregateUserFunction](type-aliases/AggregateUserFunction.md) * [CallbackUserFunction](type-aliases/CallbackUserFunction.md) * [ClientConfig](type-aliases/ClientConfig.md) * [ConnectReason](type-aliases/ConnectReason.md) * [DatabaseInfo](type-aliases/DatabaseInfo.md) * [DatabasePath](type-aliases/DatabasePath.md) * [DataChange](type-aliases/DataChange.md) * [DriverConfig](type-aliases/DriverConfig.md) * [DriverStatement](type-aliases/DriverStatement.md) * [ProcessorConfig](type-aliases/ProcessorConfig.md) * [QueryKey](type-aliases/QueryKey.md) * [RawResultData](type-aliases/RawResultData.md) * [ReactiveQuery](type-aliases/ReactiveQuery.md) * [ReactiveQueryStatus](type-aliases/ReactiveQueryStatus.md) * [ReturningStatement](type-aliases/ReturningStatement.md) * [ScalarUserFunction](type-aliases/ScalarUserFunction.md) * [Sqlite3](type-aliases/Sqlite3.md) * [Sqlite3Db](type-aliases/Sqlite3Db.md) * [Sqlite3InitModule](type-aliases/Sqlite3InitModule.md) * [Sqlite3Method](type-aliases/Sqlite3Method.md) * [Sqlite3StorageType](type-aliases/Sqlite3StorageType.md) * [SqlTag](type-aliases/SqlTag.md) * [Statement](type-aliases/Statement.md) * [StatementInput](type-aliases/StatementInput.md) * [Transaction](type-aliases/Transaction.md) * [TransactionHandle](type-aliases/TransactionHandle.md) * [UserFunction](type-aliases/UserFunction.md) * [WindowUserFunction](type-aliases/WindowUserFunction.md) --- --- url: /reference/index/interfaces/SQLocalDriver.md --- [sqlocal](../../index.md) / [index](../index.md) / SQLocalDriver # Interface: SQLocalDriver Defined in: [src/types.ts:82](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L82) ## Properties ### clear ```ts clear: () => Promise; ``` Defined in: [src/types.ts:104](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L104) #### Returns `Promise`<`void`> *** ### createFunction ```ts createFunction: (fn) => Promise; ``` Defined in: [src/types.ts:93](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L93) #### Parameters | Parameter | Type | | ------ | ------ | | `fn` | [`UserFunction`](../type-aliases/UserFunction.md) | #### Returns `Promise`<`void`> *** ### destroy ```ts destroy: () => Promise; ``` Defined in: [src/types.ts:105](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L105) #### Returns `Promise`<`void`> *** ### exec ```ts exec: (statement) => Promise; ``` Defined in: [src/types.ts:85](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L85) #### Parameters | Parameter | Type | | ------ | ------ | | `statement` | [`DriverStatement`](../type-aliases/DriverStatement.md) | #### Returns `Promise`<[`RawResultData`](../type-aliases/RawResultData.md)> *** ### execBatch ```ts execBatch: (statements, method?) => Promise; ``` Defined in: [src/types.ts:86](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L86) #### Parameters | Parameter | Type | | ------ | ------ | | `statements` | [`DriverStatement`](../type-aliases/DriverStatement.md)\[] | | `method?` | `"transaction"` | `"savepoint"` | #### Returns `Promise`<[`RawResultData`](../type-aliases/RawResultData.md)\[]> *** ### export ```ts export: () => Promise<{ data: ArrayBuffer | Uint8Array; name: string; }>; ``` Defined in: [src/types.ts:100](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L100) #### Returns `Promise`<{ `data`: `ArrayBuffer` | `Uint8Array`<`ArrayBuffer`>; `name`: `string`; }> *** ### getDatabaseSizeBytes ```ts getDatabaseSizeBytes: () => Promise; ``` Defined in: [src/types.ts:92](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L92) #### Returns `Promise`<`number`> *** ### import ```ts import: (database) => Promise; ``` Defined in: [src/types.ts:94](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L94) #### Parameters | Parameter | Type | | ------ | ------ | | `database` | | `ArrayBuffer` | `Uint8Array`<`ArrayBuffer`> | `ReadableStream`<`Uint8Array`<`ArrayBuffer`>> | #### Returns `Promise`<`void`> *** ### init ```ts init: (config) => Promise; ``` Defined in: [src/types.ts:84](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L84) #### Parameters | Parameter | Type | | ------ | ------ | | `config` | [`DriverConfig`](../type-aliases/DriverConfig.md) | #### Returns `Promise`<`void`> *** ### isDatabasePersisted ```ts isDatabasePersisted: () => Promise; ``` Defined in: [src/types.ts:91](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L91) #### Returns `Promise`<`boolean`> *** ### onWrite ```ts onWrite: (callback) => () => void; ``` Defined in: [src/types.ts:90](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L90) #### Parameters | Parameter | Type | | ------ | ------ | | `callback` | (`change`) => `void` | #### Returns () => `void` *** ### storageType ```ts readonly storageType: Sqlite3StorageType; ``` Defined in: [src/types.ts:83](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L83) --- --- url: /guide/introduction.md --- # Introduction SQLocal makes it easy to run SQLite3 in the browser, backed by the origin private file system which provides high-performance read/write access to a SQLite database file stored on the user's device. SQLocal acts as a lightweight wrapper of the [WebAssembly build of SQLite3](https://sqlite.org/wasm/doc/trunk/index.md) and gives you a simple interface to interact with databases running locally. It can also act as a database driver for [Kysely](/kysely/setup) or [Drizzle ORM](/drizzle/setup) to make fully-typed queries. Having the ability to store and query relational data on device makes it possible to build powerful, local-first web apps and games no matter the complexity of your data model. ## Examples ```javascript import { SQLocal } from 'sqlocal'; // Create a client with a name for the SQLite file to save in // the origin private file system const { sql } = new SQLocal('database.sqlite3'); // Use the "sql" tagged template to execute a SQL statement // against the SQLite database await sql`CREATE TABLE groceries (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)`; // Execute a parameterized statement just by inserting // parameters in the SQL string const items = ['bread', 'milk', 'rice']; for (let item of items) { await sql`INSERT INTO groceries (name) VALUES (${item})`; } // SELECT queries and queries with the RETURNING clause will // return the matched records as an array of objects const data = await sql`SELECT * FROM groceries`; console.log(data); ``` Log: ```javascript [ { id: 1, name: 'bread' }, { id: 2, name: 'milk' }, { id: 3, name: 'rice' }, ]; ``` ### Kysely ```typescript import { SQLocalKysely } from 'sqlocal/kysely'; import { Kysely, Generated } from 'kysely'; // Initialize SQLocalKysely and pass the dialect to Kysely const { dialect } = new SQLocalKysely('database.sqlite3'); const db = new Kysely({ dialect }); // Define your schema // (passed to the Kysely generic above) type DB = { groceries: { id: Generated; name: string; }; }; // Make type-safe queries const data = await db .selectFrom('groceries') .select('name') .orderBy('name', 'asc') .execute(); console.log(data); ``` See the Kysely documentation for [getting started](https://kysely.dev/docs/getting-started?dialect=sqlite). ### Drizzle ```typescript import { SQLocalDrizzle } from 'sqlocal/drizzle'; import { drizzle } from 'drizzle-orm/sqlite-proxy'; import { sqliteTable, int, text } from 'drizzle-orm/sqlite-core'; // Initialize SQLocalDrizzle and pass the driver to Drizzle const { driver } = new SQLocalDrizzle('database.sqlite3'); const db = drizzle(driver); // Define your schema const groceries = sqliteTable('groceries', { id: int('id').primaryKey({ autoIncrement: true }), name: text('name').notNull(), }); // Make type-safe queries const data = await db .select({ name: groceries.name }) .from(groceries) .orderBy(groceries.name) .all(); console.log(data); ``` See the Drizzle ORM documentation for [declaring your schema](https://orm.drizzle.team/docs/sql-schema-declaration) and [making queries](https://orm.drizzle.team/docs/crud). --- --- url: /reference/kysely.md --- [sqlocal](../index.md) / kysely # kysely ## Classes * [SQLocalKysely](classes/SQLocalKysely.md) --- --- url: /kysely/migrations.md --- # Kysely Migrations As you update your app, you need to ensure that every user's database schema remains compatible with your app's logic. To do this, you can run [Kysely migrations](https://kysely.dev/docs/migrations) in the frontend through your SQLocal client. ## Create Migrations Since SQLocal runs in the frontend, your app's migrations should be included in the frontend bundle as well. To prepare migrations to use with Kysely in the frontend, we need to build a migrations object where each entry has a `string` key and a value that is a Kysely `Migration` object. For example, let's consider the file structure below. We have each `Migration` live in its own file in the `migrations` directory, and we have an `index.ts` file where we combine all the `Migration`s into the full migrations object. ``` . ├── database/ │ ├── migrations/ │ │ ├── 2023-08-01.ts │ │ ├── 2023-08-02.ts │ │ └── index.ts │ ├── client.ts │ ├── migrator.ts │ └── schema.ts └── main.ts ``` Each `Migration` has an `up` method and a `down` method which run Kysely queries. The `up` method migrates the database, and the `down` method does the inverse of the `up` method in case you ever need to rollback migrations. With your `Migration` object written, import it into the `index.ts` file, and add it to the migrations object, which should be the type `Record`. The keys you use here will determine the order that Kysely runs the migrations in, so they need to be numbered or start with a date or timestamp. ::: code-group ```typescript [index.ts] import { Migration } from 'kysely'; import { Migration20230801 } from './2023-08-01'; import { Migration20230802 } from './2023-08-02'; export const migrations: Record = { '2023-08-01': Migration20230801, '2023-08-02': Migration20230802, }; ``` ```typescript [2023-08-01.ts] import type { Kysely, Migration } from 'kysely'; export const Migration20230801: Migration = { async up(db: Kysely) { await db.schema .createTable('groceries') .addColumn('id', 'integer', (cb) => cb.primaryKey().autoIncrement()) .addColumn('name', 'text', (cb) => cb.notNull()) .execute(); }, async down(db: Kysely) { await db.schema.dropTable('groceries').execute(); }, }; ``` ::: ## Create Migrator With the migrations object ready, we can create the Kysely `Migrator` that will read those migrations to execute them. `Migrator` needs to be passed `db`, which is your `Kysely` instance initialized with the `SQLocalKysely` dialect, and a `provider` which implements a `getMigrations` method to fetch the migrations object we made before. This can be accomplished with a dynamic `import` of the migrations from the `index.ts` file. ::: code-group ```typescript [migrator.ts] import { Migrator } from 'kysely'; import { db } from './client'; export const migrator = new Migrator({ db, provider: { async getMigrations() { const { migrations } = await import('./migrations/'); return migrations; }, }, }); ``` ```typescript [client.ts] import { SQLocalKysely } from 'sqlocal/kysely'; import { Kysely } from 'kysely'; import type { Database } from './schema'; const { dialect } = new SQLocalKysely('database.sqlite3'); export const db = new Kysely({ dialect }); ``` ```typescript [schema.ts] import type { Generated } from 'kysely'; export type Database = { groceries: GroceriesTable; }; export type GroceriesTable = { id: Generated; name: string; quantity: number; }; ``` ::: ## Run Migrations All that's left now is to put that `Migrator` to use. Import it wherever your app initializes and call its `migrateToLatest` method. This will execute, in order, any of the migrations that have not yet been run against the database instance that was passed to the `Migrator`. ```typescript [main.ts] import { migrator } from './database/migrator'; await migrator.migrateToLatest(); ``` The `Migrator` also has other methods to run migrations as needed. ```typescript // run the next migration await migrator.migrateUp(); // rollback the last migration await migrator.migrateDown(); // migrate to the point of the migration passed by key await migrator.migrateTo('2023-08-01'); ``` --- --- url: /kysely/setup.md --- # Kysely Query Builder Setup SQLocal provides a dialect for the [Kysely](https://kysely.dev/) query builder so that you can use it to make fully typed queries against databases in your TypeScript codebase. ## Install Install the Kysely package alongside SQLocal in your application using your package manager. ::: code-group ```sh [npm] npm install sqlocal kysely ``` ```sh [yarn] yarn add sqlocal kysely ``` ```sh [pnpm] pnpm install sqlocal kysely ``` ::: ## Initialize SQLocal provides the Kysely dialect from a child class of `SQLocal` called `SQLocalKysely` imported from `sqlocal/kysely`. This class has all the same methods as `SQLocal` and adds `dialect` which you pass to the `Kysely` instance. ```typescript import { SQLocalKysely } from 'sqlocal/kysely'; import { Kysely } from 'kysely'; const { dialect } = new SQLocalKysely('database.sqlite3'); export const db = new Kysely({ dialect }); ``` Now, any queries you run through this Kysely instance will be executed against the database passed to `SQLocalKysely`. ## Define Schema With Kysely, your schema is defined using TypeScript object types. See the [Kysely documentation](https://kysely.dev/docs/getting-started#types). ```typescript import type { Generated } from 'kysely'; export type Database = { groceries: GroceriesTable; }; export type GroceriesTable = { id: Generated; name: string; quantity: number; }; ``` With this defined, pass the database type to the Kysely instance, and your queries built with Kysely will now have auto-complete and type checking. ```typescript export const db = new Kysely({ dialect }); ``` ## Make Queries Import your Kysely instance to start making type-safe queries. ```typescript const data = await db .selectFrom('groceries') .select(['name', 'quantity']) .orderBy('name', 'asc') .execute(); console.log(data); ``` See the [Kysely documentation](https://kysely.dev/docs/category/examples) for more examples. --- --- url: /api/overwritedatabasefile.md --- # overwriteDatabaseFile Replace the contents of the SQLite database file. ## Usage Access or destructure `overwriteDatabaseFile` from the `SQLocal` client. ```javascript import { SQLocal } from 'sqlocal'; const { overwriteDatabaseFile } = new SQLocal('database.sqlite3'); ``` ::: tip NOTE If you are using the [Kysely Query Builder](/kysely/setup) or [Drizzle ORM](/drizzle/setup) for type-safe queries, you will initialize the client with a child class of `SQLocal`. See the corresponding setup page. Usage is the same otherwise. ::: The `overwriteDatabaseFile` method takes a database file as a [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File), [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob), [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream), [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer), or [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) object and returns a `Promise` to replace the `SQLocal` instance's associated database file with the one provided. For example, you can download a database file from your server to replace the local file. ```javascript const response = await fetch('https://example.com/download?id=12345'); const databaseFile = await response.blob(); await overwriteDatabaseFile(databaseFile); ``` If the database file might be large, you could alternatively pass the `ReadableStream` from the response's `body`, and SQLocal will stream the database to the client in chunks. ```javascript const response = await fetch('https://example.com/download?id=12345'); const databaseStream = response.body; if (databaseStream === null) throw new Error('No database found'); await overwriteDatabaseFile(databaseStream); ``` Or, your app may allow the user to import a database file through a form. ```javascript const fileInput = document.querySelector('input[type="file"]'); const databaseFile = fileInput.files[0]; await overwriteDatabaseFile(databaseFile); ``` The method also accepts a second, optional argument: a callback function to run after the overwrite but before connections from other SQLocal client instances are allowed to access the new database, a good time to run migrations. ```javascript await overwriteDatabaseFile(databaseFile, async () => { // Run your migrations }); ``` Since calling `overwriteDatabaseFile` will reset all connections to the database file, the configured `onInit` statements and `onConnect` hook (see [Options](../guide/setup.md#options)) will re-run on any SQLocal clients connected to the database when it is overwritten. The client that initiated the overwrite will have its `onConnect` hook run first, before the method's callback, and the other clients' `onConnect` hooks will run after the callback. --- --- url: /reference/react.md --- [sqlocal](../index.md) / react # react ## Functions * [useReactiveQuery](functions/useReactiveQuery.md) --- --- url: /api/reactivequery.md --- # reactiveQuery Subscribe to a SQL query and receive the latest results whenever the read tables change. ## Usage Access or destructure `reactiveQuery` from the `SQLocal` client. To enable this feature, the `reactive` option must be set to `true`. ```javascript import { SQLocal } from 'sqlocal'; const { reactiveQuery } = new SQLocal({ databasePath: 'database.sqlite3', reactive: true, }); ``` ::: tip NOTE If you are using the [Kysely Query Builder](/kysely/setup) or [Drizzle ORM](/drizzle/setup) for type-safe queries, you will initialize the client with a child class of `SQLocal`. See the corresponding setup page. Usage is the same otherwise. ::: The `reactiveQuery` method takes a SQL query and allows you to subscribe to its results. When you call the `subscribe` method it returns, the query will run and its result data will be passed to your callback, and any time the database tables that are read from in that query get updated, the query will automatically re-run and pass the latest results to the callback. The query can automatically react to mutations made on the database by the same *or* other `SQLocal` instances that have the `reactive` option set to `true`, even if they are done in other windows/tabs of the web app. Inserts, updates, or deletes to the relevent database tables from any scope will trigger the subscription. ```javascript const subscription = reactiveQuery( (sql) => sql`SELECT name FROM groceries` ).subscribe((data) => { console.log('Grocery List Updated:', data); }); ``` The query can be any SQL statement that reads one or more tables. It can be passed using a `sql` tag function available in the `reactiveQuery` callback that works similarly to the [`sql` tag function used for single queries](sql.md). It can also be a query built with Drizzle or Kysely; see the "Query Builders" section [below](#query-builders). You can then call `subscribe` on the object returned from `reactiveQuery` to register a callback that gets called an initial time and then again whenever the one or more of the queried tables are changed. The latest result data from the query will be passed as the first argument to your callback. You can also pass a second callback to `subscribe` that will be called if there are any errors when running your query. ```javascript const groceries = reactiveQuery((sql) => sql`SELECT name FROM groceries`); const subscription = groceries.subscribe( (data) => { console.log('Grocery List Updated:', data); }, (err) => { console.error('Query Error:', err); } ); ``` To stop receiving updates and clean up the subscription, call `unsubscribe` on the object returned from `subscribe`. ```javascript subscription.unsubscribe(); ``` Note that mutations that happen inside a [transaction](transaction.md) will not trigger reactive queries until the transaction is committed. This ensures your data does not get out of sync in the case that the transaction is rolled back. Also, because of [SQLite's "Truncate Optimization"](https://sqlite.org/lang_delete.html#truncateopt), reactive queries will not be triggered by `DELETE` statements that have no `WHERE` clause, `RETURNING` clause, or table triggers. ## Query Builders If you are using a query builder, you can use it to create the reactive query, rather than use the `sql` tag function. The data emitted in the `subscribe` callback will then be fully typed by the query builder. ### Drizzle With Drizzle ORM, construct a query and pass it to `reactiveQuery` without executing it. ```javascript const subscription = reactiveQuery( db.select({ name: groceries.name }).from(groceries) ).subscribe((data) => { // data is typed as { name: string; }[] console.log('Grocery List Updated:', data); }); ``` ### Kysely With Kysely, construct a query, call the `compile` method on it, and pass it to `reactiveQuery`. ```javascript const subscription = reactiveQuery( db.selectFrom('groceries').select('name').compile() ).subscribe((data) => { // data is typed as { name: string; }[] console.log('Grocery List Updated:', data); }); ``` ## UI Frameworks We also provide `useReactiveQuery` hook implementations to make it easier to integrate reactive queries with the reactivity systems of UI frameworks. The hook handles subscribing, returns reactive data, and automatically unsubscribes from the query when the component it's used in is destroyed. `useReactiveQuery` takes your `SQLocal` instance and a SQL query as arguments. The query can be passed using the `sql` tag function or using a query builder as described [above](#query-builders). It returns an object containing the following reactive values: * **`data`** (`Result[]`) - The result data from your SQL query. * **`error`** (`Error | undefined`) - An `Error` object if the SQL query fails. * **`status`** (`'pending' | 'error' | 'ok'`) - The string `'pending'` if the SQL query has not completed for the first time yet, `'error'` if the SQL query failed, or `'ok'` if the SQL query returned successfully. ### React Import the React version of `useReactiveQuery` from `sqlocal/react`. It requires React 18 or higher. In addition to `data`, `error`, and `status`, the object returned from this version of `useReactiveQuery` also contains `setDb` and `setQuery` functions which allow you to dynamically change the arguments from their initial values and automatically resubscribe. ```js import { SQLocal } from 'sqlocal'; import { useReactiveQuery } from 'sqlocal/react'; const db = new SQLocal({ databasePath: 'database.sqlite3', reactive: true, }); export function MyComponent() { const groceries = useReactiveQuery(db, (sql) => sql`SELECT * FROM groceries`); } ``` ### Vue Import the Vue version of `useReactiveQuery` from `sqlocal/vue`. It requires Vue 3 or higher. This version of `useReactiveQuery` returns `data`, `error`, and `status` as read-only Vue refs. It can also accept its arguments as refs, which allows you to dynamically change them from their initial values and automatically resubscribe. ```vue ``` ### Angular Import the Angular version of `useReactiveQuery` from `sqlocal/angular`. It requires Angular 17 or higher. This version of `useReactiveQuery` returns `data`, `error`, and `status` as read-only Angular signals. It can also accept its arguments as signals, which allows you to dynamically change them from their initial values and automatically resubscribe. ```ts import { Component } from '@angular/core'; import { SQLocal } from 'sqlocal'; import { useReactiveQuery } from 'sqlocal/angular'; const db = new SQLocal({ databasePath: 'database.sqlite3', reactive: true, }); @Component({ selector: 'my-component', }) export class MyComponent { groceries = useReactiveQuery(db, (sql) => sql`SELECT * FROM groceries`); } ``` ### Nano Stores Nano Stores provides an integration with SQLocal through the [`@nanostores/sql`](https://www.npmjs.com/package/@nanostores/sql) package. It uses `reactiveQuery` to create a store that can be used with Nano Store's other state management tools. ```ts import { openDb } from '@nanostores/sql'; import { sqlocalDriver } from '@nanostores/sql/sqlocal'; const db = openDb(sqlocalDriver('database.sqlite3')); const $groceries = db.store`SELECT * FROM groceries`; ``` --- --- url: /reference.md --- # Reference ## Modules * [angular](angular/index.md) * [drizzle](drizzle/index.md) * [index](index/index.md) * [kysely](kysely/index.md) * [react](react/index.md) * [vite](vite/index.md) * [vue](vue/index.md) --- --- url: /guide/setup.md --- # Setup Prepare the SQLocal client and connect to a database. ## Install Install the SQLocal package in your application using your package manager. ::: code-group ```sh [npm] npm install sqlocal ``` ```sh [yarn] yarn add sqlocal ``` ```sh [pnpm] pnpm install sqlocal ``` ::: ## Cross-Origin Isolation In order to persist data to the origin private file system, this package relies on APIs that require [cross-origin isolation](https://developer.mozilla.org/en-US/docs/Web/API/Window/crossOriginIsolated), so the page you use this package on must be served with the following HTTP headers. Otherwise, the browser will block access to the origin private file system. ```http Cross-Origin-Embedder-Policy: require-corp Cross-Origin-Opener-Policy: same-origin ``` How this is configured will depend on what web server or hosting service your application uses. If your development server uses Vite, [see the configuration below](#vite-configuration). ## Initialize Import the `SQLocal` class to initialize your client for interacting with a local SQLite database. ```javascript import { SQLocal } from 'sqlocal'; export const db = new SQLocal('database.sqlite3'); ``` Pass the file name for your SQLite database file to the `SQLocal` constructor, and the client will connect to that database file. If your file does not already exist in the origin private file system, it will be created automatically. The file extension that you use does not matter for functionality. It is conventional to use `.sqlite3` or `.db`, but feel free to use whatever extension you need to (e.g., you are using [SQLite as an application file format](https://www.sqlite.org/aff_short.html)). You will probably also want to export the client so that you can use it throughout your application. If your application needs to query multiple databases, you can initialize another instance of `SQLocal` for each database. With the client initialized, you are ready to [start making queries](/api/sql). ::: tip NOTE If you are using the [Kysely Query Builder](/kysely/setup) or [Drizzle ORM](/drizzle/setup) for type-safe queries, you will initialize the client with a child class of `SQLocal`. See the corresponding setup page. Usage is the same otherwise. ::: ## Options The `SQLocal` constructor can also be passed an object to accept additional options. ```javascript export const db = new SQLocal({ databasePath: 'database.sqlite3', readOnly: true, verbose: true, reactive: true, onInit: (sql) => {}, onConnect: (reason) => {}, }); ``` * **`databasePath`** (`string`) - The file name for the database file. This is the only required option. * **`readOnly`** (`boolean`) - If `true`, connect to the database in read-only mode. Attempts to run queries that would mutate the database will throw an error. * **`verbose`** (`boolean`) - If `true`, any SQL executed on the database will be logged to the console. * **`reactive`** (`boolean`) - If `true`, listening for database table changes is enabled, allowing the client to work with the [`reactiveQuery` method](../api/reactivequery.md). * **`onInit`** (`function`) - A callback that will be run once when the client has initialized but before it has connected to the database. This callback should return an array of SQL statements (using the passed `sql` tagged template function, similar to the [`batch` method](../api/batch.md)) that should be executed before any other statements on the database connection. The `onInit` callback will be called only once, but the statements will be executed every time the client creates a new database connection. This makes it the best way to set up any `PRAGMA` settings, temporary tables, views, or triggers for the connection. * **`onConnect`** (`function`) - A callback that will be run after the client has connected to the database. This will happen at initialization and any time [`overwriteDatabaseFile`](/api/overwritedatabasefile) or [`deleteDatabaseFile`](/api/deletedatabasefile) is called on any SQLocal client connected to the same database. The callback is passed a string (`'initial' | 'overwrite' | 'delete'`) that indicates why the callback was executed. This callback is useful for syncing your application's state with data from the newly-connected database. * **`processor`** (`SQLocalProcessor | Worker`) - Allows you to override how this instance communicates with the SQLite database. This is for advanced use-cases, such as for using custom compilations or forks of SQLite or for cases where you need to initialize the web worker yourself rather than have SQLocal do it. ## Vite Configuration Vite needs some additional configuration to handle web worker files correctly. If you or your framework uses Vite as your build tool, you can use SQLocal's Vite plugin to set this up. The plugin will also enable [cross-origin isolation](#cross-origin-isolation) (required for origin private file system persistence) for the Vite development server by default. Just don't forget to also configure your *production* web server to use the same HTTP headers. Import the plugin from `sqlocal/vite` and add it to your [Vite configuration](https://vitejs.dev/config/). ```javascript import { defineConfig } from 'vite'; import sqlocal from 'sqlocal/vite'; export default defineConfig({ plugins: [sqlocal()], }); ``` ::: details Angular Since Angular does not expose its Vite configuration, you will need to configure it differently than other Vite-based frameworks. You can reference the [SQLocal Shell codebase](https://github.com/DallasHoff/sqlocal-shell) as an example. First, it's important to disable prebundling in the development server so that Vite compiles the web workers correctly. You can do this in your serve architect in `angular.json`. This is also where you can set the headers for cross-origin isolation in development. ```json{8-12} "architect": { "build": { ... }, "serve": { "builder": "@angular/build:dev-server", "configurations": { "development": { "buildTarget": "my-app:build:development", "headers": { "Cross-Origin-Embedder-Policy": "require-corp", "Cross-Origin-Opener-Policy": "same-origin" }, "prebundle": false } }, "defaultConfiguration": "development" } } ``` You will also need to serve `sqlite3.wasm` from the root of your site. You can configure this in your build architect in `angular.json`. ```json{10-13} "architect": { "build": { "builder": "@angular/build:application", "options": { "outputPath": "dist/my-app", "index": "src/index.html", "browser": "src/main.ts", "tsConfig": "tsconfig.app.json", "assets": [ { "glob": "**/*", "input": "node_modules/@sqlite.org/sqlite-wasm/dist" }, { "glob": "**/*", "input": "public" } ], "styles": ["src/styles.scss"], "scripts": [], "webWorkerTsConfig": "tsconfig.worker.json" } }, "serve": { ... } } ``` Finally, you will need to create your own web worker file and pass it to the `SQLocal` constructor, rather than use the built-in worker. Run `ng create web-worker sqlocal` to make a web worker file, add [this code](https://github.com/DallasHoff/sqlocal-shell/blob/main/src/sqlocal.worker.ts), and pass the worker to SQLocal's `processor` option. ```typescript const db = new SQLocal({ databasePath: 'database.sqlite3', processor: new Worker(new URL('../sqlocal.worker', import.meta.url)), }); ``` ::: --- --- url: /api/sql.md --- # sql Execute SQL queries against the database. ## Usage Access or destructure `sql` from the `SQLocal` client. ```javascript import { SQLocal } from 'sqlocal'; const { sql } = new SQLocal('database.sqlite3'); ``` ::: tip NOTE If you are using the [Kysely Query Builder](/kysely/setup) or [Drizzle ORM](/drizzle/setup) for type-safe queries, you will initialize the client with a child class of `SQLocal`. See the corresponding setup page. Usage is the same otherwise. ::: `sql` is used as a [tagged template](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates). Values interpolated into the query string will be passed to the database as parameters to that query. ```javascript const item = 'Bread'; const quantity = 2; await sql`INSERT INTO groceries (name, quantity) VALUES (${item}, ${quantity})`; ``` `SELECT` queries and queries with the `RETURNING` clause will return the matched records as an array of objects. ```javascript const data = await sql`SELECT * FROM groceries`; console.log(data); ``` Example result: ```javascript [ { id: 1, name: 'Rice', quantity: 4 }, { id: 2, name: 'Milk', quantity: 1 }, { id: 3, name: 'Bread', quantity: 2 }, ]; ``` Multiple statements can be passed in the query, but note that the results returned will only include results from the first value-returning statement. Also, only one statement in the query can have parameter bindings. Because of these restrictions, it is recommended to pass only one SQL statement per call to `sql`. To run multiple statements together, use the [`batch` method](batch.md). ```javascript // Warning: only returns the row with id 1. const result = await sql` SELECT * FROM foo WHERE id = 1; SELECT * FROM foo WHERE id = 2; `; // Recommended: one statement per query const result1 = await sql`SELECT * FROM foo WHERE id = 1;`; const result2 = await sql`SELECT * FROM foo WHERE id = 2;`; ``` --- --- url: /api/transaction.md --- # transaction Execute SQL transactions against the database. ## Usage Access or destructure `transaction` from the `SQLocal` client. ```javascript import { SQLocal } from 'sqlocal'; const { transaction } = new SQLocal('database.sqlite3'); ``` ::: tip NOTE If you are using the [Kysely Query Builder](/kysely/setup) or [Drizzle ORM](/drizzle/setup) for type-safe queries, you will initialize the client with a child class of `SQLocal`. See the corresponding setup page. Usage is the same otherwise. ::: The `transaction` method provides a way to execute a transaction on the database, ensuring atomicity and isolation of the SQL queries executed within it. `transaction` takes a callback that is passed a `tx` object containing a `sql` tagged template and `batch` function for executing SQL within the transaction. The `sql` tag function and `batch` function in the `tx` object work similarly to the [`sql` tag function used for single queries](sql.md) and the [`batch` method](batch.md) respectively, but they ensure that the queries are executed in the context of the open transaction. Any logic can be carried out in the callback between the queries as needed. If any of the queries fail or any other error is thrown within the callback, `transaction` will throw an error and the transaction will be rolled back automatically. If the callback completes successfully, the transaction will be committed. The callback can return any value desired, and if the transaction succeeds, this value will be returned from `transaction`. ```javascript const productName = 'rice'; const productPrice = 2.99; const newProductId = await transaction(async (tx) => { const [product] = await tx.sql` INSERT INTO groceries (name) VALUES (${productName}) RETURNING * `; await tx.sql` INSERT INTO prices (groceryId, price) VALUES (${product.id}, ${productPrice}) `; return product.id; }); ``` ## Query Builders ### Drizzle Drizzle queries can also be used with `transaction` by passing them to the `tx` object's `query` function. `query` will execute the Drizzle query as part of the transaction and its return value will be typed according to Drizzle. This is the recommended way to execute transactions when using Drizzle with SQLocal. The [`transaction` method provided by Drizzle](https://orm.drizzle.team/docs/transactions) does not ensure isolation, so queries executed outside of the Drizzle transaction at the same time may create a data inconsistency. ```javascript const productName = 'rice'; const productPrice = 2.99; const newProductId = await transaction(async (tx) => { const [product] = await tx.query( db.insert(groceries).values({ name: productName }).returning() ); await tx.query( db.insert(prices).values({ groceryId: product.id, price: productPrice }) ); return product.id; }); ``` ### Kysely Kysely queries can be used with `transaction` by calling Kysely's `compile` method on the queries and passing them to the `tx` object's `query` function. `query` will execute the Kysely query as part of the transaction and its return value will be typed according to Kysely. Functionally, SQLocal's `transaction` method and [Kysely's `transaction` method](https://kysely.dev/docs/examples/transactions/simple-transaction) are very similar. Both can ensure atomicity and isolation of the transaction, so either method can be used to the same effect as preferred. ```javascript const productName = 'rice'; const productPrice = 2.99; const newProductId = await transaction(async (tx) => { const [product] = await tx.query( db .insertInto('groceries') .values({ name: productName }) .returningAll() .compile() ); await tx.query( db .insertInto('prices') .values({ groceryId: product.id, price: productPrice }) .compile() ); return product.id; }); ``` --- --- url: /reference/index/type-aliases/AggregateUserFunction.md --- [sqlocal](../../index.md) / [index](../index.md) / AggregateUserFunction # Type Alias: AggregateUserFunction ```ts type AggregateUserFunction = { func: { final: (...args) => any; step: (...args) => void; }; name: string; type: "aggregate"; }; ``` Defined in: [src/types.ts:183](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L183) ## Properties | Property | Type | Defined in | | ------ | ------ | ------ | | `func` | { `final`: (...`args`) => `any`; `step`: (...`args`) => `void`; } | [src/types.ts:186](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L186) | | `func.final` | (...`args`) => `any` | [src/types.ts:188](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L188) | | `func.step` | (...`args`) => `void` | [src/types.ts:187](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L187) | | `name` | `string` | [src/types.ts:185](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L185) | | `type` | `"aggregate"` | [src/types.ts:184](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L184) | --- --- url: /reference/index/type-aliases/CallbackUserFunction.md --- [sqlocal](../../index.md) / [index](../index.md) / CallbackUserFunction # Type Alias: CallbackUserFunction ```ts type CallbackUserFunction = { func: (...args) => void; name: string; type: "callback"; }; ``` Defined in: [src/types.ts:173](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L173) ## Properties | Property | Type | Defined in | | ------ | ------ | ------ | | `func` | (...`args`) => `void` | [src/types.ts:176](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L176) | | `name` | `string` | [src/types.ts:175](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L175) | | `type` | `"callback"` | [src/types.ts:174](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L174) | --- --- url: /reference/index/type-aliases/ClientConfig.md --- [sqlocal](../../index.md) / [index](../index.md) / ClientConfig # Type Alias: ClientConfig ```ts type ClientConfig = { databasePath: DatabasePath; onConnect?: (reason) => void; onInit?: (sql) => void | Statement[]; processor?: SQLocalProcessor | Worker; reactive?: boolean; readOnly?: boolean; verbose?: boolean; }; ``` Defined in: [src/types.ts:134](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L134) ## Properties | Property | Type | Defined in | | ------ | ------ | ------ | | `databasePath` | [`DatabasePath`](DatabasePath.md) | [src/types.ts:135](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L135) | | `onConnect?` | (`reason`) => `void` | [src/types.ts:140](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L140) | | `onInit?` | (`sql`) => `void` | [`Statement`](Statement.md)\[] | [src/types.ts:139](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L139) | | `processor?` | [`SQLocalProcessor`](../classes/SQLocalProcessor.md) | `Worker` | [src/types.ts:141](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L141) | | `reactive?` | `boolean` | [src/types.ts:136](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L136) | | `readOnly?` | `boolean` | [src/types.ts:137](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L137) | | `verbose?` | `boolean` | [src/types.ts:138](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L138) | --- --- url: /reference/index/type-aliases/ConnectReason.md --- [sqlocal](../../index.md) / [index](../index.md) / ConnectReason # Type Alias: ConnectReason ```ts type ConnectReason = "initial" | "overwrite" | "delete"; ``` Defined in: [src/types.ts:132](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L132) --- --- url: /reference/index/type-aliases/DatabaseInfo.md --- [sqlocal](../../index.md) / [index](../index.md) / DatabaseInfo # Type Alias: DatabaseInfo ```ts type DatabaseInfo = { databasePath?: DatabasePath; databaseSizeBytes?: number; persisted?: boolean; storageType?: Sqlite3StorageType; }; ``` Defined in: [src/types.ts:153](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L153) ## Properties | Property | Type | Defined in | | ------ | ------ | ------ | | `databasePath?` | [`DatabasePath`](DatabasePath.md) | [src/types.ts:154](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L154) | | `databaseSizeBytes?` | `number` | [src/types.ts:155](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L155) | | `persisted?` | `boolean` | [src/types.ts:157](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L157) | | `storageType?` | [`Sqlite3StorageType`](Sqlite3StorageType.md) | [src/types.ts:156](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L156) | --- --- url: /reference/index/type-aliases/DatabasePath.md --- [sqlocal](../../index.md) / [index](../index.md) / DatabasePath # Type Alias: DatabasePath ```ts type DatabasePath = | string & { } | ":memory:" | "local" | ":localStorage:" | "session" | ":sessionStorage:"; ``` Defined in: [src/types.ts:123](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L123) --- --- url: /reference/index/type-aliases/DataChange.md --- [sqlocal](../../index.md) / [index](../index.md) / DataChange # Type Alias: DataChange ```ts type DataChange = { operation: "insert" | "update" | "delete"; rowid: BigInt; table: string; }; ``` Defined in: [src/types.ts:160](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L160) ## Properties | Property | Type | Defined in | | ------ | ------ | ------ | | `operation` | `"insert"` | `"update"` | `"delete"` | [src/types.ts:161](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L161) | | `rowid` | `BigInt` | [src/types.ts:163](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L163) | | `table` | `string` | [src/types.ts:162](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L162) | --- --- url: /reference/index/type-aliases/DriverConfig.md --- [sqlocal](../../index.md) / [index](../index.md) / DriverConfig # Type Alias: DriverConfig ```ts type DriverConfig = { databasePath?: DatabasePath; reactive?: boolean; readOnly?: boolean; verbose?: boolean; }; ``` Defined in: [src/types.ts:108](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L108) ## Properties | Property | Type | Defined in | | ------ | ------ | ------ | | `databasePath?` | [`DatabasePath`](DatabasePath.md) | [src/types.ts:109](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L109) | | `reactive?` | `boolean` | [src/types.ts:110](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L110) | | `readOnly?` | `boolean` | [src/types.ts:111](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L111) | | `verbose?` | `boolean` | [src/types.ts:112](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L112) | --- --- url: /reference/index/type-aliases/DriverStatement.md --- [sqlocal](../../index.md) / [index](../index.md) / DriverStatement # Type Alias: DriverStatement ```ts type DriverStatement = { method?: Sqlite3Method; params?: any[]; sql: string; }; ``` Defined in: [src/types.ts:115](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L115) ## Properties | Property | Type | Defined in | | ------ | ------ | ------ | | `method?` | [`Sqlite3Method`](Sqlite3Method.md) | [src/types.ts:118](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L118) | | `params?` | `any`\[] | [src/types.ts:117](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L117) | | `sql` | `string` | [src/types.ts:116](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L116) | --- --- url: /reference/index/type-aliases/ProcessorConfig.md --- [sqlocal](../../index.md) / [index](../index.md) / ProcessorConfig # Type Alias: ProcessorConfig ```ts type ProcessorConfig = { clientKey?: QueryKey; databasePath?: DatabasePath; onInitStatements?: Statement[]; reactive?: boolean; readOnly?: boolean; verbose?: boolean; }; ``` Defined in: [src/types.ts:144](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L144) ## Properties | Property | Type | Defined in | | ------ | ------ | ------ | | `clientKey?` | [`QueryKey`](QueryKey.md) | [src/types.ts:149](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L149) | | `databasePath?` | [`DatabasePath`](DatabasePath.md) | [src/types.ts:145](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L145) | | `onInitStatements?` | [`Statement`](Statement.md)\[] | [src/types.ts:150](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L150) | | `reactive?` | `boolean` | [src/types.ts:146](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L146) | | `readOnly?` | `boolean` | [src/types.ts:147](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L147) | | `verbose?` | `boolean` | [src/types.ts:148](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L148) | --- --- url: /reference/index/type-aliases/QueryKey.md --- [sqlocal](../../index.md) / [index](../index.md) / QueryKey # Type Alias: QueryKey ```ts type QueryKey = string; ``` Defined in: [src/types.ts:131](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L131) --- --- url: /reference/index/type-aliases/RawResultData.md --- [sqlocal](../../index.md) / [index](../index.md) / RawResultData # Type Alias: RawResultData ```ts type RawResultData = { columns: string[]; numAffectedRows?: bigint; rows: unknown[] | unknown[][]; }; ``` Defined in: [src/types.ts:74](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L74) ## Properties | Property | Type | Defined in | | ------ | ------ | ------ | | `columns` | `string`\[] | [src/types.ts:76](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L76) | | `numAffectedRows?` | `bigint` | [src/types.ts:77](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L77) | | `rows` | `unknown`\[] | `unknown`\[]\[] | [src/types.ts:75](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L75) | --- --- url: /reference/index/type-aliases/ReactiveQuery.md --- [sqlocal](../../index.md) / [index](../index.md) / ReactiveQuery # Type Alias: ReactiveQuery\ ```ts type ReactiveQuery = { subscribe: (onData, onError?) => { unsubscribe: () => void; }; value: Result[]; }; ``` Defined in: [src/types.ts:63](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L63) ## Type Parameters | Type Parameter | Default type | | ------ | ------ | | `Result` | `unknown` | ## Properties | Property | Modifier | Type | Defined in | | ------ | ------ | ------ | ------ | | `subscribe` | `public` | (`onData`, `onError?`) => { `unsubscribe`: () => `void`; } | [src/types.ts:65](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L65) | | `value` | `readonly` | `Result`\[] | [src/types.ts:64](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L64) | --- --- url: /reference/index/type-aliases/ReactiveQueryStatus.md --- [sqlocal](../../index.md) / [index](../index.md) / ReactiveQueryStatus # Type Alias: ReactiveQueryStatus ```ts type ReactiveQueryStatus = "pending" | "ok" | "error"; ``` Defined in: [src/types.ts:72](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L72) --- --- url: /reference/index/type-aliases/ReturningStatement.md --- [sqlocal](../../index.md) / [index](../index.md) / ReturningStatement # Type Alias: ReturningStatement\ ```ts type ReturningStatement = | Statement | IsAny> extends true ? never : KyselyQuery | IsAny> extends true ? never : DrizzleQuery ? any : Result[], "sqlite">; ``` Defined in: [src/types.ts:30](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L30) ## Type Parameters | Type Parameter | Default type | | ------ | ------ | | `Result` | `unknown` | --- --- url: /reference/index/type-aliases/ScalarUserFunction.md --- [sqlocal](../../index.md) / [index](../index.md) / ScalarUserFunction # Type Alias: ScalarUserFunction ```ts type ScalarUserFunction = { func: (...args) => any; name: string; type: "scalar"; }; ``` Defined in: [src/types.ts:178](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L178) ## Properties | Property | Type | Defined in | | ------ | ------ | ------ | | `func` | (...`args`) => `any` | [src/types.ts:181](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L181) | | `name` | `string` | [src/types.ts:180](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L180) | | `type` | `"scalar"` | [src/types.ts:179](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L179) | --- --- url: /reference/index/type-aliases/Sqlite3.md --- [sqlocal](../../index.md) / [index](../index.md) / Sqlite3 # Type Alias: Sqlite3 ```ts type Sqlite3 = Sqlite3Static; ``` Defined in: [src/types.ts:12](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L12) --- --- url: /reference/index/type-aliases/Sqlite3Db.md --- [sqlocal](../../index.md) / [index](../index.md) / Sqlite3Db # Type Alias: Sqlite3Db ```ts type Sqlite3Db = Database; ``` Defined in: [src/types.ts:14](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L14) --- --- url: /reference/index/type-aliases/Sqlite3InitModule.md --- [sqlocal](../../index.md) / [index](../index.md) / Sqlite3InitModule # Type Alias: Sqlite3InitModule ```ts type Sqlite3InitModule = () => Promise; ``` Defined in: [src/types.ts:13](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L13) ## Returns `Promise`<[`Sqlite3`](Sqlite3.md)> --- --- url: /reference/index/type-aliases/Sqlite3Method.md --- [sqlocal](../../index.md) / [index](../index.md) / Sqlite3Method # Type Alias: Sqlite3Method ```ts type Sqlite3Method = "get" | "all" | "run" | "values"; ``` Defined in: [src/types.ts:15](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L15) --- --- url: /reference/index/type-aliases/Sqlite3StorageType.md --- [sqlocal](../../index.md) / [index](../index.md) / Sqlite3StorageType # Type Alias: Sqlite3StorageType ```ts type Sqlite3StorageType = | string & { } | "memory" | "opfs" | "local" | "session"; ``` Defined in: [src/types.ts:16](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L16) --- --- url: /reference/index/type-aliases/SqlTag.md --- [sqlocal](../../index.md) / [index](../index.md) / SqlTag # Type Alias: SqlTag ```ts type SqlTag = typeof sqlTag; ``` Defined in: [src/types.ts:40](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L40) --- --- url: /reference/index/type-aliases/Statement.md --- [sqlocal](../../index.md) / [index](../index.md) / Statement # Type Alias: Statement ```ts type Statement = { params: unknown[]; sql: string; }; ``` Defined in: [src/types.ts:25](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L25) ## Properties | Property | Type | Defined in | | ------ | ------ | ------ | | `params` | `unknown`\[] | [src/types.ts:27](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L27) | | `sql` | `string` | [src/types.ts:26](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L26) | --- --- url: /reference/index/type-aliases/StatementInput.md --- [sqlocal](../../index.md) / [index](../index.md) / StatementInput # Type Alias: StatementInput\ ```ts type StatementInput = | ReturningStatement | ((sql) => ReturningStatement); ``` Defined in: [src/types.ts:41](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L41) ## Type Parameters | Type Parameter | Default type | | ------ | ------ | | `Result` | `unknown` | --- --- url: /reference/index/type-aliases/Transaction.md --- [sqlocal](../../index.md) / [index](../index.md) / Transaction # Type Alias: Transaction ```ts type Transaction = { batch: (passStatements) => Promise; commit: () => Promise; lastAffectedRows?: bigint; query: (passStatement) => Promise; rollback: () => Promise; sql: (queryTemplate, ...params) => Promise; transactionKey: QueryKey; }; ``` Defined in: [src/types.ts:45](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L45) ## Properties | Property | Type | Defined in | | ------ | ------ | ------ | | `batch` | <`Result`>(`passStatements`) => `Promise`<`Result`\[]\[]> | [src/types.ts:55](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L55) | | `commit` | () => `Promise`<`void`> | [src/types.ts:58](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L58) | | `lastAffectedRows?` | `bigint` | [src/types.ts:47](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L47) | | `query` | <`Result`>(`passStatement`) => `Promise`<`Result`\[]> | [src/types.ts:48](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L48) | | `rollback` | () => `Promise`<`void`> | [src/types.ts:59](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L59) | | `sql` | <`Result`>(`queryTemplate`, ...`params`) => `Promise`<`Result`\[]> | [src/types.ts:51](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L51) | | `transactionKey` | [`QueryKey`](QueryKey.md) | [src/types.ts:46](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L46) | --- --- url: /reference/index/type-aliases/TransactionHandle.md --- [sqlocal](../../index.md) / [index](../index.md) / TransactionHandle # Type Alias: TransactionHandle ```ts type TransactionHandle = Pick; ``` Defined in: [src/types.ts:61](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L61) --- --- url: /reference/index/type-aliases/UserFunction.md --- [sqlocal](../../index.md) / [index](../index.md) / UserFunction # Type Alias: UserFunction ```ts type UserFunction = | CallbackUserFunction | ScalarUserFunction | AggregateUserFunction | WindowUserFunction; ``` Defined in: [src/types.ts:168](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L168) --- --- url: /reference/vite/type-aliases/VitePluginConfig.md --- [sqlocal](../../index.md) / [vite](../index.md) / VitePluginConfig # Type Alias: VitePluginConfig ```ts type VitePluginConfig = { coi?: boolean; }; ``` Defined in: [src/vite/index.ts:7](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/vite/index.ts#L7) Represents the configuration that SQLocal's Vite plugin accepts. ## See ## Properties | Property | Type | Description | Defined in | | ------ | ------ | ------ | ------ | | `coi?` | `boolean` | If set to `false`, the plugin will not add the HTTP response headers required for [cross-origin isolation](https://sqlocal.dev/guide/setup#cross-origin-isolation) to the Vite development server. **Default** `true` | [src/vite/index.ts:15](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/vite/index.ts#L15) | --- --- url: /reference/index/type-aliases/WindowUserFunction.md --- [sqlocal](../../index.md) / [index](../index.md) / WindowUserFunction # Type Alias: WindowUserFunction ```ts type WindowUserFunction = { func: { final: (...args) => any; inverse: (...args) => void; step: (...args) => void; value: (...args) => any; }; name: string; type: "window"; }; ``` Defined in: [src/types.ts:191](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L191) ## Properties | Property | Type | Defined in | | ------ | ------ | ------ | | `func` | { `final`: (...`args`) => `any`; `inverse`: (...`args`) => `void`; `step`: (...`args`) => `void`; `value`: (...`args`) => `any`; } | [src/types.ts:194](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L194) | | `func.final` | (...`args`) => `any` | [src/types.ts:198](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L198) | | `func.inverse` | (...`args`) => `void` | [src/types.ts:197](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L197) | | `func.step` | (...`args`) => `void` | [src/types.ts:195](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L195) | | `func.value` | (...`args`) => `any` | [src/types.ts:196](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L196) | | `name` | `string` | [src/types.ts:193](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L193) | | `type` | `"window"` | [src/types.ts:192](https://github.com/DallasHoff/sqlocal/blob/0bed8d150424126863280c8024af2135bb9ebb53/src/types.ts#L192) | --- --- url: /reference/vite.md --- [sqlocal](../index.md) / vite # vite ## Type Aliases * [VitePluginConfig](type-aliases/VitePluginConfig.md) ## Functions * [default](functions/default.md) --- --- url: /reference/vue.md --- [sqlocal](../index.md) / vue # vue ## Functions * [useReactiveQuery](functions/useReactiveQuery.md)