sqlite3 and knexjs+sqlite3 demo refresh

This commit is contained in:
SheetJS 2024-04-09 23:39:12 -04:00
parent 92e3c5aa72
commit 98a3b79e9c
2 changed files with 54 additions and 9 deletions

@ -39,10 +39,10 @@ This demo was tested in the following environments:
| Platform | Connector Library | Date |
|:-----------------|:---------------------------|:-----------|
| Chromium 119 | `sql.js` (`1.8.0`) | 2023-12-04 |
| NodeJS `20.10.0` | `better-sqlite3` (`9.2.0`) | 2023-12-04 |
| BunJS `1.0.15` | (built-in) | 2023-12-04 |
| Deno `1.38.4` | `sqlite` (`3.8`) | 2023-12-04 |
| Chromium 122 | `sql.js` (`1.8.0`) | 2024-04-09 |
| NodeJS `20.12.1` | `better-sqlite3` (`9.4.5`) | 2024-04-09 |
| BunJS `1.1.3` | (built-in) | 2024-04-09 |
| Deno `1.42.1` | `sqlite` (`3.8`) | 2024-04-09 |
:::

@ -23,10 +23,12 @@ and how to add data from spreadsheets into a database.
This demo was tested in the following environments:
| Version | Database | Connector Module | Date |
|:--------|:---------|:-----------------|:-----------|
| `2.5.1` | SQLite | `better-sqlite` | 2023-12-04 |
| `3.0.1` | SQLite | `better-sqlite` | 2023-12-04 |
| Version | Database | Connector Module | Date |
|:----------|:---------|:-----------------|:-----------|
| `0.21.20` | SQLite | `sqlite3` | 2024-04-09 |
| `2.4.2` | SQLite | `better-sqlite3` | 2024-04-09 |
| `2.5.1` | SQLite | `better-sqlite3` | 2024-04-09 |
| `3.1.0` | SQLite | `better-sqlite3` | 2024-04-09 |
:::
@ -185,6 +187,16 @@ let knex = Knex({
npm i --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz knex better-sqlite3`}
</CodeBlock>
:::note pass
For KnexJS version `0.21.20`, the `sqlite3` module must be installed:
<CodeBlock language="bash">{`\
npm i --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz knex sqlite3`}
</CodeBlock>
:::
2) Download the [test file](https://sheetjs.com/pres.numbers)
```bash
@ -197,7 +209,6 @@ curl -LO https://sheetjs.com/pres.numbers
curl -LO https://docs.sheetjs.com/knex/SheetJSKnexTest.js
```
This script will:
- read and parse the test file `pres.numbers`
- create a connection to a SQLite database stored at `SheetJSKnex.db`
@ -226,6 +237,40 @@ npx xlsx-cli SheetJSKnex.xlsx
sqlite3 SheetJSKnex.db 'select * from Test_Table'
```
:::caution pass
Older versions of KnexJS will throw an error:
```
Error: knex: Unknown configuration option 'client' value better-sqlite3. Note that it is case-sensitive, check documentation for supported values.
```
Older versions of KnexJS do not support the `better-sqlite3` module. The
`SheetJSKnexTest.js` script must be edited to use `sqlite3`:
```js title="SheetJSKnexTest.js (edit highlighted lines)"
(async() => {
/* open connection to SheetJSKnex.db */
// highlight-next-line
let knex = Knex({ client: 'sqlite3', connection: { filename: "SheetJSKnex.db" }, useNullAsDefault: true });
try {
/* generate array of objects from worksheet */
const aoo = XLSX.utils.sheet_to_json(oldws);
/* create table and load data */
await aoo_to_knex_table(knex, aoo, "Test_Table");
} finally {
/* disconnect */
knex.destroy();
}
/* reconnect to SheetJSKnex.db */
// highlight-next-line
knex = Knex({ client: 'sqlite3', connection: { filename: "SheetJSKnex.db" }, useNullAsDefault: true });
```
:::
[^1]: See [`select`](https://knexjs.org/guide/query-builder.html#select) in the KnexJS query builder documentation.
[^2]: See [`json_to_sheet` in "Utilities"](/docs/api/utilities/array#array-of-objects-input)
[^3]: See ["Sheet Objects"](/docs/csf/sheet) in "SheetJS Data Model" for more details.