Skip to content

Commit

Permalink
feat: dbml parse to er works
Browse files Browse the repository at this point in the history
  • Loading branch information
alswl committed May 2, 2024
1 parent 16ee195 commit b3e7e0b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { debounce } from 'lodash-es';
import { useEffect, useRef, useState } from 'react';
import MonacoEditor from 'react-monaco-editor';

import parseDatabaseToER from '@/parser/parser';
import './index.less';

const CustomComponent = ({ node }: { node: Node }) => {
Expand Down Expand Up @@ -278,6 +279,7 @@ export default () => {
email varchar
}`;
const [code, setCode] = useState(initCode);
const [models, setModels] = useState(data);
const containerRef = useRef(null);
const parser = new Parser();

Expand All @@ -291,10 +293,10 @@ export default () => {
},
});

graph.fromJSON(data);
graph.fromJSON(models);
graph.centerContent();
}
}, []);
}, [models]);

// editorDidMount
const editorDidMount = (editor: any, monaco: any) => {
Expand All @@ -305,6 +307,9 @@ export default () => {
const onChange = (newValue: any, e: any) => {
const database = parser.parse(newValue, 'dbmlv2');
console.log(database);
let models = parseDatabaseToER(database);
console.log(models);
setModels(models);
};
const debouncedOnChange = debounce(onChange, 500);

Expand Down
80 changes: 80 additions & 0 deletions src/parser/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import Database from '@dbml/core/types/model_structure/database';

function parseDatabaseToER(database: Database): any {
const a = {
id: '1',
shape: 'er-rect',
label: '学生',
width: 150,
height: 24,
ports: [
{
id: '1-1',
group: 'list',
attrs: {
portNameLabel: {
text: 'ID',
},
portTypeLabel: {
text: 'STRING',
},
},
},
{
id: '1-2',
group: 'list',
attrs: {
portNameLabel: {
text: 'Name',
},
portTypeLabel: {
text: 'STRING',
},
},
},
{
id: '1-3',
group: 'list',
attrs: {
portNameLabel: {
text: 'Class',
},
portTypeLabel: {
text: 'NUMBER',
},
},
},
{
id: '1-4',
group: 'list',
attrs: {
portNameLabel: {
text: 'Gender',
},
portTypeLabel: {
text: 'BOOLEAN',
},
},
},
],
};

let models: any[] = [];
for (let i = 0; i < database.schemas.length; i++) {
for (let j = 0; j < database.schemas[i].tables.length; j++) {
const table = database.schemas[i].tables[j];
const model = {
id: '1',
shape: 'er-rect',
label: table.name,
width: 150,
height: 24,
ports: [],
};
models.push(model);
}
}
return models;
}

export default parseDatabaseToER;

0 comments on commit b3e7e0b

Please sign in to comment.