Skip to content

Commit

Permalink
Update example/with-typescript-knex #64
Browse files Browse the repository at this point in the history
  • Loading branch information
gionatamettifogo committed Feb 28, 2024
1 parent 85f6e50 commit 62c90dc
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 57 deletions.
2 changes: 2 additions & 0 deletions examples/with-typescript-knex/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# connection string for your database node on sqlitecloud.io
DATABASE_URL="sqlitecloud://user:password@host.sqlite.cloud:8860"
10 changes: 8 additions & 2 deletions examples/with-typescript-knex/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Getting Started

This example shows how you can use sqlitecloud-js with [Knex](https://knexjs.org/), a SQL query builder for Javascript.
This example shows how you can use @sqlitecloud/drivers with [Knex](https://knexjs.org/), a SQL query builder for Javascript.

You can launch example.ts from VS Code "Run and Debug" menu.
You can launch example.ts from VS Code "Run and Debug" menu which will launch node.

You can also use this example to try @sqlitecloud/drivers using bun:

```
bun example.ts
```
10 changes: 5 additions & 5 deletions examples/with-typescript-knex/example.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Using sqlitecloud drivers with knex
// Using @sqlitecloud/drivers with Typescript and Knex
//

import { knex } from 'knex'
Expand All @@ -9,22 +9,22 @@ const Client_SQLite3 = require('knex/lib/dialects/sqlite3')
// client will have sqlite3 dialect, but will use sqlitecloud-js driver
class Client_Libsql extends Client_SQLite3 {
_driver() {
return require('sqlitecloud-js')
return require('@sqlitecloud/drivers')
}
}

console.assert(process.env.CHINOOK_DATABASE_URL, 'Define CHINOOK_URL environment variable')
console.assert(process.env.DATABASE_URL, 'Define DATABASE_URL environment variable')

// create knex instance with sqlitecloud-js driver
// database url is passed as filename parameter
const db = knex({
client: Client_Libsql as any,
connection: {
filename: process.env.CHINOOK_DATABASE_URL as string
filename: process.env.DATABASE_URL as string
}
})

db.raw('select * from customers')
db.raw('USE DATABASE chinook.sqlite; SELECT * FROM customers')
.then(result => {
console.log(`Connected to database via knex and received ${result.length} rows`)
console.log(JSON.stringify(result, null, 2))
Expand Down
271 changes: 227 additions & 44 deletions examples/with-typescript-knex/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 62c90dc

Please sign in to comment.