add support for mounting existing dbs by id

This commit is contained in:
HRG @ SCExC 2024-06-04 01:21:32 -04:00
parent 7aac7aabdd
commit 365b297397
3 changed files with 65 additions and 19 deletions

View file

@ -23,7 +23,7 @@ app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.use(fileUpload());
const db = betterSqlite3("./prime.db");
const db = betterSqlite3("./dbs/0.sqlite");
db.pragma("journal_mode = WAL");
@ -297,11 +297,7 @@ function createDb(db, structId, name) {
`);
insertStructureDbStmt.run(dbId, structId, name);
const dbDir = path.join("dbs", dbId.toString());
if (!fs.existsSync(dbDir)) {
fs.mkdirSync(dbDir, { recursive: true });
}
const newDbPath = path.join(dbDir, `${name}.sqlite`);
const newDbPath = path.join("dbs", `${dbId}.sqlite`);
const newDb = betterSqlite3(newDbPath);
newDb.close();
@ -311,6 +307,14 @@ function createDb(db, structId, name) {
return transaction();
}
function attachDb(db, structId, dbId, alias) {
const insertStructureDbStmt = db.prepare(`
INSERT INTO structure_dbs (db_id, structure_id, alias)
VALUES (?, ?, ?)
`);
insertStructureDbStmt.run(dbId, structId, alias);
}
function getFilesForStruct(db, structureId) {
let test = db
.prepare("SELECT * FROM files WHERE structure_id = ? ORDER BY id DESC")
@ -446,6 +450,12 @@ app.post("/workshop/:structure_id/db", (req, res) => {
return smartRedirect(req, res, redirectUrl);
});
app.post("/workshop/:structure_id/db/attach", (req, res) => {
attachDb(db, req.params.structure_id, req.body.db_id, req.body.alias);
const redirectUrl = `/workshop/${req.params.structure_id}/db/${req.body.db_id}`;
return smartRedirect(req, res, redirectUrl);
});
app.get("/workshop/:structure_id/db/:db_id", (req, res) => {
const structdb = getDbForStructure(
db,

View file

@ -20,7 +20,7 @@ CREATE TABLE structures (
CREATE TABLE dbs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
structure_id INTEGER,
structure_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
library TEXT NOT NULL DEFAULT "",
@ -75,6 +75,16 @@ CREATE TABLE files (
UNIQUE(path, structure_id)
);
INSERT INTO
structures (id, name, user_id)
VALUES
(0, 'prime', NULL);
INSERT INTO
dbs (id, name, structure_id, library)
VALUES
(0, 'prime', 0, '');
-- ===========================
-- TRIGGERS
-- ===========================

View file

@ -7,16 +7,42 @@
</div>
</div>
<div class="window-body">
<form hx-post="/workshop/<%= it.structure.id %>/db" hx-target="#response">
<div class="field-row-stacked" style="width: 200px">
<label for="name">Name</label>
<input name="name" id="name" type="text" />
</div>
<section class="field-row" style="justify-content: flex-end">
<button type="submit">OK</button>
<button type="button" _="on click trigger closeModal">Cancel</button>
</section>
</form>
<div id="response"></div>
<section class="tabs" style="max-width: 500px">
<menu role="tablist" aria-label="Sample Tabs">
<button role="tab" aria-selected="true" aria-controls="new" _="on click add .hidden to #existing then remove .hidden from #new then setAttribute('aria-selected', 'true')">New</button>
<button role="tab" aria-controls="existing" _="on click add .hidden to #new then remove .hidden from #existing then setAttribute('aria-selected', 'true') to me">Existing</button>
</menu>
<article role="tabpanel" id="new">
<form hx-post="/workshop/<%= it.structure.id %>/db" hx-target="#response">
<div class="field-row-stacked" style="width: 200px">
<label for="name">Name</label>
<input name="name" id="name" type="text" />
</div>
<section class="field-row" style="justify-content: flex-end">
<button type="submit">OK</button>
<button type="button" _="on click trigger closeModal">Cancel</button>
</section>
</form>
<div id="response"></div>
</article>
<article role="tabpanel" class="hidden" id="existing">
<form hx-post="/workshop/<%= it.structure.id %>/db/attach" hx-target="#response">
<div class="field-row-stacked" style="width: 200px">
<label for="alias">Alias</label>
<input name="alias" id="alias" type="text" />
</div>
<div class="field-row-stacked" style="width: 200px">
<label for="db_id">DB ID</label>
<input name="db_id" id="db_id" type="text" />
</div>
<section class="field-row" style="justify-content: flex-end">
<button type="submit">OK</button>
<button type="button" _="on click trigger closeModal">Cancel</button>
</section>
</form>
<div id="response"></div>
</article>
</section>
</div>
</div>
</div>