Skip to content

Commit 7a45a6e

Browse files
committed
fix(js): example always reconnect to db
1 parent a56df84 commit 7a45a6e

4 files changed

Lines changed: 19 additions & 29 deletions

File tree

sqlite-cloud/quick-start-next.mdx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ export function DatabaseProvider({ children, config }: DatabaseProviderProps) {
5353
const dbRef = useRef<Database | null>(null);
5454

5555
useEffect(() => {
56-
if (dbRef.current && dbRef.current.isConnected()) return; // Connection already exists
57-
5856
try {
5957
dbRef.current = new Database(config.connectionString);
6058

sqlite-cloud/quick-start-node.mdx

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,13 @@ npm install express @sqlitecloud/drivers --save
3232
const express = require("express");
3333
const { Database } = require("@sqlitecloud/drivers");
3434

35+
const connectionString = process.env.SQLITECLOUD_CONNECTION_STRING
3536
const app = express();
36-
let db;
37-
38-
function getDatabase() {
39-
if (!db || !db.isConnected()) {
40-
db = new Database("<connection-string>", (error) => {
41-
if (error) {
42-
console.log("Error during the connection", error);
43-
} else {
44-
console.log("Connected to the database");
45-
}
46-
});
47-
}
48-
49-
return db;
50-
}
5137

5238
app.get("/albums", async (req, res) => {
5339
try {
54-
const result = await getDatabase().sql(`
40+
const db = new Database(connectionString)
41+
const result = await db.sql(`
5542
USE DATABASE chinook.sqlite;
5643
SELECT albums.AlbumId as id, albums.Title as title, artists.name as artist
5744
FROM albums

sqlite-cloud/quick-start-react-native.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ export default function App() {
4949
const db = new Database('<your-connection-string>');
5050

5151
const result =
52-
await db.sql`USE DATABASE chinook.sqlite;
52+
await db.sql(`USE DATABASE chinook.sqlite;
5353
SELECT albums.AlbumId as id, albums.Title as title, artists.name as artist
5454
FROM albums
5555
INNER JOIN artists
56-
WHERE artists.ArtistId = albums.ArtistId LIMIT 20;`;
56+
WHERE artists.ArtistId = albums.ArtistId LIMIT 20;`);
5757

5858
setAlbums(result);
5959
}

sqlite-cloud/quick-start-react.mdx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,25 @@ cd sqlc-quickstart && npm install @sqlitecloud/drivers
2929
import { useEffect, useState } from "react";
3030
import { Database } from '@sqlitecloud/drivers';
3131

32-
const db = new Database('<your-connection-string>');
32+
const connectionString = process.env.SQLITECLOUD_CONNECTION_STRING
3333

3434
function App() {
3535
const [data, setData] = useState([]);
3636

3737
const getAlbums = async () => {
38-
const result = await db.sql`
39-
USE DATABASE chinook.sqlite;
40-
SELECT albums.AlbumId as id, albums.Title as title, artists.name as artist
41-
FROM albums
42-
INNER JOIN artists
43-
WHERE artists.ArtistId = albums.ArtistId
44-
LIMIT 20;`;
45-
setData(result);
38+
try {
39+
const db = new Database(connectionString)
40+
const result = await db.sql(`
41+
USE DATABASE chinook.sqlite;
42+
SELECT albums.AlbumId as id, albums.Title as title, artists.name as artist
43+
FROM albums
44+
INNER JOIN artists
45+
WHERE artists.ArtistId = albums.ArtistId
46+
LIMIT 20;`);
47+
setData(result);
48+
} catch (err) {
49+
// manage error case
50+
}
4651
};
4752

4853
useEffect(() => {

0 commit comments

Comments
 (0)