Skip to content

Commit 6d0a24c

Browse files
author
Daniele Briggi
committed
feat(examples): modify to use isConnect()
1 parent 94723d3 commit 6d0a24c

File tree

14 files changed

+53
-35
lines changed

14 files changed

+53
-35
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ let database = new Database('sqlitecloud://user:password@xxx.sqlite.cloud:8860/c
5454

5555
let name = 'Breaking The Rules'
5656

57-
let results = await database.sql`SELECT * FROM tracks WHERE name = ${name}`
57+
let results = await database.sql('SELECT * FROM tracks WHERE name = ?', name)
5858
// => returns [{ AlbumId: 1, Name: 'Breaking The Rules', Composer: 'Angus Young... }]
5959
```
6060

@@ -82,7 +82,7 @@ await pubSub.listen(PUBSUB_ENTITY_TYPE.TABLE, 'albums', (error, results, data) =
8282
}
8383
})
8484

85-
await database.sql`INSERT INTO albums (Title, ArtistId) values ('Brand new song', 1)`
85+
await database.sql("INSERT INTO albums (Title, ArtistId) values ('Brand new song', 1)")
8686

8787
// Stop listening changes on the table
8888
await pubSub.unlisten(PUBSUB_ENTITY_TYPE.TABLE, 'albums')

examples/with-javascript-browser/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ <h2 class="pb-4">Results:</h2>
4848
var database = null;
4949

5050
sendButton.addEventListener('click', () => {
51-
if (!database) {
51+
if (!database || !database.isConnected()) {
5252
// Get the input element by ID
5353
var connectionStringinputElement = document.getElementById('connectionStringInput');
5454
var connectionstring = connectionStringinputElement.value;

examples/with-javascript-expo/components/AddTaskModal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useState, useEffect } from "react";
22
import { View, StyleSheet, Alert, Platform } from "react-native";
33
import { TextInput, Button, Modal } from "react-native-paper";
44
import DropdownMenu from "./DropdownMenu";
5-
import db from "../db/dbConnection";
5+
import getDbConnection from "../db/dbConnection";
66

77
export default AddTaskModal = ({
88
modalVisible,
@@ -30,7 +30,7 @@ export default AddTaskModal = ({
3030

3131
const getTags = async () => {
3232
try {
33-
const tags = await db.sql("SELECT * FROM tags");
33+
const tags = await getDbConnection().sql("SELECT * FROM tags");
3434
setTagsList(tags);
3535
} catch (error) {
3636
console.error("Error getting tags", error);
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
import { DATABASE_URL } from "@env";
22
import { Database } from "@sqlitecloud/drivers";
33

4-
export default db = new Database(DATABASE_URL);
4+
/**
5+
* @type {Database}
6+
*/
7+
let database = null;
8+
9+
export function getDbConnection() {
10+
if (!database || !database.isConnected()) {
11+
database = new Database(DATABASE_URL);
12+
}
13+
return database;
14+
}

examples/with-javascript-expo/hooks/useCategories.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { useState, useEffect } from "react";
2-
import db from "../db/dbConnection";
2+
import getDbConnection from "../db/dbConnection";
33

44
const useCategories = () => {
55
const [moreCategories, setMoreCategories] = useState(["Work", "Personal"]);
66

77
const getCategories = async () => {
88
try {
9-
const tags = await db.sql("SELECT * FROM tags");
9+
const tags = await getDbConnection().sql("SELECT * FROM tags");
1010
const filteredTags = tags.filter((tag) => {
1111
return tag["name"] !== "Work" && tag["name"] !== "Personal";
1212
});
@@ -21,7 +21,7 @@ const useCategories = () => {
2121

2222
const addCategory = async (newCategory) => {
2323
try {
24-
await db.sql(
24+
await getDbConnection().sql(
2525
"INSERT INTO tags (name) VALUES (?) RETURNING *",
2626
newCategory
2727
);
@@ -33,15 +33,15 @@ const useCategories = () => {
3333

3434
const initializeTables = async () => {
3535
try {
36-
const createTasksTable = await db.sql(
36+
const createTasksTable = await getDbConnection().sql(
3737
"CREATE TABLE IF NOT EXISTS tasks (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, isCompleted INT NOT NULL);"
3838
);
3939

40-
const createTagsTable = await db.sql(
40+
const createTagsTable = await getDbConnection().sql(
4141
"CREATE TABLE IF NOT EXISTS tags (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, UNIQUE(name));"
4242
);
4343

44-
const createTagsTasksTable = await db.sql(
44+
const createTagsTasksTable = await getDbConnection().sql(
4545
"CREATE TABLE IF NOT EXISTS tasks_tags (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, task_id INTEGER NOT NULL, tag_id INTEGER NOT NULL, FOREIGN KEY (task_id) REFERENCES tasks(id), FOREIGN KEY (tag_id) REFERENCES tags(id));"
4646
);
4747

@@ -52,8 +52,8 @@ const useCategories = () => {
5252
) {
5353
console.log("Successfully created tables");
5454

55-
await db.sql("INSERT OR IGNORE INTO tags (name) VALUES (?)", "Work");
56-
await db.sql(
55+
await getDbConnection().sql("INSERT OR IGNORE INTO tags (name) VALUES (?)", "Work");
56+
await getDbConnection().sql(
5757
"INSERT OR IGNORE INTO tags (name) VALUES (?)",
5858
"Personal"
5959
);

examples/with-javascript-expo/hooks/useTasks.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState, useEffect, useCallback } from "react";
2-
import db from "../db/dbConnection";
2+
import getDbConnection from "../db/dbConnection";
33

44
const useTasks = (tag = null) => {
55
const [taskList, setTaskList] = useState([]);
@@ -8,7 +8,7 @@ const useTasks = (tag = null) => {
88
try {
99
let result;
1010
if (tag) {
11-
result = await db.sql(
11+
result = await getDbConnection().sql(
1212
`
1313
SELECT tasks.*, tags.id AS tag_id, tags.name AS tag_name
1414
FROM tasks
@@ -19,11 +19,11 @@ const useTasks = (tag = null) => {
1919
);
2020
setTaskList(result);
2121
} else {
22-
result = await db.sql`
22+
result = await getDbConnection().sql(`
2323
SELECT tasks.*, tags.id AS tag_id, tags.name AS tag_name
2424
FROM tasks
2525
JOIN tasks_tags ON tasks.id = tasks_tags.task_id
26-
JOIN tags ON tags.id = tasks_tags.tag_id`;
26+
JOIN tags ON tags.id = tasks_tags.tag_id`);
2727
setTaskList(result);
2828
}
2929
} catch (error) {
@@ -33,7 +33,7 @@ const useTasks = (tag = null) => {
3333

3434
const updateTask = async (completedStatus, taskId) => {
3535
try {
36-
await db.sql(
36+
await getDbConnection().sql(
3737
"UPDATE tasks SET isCompleted=? WHERE id=? RETURNING *",
3838
completedStatus,
3939
taskId
@@ -47,21 +47,21 @@ const useTasks = (tag = null) => {
4747
const addTaskTag = async (newTask, tag) => {
4848
try {
4949
if (tag.id) {
50-
const addNewTask = await db.sql(
50+
const addNewTask = await getDbConnection().sql(
5151
"INSERT INTO tasks (title, isCompleted) VALUES (?, ?) RETURNING *",
5252
newTask.title,
5353
newTask.isCompleted
5454
);
5555
addNewTask[0].tag_id = tag.id;
5656
addNewTask[0].tag_name = tag.name;
5757
setTaskList([...taskList, addNewTask[0]]);
58-
await db.sql(
58+
await getDbConnection().sql(
5959
"INSERT INTO tasks_tags (task_id, tag_id) VALUES (?, ?)",
6060
addNewTask[0].id,
6161
tag.id
6262
);
6363
} else {
64-
const addNewTaskNoTag = await db.sql(
64+
const addNewTaskNoTag = await getDbConnection().sql(
6565
"INSERT INTO tasks (title, isCompleted) VALUES (?, ?) RETURNING *",
6666
newTask.title,
6767
newTask.isCompleted
@@ -75,8 +75,8 @@ const useTasks = (tag = null) => {
7575

7676
const deleteTask = async (taskId) => {
7777
try {
78-
await db.sql("DELETE FROM tasks_tags WHERE task_id=?", taskId);
79-
const result = await db.sql("DELETE FROM tasks WHERE id=?", taskId);
78+
await getDbConnection().sql("DELETE FROM tasks_tags WHERE task_id=?", taskId);
79+
const result = await getDbConnection().sql("DELETE FROM tasks WHERE id=?", taskId);
8080
console.log(`Deleted ${result.totalChanges} task`);
8181
getTasks();
8282
} catch (error) {

examples/with-javascript-express/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ app.use(express.json())
1515
/* http://localhost:3001/ returns chinook tracks as json */
1616
app.get('/', async function (req, res, next) {
1717
var database = new sqlitecloud.Database(DATABASE_URL)
18-
var tracks = await database.sql`USE DATABASE chinook.sqlite; SELECT * FROM tracks LIMIT 20;`
18+
var tracks = await database.sql('USE DATABASE chinook.sqlite; SELECT * FROM tracks LIMIT 20;')
1919
res.send({ tracks })
2020
})
2121

examples/with-javascript-vite/src/App.jsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
import { useEffect, useState } from "react";
22
import { Database } from "@sqlitecloud/drivers";
33

4-
const db = new Database(import.meta.env.VITE_DATABASE_URL);
4+
let db = null
5+
6+
function getDatabase() {
7+
if (!db || !db.isConnected()) {
8+
db = new Database("chinook.sqlite");
9+
}
10+
11+
return db;
12+
}
13+
514

615
function App() {
716
const [data, setData] = useState([]);
817

918
const getAlbums = async () => {
10-
const result = await db.sql`
19+
const result = await getDatabase().sql(`
1120
USE DATABASE chinook.sqlite;
1221
SELECT albums.AlbumId as id, albums.Title as title, artists.name as artist
1322
FROM albums
1423
INNER JOIN artists
1524
WHERE artists.ArtistId = albums.ArtistId
1625
LIMIT 20;
17-
`;
26+
`);
1827
setData(result);
1928
};
2029

examples/with-plain-javascript/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async function selectTracks() {
1313
var database = new sqlitecloud.Database(DATABASE_URL)
1414

1515
// run async query
16-
var tracks = await database.sql`USE DATABASE chinook.sqlite; SELECT * FROM tracks LIMIT 20;`
16+
var tracks = await database.sql('USE DATABASE chinook.sqlite; SELECT * FROM tracks LIMIT 20;')
1717
console.log(`selectTracks returned:`, tracks)
1818

1919
// You can also use all the regular sqlite3 api with callbacks, see:

examples/with-typescript-nextjs/app/api/hello/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export async function GET(request: NextRequest) {
1515
const database = new Database(DATABASE_URL)
1616

1717
// retrieve rows from chinook database using a plain SQL query
18-
const tracks = await database.sql`USE DATABASE chinook.sqlite; SELECT * FROM tracks LIMIT 20;`
18+
const tracks = await database.sql('USE DATABASE chinook.sqlite; SELECT * FROM tracks LIMIT 20;')
1919

2020
// return as json response
2121
return NextResponse.json<{ data: any }>({ data: tracks })

0 commit comments

Comments
 (0)