Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sqlite-cloud/quick-start-next.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function DatabaseProvider({ children, config }: DatabaseProviderProps) {
const dbRef = useRef<Database | null>(null);

useEffect(() => {
if (dbRef.current) return; // Connection already exists
if (dbRef.current && dbRef.current.isConnected()) return; // Connection already exists

try {
dbRef.current = new Database(config.connectionString);
Expand Down
44 changes: 31 additions & 13 deletions sqlite-cloud/quick-start-node.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,43 @@ npm install express @sqlitecloud/drivers --save
- Paste the following into your `index.js` file:

```javascript
const express = require('express');
const { Database } = require('@sqlitecloud/drivers');
const express = require("express");
const { Database } = require("@sqlitecloud/drivers");

const app = express();
const db = new Database('<your-connection-string>');

app.get('/albums', async (req, res) => {
const result = await db.sql`
USE DATABASE chinook.sqlite;
SELECT albums.AlbumId as id, albums.Title as title, artists.name as artist
FROM albums
INNER JOIN artists
WHERE artists.ArtistId = albums.ArtistId
LIMIT 20;`;
let db;

function getDatabase() {
if (!db || !db.isConnected()) {
db = new Database("<connection-string>", (error) => {
if (error) {
console.log("Error during the connection", error);
} else {
console.log("Connected to the database");
}
});
}

return db;
}

app.get("/albums", async (req, res) => {
try {
const result = await getDatabase().sql(`
USE DATABASE chinook.sqlite;
SELECT albums.AlbumId as id, albums.Title as title, artists.name as artist
FROM albums
INNER JOIN artists
WHERE artists.ArtistId = albums.ArtistId
LIMIT 20;`);
res.json(result);
} catch (error) {
res.status(500).json({ error: error.message });
}
});

app.listen(3000, () => {
console.log('Server running on port 3000');
console.log("Server running on port 3000");
});
```
5. **Run your app**
Expand Down