From 094f27d9ad8667ede76137b059d43709d2185173 Mon Sep 17 00:00:00 2001 From: Peaceultime Date: Wed, 4 Sep 2024 22:39:20 +0200 Subject: [PATCH] Sync now includes tags --- db.sqlite | Bin 839680 -> 839680 bytes .../api/project/[projectId]/tags/[tag].get.ts | 15 ++-- server/tasks/sync.ts | 68 +++++++++++------- 3 files changed, 50 insertions(+), 33 deletions(-) diff --git a/db.sqlite b/db.sqlite index 7f7119b4e0d02e0f80070a00f2aa4f47e38834aa..d893593c2758168ef6be4fe598e03f84bc69243c 100644 GIT binary patch delta 3795 zcma)<4R9O96@Ycp?n$fD-N}+=*;W$C|8`G_Vg+rdE#&%C<`pB}yU4e#1`R7>`>kUUl z&sw2Rh=+a6{y>Ajh1z#S_{R##Lm9h~ylhjGU+fi|{a!)r3woEdwe)&>8(T$hbFa|c z)+U6S#J=YA-OILt!?y(1|8jXxDqG7byYD$Zix}%1EMbyi}vFW-i6B?lFJEEP0Fo^ z2ZU%?=#7YYKW;fzgMZI|V_#l?Oxj?caYpLM2I+8|mqkyPx%1B3C{a5!=N zVCKM^a0yi?SqmVlN~xow!ca0UMsVeuaMY68Y=y`%*(Q(6oO}bWql%>#9wKjQP4N3d zykAWC;-dP7Po9Rg_9 z#b)xXVG}y;GWQVo2QJN>;I?s3b7S0x+(xd8Tguh5N7x6s1su)K<7xI7yPHk$r}^jj zKK?2GMt%=p$1mll`8(JP{CV~Z_9Xui{~3FhWbAx)v=|^i6*_(}Aq)%AA)#ON3B8F} zBw14cP}LQR>Oz1PQCFE&u>j4_3oFe4IjK;1I2^6u0V<;g#dso|7$P&5GXRxP$!K_6 zQY^CqRH2^lC|NTc7d;koX4HVtFRR^7fSRb@STYzPapTM9!vbY3O+H_7kVGp9VUm0# zFXmk#ku$=jEUMa+s_x6FN^)Y07#t#(Ny#V&kegC+MNZ&i8$dN`S4&){x}P|x<$)M?y)$S-AtqTHFE>nVX8AeMsI-!z+Zq*|402Az5V}O^*SB(o&Ryw z6IJWns~(f9em(y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yIjaK?l!cF+!HwK1djgIM!HWna`3#aWpe{GcL(?Ta6T$-FTR{3w?n zm;brvod4l)tz5g6YtMRI#|1(7f>Xm8sWEA|+uzw5+dbN{Co&vq+1=3+&%y1St6PzSx&VIYp zj^^#FrHif;T-S2w-McWeo~~I!IbsyhEo?YMx-8ITdX10$E`?=^e7pY7v|0gKcj%cQ z$q*B9L=I-eB)3m5WR2A#kqkFJFVIi58?vQN)a11ld|n%`9NT4#>**&`b&A{PL$4jR zy`%|ISiephfPGDD+a=ZAr>PCmiTGqJKG~oS9DI7fd^Erb_4hZ!+7BdTzvDb+pL0?; z_am9+q6Xglj%>Dz@*$`8fEva-{?i*u*NLX5pSf!|RxxBexFRy^Tk@NT>fnV9a!3?D zf&2#f2=7n1ZHl(Rg`ardhu@Rmktn)t19>WuMro@ch`-oPK@TDfHOk z8e%sq_pX-7bQXQiW)f1I>Kakq$Tt~#X>tnwn3Yb6@}FRNwMMpRNl(a{O4KdrpVuXv zk@`^7rtkN&U`bl*X%y};r*w52XA4i-E;v-7k~zSM9*Mj-HZn04XKjXj6QLMusFi{XI01I2Sa$HQX7DM5&dd4`Ln0+ z4JcvdA@E1_Yt@*ssHn_4qIdl3Or44fvyJM@U=8v%++;8K<9aL958?UhaQbulWeD$5 z9)^`SFonS@SciSj;HyM8e@5Vh! diff --git a/server/api/project/[projectId]/tags/[tag].get.ts b/server/api/project/[projectId]/tags/[tag].get.ts index b1bcb54..d920d59 100644 --- a/server/api/project/[projectId]/tags/[tag].get.ts +++ b/server/api/project/[projectId]/tags/[tag].get.ts @@ -20,16 +20,13 @@ export default defineCachedEventHandler(async (e) => { const where = ["project = $project", "tag = $tag"]; const criteria: Record = { $project: project, $tag: tag }; - if(where.length > 1) + const db = useDatabase(); + + const content = db.query(`SELECT * FROM explorer_tags WHERE ${where.join(" and ")}`).get(criteria) as Tag; + + if(content !== undefined) { - const db = useDatabase(); - - const content = db.query(`SELECT * FROM explorer_tags WHERE ${where.join(" and ")}`).get(criteria) as Tag; - - if(content !== undefined) - { - return content; - } + return content; } setResponseStatus(e, 404); diff --git a/server/tasks/sync.ts b/server/tasks/sync.ts index 5a998e5..67ba67e 100644 --- a/server/tasks/sync.ts +++ b/server/tasks/sync.ts @@ -1,13 +1,13 @@ import useDatabase from "~/composables/useDatabase"; import { extname, basename } from 'node:path'; -import type { File, FileType } from '~/types/api'; +import type { File, FileType, Tag } from '~/types/api'; import { CanvasColor, CanvasContent } from "~/types/canvas"; import useMarkdown from "~/composables/useMarkdown"; const typeMapping: Record = { ".md": "Markdown", ".canvas": "Canvas" -} +}; export default defineTask({ meta: { @@ -57,25 +57,36 @@ export default defineTask({ } })); - /*let tags = []; + let tags: Tag[] = []; const tagFile = files.find(e => e.path === "tags"); if(tagFile) { - useMarkdown()(tagFile.content); - tags.push() - }*/ + const parsed = useMarkdown()(tagFile.content); + const titles = parsed.children.filter(e => e.type === 'element' && e.tagName.match(/h\d/)); + for(let i = 0; i < titles.length; i++) + { + const start = titles[i].position?.start.offset ?? 0; + const end = titles.length === i + 1 ? tagFile.content.length : titles[i + 1].position.start.offset - 1; + tags.push({ tag: titles[i].properties.id, description: tagFile.content.substring(titles[i].position?.end.offset + 1, end) }); + } + } const db = useDatabase(); const oldFiles = db.prepare(`SELECT * FROM explorer_files WHERE project = ?1`).all('1') as File[]; - const remove = db.prepare(`DELETE FROM explorer_files WHERE project = ?1 AND path = ?2`); - db.transaction(data => data.forEach(e => remove.run('1', e.path)))(oldFiles.filter(e => !files.find(f => f.path = e.path))); - remove.finalize(); + const removeFiles = db.prepare(`DELETE FROM explorer_files WHERE project = ?1 AND path = ?2`); + db.transaction((data: File[]) => data.forEach(e => removeFiles.run('1', e.path)))(oldFiles.filter(e => !files.find(f => f.path = e.path))); + removeFiles.finalize(); - const insert = db.prepare(`INSERT INTO explorer_files("project", "path", "owner", "title", "order", "type", "content") VALUES (1, $path, 1, $title, $order, $type, $content)`); - const update = db.prepare(`UPDATE explorer_files SET content = $content WHERE project = 1 AND path = $path`); - const transaction = db.transaction((content) => { + const oldTags = db.prepare(`SELECT * FROM explorer_tags WHERE project = ?1`).all('1') as Tag[]; + const removeTags = db.prepare(`DELETE FROM explorer_tags WHERE project = ?1 AND tag = ?2`); + db.transaction((data: Tag[]) => data.forEach(e => removeTags.run('1', e.tag)))(oldTags.filter(e => !tags.find(f => f.tag = e.tag))); + removeTags.finalize(); + + const insertFiles = db.prepare(`INSERT INTO explorer_files("project", "path", "owner", "title", "order", "type", "content") VALUES (1, $path, 1, $title, $order, $type, $content)`); + const updateFiles = db.prepare(`UPDATE explorer_files SET content = $content WHERE project = 1 AND path = $path`); + db.transaction((content) => { for (const item of content) { let order = item.order; @@ -86,23 +97,32 @@ export default defineTask({ order = 999; if(oldFiles.find(e => item.path === e.path)) - { - console.log(`Updating ${item.title} (${item.path})`); - update.run({ $path: item.path, $content: item.content }); - } + updateFiles.run({ $path: item.path, $content: item.content }); else - { - /* console.log(`Inserting ${item.title}`) */ - insert.run({ $path: item.path, $title: item.title, $type: item.type, $content: item.content, $order: order }); - } + insertFiles.run({ $path: item.path, $title: item.title, $type: item.type, $content: item.content, $order: order }); } - }); - transaction(files); + })(files); - insert.finalize(); - update.finalize(); + insertFiles.finalize(); + updateFiles.finalize(); + + const insertTags = db.prepare(`INSERT INTO explorer_tags("project", "tag", "description") VALUES (1, $tag, $description)`); + const updateTags = db.prepare(`UPDATE explorer_tags SET description = $description WHERE project = 1 AND tag = $tag`); + db.transaction((content) => { + for (const item of content) { + if (oldTags.find(e => item.tag === e.tag)) + updateTags.run({ $tag: item.tag, $description: item.description }); + else + insertTags.run({ $tag: item.tag, $description: item.description }); + } + })(tags); + + insertTags.finalize(); + updateTags.finalize(); useStorage('cache').clear(); + + console.log("Sync finished"); return { result: true }; }