1
1
mirror of https://github.com/Fabio286/antares.git synced 2025-06-05 21:59:22 +02:00

refactor: moved table fields informations to vuex

This commit is contained in:
2020-08-14 18:07:29 +02:00
parent 6d0724dc90
commit 744728a14f
12 changed files with 193 additions and 33 deletions

View File

@@ -170,7 +170,7 @@ export default {
user: 'root',
password: '',
ask: false,
uid: uidGen()
uid: uidGen('C')
},
toast: {
status: '',

View File

@@ -42,6 +42,7 @@
v-for="tab of queryTabs"
v-show="selectedTab === tab.uid"
:key="tab.uid"
:tab-uid="tab.uid"
:connection="connection"
/>
</div>

View File

@@ -27,9 +27,10 @@
<div class="workspace-query-results column col-12">
<WorkspaceQueryTable
v-if="results"
v-show="!isQuering"
ref="queryTable"
:results="results"
:fields="fields"
:tab-uid="tabUid"
@updateField="updateField"
@deleteSelected="deleteSelected"
/>
@@ -53,7 +54,8 @@ export default {
},
mixins: [tableTabs],
props: {
connection: Object
connection: Object,
tabUid: String
},
data () {
return {
@@ -61,7 +63,7 @@ export default {
lastQuery: '',
isQuering: false,
results: {},
fields: []
selectedFields: []
};
},
computed: {
@@ -72,37 +74,39 @@ export default {
return this.getWorkspace(this.connection.uid);
},
table () {
if (this.results.fields.length)
if ('fields' in this.results && this.results.fields.length)
return this.results.fields[0].orgTable;
return '';
},
schema () {
if (this.results.fields.length)
if ('fields' in this.results && this.results.fields.length)
return this.results.fields[0].db;
return '';
return this.workspace.breadcrumbs.schema;
}
},
methods: {
...mapActions({
addNotification: 'notifications/addNotification'
addNotification: 'notifications/addNotification',
setTabFields: 'workspaces/setTabFields',
setTabKeyUsage: 'workspaces/setTabKeyUsage'
}),
async runQuery (query) {
if (!query) return;
this.isQuering = true;
this.results = {};
this.fields = [];
let selectedFields = [];
this.setTabFields({ cUid: this.connection.uid, tUid: this.tabUid, fields: [] });
try {
try { // Query Data
const params = {
uid: this.connection.uid,
schema: this.schema,
query
};
const { status, response } = await Connection.rawQuery(params);
if (status === 'success') {
this.results = response;
selectedFields = response.fields.map(field => field.orgName);
this.selectedFields = response.fields.map(field => field.orgName);
}
else
this.addNotification({ status: 'error', message: response });
@@ -111,7 +115,7 @@ export default {
this.addNotification({ status: 'error', message: err.stack });
}
try {
try { // Table data
const params = {
uid: this.connection.uid,
schema: this.schema,
@@ -119,8 +123,27 @@ export default {
};
const { status, response } = await Tables.getTableColumns(params);
if (status === 'success') {
const fields = response.filter(field => this.selectedFields.includes(field.name));
this.setTabFields({ cUid: this.connection.uid, tUid: this.tabUid, fields });
}
else
this.addNotification({ status: 'error', message: response });
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
try { // Key usage (foreign keys)
const params = {
uid: this.connection.uid,
schema: this.schema,
table: this.table
};
const { status, response } = await Tables.getKeyUsage(params);
if (status === 'success')
this.fields = response.filter(field => selectedFields.includes(field.name));
this.setTabKeyUsage({ cUid: this.connection.uid, tUid: this.tabUid, keyUsage: response });
else
this.addNotification({ status: 'error', message: response });
}

View File

@@ -53,6 +53,7 @@
:key="row._id"
:row="row"
:fields="fields"
:key-usage="keyUsage"
class="tr"
:class="{'selected': selectedRows.includes(row._id)}"
@selectRow="selectRow($event, row._id)"
@@ -70,7 +71,7 @@ import { uidGen } from 'common/libs/uidGen';
import BaseVirtualScroll from '@/components/BaseVirtualScroll';
import WorkspaceQueryTableRow from '@/components/WorkspaceQueryTableRow';
import TableContext from '@/components/WorkspaceQueryTableContext';
import { mapActions } from 'vuex';
import { mapActions, mapGetters } from 'vuex';
export default {
name: 'WorkspaceQueryTable',
@@ -81,7 +82,7 @@ export default {
},
props: {
results: Object,
fields: Array
tabUid: [String, Number]
},
data () {
return {
@@ -96,6 +97,9 @@ export default {
};
},
computed: {
...mapGetters({
getWorkspaceTab: 'workspaces/getWorkspaceTab'
}),
primaryField () {
return this.fields.filter(field => ['pri', 'uni'].includes(field.key))[0] || false;
},
@@ -114,6 +118,12 @@ export default {
else
return this.localResults;
},
fields () {
return this.getWorkspaceTab(this.tabUid) ? this.getWorkspaceTab(this.tabUid).fields : [];
},
keyUsage () {
return this.getWorkspaceTab(this.tabUid) ? this.getWorkspaceTab(this.tabUid).keyUsage : [];
},
scrollElement () {
return this.$refs.tableWrapper;
}

View File

@@ -178,7 +178,8 @@ export default {
},
props: {
row: Object,
fields: Array
fields: Array,
keyUsage: Array
},
data () {
return {
@@ -318,7 +319,7 @@ export default {
editOFF () {
this.isInlineEditor[this.editingField] = false;
let content;
if (!['blob', 'mediumblob', 'longblob'].includes(this.editingType)) {
if (!BLOB.includes(this.editingType)) {
if (this.editingContent === this.$options.filters.typeFormat(this.originalContent, this.editingType)) return;// If not changed
content = this.editingContent;
}

View File

@@ -33,9 +33,10 @@
<div class="workspace-query-results column col-12">
<WorkspaceQueryTable
v-if="results"
v-show="!isQuering"
ref="queryTable"
:results="results"
:fields="fields"
:tab-uid="tabUid"
@updateField="updateField"
@deleteSelected="deleteSelected"
/>
@@ -56,6 +57,7 @@ import WorkspaceQueryTable from '@/components/WorkspaceQueryTable';
import ModalNewTableRow from '@/components/ModalNewTableRow';
import { mapGetters, mapActions } from 'vuex';
import tableTabs from '@/mixins/tableTabs';
// import { TEXT, LONG_TEXT } from 'common/fieldTypes';
export default {
name: 'WorkspaceTableTab',
@@ -70,9 +72,11 @@ export default {
},
data () {
return {
tabUid: 1,
isQuering: false,
results: {},
fields: [],
keyUsage: [],
lastTable: null,
isAddModal: false
};
@@ -86,6 +90,13 @@ export default {
},
isSelected () {
return this.workspace.selected_tab === 1;
},
firstTextField () { // TODO: move inside new row modal and row components
if (this.fields.length) {
const textField = this.fields.find(field => [...TEXT, ...LONG_TEXT].includes(field.type));
return textField ? textField.name : '';
}
return '';
}
},
watch: {
@@ -107,12 +118,15 @@ export default {
},
methods: {
...mapActions({
addNotification: 'notifications/addNotification'
addNotification: 'notifications/addNotification',
setTabFields: 'workspaces/setTabFields',
setTabKeyUsage: 'workspaces/setTabKeyUsage'
}),
async getTableData () {
if (!this.table) return;
this.isQuering = true;
this.results = {};
this.setTabFields({ cUid: this.connection.uid, tUid: this.tabUid, fields: [] });
const params = {
uid: this.connection.uid,
@@ -120,10 +134,12 @@ export default {
table: this.workspace.breadcrumbs.table
};
try {
try { // Columns data
const { status, response } = await Tables.getTableColumns(params);
if (status === 'success')
this.fields = response;
if (status === 'success') {
this.fields = response;// Needed to add new rows
this.setTabFields({ cUid: this.connection.uid, tUid: this.tabUid, fields: response });
}
else
this.addNotification({ status: 'error', message: response });
}
@@ -131,7 +147,7 @@ export default {
this.addNotification({ status: 'error', message: err.stack });
}
try {
try { // Table data
const { status, response } = await Tables.getTableData(params);
if (status === 'success')
@@ -143,6 +159,19 @@ export default {
this.addNotification({ status: 'error', message: err.stack });
}
try { // Key usage (foreign keys)
const { status, response } = await Tables.getKeyUsage(params);
if (status === 'success') {
this.keyUsage = response;// Needed to add new rows
this.setTabKeyUsage({ cUid: this.connection.uid, tUid: this.tabUid, keyUsage: response });
}
else
this.addNotification({ status: 'error', message: response });
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
this.isQuering = false;
},
reloadTable () {