Sync now includes tags

This commit is contained in:
Peaceultime 2024-09-04 22:39:20 +02:00
parent 85c8fbae0f
commit 094f27d9ad
3 changed files with 50 additions and 33 deletions

BIN
db.sqlite

Binary file not shown.

View File

@ -20,8 +20,6 @@ export default defineCachedEventHandler(async (e) => {
const where = ["project = $project", "tag = $tag"];
const criteria: Record<string, any> = { $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;
@ -30,7 +28,6 @@ export default defineCachedEventHandler(async (e) => {
{
return content;
}
}
setResponseStatus(e, 404);
}, {

View File

@ -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<string, FileType> = {
".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,24 +97,33 @@ 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 };
}
catch(e)