mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 09:36:01 +00:00
deployDB shell WIP
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { ColumnInfo, TableInfo } from 'dbgate-types';
|
||||
import { ColumnInfo, TableInfo, ForeignKeyInfo } from 'dbgate-types';
|
||||
import _ from 'lodash';
|
||||
import _cloneDeep from 'lodash/cloneDeep';
|
||||
|
||||
@@ -19,12 +19,10 @@ export interface TableInfoYaml {
|
||||
}
|
||||
|
||||
export interface ForeignKeyInfoYaml {
|
||||
deleteAction?: string;
|
||||
deleteAction?: string;
|
||||
}
|
||||
|
||||
function foreignKeyInfoToYaml() {
|
||||
|
||||
}
|
||||
// function foreignKeyInfoToYaml() {}
|
||||
|
||||
function columnInfoToYaml(column: ColumnInfo, table: TableInfo): ColumnInfoYaml {
|
||||
const res: ColumnInfoYaml = {
|
||||
@@ -55,6 +53,17 @@ function columnInfoToYaml(column: ColumnInfo, table: TableInfo): ColumnInfoYaml
|
||||
return res;
|
||||
}
|
||||
|
||||
function columnInfoFromYaml(column: ColumnInfoYaml, table: TableInfoYaml): ColumnInfo {
|
||||
const res: ColumnInfo = {
|
||||
pureName: table.name,
|
||||
columnName: column.name,
|
||||
dataType: column.type,
|
||||
autoIncrement: column.autoIncrement,
|
||||
notNull: column.notNull,
|
||||
};
|
||||
return res;
|
||||
}
|
||||
|
||||
export function tableInfoToYaml(table: TableInfo): TableInfoYaml {
|
||||
const tableCopy = _cloneDeep(table);
|
||||
const res: TableInfoYaml = {
|
||||
@@ -65,6 +74,40 @@ export function tableInfoToYaml(table: TableInfo): TableInfoYaml {
|
||||
if (tableCopy.primaryKey && !tableCopy.primaryKey['_dumped']) {
|
||||
res.primaryKey = tableCopy.primaryKey.columns.map(x => x.columnName);
|
||||
}
|
||||
const foreignKeys = (tableCopy.foreignKeys || []).filter(x => !x['_dumped']).map(foreignKeyInfoToYaml);
|
||||
// const foreignKeys = (tableCopy.foreignKeys || []).filter(x => !x['_dumped']).map(foreignKeyInfoToYaml);
|
||||
return res;
|
||||
}
|
||||
|
||||
function convertForeignKeyFromYaml(col: ColumnInfoYaml, table: TableInfoYaml, allTables: TableInfoYaml[]): ForeignKeyInfo {
|
||||
const refTable = allTables.find(x => x.name == col.references);
|
||||
if (!refTable || !refTable.primaryKey) return null;
|
||||
return {
|
||||
constraintType: 'foreignKey',
|
||||
pureName: table.name,
|
||||
refTableName: col.references,
|
||||
columns: [
|
||||
{
|
||||
columnName: col.name,
|
||||
refColumnName: refTable.primaryKey[0],
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
export function tableInfoFromYaml(table: TableInfoYaml, allTables: TableInfoYaml[]): TableInfo {
|
||||
const res: TableInfo = {
|
||||
pureName: table.name,
|
||||
columns: table.columns.map(c => columnInfoFromYaml(c, table)),
|
||||
foreignKeys: _.compact(
|
||||
table.columns.filter(x => x.references).map(col => convertForeignKeyFromYaml(col, table, allTables))
|
||||
),
|
||||
};
|
||||
if (table.primaryKey) {
|
||||
res.primaryKey = {
|
||||
pureName: table.name,
|
||||
constraintType: 'primaryKey',
|
||||
columns: table.primaryKey.map(columnName => ({ columnName })),
|
||||
};
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user