Skip to content

Commit

Permalink
feat: edge
Browse files Browse the repository at this point in the history
  • Loading branch information
alswl committed May 3, 2024
1 parent 91da786 commit 5a1a716
Showing 1 changed file with 59 additions and 65 deletions.
124 changes: 59 additions & 65 deletions src/parser/parser.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import Database from '@dbml/core/types/model_structure/database';
import Field from '@dbml/core/types/model_structure/field';
import Ref from '@dbml/core/types/model_structure/ref';
import Table from '@dbml/core/types/model_structure/table';

function parseFieldToPort(field: Field): any {
return {
id: `${field.table}-${field.name}`,
function parseFieldToPort(
field: Field,
schemaName: string,
tableName: string,
): any {
const port = {
id: `${schemaName}-${tableName}-${field.name}`,
group: 'list',
attrs: {
portNameLabel: {
Expand All @@ -15,17 +20,18 @@ function parseFieldToPort(field: Field): any {
},
},
};
return port;
}

function parseTableToNode(table: Table, dbID: string): any {
function parseTableToNode(table: Table, schemaName: string): any {
let fields: any[] = [];
for (let k = 0; k < table.fields.length; k++) {
const f = table.fields[k];
const field = parseFieldToPort(f);
const field = parseFieldToPort(f, schemaName, table.name);
fields.push(field);
}
return {
id: `${dbID}-${table.name}`,
id: `${schemaName}-${table.name}`,
shape: 'er-rect',
label: table.name,
width: 150,
Expand All @@ -34,76 +40,64 @@ function parseTableToNode(table: Table, dbID: string): any {
};
}

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',
},
},
},
],
function parseRef(ref: Ref): any {
if (ref.endpoints.length !== 2) {
console.warn('ref.endpoints.length !== 2');
return null;
}
const source = ref.endpoints[0];
const target = ref.endpoints[1];

const sourceFieldName = source.fieldNames[0];
const targetFieldName = target.fieldNames[0];
const sSchemaName = source.schemaName || 'public';
const tSchemaName = target.schemaName || 'public';

const edge = {
id: ``,
shape: 'edge',
source: {
cell: `${sSchemaName}-${source.tableName}`,
port: `${sSchemaName}-${source.tableName}-${sourceFieldName}`,
},
target: {
cell: `${tSchemaName}-${target.tableName}`,
port: `${tSchemaName}-${target.tableName}-${targetFieldName}`,
},
// text: `${sourceFieldName} -> ${targetFieldName}`,
};
return edge;
}

function parseDatabaseToER(database: Database): any {
// parse nodes
let nodes: any[] = [];
for (let i = 0; i < database.schemas.length; i++) {
const schema = database.schemas[i];
for (let j = 0; j < database.schemas[i].tables.length; j++) {
const table = database.schemas[i].tables[j];
// handle fields
const node = parseTableToNode(table, `${i}`);
const node = parseTableToNode(table, schema.name);
nodes.push(node);
}
}

return { nodes: nodes, edges: [] };
// passe edges
let edges: any[] = [];
for (let i = 0; i < database.schemas.length; i++) {
const schema = database.schemas[i];
for (let j = 0; j < database.schemas[i].refs.length; j++) {
const ref = database.schemas[i].refs[j];
const edge = parseRef(ref);
if (edge === null) {
continue;
}
console.log(edge);
edges.push(edge);
}
}

return { nodes: nodes, edges: edges };
}

export default parseDatabaseToER;

0 comments on commit 5a1a716

Please sign in to comment.