add support for mounting existing dbs by id
This commit is contained in:
parent
7aac7aabdd
commit
365b297397
3 changed files with 65 additions and 19 deletions
22
index.js
22
index.js
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue