diff --git a/snippets/db/dot_parser.js b/snippets/db/dot_parser.js
index 72ea502..32299a8 100644
--- a/snippets/db/dot_parser.js
+++ b/snippets/db/dot_parser.js
@@ -61,7 +61,7 @@ function get_table_info (str) {
columns.push (col);
}
- return { name, columns };
+ return { name, columns, foreign_keys: [] };
}
/**
@@ -80,32 +80,48 @@ async function get_tables (file) {
curr.push (l);
if ((/>.*?\]/u).test (l)) {
- tables.push (curr.join ('\n'));
+ const val = curr.join ('\n');
+ if (val)
+ tables.push (get_table_info (val));
curr.splice (0, curr.length);
}
- const fk = (/(?
\S+) -> (?[\S+)/u).exec (l);
- if (fk) {
- const col = fk.groups.col.split (':');
- const ref = fk.groups.ref.split (':');
+ get_foreign_key (l, foreign);
+ }
- const foreign_key = {
- table: col[0],
- column: col[1],
- ref_table: ref[0],
- ref_column: ref[1]
- };
-
- foreign.push (foreign_key);
+ for (const fk of foreign) {
+ for (let i = 0; i < tables.length; i++) {
+ if (tables[i].name === fk.table) {
+ tables[i].foreign_keys.push (fk);
+ break;
+ }
}
}
- return {
- tables: tables
- .filter ((val) => val)
- .map ((val) => get_table_info (val)),
- foreign_keys: foreign
- };
+ return tables;
+}
+
+/**
+ * gets foreign keys from a line
+ *
+ * @param {string} line line to check
+ * @param {Array]