Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
env:
CHINOOK_DATABASE_URL: ${{ secrets.CHINOOK_DATABASE_URL }}
CHINOOK_API_KEY: ${{ secrets.CHINOOK_API_KEY }}
GATEWAY_URL: ${{ secrets.GATEWAY_URL }}
GATEWAY_URL: ${{ vars.GATEWAY_URL }}
run: npm test
- name: Upload Code Coverage
uses: codecov/codecov-action@v4
Expand Down Expand Up @@ -260,6 +260,8 @@ jobs:
run: npm i && npx playwright test
env:
VITE_DATABASE_URL: ${{ secrets.CHINOOK_DATABASE_URL }}
VITE_DATABASE_API_KEY: ${{ secrets.CHINOOK_API_KEY }}
VITE_GATEWAY_URL: ${{ vars.GATEWAY_URL }}

- name: bun with-javascript-vite
if: matrix.os != 'LinuxARM64'
Expand All @@ -271,13 +273,17 @@ jobs:
bun playwright test
env:
VITE_DATABASE_URL: ${{ secrets.CHINOOK_DATABASE_URL }}
VITE_DATABASE_API_KEY: ${{ secrets.CHINOOK_API_KEY }}
VITE_GATEWAY_URL: ${{ vars.GATEWAY_URL }}

- name: deno with-javascript-vite
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
working-directory: examples/with-javascript-vite
run: deno add npm:@playwright/test && deno run --allow-all npm:playwright test
env:
VITE_DATABASE_URL: ${{ secrets.CHINOOK_DATABASE_URL }}
VITE_DATABASE_API_KEY: ${{ secrets.CHINOOK_API_KEY }}
VITE_GATEWAY_URL: ${{ vars.GATEWAY_URL }}
PW_DISABLE_TS_ESM: true

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

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

# See issue https://github.com/sqlitecloud/sqlitecloud-js/issues/265
# - name: deno with-javascript-browser
Expand Down
28 changes: 0 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,6 @@ We aim for full compatibility with the established [sqlite3 API](https://www.npm

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.

## Publish / Subscribe (Pub/Sub)

```ts
import { Database } from '@sqlitecloud/drivers'
import { PubSub, PUBSUB_ENTITY_TYPE } from '@sqlitecloud/drivers/lib/drivers/pubsub'

let database = new Database('sqlitecloud://user:password@xxx.sqlite.cloud:8860/chinook.sqlite')
// or use sqlitecloud://xxx.sqlite.cloud:8860?apikey=xxxxxxx

const pubSub: PubSub = await database.getPubSub()

await pubSub.listen(PUBSUB_ENTITY_TYPE.TABLE, 'albums', (error, results, data) => {
if (results) {
// Changes on albums table will be received here as JSON object
console.log('Received message:', results)
}
})

await database.sql("INSERT INTO albums (Title, ArtistId) values ('Brand new song', 1)")

// Stop listening changes on the table
await pubSub.unlisten(PUBSUB_ENTITY_TYPE.TABLE, 'albums')
```

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.

Pub/Sub Documentation: [https://docs.sqlitecloud.io/docs/pub-sub](https://docs.sqlitecloud.io/docs/pub-sub)

## Examples

Check out all the supported platforms with related examples [here](https://github.com/sqlitecloud/sqlitecloud-js/tree/main/examples)!
Expand Down
13 changes: 10 additions & 3 deletions examples/with-javascript-browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ <h1>SQLite Cloud Drivers</h1>
<input type="text" id="connectionStringInput"
placeholder="Example: sqlitecloud://admin:password@host.sqlite.cloud:8860/chinook.sqlite"
value="sqlitecloud://host.sqlite.cloud:8860?apikey=apikey" class="border rounded w-full pl-2 pr-2 mb-2" />
<div class="text-xs w-12">gatewayUrl</div>
<input type="text" id="gatewayUrlInput"
placeholder="Example: localhost (leave empty to use the default)"
value="" class="border rounded w-full pl-2 pr-2 mb-2" />
<div class="text-sm w-12">sql:</div>
<input type="text" id="messageInput"
placeholder="Example: USE DATABASE chinook.sqlite; select * from customers limit 3"
Expand Down Expand Up @@ -47,12 +51,15 @@ <h2 class="pb-4">Results:</h2>
// Get the input element by ID
var connectionStringinputElement = document.getElementById('connectionStringInput');
var connectionstring = connectionStringinputElement.value;
var gatewayUrl = document.getElementById('gatewayUrlInput').value.trim();
// connect via websocket to the gateway on the same server
const connectionConfig = {
gatewayUrl: `${window.location.protocol === 'https:' ? 'wss' : 'ws'
}://${window.location.hostname}:443`,
connectionstring: connectionstring,
apikey: connectionstring.split('apikey=')[1],
connectionstring: connectionstring.split('?apikey=')[0],
};
if (gatewayUrl) {
connectionConfig.gatewayurl = gatewayUrl;
}
var database = new window.sqlitecloud.Database(
connectionConfig,
(error) => {
Expand Down
3 changes: 3 additions & 0 deletions examples/with-javascript-browser/test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const { chromium, firefox, webkit } = require('playwright');
if (messageInput != 'USE DATABASE chinook.sqlite; select * from customers limit 3') throw Error('Invalid message input');

await page.fill('#connectionStringInput', process.env.DATABASE_URL);
if (process.env.GATEWAY_URL) {
await page.fill('#gatewayUrlInput', process.env.GATEWAY_URL);
}
await page.click('button#sendButton');

//sleep 3s
Expand Down
9 changes: 7 additions & 2 deletions examples/with-javascript-vite/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useState } from "react";
import { Database } from "@sqlitecloud/drivers";
import { Database, parseconnectionstring } from "@sqlitecloud/drivers";


function App() {
Expand All @@ -8,7 +8,12 @@ function App() {
const getAlbums = async () => {
let database = null;
try {
database = new Database(import.meta.env.VITE_DATABASE_URL)
let config = parseconnectionstring(import.meta.env.VITE_DATABASE_URL)
delete config.username
delete config.password
config.apikey = import.meta.env.VITE_DATABASE_API_KEY
if(import.meta.env.VITE_GATEWAY_URL) config.gatewayurl = import.meta.env.VITE_GATEWAY_URL
database = new Database(config)
const result = await database.sql(`
USE DATABASE chinook.sqlite;
SELECT albums.AlbumId as id, albums.Title as title, artists.name as artist
Expand Down
Loading
Loading