Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Avoid deleting DB when refreshing
Browse files Browse the repository at this point in the history
  • Loading branch information
nicognaW authored Mar 31, 2024
1 parent 4801d49 commit e4827d1
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
</div>

<script type="module">
import { PGlite } from "https://cdn.jsdelivr.net/npm/@electric-sql/pglite/dist/index.js";
import {PGlite} from "https://cdn.jsdelivr.net/npm/@electric-sql/pglite/dist/index.js";

const addRowButton = document.getElementById('addRowButton');
const statusElement = document.getElementById('status');
Expand All @@ -139,32 +139,39 @@
const ddlInput = document.getElementById('ddlInput');
let db = null;
let stagedRows = [];

async function updateDDLAndReinitializeDB() {
const ddl = ddlInput.value.trim();
if (!ddl) {
setStatus("No DDL command entered.");
return;
}
await initializeDB(ddl); // Pass the DDL command to initializeDB
await nukeIndexedDB();
await initializeDB(ddl);
}

function enableUI() {
document.getElementById('addRowButton').disabled = false;
document.getElementById('refreshButton').disabled = false;
document.getElementById('executeSqlButton').disabled = false;
document.getElementById('sqlInput').disabled = false;
}

function disableUI() {
document.getElementById('addRowButton').disabled = true;
document.getElementById('refreshButton').disabled = true;
document.getElementById('executeSqlButton').disabled = true;
document.getElementById('sqlInput').disabled = true;
}

async function initializeDB(ddl = "CREATE TABLE IF NOT EXISTS test (id SERIAL PRIMARY KEY, message TEXT);") {
disableUI()
async function nukeIndexedDB() {
await window.indexedDB.databases().then((r) => {
for (let i = 0; i < r.length; i++) window.indexedDB.deleteDatabase(r[i].name);
})
}

async function initializeDB(ddl = "CREATE TABLE IF NOT EXISTS test (id SERIAL PRIMARY KEY, message TEXT);") {
disableUI()
setStatus("Initializing database...");
db = new PGlite("idb://my-pgdata");
await db.query(ddl); // Use the DDL parameter
Expand Down Expand Up @@ -267,7 +274,7 @@
}
setStatus(`SQL executed. Rows returned: ${result.rows.length}.`);
} else {
fetchTableData(); // Refresh the table data
fetchTableData();
setStatus(`SQL executed. Rows affected: ${result.affectedRows ?? 'N/A'}.`);
}
} catch (error) {
Expand Down

0 comments on commit e4827d1

Please sign in to comment.