You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
cd sqlc-quickstart && npm install @sqlitecloud/drivers
24
24
```
25
-
4.**Query data**
26
-
- Replace the code in ```layout.js``` and ```page.js``` with the following snippets.
27
-
- Click a node in your account dashboard and copy the connection string. Replace ```<your-connection-string>``` in ```page.js``` with your connection string.
25
+
4.**Instantiate a connection**
26
+
- The Database driver establishes a TLS connection when used in Node.js, and a websocket connection when used in the browser.
27
+
- It is recommended that you use the Database driver in client-side components.
28
+
- To share the connection across pages, you can instantiate the connection in a context provider with the ```use client``` directive. Below is a simplified sample implementation.
setError(errinstanceofError?err:newError('Failed to initialize database'));
82
+
setIsConnecting(false);
83
+
}
35
84
85
+
// Cleanup function
86
+
return () => {
87
+
if (dbRef.current) {
88
+
dbRef.current.close();
89
+
dbRef.current=null;
90
+
}
91
+
};
92
+
}, [config]);
93
+
94
+
return (
95
+
<DatabaseContext.Provider
96
+
value={{
97
+
db: dbRef.current,
98
+
isConnecting,
99
+
error
100
+
}}
101
+
>
102
+
{children}
103
+
</DatabaseContext.Provider>
104
+
);
105
+
}
106
+
107
+
exportfunction useDatabaseConnection() {
108
+
const context =useContext(DatabaseContext);
109
+
110
+
if (context===undefined) {
111
+
thrownewError('useDatabaseConnection must be used within a DatabaseProvider');
112
+
}
113
+
114
+
returncontext;
115
+
}
116
+
```
117
+
5.**Query data**
118
+
- Click the ```Connect``` button in your account dashboard and copy the connection string. Replace ```<your-connection-string>``` in ```page.js``` with your connection string.
119
+
- Replace the code in ```layout.js``` and ```page.js``` with the following snippets.
0 commit comments