Skip to content

Commit

Permalink
Run PRAGMA optimize in destroy method
Browse files Browse the repository at this point in the history
  • Loading branch information
DallasHoff committed Jul 6, 2024
1 parent 13b21a2 commit 406eaef
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/api/destroy.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export const { destroy } = new SQLocal('database.sqlite3');

The `destroy` method takes no arguments. It will return a `Promise` to close the connection to the SQLite database file and then terminate the Web Worker that the `SQLocal` client uses internally to run queries.

It will also execute [`PRAGMA optimize`](https://www.sqlite.org/pragma.html#pragma_optimize) on the database before closing the connection.

Call `destroy` if you want to clean up an `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
Expand Down
7 changes: 5 additions & 2 deletions src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,11 @@ export class SQLocalProcessor {
};

protected destroy = (message: DestroyMessage) => {
this.db?.close();
this.db = undefined;
if (this.db) {
this.db.exec({ sql: 'PRAGMA optimize' });
this.db.close();
this.db = undefined;
}

this.emitMessage({
type: 'success',
Expand Down

0 comments on commit 406eaef

Please sign in to comment.