From a9e34339f5a895e03f6c5c4707a01e0f8484fd47 Mon Sep 17 00:00:00 2001 From: flisowna Date: Thu, 1 Dec 2022 21:00:34 +0300 Subject: [PATCH] WIP barcode column type --- .../en/setup-and-usages/column-types.md | 13 ++++ packages/nocodb-sdk/src/lib/UITypes.ts | 1 + .../{QrCodeRules.ts => QrAndBarcodeRules.ts} | 0 .../nocodb/src/lib/models/BarcodeColumn.ts | 67 +++++++++++++++++++ packages/nocodb/src/lib/models/Column.ts | 56 ++++++++++++++++ packages/nocodb/src/lib/utils/globals.ts | 2 + packages/nocodb/tests/unit/factory/column.ts | 30 +++++++++ 7 files changed, 169 insertions(+) rename packages/nocodb-sdk/src/lib/columnRules/{QrCodeRules.ts => QrAndBarcodeRules.ts} (100%) create mode 100644 packages/nocodb/src/lib/models/BarcodeColumn.ts diff --git a/packages/noco-docs/content/en/setup-and-usages/column-types.md b/packages/noco-docs/content/en/setup-and-usages/column-types.md index afeeaa55c6..97f44dd51d 100644 --- a/packages/noco-docs/content/en/setup-and-usages/column-types.md +++ b/packages/noco-docs/content/en/setup-and-usages/column-types.md @@ -33,6 +33,7 @@ menuTitle: 'Column Types' |[Rating](#rating)| Rating | |[Formula](#formula)| Formula based generated column | |[QR Code](#qr-code)| QR Code visualization of another referenced column | +|[Barcode](#barcode)| Barcode visualization of another referenced column | | [Count](#count) | | |[DateTime](#datetime)| Date & Time selector | |[CreateTime](#createtime)| | @@ -279,6 +280,18 @@ Encodes the value of a reference column as QR code. The following column types a * Email Since it's a virtual column, the cell content (QR code) cannot be changed directly. + +### Barcode + +Encodes the value of a reference column as Barcode. Supported barcode formats: CODE128, EAN, EAN-13, EAN-8, EAN-5, EAN-2, UPC (A), CODE39, ITF-14, MSI, Pharmacode, Codabar. The following column types are supported for the for reference column: +* Formula +* Single Line Text +* Long Text +* Phone Number +* URL +* Email + +Since it's a virtual column, the cell content (Barcode) cannot be changed directly. ### Count #### Available Database Types diff --git a/packages/nocodb-sdk/src/lib/UITypes.ts b/packages/nocodb-sdk/src/lib/UITypes.ts index e8d069bf81..efef013b65 100644 --- a/packages/nocodb-sdk/src/lib/UITypes.ts +++ b/packages/nocodb-sdk/src/lib/UITypes.ts @@ -52,6 +52,7 @@ export function isVirtualCol( UITypes.LinkToAnotherRecord, UITypes.Formula, UITypes.QrCode, + UITypes.Barcode, UITypes.Rollup, UITypes.Lookup, // UITypes.Count, diff --git a/packages/nocodb-sdk/src/lib/columnRules/QrCodeRules.ts b/packages/nocodb-sdk/src/lib/columnRules/QrAndBarcodeRules.ts similarity index 100% rename from packages/nocodb-sdk/src/lib/columnRules/QrCodeRules.ts rename to packages/nocodb-sdk/src/lib/columnRules/QrAndBarcodeRules.ts diff --git a/packages/nocodb/src/lib/models/BarcodeColumn.ts b/packages/nocodb/src/lib/models/BarcodeColumn.ts new file mode 100644 index 0000000000..83f90faaf2 --- /dev/null +++ b/packages/nocodb/src/lib/models/BarcodeColumn.ts @@ -0,0 +1,67 @@ +import Noco from '../Noco'; +import { CacheGetType, CacheScope, MetaTable } from '../utils/globals'; +import NocoCache from '../cache/NocoCache'; +import { extractProps } from '../meta/helpers/extractProps'; + +export default class BarcodeColumn { + fk_column_id: string; + fk_barcode_value_column_id: string; + + constructor(data: Partial) { + Object.assign(this, data); + } + + public static async insert( + data: Partial, + ncMeta = Noco.ncMeta + ) { + await ncMeta.metaInsert2(null, null, MetaTable.COL_BARCODE, { + fk_column_id: data.fk_column_id, + fk_barcode_value_column_id: data.fk_barcode_value_column_id, + }); + + return this.read(data.fk_column_id, ncMeta); + } + public static async read(columnId: string, ncMeta = Noco.ncMeta) { + let column = + columnId && + (await NocoCache.get( + `${CacheScope.COL_BARCODE}:${columnId}`, + CacheGetType.TYPE_OBJECT + )); + if (!column) { + column = await ncMeta.metaGet2( + null, //, + null, //model.db_alias, + MetaTable.COL_BARCODE, + { fk_column_id: columnId } + ); + await NocoCache.set(`${CacheScope.COL_BARCODE}:${columnId}`, column); + } + + return column ? new BarcodeColumn(column) : null; + } + + id: string; + + static async update( + id: string, + barcode: Partial, + ncMeta = Noco.ncMeta + ) { + const updateObj = extractProps(barcode, [ + 'fk_column_id', + 'fk_barcode_value_column_id', + ]); + // get existing cache + const key = `${CacheScope.COL_BARCODE}:${id}`; + let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT); + if (o) { + o = { ...o, ...updateObj }; + // set cache + await NocoCache.set(key, o); + } + // set meta + await ncMeta.metaUpdate(null, null, MetaTable.COL_BARCODE, updateObj, id); + } +} diff --git a/packages/nocodb/src/lib/models/Column.ts b/packages/nocodb/src/lib/models/Column.ts index b4dd1f74f3..8a3ae56593 100644 --- a/packages/nocodb/src/lib/models/Column.ts +++ b/packages/nocodb/src/lib/models/Column.ts @@ -24,6 +24,7 @@ import Filter from './Filter'; import addFormulaErrorIfMissingColumn from '../meta/helpers/addFormulaErrorIfMissingColumn'; import { NcError } from '../meta/helpers/catchError'; import QrCodeColumn from './QrCodeColumn'; +import BarcodeColumn from './BarcodeColumn'; export default class Column implements ColumnType { public fk_model_id: string; @@ -234,6 +235,16 @@ export default class Column implements ColumnType { ); break; } + case UITypes.Barcode: { + await BarcodeColumn.insert( + { + fk_column_id: colId, + fk_barcode_value_column_id: column.fk_barcode_value_column_id, + }, + ncMeta + ); + break; + } case UITypes.Formula: { await FormulaColumn.insert( { @@ -413,6 +424,9 @@ export default class Column implements ColumnType { case UITypes.QrCode: res = await QrCodeColumn.read(this.id, ncMeta); break; + case UITypes.Barcode: + res = await BarcodeColumn.read(this.id, ncMeta); + break; // default: // res = await DbColumn.read(this.id); // break; @@ -590,6 +604,20 @@ export default class Column implements ColumnType { } } + { + const barcodeCols = await ncMeta.metaList2( + null, + null, + MetaTable.COL_BARCODE, + { + condition: { fk_barcode_value_column_id: id }, + } + ); + for (const barcodeCol of barcodeCols) { + await Column.delete(barcodeCol.fk_column_id, ncMeta); + } + } + // get lookup columns and delete { let lookups = await NocoCache.getList(CacheScope.COL_LOOKUP, [id]); @@ -728,6 +756,10 @@ export default class Column implements ColumnType { colOptionTableName = MetaTable.COL_QRCODE; cacheScopeName = CacheScope.COL_QRCODE; break; + case UITypes.Barcode: + colOptionTableName = MetaTable.COL_BARCODE; + cacheScopeName = CacheScope.COL_BARCODE; + break; } if (colOptionTableName && cacheScopeName) { @@ -905,6 +937,19 @@ export default class Column implements ColumnType { break; } + case UITypes.Barcode: { + await ncMeta.metaDelete(null, null, MetaTable.COL_BARCODE, { + fk_column_id: colId, + }); + + await NocoCache.deepDel( + CacheScope.COL_BARCODE, + `${CacheScope.COL_BARCODE}:${colId}`, + CacheDelDirection.CHILD_TO_PARENT + ); + break; + } + case UITypes.MultiSelect: case UITypes.SingleSelect: { await ncMeta.metaDelete(null, null, MetaTable.COL_SELECT_OPTIONS, { @@ -963,9 +1008,20 @@ export default class Column implements ColumnType { condition: { fk_qr_value_column_id: colId }, } ); + const barcodeCols = await ncMeta.metaList2( + null, + null, + MetaTable.COL_BARCODE, + { + condition: { fk_barcode_value_column_id: colId }, + } + ); for (const qrCodeCol of qrCodeCols) { await Column.delete(qrCodeCol.fk_column_id, ncMeta); } + for (const barcodeCol of barcodeCols) { + await Column.delete(barcodeCol.fk_column_id, ncMeta); + } } // get existing cache diff --git a/packages/nocodb/src/lib/utils/globals.ts b/packages/nocodb/src/lib/utils/globals.ts index ea037b1f10..4acdfabec5 100644 --- a/packages/nocodb/src/lib/utils/globals.ts +++ b/packages/nocodb/src/lib/utils/globals.ts @@ -11,6 +11,7 @@ export enum MetaTable { COL_ROLLUP = 'nc_col_rollup_v2', COL_FORMULA = 'nc_col_formula_v2', COL_QRCODE = 'nc_col_qrcode_v2', + COL_BARCODE = 'nc_col_barcode_v2', FILTER_EXP = 'nc_filter_exp_v2', // HOOK_FILTER_EXP = 'nc_hook_filter_exp_v2', SORT = 'nc_sort_v2', @@ -114,6 +115,7 @@ export enum CacheScope { COL_ROLLUP = 'colRollup', COL_FORMULA = 'colFormula', COL_QRCODE = 'colQRCode', + COL_BARCODE = 'colBarcode', FILTER_EXP = 'filterExp', SORT = 'sort', SHARED_VIEW = 'sharedView', diff --git a/packages/nocodb/tests/unit/factory/column.ts b/packages/nocodb/tests/unit/factory/column.ts index c2843f070a..391d766867 100644 --- a/packages/nocodb/tests/unit/factory/column.ts +++ b/packages/nocodb/tests/unit/factory/column.ts @@ -184,6 +184,36 @@ const createQrCodeColumn = async ( return qrCodeColumn; }; +const createBarcodeColumn = async ( + context, + { + title, + table, + referencedBarcodeValueTableColumnTitle, + }: { + title: string; + table: Model; + referencedBarcodeValueTableColumnTitle: string; + } +) => { + const referencedBarcodeValueTableColumnId = await table + .getColumns() + .then( + (cols) => + cols.find( + (column) => column.title == referencedBarcodeValueTableColumnTitle + )['id'] + ); + + const barcodeColumn = await createColumn(context, table, { + title: title, + uidt: UITypes.Barcode, + column_name: title, + fk_barcode_value_column_id: referencedBarcodeValueTableColumnId, + }); + return barcodeColumn; +}; + const createLtarColumn = async ( context, {