Skip to content

Commit c4f06bf

Browse files
Moving gateway to drivers repo
1 parent fc81e3a commit c4f06bf

29 files changed

+2511
-42
lines changed

package-lock.json

Lines changed: 881 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sqlitecloud/drivers",
3-
"version": "0.0.41",
3+
"version": "0.0.50",
44
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",
@@ -14,7 +14,9 @@
1414
"prettier": "prettier --write 'src/**/*'",
1515
"lint": "eslint ./src/ --fix && tsc --noEmit",
1616
"typedoc": "rm -rf ./docs/ && typedoc --out docs && typedoc --plugin typedoc-plugin-markdown --out docs/markdown",
17-
"npmgui": "npx npm-gui@latest"
17+
"npmgui": "npx npm-gui@latest",
18+
"gateway-dev": "bun --watch ./src/gateway/gateway.ts",
19+
"gateway-test": "bun test ./src/gateway/connection-bun.test.ts --watch"
1820
},
1921
"repository": {
2022
"type": "git",
@@ -43,10 +45,14 @@
4345
"homepage": "https://github.com/sqlitecloud/sqlitecloud-js#readme",
4446
"dependencies": {
4547
"eventemitter3": "^5.0.1",
48+
"express": "^4.18.2",
4649
"lz4js": "^0.2.0",
50+
"socket.io": "^4.7.4",
4751
"socket.io-client": "^4.7.4"
4852
},
4953
"devDependencies": {
54+
"@types/bun": "^1.0.5",
55+
"@types/express": "^4.17.21",
5056
"@types/jest": "^29.5.11",
5157
"@types/lz4": "^0.6.4",
5258
"@types/node": "^12.20.55",

public/drivers.html

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Driver | SQLite Cloud</title>
6+
<script src="https://cdn.tailwindcss.com"></script>
7+
<script src="sqlitecloud.v0.0.37.js"></script>
8+
</head>
9+
10+
<body class="p-4">
11+
<h1>SQLite Cloud Driver</h1>
12+
<div class="pb-6">
13+
<a href="https://www.npmjs.com/package/sqlitecloud">npm sqlitecloud</a>
14+
</div>
15+
16+
<!-- Add the text field and button here -->
17+
<div class="pb-2">
18+
<div class="text-sm w-12">connectionString:</div>
19+
<input
20+
type="text"
21+
id="connectionStringInput"
22+
placeholder="Type your connection string here"
23+
value="sqlitecloud://admin:password@host.sqlite.cloud:8860/chinook.db"
24+
class="border rounded w-full pl-2 pr-2 mb-2"
25+
/>
26+
<div class="text-sm w-12">sql:</div>
27+
<input
28+
type="text"
29+
id="messageInput"
30+
placeholder="Type your SQL query"
31+
value="select * from customers limit 3"
32+
class="border rounded w-full pl-2 pr-2 mb-2"
33+
/>
34+
</div>
35+
<button id="sendButton" class="border rounded w-32 mb-6">query</button>
36+
37+
<h2 class="pb-4">Results:</h2>
38+
<ul id="messages" class="pl-4"></ul>
39+
40+
<script type="module">
41+
// import { io } from 'https://cdn.socket.io/4.7.4/socket.io.esm.min.js'
42+
const status = document.getElementById('status')
43+
const messages = document.getElementById('messages')
44+
45+
// connection string is stored in local storage so you don't have to type it every time
46+
let connectionString = localStorage.getItem('connectionString')
47+
if (!connectionString) {
48+
connectionString = 'sqlitecloud://admin:password@host.sqlite.cloud:8860/database.db'
49+
localStorage.setItem('connectionString', connectionString)
50+
} else connectionStringInput.value = connectionString
51+
connectionStringInput.addEventListener('change', () => {
52+
localStorage.setItem('connectionString', connectionStringInput.value)
53+
})
54+
55+
const appendMessage = content => {
56+
const item = document.createElement('li')
57+
item.classList.add('pb-4')
58+
item.classList.add('text-sm')
59+
item.textContent = content
60+
messages.prepend(item)
61+
}
62+
63+
// socket is connected the first time the sql button is clicked and stays connected until the page is refreshed
64+
var database = null
65+
66+
sendButton.addEventListener('click', () => {
67+
if (!database) {
68+
database = new window.sqlitecloud.Database(connectionString, error => {
69+
if (error) {
70+
database = null
71+
appendMessage(`connection error: ${error}`)
72+
} else {
73+
appendMessage(`connected`)
74+
}
75+
})
76+
}
77+
78+
const sql = messageInput.value
79+
const startTime = Date.now()
80+
81+
// send an async sql request to the server
82+
database.all(sql, (error, rows) => {
83+
if (error) {
84+
console.error(`sql: ${sql}, error: ${error}`, error)
85+
appendMessage(`sql: ${sql}, error: ${error}`)
86+
} else {
87+
console.debug(`sql: ${sql}, elapsed: ${Date.now() - startTime}`, rows)
88+
appendMessage(JSON.stringify(rows))
89+
}
90+
})
91+
})
92+
</script>
93+
</body>
94+
</html>

public/favicon.ico

15 KB
Binary file not shown.

public/index.html

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Gateway | SQLite Cloud</title>
6+
<script src="https://cdn.tailwindcss.com"></script>
7+
</head>
8+
9+
<body class="p-4">
10+
<h1>SQLite Cloud Gateway</h1>
11+
<h2 id="status" class="pb-6 font-bold cursor-pointer">Disconnected</h2>
12+
13+
<!-- Add the text field and button here -->
14+
<div class="pb-2">
15+
<div class="text-sm w-12">connectionString:</div>
16+
<input
17+
type="text"
18+
id="connectionStringInput"
19+
placeholder="Type your connection string here"
20+
value="sqlitecloud://admin:password@host.sqlite.cloud:8860/chinook.db"
21+
class="border rounded w-full pl-2 pr-2 mb-2"
22+
/>
23+
<div class="text-sm w-12">sql:</div>
24+
<input
25+
type="text"
26+
id="messageInput"
27+
placeholder="Type your SQL query"
28+
value="select * from tracks where trackid = 1"
29+
class="border rounded w-full pl-2 pr-2 mb-2"
30+
/>
31+
</div>
32+
<button id="sendButton" class="border rounded w-32 mb-6">send</button>
33+
34+
<h2>Messages:</h2>
35+
<ul id="messages" class="pl-4"></ul>
36+
37+
<script type="module">
38+
import { io } from 'https://cdn.socket.io/4.7.4/socket.io.esm.min.js'
39+
const status = document.getElementById('status')
40+
const messages = document.getElementById('messages')
41+
42+
// connection string is stored in local storage so you don't have to type it every time
43+
let connectionString = localStorage.getItem('connectionString')
44+
if (!connectionString) {
45+
connectionString = 'sqlitecloud://admin:password@host.sqlite.cloud:8860/database.db'
46+
localStorage.setItem('connectionString', connectionString)
47+
} else connectionStringInput.value = connectionString
48+
connectionStringInput.addEventListener('change', () => {
49+
localStorage.setItem('connectionString', connectionStringInput.value)
50+
})
51+
52+
const appendMessage = content => {
53+
const item = document.createElement('li')
54+
item.classList.add('pb-4')
55+
item.classList.add('text-sm')
56+
item.textContent = content
57+
messages.prepend(item)
58+
}
59+
60+
function setupSocket() {
61+
let host = window.location.host
62+
if (host.indexOf(':') !== -1) host = host.substring(0, host.indexOf(':'))
63+
console.debug(`socket/connect - connecting to '${host}'`)
64+
socket = io(`ws://${host}:4000`, { auth: { token: connectionStringInput.value } })
65+
socket.on('connect', () => {
66+
status.innerText = 'Connected'
67+
appendMessage(`connect | session id: ${socket.id}`)
68+
})
69+
70+
socket.on('connect_error', err => {
71+
appendMessage(`connect_error | reason: ${err.message}`)
72+
})
73+
74+
socket.on('disconnect', reason => {
75+
status.innerText = 'Disconnected'
76+
appendMessage(`disconnect | reason: ${reason}`)
77+
})
78+
79+
return socket
80+
}
81+
82+
// socket is connected the first time the sql button is clicked and stays connected until the page is refreshed
83+
var socket = null
84+
85+
sendButton.addEventListener('click', () => {
86+
if (!socket) {
87+
socket = setupSocket()
88+
}
89+
90+
// send an async request to the server
91+
const sql = messageInput.value
92+
const startTime = Date.now()
93+
socket.emit('v1/sql', { sql }, response => {
94+
console.debug(`sql - sql: ${sql}, elapsed: ${Date.now() - startTime}`, response)
95+
appendMessage(`sql | ${JSON.stringify(response)}`)
96+
})
97+
})
98+
99+
status.addEventListener('click', () => {
100+
if (!socket) {
101+
socket = setupSocket()
102+
}
103+
socket.emit('v1/info', { }, response => {
104+
appendMessage(`info | rss: ${(response.data.rss/1024/1024).toFixed(2)}mb, ${JSON.stringify(response)}`)
105+
})
106+
})
107+
108+
</script>
109+
</body>
110+
</html>

0 commit comments

Comments
 (0)