From 0d1c477b3f5939998834b798717995d0ea4d3224 Mon Sep 17 00:00:00 2001 From: Daniele Briggi <=> Date: Tue, 11 Mar 2025 10:12:34 +0100 Subject: [PATCH 1/6] chore(examples): always recreate the db connection --- examples/with-javascript-browser/index.html | 45 +++++++++---------- .../with-javascript-expo/db/dbConnection.js | 9 +--- examples/with-javascript-express/app.js | 15 +++++-- examples/with-javascript-vite/src/App.jsx | 36 +++++++-------- examples/with-plain-javascript/app.js | 17 ++++--- .../app/api/hello/route.ts | 17 ++++--- examples/with-typescript-react-native/App.tsx | 15 +++++-- package.json | 2 +- 8 files changed, 86 insertions(+), 70 deletions(-) diff --git a/examples/with-javascript-browser/index.html b/examples/with-javascript-browser/index.html index e5b9656..e4d1bc4 100644 --- a/examples/with-javascript-browser/index.html +++ b/examples/with-javascript-browser/index.html @@ -44,33 +44,28 @@

Results:

messages.prepend(item); }; - // socket is connected the first time the sql button is clicked and stays connected until the page is refreshed - var database = null; - sendButton.addEventListener('click', () => { - if (!database || !database.isConnected()) { - // Get the input element by ID - var connectionStringinputElement = document.getElementById('connectionStringInput'); - var connectionstring = connectionStringinputElement.value; - // connect via websocket to the gateway on the same server - const connectionConfig = { - gatewayUrl: `${window.location.protocol === 'https:' ? 'wss' : 'ws' - }://${window.location.hostname}:4000`, - connectionstring: connectionstring, - }; - database = new window.sqlitecloud.Database( - connectionConfig, - (error) => { - if (error) { - database = null; - appendMessage(`connection error: ${error}`); - } else { - console.log('connected'); - appendMessage(`connected`); - } + // Get the input element by ID + var connectionStringinputElement = document.getElementById('connectionStringInput'); + var connectionstring = connectionStringinputElement.value; + // connect via websocket to the gateway on the same server + const connectionConfig = { + gatewayUrl: `${window.location.protocol === 'https:' ? 'wss' : 'ws' + }://${window.location.hostname}:4000`, + connectionstring: connectionstring, + }; + var database = new window.sqlitecloud.Database( + connectionConfig, + (error) => { + if (error) { + database = null; + appendMessage(`connection error: ${error}`); + } else { + console.log('connected'); + appendMessage(`connected`); } - ); - } + } + ); var messageInputElement = document.getElementById('messageInput'); const sql = messageInputElement.value; diff --git a/examples/with-javascript-expo/db/dbConnection.js b/examples/with-javascript-expo/db/dbConnection.js index 26958e4..4d0b8dd 100644 --- a/examples/with-javascript-expo/db/dbConnection.js +++ b/examples/with-javascript-expo/db/dbConnection.js @@ -1,14 +1,7 @@ import { DATABASE_URL } from "@env"; import { Database } from "@sqlitecloud/drivers"; -/** - * @type {Database} - */ -let database = null; export default function getDbConnection() { - if (!database || !database.isConnected()) { - database = new Database(DATABASE_URL); - } - return database; + return new Database(DATABASE_URL); } \ No newline at end of file diff --git a/examples/with-javascript-express/app.js b/examples/with-javascript-express/app.js index ead1269..80af967 100644 --- a/examples/with-javascript-express/app.js +++ b/examples/with-javascript-express/app.js @@ -14,9 +14,18 @@ app.use(express.json()) /* http://localhost:3001/ returns chinook tracks as json */ app.get('/', async function (req, res, next) { - var database = new sqlitecloud.Database(DATABASE_URL) - var tracks = await database.sql('USE DATABASE chinook.sqlite; SELECT * FROM tracks LIMIT 20;') - res.send({ tracks }) + var database = null + try { + database = new sqlitecloud.Database(DATABASE_URL) + var tracks = await database.sql('USE DATABASE chinook.sqlite; SELECT * FROM tracks LIMIT 20;') + res.send({ tracks }) + } catch (error) { + res.send({ error: error.message }) + } finally { + if (database) { + database.close() + } + } }) const port = process.env.PORT || 3000 diff --git a/examples/with-javascript-vite/src/App.jsx b/examples/with-javascript-vite/src/App.jsx index cb79747..2d05d69 100644 --- a/examples/with-javascript-vite/src/App.jsx +++ b/examples/with-javascript-vite/src/App.jsx @@ -1,30 +1,28 @@ import { useEffect, useState } from "react"; import { Database } from "@sqlitecloud/drivers"; -let db = null - -function getDatabase() { - if (!db || !db.isConnected()) { - db = new Database(import.meta.env.VITE_DATABASE_URL); - } - - return db; -} - function App() { const [data, setData] = useState([]); const getAlbums = async () => { - 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; - `); - setData(result); + let database = null; + try { + database = new Database(import.meta.env.VITE_DATABASE_URL) + const result = await database.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; + `); + setData(result); + } catch (error) { + console.error("Error getting albums", error); + } finally { + database.close(); + } }; useEffect(() => { diff --git a/examples/with-plain-javascript/app.js b/examples/with-plain-javascript/app.js index f0e44c4..14040e5 100644 --- a/examples/with-plain-javascript/app.js +++ b/examples/with-plain-javascript/app.js @@ -9,12 +9,19 @@ var DATABASE_URL = process.env.DATABASE_URL console.assert(DATABASE_URL, 'DATABASE_URL environment variable not set in .env') async function selectTracks() { - // create a connection with sqlitecloud - var database = new sqlitecloud.Database(DATABASE_URL) + var database = null + try { + // create a connection with sqlitecloud + database = new sqlitecloud.Database(DATABASE_URL) - // run async query - var tracks = await database.sql('USE DATABASE chinook.sqlite; SELECT * FROM tracks LIMIT 20;') - console.log(`selectTracks returned:`, tracks) + // run async query + var tracks = await database.sql('USE DATABASE chinook.sqlite; SELECT * FROM tracks LIMIT 20;') + console.log(`selectTracks returned:`, tracks) + } catch (error) { + console.error(`selectTracks error:`, error) + } finally { + database.close() + } // You can also use all the regular sqlite3 api with callbacks, see: // https://docs.sqlitecloud.io/docs/sdk/js/intro diff --git a/examples/with-typescript-nextjs/app/api/hello/route.ts b/examples/with-typescript-nextjs/app/api/hello/route.ts index a191eff..983841f 100644 --- a/examples/with-typescript-nextjs/app/api/hello/route.ts +++ b/examples/with-typescript-nextjs/app/api/hello/route.ts @@ -12,11 +12,18 @@ console.assert(DATABASE_URL, 'Please configure a .env file with DATABASE_URL poi // route for /api/hello export async function GET(request: NextRequest) { // connect to database using connection string provided in https://dashboard.sqlitecloud.io/ - const database = new Database(DATABASE_URL) + let database + try { + database = new Database(DATABASE_URL) - // retrieve rows from chinook database using a plain SQL query - const tracks = await database.sql('USE DATABASE chinook.sqlite; SELECT * FROM tracks LIMIT 20;') + // retrieve rows from chinook database using a plain SQL query + const tracks = await database.sql('USE DATABASE chinook.sqlite; SELECT * FROM tracks LIMIT 20;') - // return as json response - return NextResponse.json<{ data: any }>({ data: tracks }) + // return as json response + return NextResponse.json<{ data: any }>({ data: tracks }) + } catch (error) { + return NextResponse.json({ error }, { status: 500 }) + } finally { + database?.close() + } } diff --git a/examples/with-typescript-react-native/App.tsx b/examples/with-typescript-react-native/App.tsx index b71df81..454e2dd 100644 --- a/examples/with-typescript-react-native/App.tsx +++ b/examples/with-typescript-react-native/App.tsx @@ -8,12 +8,19 @@ export default function App() { useEffect(() => { async function getAlbums() { - const db = new Database(`${DATABASE_URL}`); + let db = null; + try { + db = new Database(`${DATABASE_URL}`); - 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;'); + 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;'); - setAlbums(result); + setAlbums(result); + } catch (error) { + console.error(error); + } finally { + db?.close(); + } } getAlbums(); diff --git a/package.json b/package.json index 311524b..c7fd548 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sqlitecloud/drivers", - "version": "1.0.422", + "version": "1.0.435", "description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients", "main": "./lib/index.js", "types": "./lib/index.d.ts", From 0290adadee0c3f43139d1cbf1e6265d698f779c8 Mon Sep 17 00:00:00 2001 From: Daniele Briggi <=> Date: Wed, 12 Mar 2025 12:12:57 +0100 Subject: [PATCH 2/6] fix(workflow): ios18 missing python distutils --- .github/workflows/test.yml | 5 ++++- package.json | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 68c3cf2..4b4720f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -129,10 +129,12 @@ jobs: working-directory: examples/with-plain-javascript run: | npm i + echo "Dependencies installed" app_log=$(mktemp) npm start > "$app_log" 2>&1 & app_pid=$! + echo "App started" sleep 3 kill "$app_pid" 2>/dev/null @@ -489,7 +491,8 @@ jobs: xcode-version: 14.3 - name: build driver - run: npm i && npm run build && echo "DRIVER=$(npm pack --json | jq '.[0].filename')" >> $GITHUB_ENV + # distutils is required for the driver build and it was removed since python 3.13 + run: python3 -m pip install setuptools && npm i && npm run build && echo "DRIVER=$(npm pack --json | jq '.[0].filename')" >> $GITHUB_ENV - name: install driver working-directory: examples/with-javascript-expo diff --git a/package.json b/package.json index c7fd548..53d18d9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sqlitecloud/drivers", - "version": "1.0.435", + "version": "1.0.436", "description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients", "main": "./lib/index.js", "types": "./lib/index.d.ts", From 3bb8b75e0ede25e55af318f5e8c1b8c4301a030d Mon Sep 17 00:00:00 2001 From: Daniele Briggi <=> Date: Wed, 12 Mar 2025 12:19:25 +0100 Subject: [PATCH 3/6] fix(workflow): test to complete and lost disutils package --- .github/workflows/test.yml | 39 +++++++------------------------------- package-lock.json | 4 ++-- 2 files changed, 9 insertions(+), 34 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4b4720f..a92ff0a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -39,7 +39,7 @@ jobs: - name: Upload Code Coverage uses: codecov/codecov-action@v4 env: - CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} examples-test: needs: code-test @@ -129,17 +129,7 @@ jobs: working-directory: examples/with-plain-javascript run: | npm i - echo "Dependencies installed" - app_log=$(mktemp) - npm start > "$app_log" 2>&1 & - - app_pid=$! - echo "App started" - sleep 3 - kill "$app_pid" 2>/dev/null - - node_test=$(<"$app_log") - rm "$app_log" + node_test=$(npm start) if [[ "$node_test" == *"Composer: 'Angus Young, Malcolm Young, Brian Johnson',"* ]]; then echo "✅ node with-plain-javascript test passed" @@ -154,15 +144,8 @@ jobs: if [ "$RUNNER_OS" != "Windows" ]; then bun i #re-installing dependencies in windows with bash causes a panic fi - app_log=$(mktemp) - bun start > "$app_log" 2>&1 & - - app_pid=$! - sleep 3 - kill "$app_pid" 2>/dev/null - - bun_test=$(<"$app_log") - rm "$app_log" + + bun_test=$(bun start) if [[ "$bun_test" == *"Composer: 'Angus Young, Malcolm Young, Brian Johnson',"* ]]; then echo "✅ bun with-plain-javascript test passed" @@ -174,15 +157,7 @@ jobs: - name: deno with-plain-javascript working-directory: examples/with-plain-javascript run: | - app_log=$(mktemp) - deno run start > "$app_log" 2>&1 & - - app_pid=$! - sleep 3 - kill "$app_pid" 2>/dev/null - - deno_test=$(<"$app_log") - rm "$app_log" + deno_test=$(deno run start) if [[ "$deno_test" == *"Composer: 'Angus Young, Malcolm Young, Brian Johnson',"* ]]; then echo "✅ deno with-plain-javascript test passed" @@ -491,8 +466,8 @@ jobs: xcode-version: 14.3 - name: build driver - # distutils is required for the driver build and it was removed since python 3.13 - run: python3 -m pip install setuptools && npm i && npm run build && echo "DRIVER=$(npm pack --json | jq '.[0].filename')" >> $GITHUB_ENV + # distutils is required for the driver build (dep. `node-gyp`) and it was removed since python 3.13 + run: brew install python-setuptools && npm i && npm run build && echo "DRIVER=$(npm pack --json | jq '.[0].filename')" >> $GITHUB_ENV - name: install driver working-directory: examples/with-javascript-expo diff --git a/package-lock.json b/package-lock.json index ee63c23..6aed0b6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sqlitecloud/drivers", - "version": "1.0.422", + "version": "1.0.436", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@sqlitecloud/drivers", - "version": "1.0.422", + "version": "1.0.436", "license": "MIT", "dependencies": { "buffer": "^6.0.3", From 73239e52098d5fce908742fc688c518392807279 Mon Sep 17 00:00:00 2001 From: Daniele Briggi <=> Date: Wed, 12 Mar 2025 17:43:02 +0100 Subject: [PATCH 4/6] fix(workflow): remove files not folder --- .github/workflows/test.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a92ff0a..6e4e90f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -123,7 +123,7 @@ jobs: exit 1 - name: remove with-javascript-express - run: rm -rf examples/with-javascript-express + run: rm -rf examples/with-javascript-express/* - name: node with-plain-javascript working-directory: examples/with-plain-javascript @@ -168,7 +168,7 @@ jobs: - name: remove with-plain-javascript if: matrix.os != 'windows-latest' #kill command doesn not work on windows with bash, we can skip this step - run: rm -rf examples/with-plain-javascript + run: rm -rf examples/with-plain-javascript/* - name: node with-typescript-knex working-directory: examples/with-typescript-knex @@ -199,7 +199,7 @@ jobs: exit 1 - name: remove with-typescript-knex - run: rm -rf examples/with-typescript-knex + run: rm -rf examples/with-typescript-knex/* - name: node with-typescript-nextjs working-directory: examples/with-typescript-nextjs @@ -253,7 +253,7 @@ jobs: - name: remove with-typescript-nextjs if: matrix.os != 'ubuntu-latest' #rm: cannot remove examples/with-typescript-nextjs: Directory not empty - run: rm -rf examples/with-typescript-nextjs + run: rm -rf examples/with-typescript-nextjs/* - name: node with-javascript-vite if: matrix.os != 'LinuxARM64' @@ -282,7 +282,7 @@ jobs: PW_DISABLE_TS_ESM: true - name: remove with-javascript-vite - run: rm -rf examples/with-javascript-vite + run: rm -rf examples/with-javascript-vite/* - name: node with-javascript-browser if: matrix.os != 'LinuxARM64' @@ -303,7 +303,7 @@ jobs: command: cd examples/with-javascript-browser && deno --allow-all test.cjs - name: remove with-javascript-browser - run: rm -rf examples/with-javascript-browser + run: rm -rf examples/with-javascript-browser/* rn-ios-test: needs: code-test From 64eda5e3a742d644e7adad4b379c00126fddee8a Mon Sep 17 00:00:00 2001 From: Daniele Briggi <=> Date: Thu, 13 Mar 2025 09:15:47 +0100 Subject: [PATCH 5/6] fix(examples): missing close() --- .../components/AddTaskModal.js | 6 +- .../hooks/useCategories.js | 94 +++++++++---------- .../with-javascript-expo/hooks/useTasks.js | 32 +++++-- package.json | 2 +- 4 files changed, 74 insertions(+), 60 deletions(-) diff --git a/examples/with-javascript-expo/components/AddTaskModal.js b/examples/with-javascript-expo/components/AddTaskModal.js index 57b6e47..757ccf1 100644 --- a/examples/with-javascript-expo/components/AddTaskModal.js +++ b/examples/with-javascript-expo/components/AddTaskModal.js @@ -29,11 +29,15 @@ export default AddTaskModal = ({ }; const getTags = async () => { + let db = null; try { - const tags = await getDbConnection().sql("SELECT * FROM tags"); + db = getDbConnection(); + const tags = await db.sql("SELECT * FROM tags"); setTagsList(tags); } catch (error) { console.error("Error getting tags", error); + } finally { + db?.close(); } }; diff --git a/examples/with-javascript-expo/hooks/useCategories.js b/examples/with-javascript-expo/hooks/useCategories.js index db425b0..f2beca8 100644 --- a/examples/with-javascript-expo/hooks/useCategories.js +++ b/examples/with-javascript-expo/hooks/useCategories.js @@ -1,78 +1,72 @@ -import { useState, useEffect } from "react"; -import getDbConnection from "../db/dbConnection"; +import { useState, useEffect } from 'react' +import getDbConnection from '../db/dbConnection' const useCategories = () => { - const [moreCategories, setMoreCategories] = useState(["Work", "Personal"]); + const [moreCategories, setMoreCategories] = useState(['Work', 'Personal']) const getCategories = async () => { + let db = null; try { - const tags = await getDbConnection().sql("SELECT * FROM tags"); - const filteredTags = tags.filter((tag) => { - return tag["name"] !== "Work" && tag["name"] !== "Personal"; - }); - setMoreCategories((prevCategories) => [ - ...prevCategories, - ...filteredTags.map((tag) => tag.name), - ]); + const tags = await db.sql('SELECT * FROM tags') + const filteredTags = tags.filter(tag => { + return tag['name'] !== 'Work' && tag['name'] !== 'Personal' + }) + setMoreCategories(prevCategories => [...prevCategories, ...filteredTags.map(tag => tag.name)]) } catch (error) { - console.error("Error getting tags/categories", error); + console.error('Error getting tags/categories', error) } - }; + } - const addCategory = async (newCategory) => { + const addCategory = async newCategory => { + let db = null; try { - await getDbConnection().sql( - "INSERT INTO tags (name) VALUES (?) RETURNING *", - newCategory - ); - setMoreCategories((prevCategories) => [...prevCategories, newCategory]); + db = getDbConnection(); + await db.sql('INSERT INTO tags (name) VALUES (?) RETURNING *', newCategory) + setMoreCategories(prevCategories => [...prevCategories, newCategory]) } catch (error) { - console.error("Error adding category", error); + console.error('Error adding category', error) + } finally { + db?.close(); } - }; + } const initializeTables = async () => { + let db = null; try { - const createTasksTable = await getDbConnection().sql( - "CREATE TABLE IF NOT EXISTS tasks (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, isCompleted INT NOT NULL);" - ); + db = getDbConnection(); + const createTasksTable = await db.sql( + 'CREATE TABLE IF NOT EXISTS tasks (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, isCompleted INT NOT NULL);' + ) - const createTagsTable = await getDbConnection().sql( - "CREATE TABLE IF NOT EXISTS tags (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, UNIQUE(name));" - ); + const createTagsTable = await db.sql('CREATE TABLE IF NOT EXISTS tags (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, UNIQUE(name));') - const createTagsTasksTable = await getDbConnection().sql( - "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));" - ); + const createTagsTasksTable = await db.sql( + '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));' + ) - if ( - createTasksTable === "OK" && - createTagsTable === "OK" && - createTagsTasksTable === "OK" - ) { - console.log("Successfully created tables"); + if (createTasksTable === 'OK' && createTagsTable === 'OK' && createTagsTasksTable === 'OK') { + console.log('Successfully created tables') - await getDbConnection().sql("INSERT OR IGNORE INTO tags (name) VALUES (?)", "Work"); - await getDbConnection().sql( - "INSERT OR IGNORE INTO tags (name) VALUES (?)", - "Personal" - ); - getCategories(); + await db.sql('INSERT OR IGNORE INTO tags (name) VALUES (?)', 'Work') + await db.sql('INSERT OR IGNORE INTO tags (name) VALUES (?)', 'Personal') + getCategories() } } catch (error) { - console.error("Error creating tables", error); + console.error('Error creating tables', error) + } finally { + db?.close(); } - }; + } useEffect(() => { - initializeTables(); - }, []); + initializeTables() + }, []) return { moreCategories, addCategory, - getCategories, - }; -}; + getCategories + } +} -export default useCategories; +export default useCategories diff --git a/examples/with-javascript-expo/hooks/useTasks.js b/examples/with-javascript-expo/hooks/useTasks.js index 0a7ccad..6e7906e 100644 --- a/examples/with-javascript-expo/hooks/useTasks.js +++ b/examples/with-javascript-expo/hooks/useTasks.js @@ -5,10 +5,12 @@ const useTasks = (tag = null) => { const [taskList, setTaskList] = useState([]); const getTasks = useCallback(async () => { + let db = null; try { let result; + db = getDbConnection(); if (tag) { - result = await getDbConnection().sql( + result = await db.sql( ` SELECT tasks.*, tags.id AS tag_id, tags.name AS tag_name FROM tasks @@ -19,7 +21,7 @@ const useTasks = (tag = null) => { ); setTaskList(result); } else { - result = await getDbConnection().sql(` + result = await db.sql(` SELECT tasks.*, tags.id AS tag_id, tags.name AS tag_name FROM tasks JOIN tasks_tags ON tasks.id = tasks_tags.task_id @@ -28,12 +30,16 @@ const useTasks = (tag = null) => { } } catch (error) { console.error("Error getting tasks", error); + } finally { + db?.close(); } }, [tag]); const updateTask = async (completedStatus, taskId) => { + let db = null; try { - await getDbConnection().sql( + db = getDbConnection(); + await db.sql( "UPDATE tasks SET isCompleted=? WHERE id=? RETURNING *", completedStatus, taskId @@ -41,13 +47,17 @@ const useTasks = (tag = null) => { getTasks(); } catch (error) { console.error("Error updating tasks", error); + } finally { + db?.close(); } }; const addTaskTag = async (newTask, tag) => { + let db = null; try { + db = getDbConnection(); if (tag.id) { - const addNewTask = await getDbConnection().sql( + const addNewTask = await db.sql( "INSERT INTO tasks (title, isCompleted) VALUES (?, ?) RETURNING *", newTask.title, newTask.isCompleted @@ -55,13 +65,13 @@ const useTasks = (tag = null) => { addNewTask[0].tag_id = tag.id; addNewTask[0].tag_name = tag.name; setTaskList([...taskList, addNewTask[0]]); - await getDbConnection().sql( + await db.sql( "INSERT INTO tasks_tags (task_id, tag_id) VALUES (?, ?)", addNewTask[0].id, tag.id ); } else { - const addNewTaskNoTag = await getDbConnection().sql( + const addNewTaskNoTag = await db.sql( "INSERT INTO tasks (title, isCompleted) VALUES (?, ?) RETURNING *", newTask.title, newTask.isCompleted @@ -70,17 +80,23 @@ const useTasks = (tag = null) => { } } catch (error) { console.error("Error adding task to database", error); + } finally { + db?.close(); } }; const deleteTask = async (taskId) => { + let db = null; try { - await getDbConnection().sql("DELETE FROM tasks_tags WHERE task_id=?", taskId); - const result = await getDbConnection().sql("DELETE FROM tasks WHERE id=?", taskId); + db = getDbConnection(); + await db.sql("DELETE FROM tasks_tags WHERE task_id=?", taskId); + const result = await db.sql("DELETE FROM tasks WHERE id=?", taskId); console.log(`Deleted ${result.totalChanges} task`); getTasks(); } catch (error) { console.error("Error deleting task", error); + } finally { + db?.close(); } }; diff --git a/package.json b/package.json index 53d18d9..00f6f78 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sqlitecloud/drivers", - "version": "1.0.436", + "version": "1.0.437", "description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients", "main": "./lib/index.js", "types": "./lib/index.d.ts", From 193fb7de548d84031a361b5080bd03fa47ab98b5 Mon Sep 17 00:00:00 2001 From: Daniele Briggi <=> Date: Thu, 13 Mar 2025 10:14:00 +0100 Subject: [PATCH 6/6] fix(workflow): ios18 missing python distutils --- .github/workflows/test.yml | 4 ++-- examples/with-javascript-expo/hooks/useCategories.js | 1 + package-lock.json | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6e4e90f..c26566e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -252,7 +252,6 @@ jobs: exit 1 - name: remove with-typescript-nextjs - if: matrix.os != 'ubuntu-latest' #rm: cannot remove examples/with-typescript-nextjs: Directory not empty run: rm -rf examples/with-typescript-nextjs/* - name: node with-javascript-vite @@ -339,7 +338,8 @@ jobs: xcode-version: 14.3 - name: build driver - run: npm i && npm run build && echo "DRIVER=$(npm pack --json | jq '.[0].filename')" >> $GITHUB_ENV + # distutils is required for the driver build (dep. `node-gyp`) and it was removed since python 3.13 + run: brew install python-setuptools && npm i && npm run build && echo "DRIVER=$(npm pack --json | jq '.[0].filename')" >> $GITHUB_ENV - name: install driver working-directory: examples/with-typescript-react-native diff --git a/examples/with-javascript-expo/hooks/useCategories.js b/examples/with-javascript-expo/hooks/useCategories.js index f2beca8..fe0502d 100644 --- a/examples/with-javascript-expo/hooks/useCategories.js +++ b/examples/with-javascript-expo/hooks/useCategories.js @@ -7,6 +7,7 @@ const useCategories = () => { const getCategories = async () => { let db = null; try { + db = getDbConnection(); const tags = await db.sql('SELECT * FROM tags') const filteredTags = tags.filter(tag => { return tag['name'] !== 'Work' && tag['name'] !== 'Personal' diff --git a/package-lock.json b/package-lock.json index 6aed0b6..ecda972 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sqlitecloud/drivers", - "version": "1.0.436", + "version": "1.0.437", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@sqlitecloud/drivers", - "version": "1.0.436", + "version": "1.0.437", "license": "MIT", "dependencies": { "buffer": "^6.0.3",