Skip to content

Commit a761087

Browse files
author
Daniele Briggi
committed
fix(examples): missing close()
1 parent 73239e5 commit a761087

File tree

4 files changed

+75
-60
lines changed

4 files changed

+75
-60
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ export default AddTaskModal = ({
2929
};
3030

3131
const getTags = async () => {
32+
let db = null
3233
try {
33-
const tags = await getDbConnection().sql("SELECT * FROM tags");
34+
db = getDbConnection();
35+
const tags = await db.sql("SELECT * FROM tags");
3436
setTagsList(tags);
3537
} catch (error) {
3638
console.error("Error getting tags", error);
39+
} finally {
40+
db?.close();
3741
}
3842
};
3943

Original file line numberDiff line numberDiff line change
@@ -1,78 +1,72 @@
1-
import { useState, useEffect } from "react";
2-
import getDbConnection from "../db/dbConnection";
1+
import { useState, useEffect } from 'react'
2+
import getDbConnection from '../db/dbConnection'
33

44
const useCategories = () => {
5-
const [moreCategories, setMoreCategories] = useState(["Work", "Personal"]);
5+
const [moreCategories, setMoreCategories] = useState(['Work', 'Personal'])
66

77
const getCategories = async () => {
8+
let db = null;
89
try {
9-
const tags = await getDbConnection().sql("SELECT * FROM tags");
10-
const filteredTags = tags.filter((tag) => {
11-
return tag["name"] !== "Work" && tag["name"] !== "Personal";
12-
});
13-
setMoreCategories((prevCategories) => [
14-
...prevCategories,
15-
...filteredTags.map((tag) => tag.name),
16-
]);
10+
const tags = await db.sql('SELECT * FROM tags')
11+
const filteredTags = tags.filter(tag => {
12+
return tag['name'] !== 'Work' && tag['name'] !== 'Personal'
13+
})
14+
setMoreCategories(prevCategories => [...prevCategories, ...filteredTags.map(tag => tag.name)])
1715
} catch (error) {
18-
console.error("Error getting tags/categories", error);
16+
console.error('Error getting tags/categories', error)
1917
}
20-
};
18+
}
2119

22-
const addCategory = async (newCategory) => {
20+
const addCategory = async newCategory => {
21+
let db = null
2322
try {
24-
await getDbConnection().sql(
25-
"INSERT INTO tags (name) VALUES (?) RETURNING *",
26-
newCategory
27-
);
28-
setMoreCategories((prevCategories) => [...prevCategories, newCategory]);
23+
db = getDbConnection()
24+
await db.sql('INSERT INTO tags (name) VALUES (?) RETURNING *', newCategory)
25+
setMoreCategories(prevCategories => [...prevCategories, newCategory])
2926
} catch (error) {
30-
console.error("Error adding category", error);
27+
console.error('Error adding category', error)
28+
} finally {
29+
db?.close();
3130
}
32-
};
31+
}
3332

3433
const initializeTables = async () => {
34+
let db = null
3535
try {
36-
const createTasksTable = await getDbConnection().sql(
37-
"CREATE TABLE IF NOT EXISTS tasks (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, isCompleted INT NOT NULL);"
38-
);
36+
db = getDbConnection()
37+
const createTasksTable = await db.sql(
38+
'CREATE TABLE IF NOT EXISTS tasks (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, isCompleted INT NOT NULL);'
39+
)
3940

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

44-
const createTagsTasksTable = await getDbConnection().sql(
45-
"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));"
46-
);
43+
const createTagsTasksTable = await db.sql(
44+
'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));'
45+
)
4746

48-
if (
49-
createTasksTable === "OK" &&
50-
createTagsTable === "OK" &&
51-
createTagsTasksTable === "OK"
52-
) {
53-
console.log("Successfully created tables");
47+
if (createTasksTable === 'OK' && createTagsTable === 'OK' && createTagsTasksTable === 'OK') {
48+
console.log('Successfully created tables')
5449

55-
await getDbConnection().sql("INSERT OR IGNORE INTO tags (name) VALUES (?)", "Work");
56-
await getDbConnection().sql(
57-
"INSERT OR IGNORE INTO tags (name) VALUES (?)",
58-
"Personal"
59-
);
60-
getCategories();
50+
await db.sql('INSERT OR IGNORE INTO tags (name) VALUES (?)', 'Work')
51+
await db.sql('INSERT OR IGNORE INTO tags (name) VALUES (?)', 'Personal')
52+
getCategories()
6153
}
6254
} catch (error) {
63-
console.error("Error creating tables", error);
55+
console.error('Error creating tables', error)
56+
} finally {
57+
db?.close();
6458
}
65-
};
59+
}
6660

6761
useEffect(() => {
68-
initializeTables();
69-
}, []);
62+
initializeTables()
63+
}, [])
7064

7165
return {
7266
moreCategories,
7367
addCategory,
74-
getCategories,
75-
};
76-
};
68+
getCategories
69+
}
70+
}
7771

78-
export default useCategories;
72+
export default useCategories

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

+25-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { useState, useEffect, useCallback } from "react";
22
import getDbConnection from "../db/dbConnection";
3+
import { get } from "http";
34

45
const useTasks = (tag = null) => {
56
const [taskList, setTaskList] = useState([]);
67

78
const getTasks = useCallback(async () => {
9+
let db = null;
810
try {
911
let result;
12+
db = getDbConnection();
1013
if (tag) {
11-
result = await getDbConnection().sql(
14+
result = await db.sql(
1215
`
1316
SELECT tasks.*, tags.id AS tag_id, tags.name AS tag_name
1417
FROM tasks
@@ -19,7 +22,7 @@ const useTasks = (tag = null) => {
1922
);
2023
setTaskList(result);
2124
} else {
22-
result = await getDbConnection().sql(`
25+
result = await db.sql(`
2326
SELECT tasks.*, tags.id AS tag_id, tags.name AS tag_name
2427
FROM tasks
2528
JOIN tasks_tags ON tasks.id = tasks_tags.task_id
@@ -28,40 +31,48 @@ const useTasks = (tag = null) => {
2831
}
2932
} catch (error) {
3033
console.error("Error getting tasks", error);
34+
} finally {
35+
db?.close();
3136
}
3237
}, [tag]);
3338

3439
const updateTask = async (completedStatus, taskId) => {
40+
let db = null;
3541
try {
36-
await getDbConnection().sql(
42+
db = getDbConnection();
43+
await db.sql(
3744
"UPDATE tasks SET isCompleted=? WHERE id=? RETURNING *",
3845
completedStatus,
3946
taskId
4047
);
4148
getTasks();
4249
} catch (error) {
4350
console.error("Error updating tasks", error);
51+
} finally {
52+
db?.close();
4453
}
4554
};
4655

4756
const addTaskTag = async (newTask, tag) => {
57+
let db = null;
4858
try {
59+
db = getDbConnection();
4960
if (tag.id) {
50-
const addNewTask = await getDbConnection().sql(
61+
const addNewTask = await db.sql(
5162
"INSERT INTO tasks (title, isCompleted) VALUES (?, ?) RETURNING *",
5263
newTask.title,
5364
newTask.isCompleted
5465
);
5566
addNewTask[0].tag_id = tag.id;
5667
addNewTask[0].tag_name = tag.name;
5768
setTaskList([...taskList, addNewTask[0]]);
58-
await getDbConnection().sql(
69+
await db.sql(
5970
"INSERT INTO tasks_tags (task_id, tag_id) VALUES (?, ?)",
6071
addNewTask[0].id,
6172
tag.id
6273
);
6374
} else {
64-
const addNewTaskNoTag = await getDbConnection().sql(
75+
const addNewTaskNoTag = await db.sql(
6576
"INSERT INTO tasks (title, isCompleted) VALUES (?, ?) RETURNING *",
6677
newTask.title,
6778
newTask.isCompleted
@@ -70,17 +81,23 @@ const useTasks = (tag = null) => {
7081
}
7182
} catch (error) {
7283
console.error("Error adding task to database", error);
84+
} finally {
85+
db?.close();
7386
}
7487
};
7588

7689
const deleteTask = async (taskId) => {
90+
let db = null;
7791
try {
78-
await getDbConnection().sql("DELETE FROM tasks_tags WHERE task_id=?", taskId);
79-
const result = await getDbConnection().sql("DELETE FROM tasks WHERE id=?", taskId);
92+
db = getDbConnection();
93+
await db.sql("DELETE FROM tasks_tags WHERE task_id=?", taskId);
94+
const result = await db.sql("DELETE FROM tasks WHERE id=?", taskId);
8095
console.log(`Deleted ${result.totalChanges} task`);
8196
getTasks();
8297
} catch (error) {
8398
console.error("Error deleting task", error);
99+
} finally {
100+
db?.close();
84101
}
85102
};
86103

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sqlitecloud/drivers",
3-
"version": "1.0.436",
3+
"version": "1.0.437",
44
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

0 commit comments

Comments
 (0)