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
25
4.**Instantiate a connection**
26
+
- Create a `.env.local` file in the root of your Next.js project and add your SQLiteCloud connection string:
27
+
``bash
28
+
SQLITECLOUD_URL=your_connection_string_here
29
+
```
26
30
- 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'));
80
-
setIsConnecting(false);
81
-
}
82
31
83
-
// Cleanup function
84
-
return () => {
85
-
if (dbRef.current) {
86
-
dbRef.current.close();
87
-
dbRef.current=null;
88
-
}
89
-
};
90
-
}, [config]);
91
32
92
-
return (
93
-
<DatabaseContext.Provider
94
-
value={{
95
-
db: dbRef.current,
96
-
isConnecting,
97
-
error
98
-
}}
99
-
>
100
-
{children}
101
-
</DatabaseContext.Provider>
102
-
);
103
-
}
104
-
105
-
exportfunction useDatabaseConnection() {
106
-
const context =useContext(DatabaseContext);
107
-
108
-
if (context===undefined) {
109
-
thrownewError('useDatabaseConnection must be used within a DatabaseProvider');
110
-
}
33
+
---
111
34
112
-
returncontext;
113
-
}
114
-
```
115
-
5.**Query data**
116
-
- Click the ```Connect``` button in your account dashboard and copy the connection string. Replace ```<your-connection-string>``` in ```page.js``` with your connection string.
117
-
- Replace the code in ```layout.js``` and ```page.js``` with the following snippets.
0 commit comments