Skip to content

Commit 6ed8cfa

Browse files
Merge pull request #1060 from rocket-admin/foreign-key-widget
Foreign key widget
2 parents b87f95c + 97790ef commit 6ed8cfa

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

frontend/src/app/components/dashboard/db-table-widgets/db-table-widgets.component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ export class DbTableWidgetsComponent implements OnInit {
142142
}
143143
`,
144144
URL: `// No settings required`,
145+
Foreign_key: `// Provide settings for foreign key widget
146+
{
147+
"column_name": "", // copy the name of the column you selected
148+
"referenced_column_name": "",
149+
"referenced_table_name": ""
150+
}
151+
`,
145152
}
146153

147154
constructor(

frontend/src/app/components/ui-components/row-fields/foreign-key/foreign-key.component.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { MatTooltipModule } from '@angular/material/tooltip';
1515
import { Subject } from 'rxjs';
1616
import { TablesService } from 'src/app/services/tables.service';
1717
import { RouterModule } from '@angular/router';
18+
import { TableForeignKey } from 'src/app/models/table';
1819

1920
interface Suggestion {
2021
displayString: string;
@@ -50,6 +51,7 @@ export class ForeignKeyRowComponent extends BaseRowFieldComponent {
5051
public identityColumn: string;
5152
public primaeyKeys: {data_type: string, column_name: string}[];
5253

54+
public fkRelations: TableForeignKey = null;
5355
autocmpleteUpdate = new Subject<string>();
5456

5557
constructor(
@@ -69,12 +71,22 @@ export class ForeignKeyRowComponent extends BaseRowFieldComponent {
6971
ngOnInit(): void {
7072
super.ngOnInit();
7173
this.connectionID = this._connections.currentConnectionID;
72-
this.relations && this._tables.fetchTable({
74+
75+
if (this.widgetStructure && this.widgetStructure.widget_params) {
76+
this.fkRelations = this.widgetStructure.widget_params as TableForeignKey;
77+
} else if (this.relations) {
78+
this.fkRelations = this.relations;
79+
}
80+
81+
console.log('test fkRelations');
82+
console.log(this.fkRelations);
83+
84+
this.fkRelations && this._tables.fetchTable({
7385
connectionID: this.connectionID,
74-
tableName: this.relations.referenced_table_name,
86+
tableName: this.fkRelations.referenced_table_name,
7587
requstedPage: 1,
7688
chunkSize: 10,
77-
foreignKeyRowName: this.relations.referenced_column_name,
89+
foreignKeyRowName: this.fkRelations.referenced_column_name,
7890
foreignKeyRowValue: this.value
7991
}).subscribe((res: any) => {
8092
if (res.rows.length) {
@@ -88,28 +100,28 @@ export class ForeignKeyRowComponent extends BaseRowFieldComponent {
88100
Object.values(modifiedRow).filter(value => value).join(' | ');
89101
console.log('test identityColumn');
90102
console.log(this.currentDisplayedString);
91-
this.currentFieldValue = res.rows[0][this.relations.referenced_column_name];
103+
this.currentFieldValue = res.rows[0][this.fkRelations.referenced_column_name];
92104
this.currentFieldQueryParams = Object.assign({}, ...res.primaryColumns.map((primaeyKey) => ({[primaeyKey.column_name]: res.rows[0][primaeyKey.column_name]})));
93105
this.onFieldChange.emit(this.currentFieldValue);
94106
}
95107
}
96108

97109
this._tables.fetchTable({
98110
connectionID: this.connectionID,
99-
tableName: this.relations.referenced_table_name,
111+
tableName: this.fkRelations.referenced_table_name,
100112
requstedPage: 1,
101113
chunkSize: 20,
102114
foreignKeyRowName: 'autocomplete',
103115
foreignKeyRowValue: '',
104-
referencedColumn: this.relations.referenced_column_name
116+
referencedColumn: this.fkRelations.referenced_column_name
105117
}).subscribe((res:any) => {
106118
this.identityColumn = res.identity_column;
107119
this.suggestions = res.rows.map(row => {
108120
const modifiedRow = this.getModifiedRow(row);
109121
return {
110122
displayString: this.identityColumn ? `${row[this.identityColumn]} (${Object.values(modifiedRow).filter(value => value).join(' | ')})` : Object.values(modifiedRow).filter(value => value).join(' | '),
111123
primaryKeys: Object.assign({}, ...res.primaryColumns.map((primaeyKey) => ({[primaeyKey.column_name]: row[primaeyKey.column_name]}))),
112-
fieldValue: row[this.relations.referenced_column_name]
124+
fieldValue: row[this.fkRelations.referenced_column_name]
113125
}
114126
});
115127
this.fetching = false;
@@ -127,12 +139,12 @@ export class ForeignKeyRowComponent extends BaseRowFieldComponent {
127139
this.fetching = true;
128140
this._tables.fetchTable({
129141
connectionID: this.connectionID,
130-
tableName: this.relations.referenced_table_name,
142+
tableName: this.fkRelations.referenced_table_name,
131143
requstedPage: 1,
132144
chunkSize: 20,
133145
foreignKeyRowName: 'autocomplete',
134146
foreignKeyRowValue: this.currentDisplayedString,
135-
referencedColumn: this.relations.referenced_column_name
147+
referencedColumn: this.fkRelations.referenced_column_name
136148
}).subscribe((res: any) => {
137149
this.identityColumn = res.identity_column;
138150
if (res.rows.length === 0) {
@@ -145,7 +157,7 @@ export class ForeignKeyRowComponent extends BaseRowFieldComponent {
145157
return {
146158
displayString: this.identityColumn ? `${row[this.identityColumn]} (${Object.values(modifiedRow).filter(value => value).join(' | ')})` : Object.values(modifiedRow).filter(value => value).join(' | '),
147159
primaryKeys: Object.assign({}, ...res.primaryColumns.map((primaeyKey) => ({[primaeyKey.column_name]: row[primaeyKey.column_name]}))),
148-
fieldValue: row[this.relations.referenced_column_name]
160+
fieldValue: row[this.fkRelations.referenced_column_name]
149161
}
150162
});
151163
}
@@ -156,9 +168,9 @@ export class ForeignKeyRowComponent extends BaseRowFieldComponent {
156168

157169
getModifiedRow(row) {
158170
let modifiedRow;
159-
if (this.relations.autocomplete_columns && this.relations.autocomplete_columns.length > 0) {
160-
let autocompleteColumns = [...this.relations.autocomplete_columns]
161-
if (this.identityColumn) autocompleteColumns.splice(this.relations.autocomplete_columns.indexOf(this.identityColumn), 1);
171+
if (this.fkRelations.autocomplete_columns && this.fkRelations.autocomplete_columns.length > 0) {
172+
let autocompleteColumns = [...this.fkRelations.autocomplete_columns]
173+
if (this.identityColumn) autocompleteColumns.splice(this.fkRelations.autocomplete_columns.indexOf(this.identityColumn), 1);
162174
modifiedRow = autocompleteColumns
163175
.reduce((rowObject, columnName)=> (rowObject[columnName]=row[columnName],rowObject),{});
164176
} else {

frontend/src/app/consts/field-types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export const UIwidgets = {
4141
File: FileRowComponent,
4242
Code: CodeRowComponent,
4343
Image: ImageRowComponent,
44-
URL: UrlRowComponent
44+
URL: UrlRowComponent,
45+
Foreign_key: ForeignKeyRowComponent,
4546
}
4647

4748
export const fieldTypes = {

0 commit comments

Comments
 (0)