Skip to content

Commit 2bfac5f

Browse files
authored
Added Knex.js integration documentation (#100)
* added knex integration documentation * updated knex integration doc
1 parent 09e3ea8 commit 2bfac5f

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

sqlite-cloud/_nav.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const sidebarNav: SidebarNavStruct = [
2323
{ title: "Gin", filePath: "quick-start-gin", type: "inner", level: 1 },
2424
{ title: "Tutorials", type: "inner", level: 0 },
2525
{ title: "Geopoly", filePath: "tutorial-geopoly", type: "inner", level: 1 },
26+
{ title: "Integrations", type: "inner", level: 0 },
27+
{ title: "Knex.js", filePath: "knex-integration", type: "inner", level: 1 },
2628

2729
{ title: "Platform", type: "secondary", icon: "docs-plat" },
2830
{ title: "Edge Functions", filePath: "edge-functions", type: "inner", level: 0 },

sqlite-cloud/knex-integration.mdx

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
"Email": "[email protected]",
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

Comments
 (0)