Skip to content

Commit 796dfe5

Browse files
authored
update driver to work with the new gateway, remove pubsub support
1 parent a2c2236 commit 796dfe5

18 files changed

Lines changed: 3183 additions & 13874 deletions

File tree

.github/workflows/test.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
env:
3535
CHINOOK_DATABASE_URL: ${{ secrets.CHINOOK_DATABASE_URL }}
3636
CHINOOK_API_KEY: ${{ secrets.CHINOOK_API_KEY }}
37-
GATEWAY_URL: ${{ secrets.GATEWAY_URL }}
37+
GATEWAY_URL: ${{ vars.GATEWAY_URL }}
3838
run: npm test
3939
- name: Upload Code Coverage
4040
uses: codecov/codecov-action@v4
@@ -260,6 +260,8 @@ jobs:
260260
run: npm i && npx playwright test
261261
env:
262262
VITE_DATABASE_URL: ${{ secrets.CHINOOK_DATABASE_URL }}
263+
VITE_DATABASE_API_KEY: ${{ secrets.CHINOOK_API_KEY }}
264+
VITE_GATEWAY_URL: ${{ vars.GATEWAY_URL }}
263265

264266
- name: bun with-javascript-vite
265267
if: matrix.os != 'LinuxARM64'
@@ -271,13 +273,17 @@ jobs:
271273
bun playwright test
272274
env:
273275
VITE_DATABASE_URL: ${{ secrets.CHINOOK_DATABASE_URL }}
276+
VITE_DATABASE_API_KEY: ${{ secrets.CHINOOK_API_KEY }}
277+
VITE_GATEWAY_URL: ${{ vars.GATEWAY_URL }}
274278

275279
- name: deno with-javascript-vite
276280
if: false #matrix.os != 'windows-latest' windows: https://github.com/denoland/deno/issues/23524#issuecomment-2292075726 linux-ubuntu: https://github.com/sqlitecloud/sqlitecloud-js/issues/197
277281
working-directory: examples/with-javascript-vite
278282
run: deno add npm:@playwright/test && deno run --allow-all npm:playwright test
279283
env:
280284
VITE_DATABASE_URL: ${{ secrets.CHINOOK_DATABASE_URL }}
285+
VITE_DATABASE_API_KEY: ${{ secrets.CHINOOK_API_KEY }}
286+
VITE_GATEWAY_URL: ${{ vars.GATEWAY_URL }}
281287
PW_DISABLE_TS_ESM: true
282288

283289
- name: remove with-javascript-vite
@@ -287,11 +293,17 @@ jobs:
287293
if: matrix.os != 'LinuxARM64'
288294
working-directory: examples/with-javascript-browser
289295
run: npm i && node test.cjs
296+
env:
297+
DATABASE_URL: ${{ secrets.CHINOOK_DATABASE_URL }}?apikey=${{ secrets.CHINOOK_API_KEY }}
298+
GATEWAY_URL: ${{ vars.GATEWAY_URL }}
290299

291300
- name: bun with-javascript-browser
292301
if: matrix.os != 'windows-latest' && matrix.os != 'LinuxARM64' #cannot launch browsers on windows with bash in commonjs?
293302
working-directory: examples/with-javascript-browser
294303
run: bun i && bun test.cjs
304+
env:
305+
DATABASE_URL: ${{ secrets.CHINOOK_DATABASE_URL }}?apikey=${{ secrets.CHINOOK_API_KEY }}
306+
GATEWAY_URL: ${{ vars.GATEWAY_URL }}
295307

296308
# See issue https://github.com/sqlitecloud/sqlitecloud-js/issues/265
297309
# - name: deno with-javascript-browser

README.md

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,34 +70,6 @@ We aim for full compatibility with the established [sqlite3 API](https://www.npm
7070

7171
The package is developed entirely in TypeScript and is fully compatible with JavaScript. It doesn't require any native libraries. This makes it a straightforward and effective tool for managing cloud-based databases in a familiar SQLite environment.
7272

73-
## Publish / Subscribe (Pub/Sub)
74-
75-
```ts
76-
import { Database } from '@sqlitecloud/drivers'
77-
import { PubSub, PUBSUB_ENTITY_TYPE } from '@sqlitecloud/drivers/lib/drivers/pubsub'
78-
79-
let database = new Database('sqlitecloud://user:password@xxx.sqlite.cloud:8860/chinook.sqlite')
80-
// or use sqlitecloud://xxx.sqlite.cloud:8860?apikey=xxxxxxx
81-
82-
const pubSub: PubSub = await database.getPubSub()
83-
84-
await pubSub.listen(PUBSUB_ENTITY_TYPE.TABLE, 'albums', (error, results, data) => {
85-
if (results) {
86-
// Changes on albums table will be received here as JSON object
87-
console.log('Received message:', results)
88-
}
89-
})
90-
91-
await database.sql("INSERT INTO albums (Title, ArtistId) values ('Brand new song', 1)")
92-
93-
// Stop listening changes on the table
94-
await pubSub.unlisten(PUBSUB_ENTITY_TYPE.TABLE, 'albums')
95-
```
96-
97-
Pub/Sub is a messaging pattern that allows multiple applications to communicate with each other asynchronously. In the context of SQLiteCloud, Pub/Sub can be used to provide real-time updates and notifications to subscribed applications whenever data changes in the database or it can be used to send payloads (messages) to anyone subscribed to a channel.
98-
99-
Pub/Sub Documentation: [https://docs.sqlitecloud.io/docs/pub-sub](https://docs.sqlitecloud.io/docs/pub-sub)
100-
10173
## Examples
10274

10375
Check out all the supported platforms with related examples [here](https://github.com/sqlitecloud/sqlitecloud-js/tree/main/examples)!

examples/with-javascript-browser/index.html

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ <h1>SQLite Cloud Drivers</h1>
2020
<input type="text" id="connectionStringInput"
2121
placeholder="Example: sqlitecloud://admin:password@host.sqlite.cloud:8860/chinook.sqlite"
2222
value="sqlitecloud://host.sqlite.cloud:8860?apikey=apikey" class="border rounded w-full pl-2 pr-2 mb-2" />
23+
<div class="text-xs w-12">gatewayUrl</div>
24+
<input type="text" id="gatewayUrlInput"
25+
placeholder="Example: localhost (leave empty to use the default)"
26+
value="" class="border rounded w-full pl-2 pr-2 mb-2" />
2327
<div class="text-sm w-12">sql:</div>
2428
<input type="text" id="messageInput"
2529
placeholder="Example: USE DATABASE chinook.sqlite; select * from customers limit 3"
@@ -47,12 +51,15 @@ <h2 class="pb-4">Results:</h2>
4751
// Get the input element by ID
4852
var connectionStringinputElement = document.getElementById('connectionStringInput');
4953
var connectionstring = connectionStringinputElement.value;
54+
var gatewayUrl = document.getElementById('gatewayUrlInput').value.trim();
5055
// connect via websocket to the gateway on the same server
5156
const connectionConfig = {
52-
gatewayUrl: `${window.location.protocol === 'https:' ? 'wss' : 'ws'
53-
}://${window.location.hostname}:443`,
54-
connectionstring: connectionstring,
57+
apikey: connectionstring.split('apikey=')[1],
58+
connectionstring: connectionstring.split('?apikey=')[0],
5559
};
60+
if (gatewayUrl) {
61+
connectionConfig.gatewayurl = gatewayUrl;
62+
}
5663
var database = new window.sqlitecloud.Database(
5764
connectionConfig,
5865
(error) => {

examples/with-javascript-browser/test.cjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const { chromium, firefox, webkit } = require('playwright');
1616
if (messageInput != 'USE DATABASE chinook.sqlite; select * from customers limit 3') throw Error('Invalid message input');
1717

1818
await page.fill('#connectionStringInput', process.env.DATABASE_URL);
19+
if (process.env.GATEWAY_URL) {
20+
await page.fill('#gatewayUrlInput', process.env.GATEWAY_URL);
21+
}
1922
await page.click('button#sendButton');
2023

2124
//sleep 3s

examples/with-javascript-vite/src/App.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useState } from "react";
2-
import { Database } from "@sqlitecloud/drivers";
2+
import { Database, parseconnectionstring } from "@sqlitecloud/drivers";
33

44

55
function App() {
@@ -8,7 +8,12 @@ function App() {
88
const getAlbums = async () => {
99
let database = null;
1010
try {
11-
database = new Database(import.meta.env.VITE_DATABASE_URL)
11+
let config = parseconnectionstring(import.meta.env.VITE_DATABASE_URL)
12+
delete config.username
13+
delete config.password
14+
config.apikey = import.meta.env.VITE_DATABASE_API_KEY
15+
if(import.meta.env.VITE_GATEWAY_URL) config.gatewayurl = import.meta.env.VITE_GATEWAY_URL
16+
database = new Database(config)
1217
const result = await database.sql(`
1318
USE DATABASE chinook.sqlite;
1419
SELECT albums.AlbumId as id, albums.Title as title, artists.name as artist

0 commit comments

Comments
 (0)