mirror of
https://github.com/chartdb/chartdb.git
synced 2025-11-05 06:23:17 +00:00
add init export script
This commit is contained in:
committed by
Jonathan Fishner
parent
985f126454
commit
15404fa113
51
src/lib/data/export-metadata/export-sql-script.ts
Normal file
51
src/lib/data/export-metadata/export-sql-script.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { Diagram } from "../domain/diagram";
|
||||
|
||||
export const exportSQL = (diagram: Diagram): string => {
|
||||
const { tables, relationships } = diagram;
|
||||
|
||||
if (!tables || tables.length === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Filter out the tables that are views
|
||||
const nonViewTables = tables.filter(table => !table.isView);
|
||||
|
||||
// Initialize the SQL script string
|
||||
let sqlScript = '';
|
||||
|
||||
// Loop through each non-view table to generate the SQL statements
|
||||
nonViewTables.forEach(table => {
|
||||
sqlScript += `CREATE TABLE ${table.name} (\n`;
|
||||
|
||||
table.fields.forEach((field, index) => {
|
||||
sqlScript += ` ${field.name} ${field.type}`;
|
||||
|
||||
if (field.primaryKey) {
|
||||
sqlScript += ' PRIMARY KEY';
|
||||
}
|
||||
|
||||
if (!field.nullable) {
|
||||
sqlScript += ' NOT NULL';
|
||||
}
|
||||
|
||||
// Add a comma after each field except the last one
|
||||
if (index < table.fields.length - 1) {
|
||||
sqlScript += ',\n';
|
||||
}
|
||||
});
|
||||
|
||||
sqlScript += '\n);\n\n';
|
||||
});
|
||||
|
||||
// Handle relationships (foreign keys) if needed
|
||||
relationships?.forEach(relationship => {
|
||||
const sourceTable = nonViewTables.find(table => table.id === relationship.sourceTableId);
|
||||
const targetTable = nonViewTables.find(table => table.id === relationship.targetTableId);
|
||||
|
||||
if (sourceTable && targetTable) {
|
||||
sqlScript += `ALTER TABLE ${sourceTable.name} ADD CONSTRAINT ${relationship.name} FOREIGN KEY (${relationship.sourceFieldId}) REFERENCES ${targetTable.name} (${relationship.targetFieldId});\n`;
|
||||
}
|
||||
});
|
||||
|
||||
return sqlScript;
|
||||
};
|
||||
Reference in New Issue
Block a user