1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+
4+ < head >
5+ < meta charset ="UTF-8 " />
6+ < title > Driver | SQLite Cloud</ title >
7+ < script src ="https://cdn.tailwindcss.com "> </ script >
8+ < script src ="../../lib/sqlitecloud.drivers.js "> </ script >
9+ </ head >
10+
11+ < body class ="p-4 ">
12+ < h1 > SQLite Cloud Drivers</ h1 >
13+ < div class ="pb-6 text-sm ">
14+ < a href ="https://www.npmjs.com/package/@sqlitecloud/drivers "> npm add @sqlitecloud/drivers</ a >
15+ </ div >
16+
17+ <!-- Add the text field and button here -->
18+ < div class ="pb-2 ">
19+ < div class ="text-xs w-12 "> databaseUrl</ div >
20+ < input type ="text " id ="connectionStringInput "
21+ placeholder ="Example: sqlitecloud://admin:password@host.sqlite.cloud:8860/chinook.sqlite "
22+ value ="sqlitecloud://host.sqlite.cloud:8860?apikey=apikey "
23+ class ="border rounded w-full pl-2 pr-2 mb-2 " />
24+ < div class ="text-sm w-12 "> sql:</ div >
25+ < input type ="text " id ="messageInput "
26+ placeholder ="Example: USE DATABASE chinook.sqlite; select * from customers limit 3 "
27+ value ="USE DATABASE chinook.sqlite; select * from customers limit 3 "
28+ class ="border rounded w-full pl-2 pr-2 mb-2 " />
29+ </ div >
30+ < button id ="sendButton " class ="border rounded w-32 mb-6 "> query</ button >
31+
32+ < h2 class ="pb-4 "> Results:</ h2 >
33+ < ul id ="messages " class ="pl-4 "> </ ul >
34+
35+ < script type ="module ">
36+ const status = document . getElementById ( 'status' ) ;
37+ const messages = document . getElementById ( 'messages' ) ;
38+
39+ const appendMessage = ( content ) => {
40+ const item = document . createElement ( 'li' ) ;
41+ item . classList . add ( 'pb-4' ) ;
42+ item . classList . add ( 'text-sm' ) ;
43+ item . textContent = content ;
44+ messages . prepend ( item ) ;
45+ } ;
46+
47+ // socket is connected the first time the sql button is clicked and stays connected until the page is refreshed
48+ var database = null ;
49+
50+ sendButton . addEventListener ( 'click' , ( ) => {
51+ if ( ! database ) {
52+ // Get the input element by ID
53+ var connectionStringinputElement = document . getElementById ( 'connectionStringInput' ) ;
54+ var connectionstring = connectionStringinputElement . value ;
55+ // connect via websocket to the gateway on the same server
56+ const connectionConfig = {
57+ gatewayUrl : `${ window . location . protocol === 'https:' ? 'wss' : 'ws'
58+ } ://${ window . location . hostname } :4000`,
59+ connectionstring : connectionstring ,
60+ } ;
61+ database = new window . sqlitecloud . Database (
62+ connectionConfig ,
63+ ( error ) => {
64+ if ( error ) {
65+ database = null ;
66+ appendMessage ( `connection error: ${ error } ` ) ;
67+ } else {
68+ console . log ( 'connected' ) ;
69+ appendMessage ( `connected` ) ;
70+ }
71+ }
72+ ) ;
73+ }
74+
75+ var messageInputElement = document . getElementById ( 'messageInput' ) ;
76+ const sql = messageInputElement . value ;
77+ const startTime = Date . now ( ) ;
78+
79+ // send an async sql request to the server
80+ database . all ( sql , ( error , rows ) => {
81+ if ( error ) {
82+ console . error ( `sql: ${ sql } , error: ${ error } ` , error ) ;
83+ appendMessage ( `sql: ${ sql } , error: ${ error } ` ) ;
84+ } else {
85+ console . debug ( `sql: ${ sql } , (${ Date . now ( ) - startTime } ms)` , rows ) ;
86+ appendMessage ( JSON . stringify ( rows ) ) ;
87+ }
88+ } ) ;
89+ } ) ;
90+ </ script >
91+ </ body >
92+
93+ </ html >
0 commit comments