Browse Source

feat: Add option to update primary value column

Signed-off-by: Pranav C Balan <pranavxc@gmail.com>
pull/341/head
Pranav C Balan 3 years ago committed by Pranav C
parent
commit
47f1e4990c
  1. 47
      packages/nc-gui/components/project/spreadsheet/components/headerCell.vue
  2. 4
      packages/nc-gui/components/project/spreadsheet/mixins/spreadsheet.js
  3. 1
      packages/nocodb/package.json
  4. 52
      packages/nocodb/src/lib/sqlMgr/code/models/xc/ModelXcMetaMysql.ts

47
packages/nc-gui/components/project/spreadsheet/components/headerCell.vue

@ -2,8 +2,6 @@
<div class="d-flex align-center">
<v-icon v-if="column.pk" color="warning" x-small class="mr-1">mdi-key-variant</v-icon>
<v-icon v-else-if="uiDatatypeIcon" small class="mr-1">{{ uiDatatypeIcon }}</v-icon>
@ -31,12 +29,21 @@
<v-icon v-on="on" small v-if="!isVirtual">mdi-menu-down</v-icon>
</template>
<v-list dense>
<v-list-item @click="editColumnMenu = true">
<v-icon small class="mr-1">mdi-pencil</v-icon>
<v-list-item dense @click="editColumnMenu = true">
<x-icon small class="mr-1" color="primary">mdi-pencil</x-icon>
<span class="caption">Edit</span>
</v-list-item>
<v-list-item dense @click="setAsPrimaryValue">
<x-icon small class="mr-1" color="primary">mdi-key-star</x-icon>
<v-tooltip bottom>
<template v-slot:activator="{on}">
<span class="caption" v-on="on">Set as Primary value</span>
</template>
<span class="caption font-weight-bold">Primary value will be shown in place of primary key</span>
</v-tooltip>
</v-list-item>
<v-list-item @click="columnDeleteDialog = true">
<v-icon small class="mr-1">mdi-delete-outline</v-icon>
<x-icon small class="mr-1" color="error">mdi-delete-outline</x-icon>
<span class="caption">Delete</span>
</v-list-item>
</v-list>
@ -89,7 +96,7 @@ import EditColumn from "@/components/project/spreadsheet/editColumn/editColumn";
export default {
components: {EditColumn},
props: ['value', 'column', 'isForeignKey', 'meta', 'nodes', 'columnIndex', 'isForm', 'isPublicView','isVirtual'],
props: ['value', 'column', 'isForeignKey', 'meta', 'nodes', 'columnIndex', 'isForm', 'isPublicView', 'isVirtual'],
name: "headerCell",
mixins: [cell],
data: () => ({
@ -116,6 +123,34 @@ export default {
} catch (e) {
console.log(e)
}
}, async setAsPrimaryValue() {
try {
const meta = JSON.parse(JSON.stringify(this.meta));
for (const col of meta.columns) {
if (col.pv) {
delete col.pv;
}
if (col.cn === this.column.cn) {
col.pv = true;
}
}
await this.$store.dispatch('sqlMgr/ActSqlOp', [{
env: this.nodes.env,
dbAlias: this.nodes.dbAlias
}, 'xcModelSet', {
tn: this.nodes.tn,
meta
}]);
this.$toast.success('Successfully updated as primary column').goAway(3000);
} catch (e) {
console.log(e)
this.$toast.error('Failed to update primary column').goAway(3000);
}
this.$emit('saved');
this.columnDeleteDialog = false;
}
}
}

4
packages/nc-gui/components/project/spreadsheet/mixins/spreadsheet.js

@ -146,8 +146,8 @@ export default {
return this.nodes.tn || this.nodes.view_name
},
primaryValueColumn() {
if (!this.meta || !this.availableColumns) return '';
return this.availableColumns.length ? this.availableColumns[0]._cn : '';
if (!this.meta || !this.availableColumns || !this.availableColumns.length) return '';
return (this.availableColumns.find(col => col.pv) || {_cn: ''})._cn;
},
},
watch: {

1
packages/nocodb/package.json

@ -125,7 +125,6 @@
"handlebars": "^4.7.6",
"import-fresh": "^3.2.1",
"inflection": "^1.12.0",
"is-docker": "^2.2.1",
"js-beautify": "^1.11.0",
"json2csv": "^5.0.6",
"jsonfile": "^6.1.0",

52
packages/nocodb/src/lib/sqlMgr/code/models/xc/ModelXcMetaMysql.ts

@ -20,7 +20,7 @@ class ModelXcMetaMysql extends BaseRender {
*/
prepare() {
const data:any = {};
const data: any = {};
/* example of simple variable */
data.tn = this.ctx.tn;
@ -181,7 +181,53 @@ class ModelXcMetaMysql extends BaseRender {
columnsArr.push(columnObj)
}
// pk can be at the end
//
/*
if PK is at the end of table
if (there is a column for PV)
make that PV
else
lets think
else if (pk is not at the end of table)
if (there is a column for PV)
make that PV
else
lets think
else if ( no pk at all)
let's think
*/
if (!columnsArr.some(column => column.pv)) {
let len = columnsArr.length;
let pkIndex = -1;
while (len--) {
if (columnsArr[len].pk) {
pkIndex = len;
break;
}
}
// if PK is at the end of table
if (pkIndex === columnsArr.length - 1) {
if (pkIndex > 0) {
columnsArr[pkIndex - 1].pv = true;
}
}
// pk is not at the end of table
else if (pkIndex > -1) {
columnsArr[pkIndex + 1].pv = true;
}
// no pk at all
else {
// todo:
}
}
return columnsArr;
}
@ -319,7 +365,7 @@ class ModelXcMetaMysql extends BaseRender {
}
_getUIDataType(col):any {
_getUIDataType(col): any {
switch (this.getAbstractType(col)) {
case 'integer':
return 'Number';
@ -356,7 +402,7 @@ class ModelXcMetaMysql extends BaseRender {
}
getAbstractType(col):any {
getAbstractType(col): any {
switch ((col.dt || col.dt).toLowerCase()) {
case "int":
case "smallint":

Loading…
Cancel
Save