-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
406eaef
commit 0c0c212
Showing
6 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# getDatabaseInfo | ||
|
||
Retrieve information about the SQLite database file. | ||
|
||
## Usage | ||
|
||
Access or destructure `getDatabaseInfo` from the `SQLocal` client. | ||
|
||
```javascript | ||
import { SQLocal } from 'sqlocal'; | ||
|
||
export const { getDatabaseInfo } = new SQLocal('database.sqlite3'); | ||
``` | ||
|
||
<!-- @include: ../_partials/initialization-note.md --> | ||
|
||
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' | 'opfs'`) - A string indicating whether the database is saved in the origin private file system or in memory. The database only falls 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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { afterEach, describe, expect, it } from 'vitest'; | ||
import { SQLocal } from '../src/index'; | ||
|
||
describe('getDatabaseInfo', () => { | ||
const { sql, getDatabaseInfo } = new SQLocal( | ||
'get-database-info-test.sqlite3' | ||
); | ||
|
||
afterEach(async () => { | ||
const opfs = await navigator.storage.getDirectory(); | ||
await opfs.removeEntry('get-database-info-test.sqlite3'); | ||
}); | ||
|
||
it('should return information about the database', async () => { | ||
const info1 = await getDatabaseInfo(); | ||
expect(info1).toEqual({ | ||
databasePath: 'get-database-info-test.sqlite3', | ||
databaseSizeBytes: 0, | ||
storageType: 'opfs', | ||
persisted: false, | ||
}); | ||
|
||
await sql`CREATE TABLE nums (num INTEGER NOT NULL)`; | ||
await sql`INSERT INTO nums (num) VALUES (493), (820), (361), (125)`; | ||
|
||
const info2 = await getDatabaseInfo(); | ||
expect(info2.databaseSizeBytes).toBeGreaterThan(0); | ||
}); | ||
}); |