From d2ad9947168e52a7079b0acf759acb72567daba2 Mon Sep 17 00:00:00 2001 From: "HRG @ SCExC" Date: Sat, 8 Jun 2024 11:42:27 -0400 Subject: [PATCH] more stuff --- index.js | 111 ++++++++++++++--------- migrations/000_bootstrap_db.sql | 1 + views/workshop/clone_structure_modal.eta | 11 ++- views/workshop/new_route_modal.eta | 4 +- views/workshop/settings.eta | 2 +- views/workshop/sidebar.eta | 5 +- 6 files changed, 84 insertions(+), 50 deletions(-) diff --git a/index.js b/index.js index 9cfdf40..cb50f90 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,7 @@ const PORT = 3000; let viewpath = path.join(__dirname, "views"); let eta = new Eta({ views: viewpath, cache: false, autoEscape: true }); -const routes = { GET: [], POST: [], PUT: [], DELETE: [] }; +let routes = { GET: [], POST: [], PUT: [], DELETE: [] }; app.use(bodyParser.urlencoded({ extended: true })); app.use(express.static("public")); @@ -34,7 +34,7 @@ function getAllRoutes(db) { SELECT routes.*, structures.route_prefix FROM routes JOIN structures ON routes.structure_id = structures.id - ORDER BY routes.id ASC; + ORDER BY routes.updated_at DESC; ` ) .all(); @@ -79,16 +79,23 @@ function applyMigrations() { } function buildRoutes() { + let newRoutes = { + GET: [], + POST: [], + PUT: [], + DELETE: [], + }; + for (let route of getAllRoutes(db)) { const prefix = route.route_prefix; - console.log(route); const p = prefix ? path.join(route.route_prefix, route.path) : route.path; - routes[route.verb].push({ + newRoutes[route.verb].push({ matcher: match(p, { decode: decodeURIComponent }), id: route.id, path: route.path, }); } + routes = newRoutes; } applyMigrations(); @@ -273,10 +280,18 @@ function createTemplate(db, structureId, name, content, testObjectString) { function getDbsForStructure(db, structureId) { return db .prepare( - `SELECT * + `SELECT + *, + CASE + WHEN structure_dbs.structure_id != dbs.structure_id THEN 1 + ELSE 0 + END AS is_aliased, + structure_dbs.structure_id as alias_struct_id, + dbs.structure_id as db_struct_id FROM structure_dbs INNER JOIN dbs ON structure_dbs.db_id = dbs.id - WHERE structure_dbs.structure_id = ?; + WHERE structure_dbs.structure_id = ? + ORDER BY structure_dbs.created_at, is_aliased ASC; ` ) .all(structureId); @@ -399,7 +414,7 @@ function cloneStructure( newStructureName, userId, routePrefix = "", - cloneDb = false + cloneDbs = [] ) { const transaction = db.transaction(() => { const cloneStructure = db.prepare(` @@ -414,47 +429,57 @@ function cloneStructure( structId ).lastInsertRowid; - if (cloneDb) { - const dbIds = db - .prepare(`select db_id from structure_dbs where structure_id = ?;`) - .all(structId); + const dbIds = db + .prepare(`select db_id from structure_dbs where structure_id = ?;`) + .all(structId); - for (let { db_id } of dbIds) { - const newDb = db - .prepare( - ` + const toClone = new Set(cloneDbs); + const toAlias = new Set(); + + console.log(toClone, dbIds) + + for (let { db_id } of dbIds) { + if (!toClone.has(db_id.toString())) { + toAlias.add(db_id.toString()); + } + } + + for (let db_id of toClone) { + const newDb = db + .prepare( + ` INSERT INTO dbs (name, structure_id, library) SELECT name, ?, library FROM dbs WHERE id = ?; ` - ) - .run(newStructId, db_id).lastInsertRowid; + ) + .run(newStructId, db_id).lastInsertRowid; - db.prepare( - ` + db.prepare( + ` INSERT INTO structure_dbs (db_id, structure_id, alias) SELECT ?, ?, alias FROM structure_dbs WHERE db_id = ? AND structure_id = ?; ` - ).run(newDb, newStructId, db_id, structId); + ).run(newDb, newStructId, db_id, structId); - db.prepare(`select id from dbs where structure_id = ?`) - .all(newStructId) - .map((new_db) => { - console.log(db_id, new_db.id); - const srcPath = path.join(__dirname, "dbs", `${db_id}.sqlite`); - const destPath = path.join(__dirname, "dbs", `${new_db.id}.sqlite`); - fs.copyFileSync(srcPath, destPath); - fs.copyFileSync(srcPath + "-shm", destPath + "-shm"); - fs.copyFileSync(srcPath + "-wal", destPath + "-wal"); - }); - } - } else { + db.prepare(`select id from dbs where structure_id = ?`) + .all(newStructId) + .map((new_db) => { + const srcPath = path.join(__dirname, "dbs", `${db_id}.sqlite`); + const destPath = path.join(__dirname, "dbs", `${new_db.id}.sqlite`); + fs.copyFileSync(srcPath, destPath); + fs.copyFileSync(srcPath + "-shm", destPath + "-shm"); + fs.copyFileSync(srcPath + "-wal", destPath + "-wal"); + }); + } + for (let db_id of toAlias) { db.prepare( ` INSERT INTO structure_dbs (db_id, structure_id, alias) - SELECT db_id, ?, alias FROM structure_dbs WHERE structure_id = ?; + SELECT db_id, ?, alias FROM structure_dbs WHERE structure_id = ? AND db_id = ?; ` - ).run(newStructId, structId); + ).run(newStructId, structId, db_id); } + const cloneTemplates = db.prepare(` INSERT INTO templates (name, content, structure_id, test_object, engine) SELECT name, content, ?, test_object, engine FROM templates WHERE structure_id = ?; @@ -509,7 +534,6 @@ function bootstrapTemplateWithHTMXetc( $.root().children().attr("data-bliss-route", blissRoute); $.root().children().attr("data-bliss-clone", blissClone); if (blissCopy) $.root().children().attr("data-bliss-copy", blissCopy); - console.log(blissCopy, "fleem") htmlString = $.html(); } @@ -560,7 +584,6 @@ app.get("/workshop/:structure_id", (req, res) => { app.post("/workshop/:structure_id/clone", (req, res) => { let routePrefix = req.body.route_prefix; routePrefix = routePrefix[0] == "/" ? routePrefix : "/" + routePrefix; - const cloneDb = req.body.clone_db == "true"; let newStructureId; try { newStructureId = cloneStructure( @@ -569,7 +592,7 @@ app.post("/workshop/:structure_id/clone", (req, res) => { 1, // req.session.userId, routePrefix, - cloneDb + req.body.clone_dbs ); buildRoutes(); } catch (e) { @@ -750,14 +773,15 @@ app.get("/workshop/:structure_id/template/:template_id/preview", (req, res) => { app.get("/workshop/:structure_id/route/:id", (req, res) => { const route = getRoute(db, req.params.id); const routePrefix = getStructure(db, req.params.structure_id).route_prefix; + let previewUrl = routePrefix + ? path.join(routePrefix || "", route.path) + : route.path; return res.send( bootstrapTemplateWithHTMXetc( eta.render("workshop/route", { route: route, previewUrl: - route["verb"] == "GET" - ? path.join(routePrefix, route.path) - : "/workshop/tip", + route["verb"] == "GET" ? previewUrl : "/workshop/do-something-here", ...sidebarStuff(db, req.params.structure_id), }) ) @@ -879,10 +903,14 @@ app.get("/workshop/:structure_id/new_db_modal", (req, res) => { app.get("/workshop/:structure_id/clone_modal", (req, res) => { const structure = getStructure(db, req.params.structure_id); + const dbs = getDbsForStructure(db, req.params.structure_id); + console.log(dbs); + return res.send( bootstrapTemplateWithHTMXetc( eta.render("workshop/clone_structure_modal", { structure, + dbs, defaultRoutePrefix: path.join(structure.route_prefix || "", "clone"), }) ) @@ -945,12 +973,13 @@ app.all("*", (req, res) => { bootstrapTemplateWithHTMXetc( args[0], `/workshop/${route.structure_id}/route/${route.id}`, - `/workshop/${route.structure_id}/clone/`, + `/workshop/${route.structure_id}/clone_modal/`, verb == "GET" ? embedHTML(req.originalUrl) : null ) ); }; res.render = (template, context) => { + context = context || {}; context.route = function (url) { return __urlPrefix ? path.join(__urlPrefix, url) : url; }; diff --git a/migrations/000_bootstrap_db.sql b/migrations/000_bootstrap_db.sql index be72e56..47b5ef3 100644 --- a/migrations/000_bootstrap_db.sql +++ b/migrations/000_bootstrap_db.sql @@ -63,6 +63,7 @@ CREATE TABLE structure_dbs ( PRIMARY KEY(db_id, structure_id), FOREIGN KEY(db_id) REFERENCES dbs(id), FOREIGN KEY(structure_id) REFERENCES structures(id) + UNIQUE(alias, structure_id) ); CREATE TABLE files ( diff --git a/views/workshop/clone_structure_modal.eta b/views/workshop/clone_structure_modal.eta index fa12058..88b6d44 100644 --- a/views/workshop/clone_structure_modal.eta +++ b/views/workshop/clone_structure_modal.eta @@ -10,10 +10,13 @@ -
- - -
+
Clone DBs
+ <% for (let db of it.dbs) { %> +
+ <%= "checked" %> <% } %>> + +
+ <% } %>
diff --git a/views/workshop/new_route_modal.eta b/views/workshop/new_route_modal.eta index bca4175..8df4860 100644 --- a/views/workshop/new_route_modal.eta +++ b/views/workshop/new_route_modal.eta @@ -8,9 +8,9 @@
-
+
- +
<%= it.structure.route_prefix %>/
verb diff --git a/views/workshop/settings.eta b/views/workshop/settings.eta index d2bfcab..c0e2672 100644 --- a/views/workshop/settings.eta +++ b/views/workshop/settings.eta @@ -20,7 +20,7 @@
- + ">
diff --git a/views/workshop/sidebar.eta b/views/workshop/sidebar.eta index 2c10444..6ce763e 100644 --- a/views/workshop/sidebar.eta +++ b/views/workshop/sidebar.eta @@ -6,8 +6,8 @@ <% it.routes.map(route => { %>
  • - <%= route.verb %> - - <%= it.structure.route_prefix %><%= route.path %> + <%= route.verb %> - + <% if (it.structure.route_prefix) { %><%= it.structure.route_prefix %><% } %><%= route.path %>
  • <% }) %> @@ -46,6 +46,7 @@
  • <%= db.alias %> + <% if (db.is_aliased) { %>(alias)<% } %>
  • <% }) %>