Browse Source

feat: text to select conversion

pull/6580/head
mertmit 1 year ago
parent
commit
f687b94695
  1. 39
      packages/nocodb/src/models/Column.ts
  2. 54
      packages/nocodb/src/services/columns.service.ts

39
packages/nocodb/src/models/Column.ts

@ -29,6 +29,19 @@ import {
import NocoCache from '~/cache/NocoCache';
import { stringifyMetaProp } from '~/utils/modelUtils';
const selectColors = [
'#cfdffe',
'#d0f1fd',
'#c2f5e8',
'#ffdaf6',
'#ffdce5',
'#fee2d5',
'#ffeab6',
'#d1f7c4',
'#ede2fe',
'#eeeeee',
];
export default class Column<T = any> implements ColumnType {
public fk_model_id: string;
public base_id: string;
@ -275,18 +288,6 @@ export default class Column<T = any> implements ColumnType {
}
case UITypes.MultiSelect: {
if (!column.colOptions?.options) {
const selectColors = [
'#cfdffe',
'#d0f1fd',
'#c2f5e8',
'#ffdaf6',
'#ffdce5',
'#fee2d5',
'#ffeab6',
'#d1f7c4',
'#ede2fe',
'#eeeeee',
];
const bulkOptions = [];
for (const [i, option] of column.dtxp?.split(',').entries() ||
[].entries()) {
@ -307,6 +308,7 @@ export default class Column<T = any> implements ColumnType {
option.title = option.title.trimEnd();
}
bulkOptions.push({
color: selectColors[i % selectColors.length], // in case color is not provided
...option,
fk_column_id: colId,
order: i + 1,
@ -319,18 +321,6 @@ export default class Column<T = any> implements ColumnType {
}
case UITypes.SingleSelect: {
if (!column.colOptions?.options) {
const selectColors = [
'#cfdffe',
'#d0f1fd',
'#c2f5e8',
'#ffdaf6',
'#ffdce5',
'#fee2d5',
'#ffeab6',
'#d1f7c4',
'#ede2fe',
'#eeeeee',
];
const bulkOptions = [];
for (const [i, option] of column.dtxp?.split(',').entries() ||
[].entries()) {
@ -351,6 +341,7 @@ export default class Column<T = any> implements ColumnType {
option.title = option.title.trimEnd();
}
bulkOptions.push({
color: selectColors[i % selectColors.length], // in case color is not provided
...option,
fk_column_id: colId,
order: i + 1,

54
packages/nocodb/src/services/columns.service.ts

@ -258,11 +258,11 @@ export class ColumnsService {
);
const driverType = dbDriver.clientType();
// MultiSelect to SingleSelect
if (
column.uidt === UITypes.MultiSelect &&
colBody.uidt === UITypes.SingleSelect
) {
// MultiSelect to SingleSelect
if (driverType === 'mysql' || driverType === 'mysql2') {
await dbDriver.raw(
`UPDATE ?? SET ?? = SUBSTRING_INDEX(??, ',', 1) WHERE ?? LIKE '%,%';`,
@ -302,6 +302,58 @@ export class ColumnsService {
],
);
}
} else if (
[UITypes.SingleLineText, UITypes.LongText].includes(column.uidt)
) {
// SingleLineText/LongText to SingleSelect/MultiSelect
const dbDriver = await reuseOrSave('dbDriver', reuse, async () =>
NcConnectionMgrv2.get(source),
);
const baseModel = await reuseOrSave('baseModel', reuse, async () =>
Model.getBaseModelSQL({
id: table.id,
dbDriver: dbDriver,
}),
);
const data = await baseModel.execAndParse(
dbDriver.raw('SELECT DISTINCT ?? FROM ??', [
column.column_name,
table.table_name,
]),
);
if (data.length) {
const existingOptions = colBody.colOptions.options.map(
(el) => el.title,
);
const options = data.reduce((acc, el) => {
if (el[column.column_name]) {
const values = el[column.column_name].split(',');
if (values.length > 1) {
if (colBody.uidt === UITypes.SingleSelect) {
NcError.badRequest(
'SingleSelect cannot have comma separated values, please use MultiSelect instead.',
);
}
}
for (const v of values) {
if (!existingOptions.includes(v.trim())) {
acc.push({
title: v.trim(),
});
existingOptions.push(v.trim());
}
}
}
return acc;
}, []);
colBody.colOptions.options = [
...colBody.colOptions.options,
...options,
];
}
}
// Handle migrations

Loading…
Cancel
Save