mirror of
https://github.com/drawdb-io/drawdb.git
synced 2025-09-19 05:14:51 +00:00
poc
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import Dexie from "dexie";
|
||||
import 'dexie-observable';
|
||||
import { templateSeeds } from "./seeds";
|
||||
import { diagramToDrawDbFile } from "../utils/parser"
|
||||
|
||||
export const db = new Dexie("drawDB");
|
||||
|
||||
db.version(6).stores({
|
||||
db.version(7).stores({
|
||||
diagrams: "++id, lastModified, loadedFromGistId",
|
||||
templates: "++id, custom",
|
||||
});
|
||||
@@ -11,3 +13,41 @@ db.version(6).stores({
|
||||
db.on("populate", (transaction) => {
|
||||
transaction.templates.bulkAdd(templateSeeds).catch((e) => console.log(e));
|
||||
});
|
||||
|
||||
|
||||
const debounce = (func, delay) => {
|
||||
let timer;
|
||||
return function(...args) {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(() => {
|
||||
func.apply(this, args);
|
||||
}, delay);
|
||||
};
|
||||
}
|
||||
|
||||
const debouncedChangesHandler = debounce(async (changes) => {
|
||||
handleDiagramChanges(changes.filter(c => c.table === "diagrams"))
|
||||
}, 1500);
|
||||
|
||||
const handleDiagramChanges = (diagramChanges) => {
|
||||
console.log("diagramChanges", diagramChanges);
|
||||
|
||||
// Handle create / update / delete separately
|
||||
|
||||
// Parse changes to ddb file format
|
||||
const ddbFiles = diagramChanges.map(d => diagramToDrawDbFile(d.obj));
|
||||
|
||||
// Write files to usercode
|
||||
ddbFiles.forEach(ddbFile => {
|
||||
fetch('/api/usercode-files', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ filename: `${ddbFile.title}.ddb`, content: ddbFile })
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
db.on('changes', debouncedChangesHandler)
|
||||
|
28
src/utils/parser.js
Normal file
28
src/utils/parser.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import { databases } from "../data/databases";
|
||||
|
||||
export function diagramToDrawDbFile(diagram) {
|
||||
const {
|
||||
name,
|
||||
lastModified,
|
||||
tables,
|
||||
references,
|
||||
notes,
|
||||
areas,
|
||||
database,
|
||||
types,
|
||||
enums
|
||||
} = diagram;
|
||||
|
||||
return {
|
||||
author: "Unnamed",
|
||||
title: name,
|
||||
date: lastModified.toISOString(),
|
||||
tables: tables,
|
||||
relationships: references,
|
||||
notes: notes,
|
||||
subjectAreas: areas,
|
||||
database: database,
|
||||
...(databases[database].hasTypes && { types: types }),
|
||||
...(databases[database].hasEnums && { enums: enums }),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user