|
| 1 | +--- |
| 2 | +title: Knex.js Integration |
| 3 | +description: Integrate SQLite Cloud with Knex.js, a popular SQL query builder. |
| 4 | +category: getting-started |
| 5 | +status: publish |
| 6 | +slug: knex-integration |
| 7 | +--- |
| 8 | + |
| 9 | +In this tutorial, we'll show you how to connect your TypeScript application to a SQLite Cloud database using the popular SQL builder, [Knex.js](https://knexjs.org/). |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +**Prerequisites** |
| 14 | + |
| 15 | +- Node.js and npm installed on your system |
| 16 | +- A SQLite Cloud account (you can sign up for a free account [here](https://sqlitecloud.io/register)) |
| 17 | + |
| 18 | +1. **How to connect** |
| 19 | + |
| 20 | +- Create a Knex.js instance that uses the SQLite Cloud JavaScript driver to connect to your database. |
| 21 | + |
| 22 | +```typescript |
| 23 | +import 'dotenv/config' |
| 24 | +import { knex } from 'knex' |
| 25 | + |
| 26 | +const Client_SQLite3 = require('knex/lib/dialects/sqlite3') |
| 27 | + |
| 28 | +// client will have sqlite3 dialect, but will use sqlitecloud-js driver |
| 29 | +class Client_Libsql extends Client_SQLite3 { |
| 30 | + _driver() { |
| 31 | + return require('@sqlitecloud/drivers') |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// Create a Knex.js instance with the custom SQLite3 client |
| 36 | +const db = knex({ |
| 37 | + client: Client_Libsql as any, |
| 38 | + connection: { |
| 39 | + filename: process.env.DATABASE_URL as string |
| 40 | + } |
| 41 | +}) |
| 42 | +``` |
| 43 | + |
| 44 | +2. **Basic Usage** |
| 45 | + |
| 46 | +In this example, we will use the sample datasets that come pre-loaded with SQLite Cloud. |
| 47 | + |
| 48 | +- Initialize a new Node project: |
| 49 | + |
| 50 | +```bash |
| 51 | +npm init -y |
| 52 | +``` |
| 53 | + |
| 54 | +- Install the required dependencies: |
| 55 | + |
| 56 | +```bash |
| 57 | +npm install @sqlitecloud/drivers knex dotenv --save |
| 58 | +``` |
| 59 | + |
| 60 | +- Install the necessary development dependencies: |
| 61 | + |
| 62 | +```bash |
| 63 | +npm install @types/node nodemon ts-node typescript --save-dev |
| 64 | +``` |
| 65 | + |
| 66 | +- Create a `.env` file in the root of your project and add your SQLite Cloud connection string: |
| 67 | + |
| 68 | +```bash |
| 69 | +DATABASE_URL="sqlitecloud://{USER}:{PASSWORD}@{HOST}.sqlite.cloud:8860" |
| 70 | +``` |
| 71 | + |
| 72 | +Replace `{USER}`, `{PASSWORD}`, and `{HOST}` with your actual SQLite Cloud credentials and server hostname. |
| 73 | + |
| 74 | +- Create a `tsconfig.json` file to configure your TypeScript compiler: |
| 75 | + |
| 76 | +```bash |
| 77 | +tsc --init |
| 78 | +``` |
| 79 | + |
| 80 | +- Create a new file called `example.ts` and add the following code: |
| 81 | + |
| 82 | +```typescript |
| 83 | +import 'dotenv/config' |
| 84 | +import { knex } from 'knex' |
| 85 | + |
| 86 | +const Client_SQLite3 = require('knex/lib/dialects/sqlite3') |
| 87 | + |
| 88 | +class Client_Libsql extends Client_SQLite3 { |
| 89 | + _driver() { |
| 90 | + return require('@sqlitecloud/drivers') |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +console.assert(process.env.DATABASE_URL, 'Define DATABASE_URL environment variable') |
| 95 | + |
| 96 | +const db = knex({ |
| 97 | + client: Client_Libsql as any, |
| 98 | + connection: { |
| 99 | + filename: process.env.DATABASE_URL as string |
| 100 | + } |
| 101 | +}) |
| 102 | + |
| 103 | +db.raw('USE DATABASE chinook.sqlite; SELECT * FROM customers') |
| 104 | + .then(result => { |
| 105 | + console.log(`Connected to database via knex and received ${result.length} rows`) |
| 106 | + console.log(JSON.stringify(result, null, 2)) |
| 107 | + db.destroy() |
| 108 | + }) |
| 109 | + .catch(err => { |
| 110 | + console.error(err) |
| 111 | + db.destroy() |
| 112 | + }) |
| 113 | +``` |
| 114 | + |
| 115 | +- Update your `package.json` file to include a script for running the example: |
| 116 | + |
| 117 | +```bash |
| 118 | +{ |
| 119 | + "scripts": { |
| 120 | + "dev": "nodemon --exec ts-node example.ts" |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +- Start the development server: |
| 126 | + |
| 127 | +```bash |
| 128 | +npm run dev |
| 129 | +``` |
| 130 | + |
| 131 | +This will run the `example.ts` file using `ts-node` and will automatically restart the server when you make changes to your code. |
| 132 | + |
| 133 | +- Observe the output in the console, which should display the customer data fetched from the SQLite Cloud database. |
| 134 | +```bash |
| 135 | + [ |
| 136 | + { |
| 137 | + "CustomerId": 1, |
| 138 | + "FirstName": "Luís", |
| 139 | + "LastName": "Gonçalves", |
| 140 | + "Company": "Embraer - Empresa Brasileira de Aeronáutica S.A.", |
| 141 | + "Address": "Av. Brigadeiro Faria Lima, 2170", |
| 142 | + "City": "São José dos Campos", |
| 143 | + "State": "SP", |
| 144 | + "Country": "Brazil", |
| 145 | + "PostalCode": "12227-000", |
| 146 | + "Phone": "+55 (12) 3923-5555", |
| 147 | + "Fax": "+55 (12) 3923-5566", |
| 148 | + |
| 149 | + "SupportRepId": 3 |
| 150 | + }, |
| 151 | + ] |
| 152 | +``` |
| 153 | + |
| 154 | +And that's it! You've successfully connected your TypeScript application to a SQLite Cloud database using Knex.js. |
0 commit comments