π₯ π₯ π₯ A Free & Self-hostable Airtable Alternative
TypeScript airtableairtable-alternativeautomatic-apihacktoberfestlow-codeno-codeno-code-databaseno-code-platformpostgresqlrest-apirestful-apispreadsheetsqliteswagger
split webhook manager to ee fix add new column use add by id on update and delete
Fendy Heryantob5c010a0e781
diff --git a/packages/nocodb/src/services/columns.service.ts b/packages/nocodb/src/services/columns.service.tsindex aacb4597fc..50e51c42fd 100644--- a/packages/nocodb/src/services/columns.service.ts+++ b/packages/nocodb/src/services/columns.service.ts@@ -347,12 +347,13 @@ export class ColumnsService implements IColumnsService {const columnWebhookManager =param.columnWebhookManager ??(- await new ColumnWebhookManagerBuilder(context, ncMeta).withModelId(- column.fk_model_id,- )- )- .addColumn(column)- .forUpdate();+ await (+ await new ColumnWebhookManagerBuilder(context, ncMeta).withModelId(+ column.fk_model_id,+ )+ ).addColumnById(column.id)+ ).forUpdate();+// TODO: Refactor the columnUpdate function to handle metaOnly changes and// DB related changes, right now both are mixed up, making this fragileif (param.column.description !== column.description) {@@ -2944,12 +2945,12 @@ export class ColumnsService implements IColumnsService {const columnWebhookManager =param.columnWebhookManager ??(- await new ColumnWebhookManagerBuilder(context, ncMeta).withModelId(- column.fk_model_id,- )- )- .addColumn(column)- .forDelete();+ await (+ await new ColumnWebhookManagerBuilder(context, ncMeta).withModelId(+ column.fk_model_id,+ )+ ).addColumnById(column.id)+ ).forDelete();const sqlMgr = await reuseOrSave('sqlMgr', reuse, async () =>ProjectMgrv2.getSqlMgr(context, { id: source.base_id }, ncMeta),diff --git a/packages/nocodb/src/utils/column-webhook-manager.ts b/packages/nocodb/src/utils/column-webhook-manager.tsindex 098b2609c2..562aef2d54 100644--- a/packages/nocodb/src/utils/column-webhook-manager.ts+++ b/packages/nocodb/src/utils/column-webhook-manager.ts@@ -1,16 +1,11 @@-import {- objRemoveEmptyStringProps,- WebhookActions,- WebhookEvents,-} from 'nocodb-sdk';+import { WebhookActions } from 'nocodb-sdk';import type { NcContext } from 'nocodb-sdk';import type { IColumnsV3Service } from 'src/services/v3/columns-v3.types';import type { MetaService } from '~/meta/meta.service';import type { Model } from '~/models';import type { ModelWebhookManager } from '~/utils/model-webhook-manager';-import { NcError } from '~/helpers/ncError';import Noco from '~/Noco';-import { HANDLE_WEBHOOK } from '~/services/hook-handler.service';+import { NcError } from '~/helpers/ncError';export class ColumnWebhookManagerBuilder {constructor(@@ -134,231 +129,23 @@ export class ColumnWebhookManager {this.params.newColumns = this.params.newColumns ?? [];}- relatedColumnWebhookManager: Map<- string,- Map<WebhookActions, ColumnWebhookManager>- > = new Map();-protected emitted = false;- getOrCreateRelatedColumnWebhookManager(- tableId: string,- action: WebhookActions,- ) {- if (!this.relatedColumnWebhookManager.has(tableId)) {- this.relatedColumnWebhookManager.set(tableId, new Map());- }- const tableActions = this.relatedColumnWebhookManager.get(tableId);- if (!tableActions.has(action)) {- tableActions.set(- action,- new ColumnWebhookManager(this.context, {- action: action,- modelId: tableId,- oldColumns: [],- }),- );- }- return tableActions.get(action);- }-- async populateRelatedNewColumns() {- const promises: Promise<any | void>[] = [];- for (const [_action, actionMap] of this.relatedColumnWebhookManager) {- for (const [_tableId, relatedManager] of actionMap) {- promises.push(relatedManager.populateNewColumns());- }- }- return await Promise.all(promises);- }-async populateNewColumns() {- if (- this.params.action !== WebhookActions.UPDATE ||- !this.params.oldColumns.length- ) {- await this.populateRelatedNewColumns();- return;- }- // needed to prevent circular dependencies- const columnsV3Service: IColumnsV3Service =- Noco.nestApp.get('IColumnsV3Service');- const result = [];- for (const column of this.params.oldColumns) {- result.push(- await columnsV3Service.columnGet(- this.context,- { columnId: column.id },- this.ncMeta,- ),- );- }- this.params.newColumns = result;- await this.populateRelatedNewColumns();return this;}- async addOldColumnById(columnId: string, action?: WebhookActions) {- // if column id exists, ignore- if (this.params.oldColumns.find((col) => col.id === columnId)) {- return this;- }-- // needed to prevent circular dependencies- const columnsV3Service: IColumnsV3Service =- Noco.nestApp.get('IColumnsV3Service');- const newColumn = await columnsV3Service.columnGet(- this.context,- { columnId },- this.ncMeta,- );- await this.addOldColumn(newColumn, action);+ async addOldColumnById(_columnId: string, _action?: WebhookActions) {return this;}- async addOldColumn(column: any, action?: WebhookActions) {- // if column id exists, ignore- if (this.params.oldColumns.find((col) => col.id === column.id)) {- return this;- }- if (column.system) {- return this;- }-- if (- // if table id is different- column.table_id !== this.params.modelId ||- // or if the action is somehow different- (action && action !== this.params.action)- ) {- const relatedColumnWebhookManager =- this.getOrCreateRelatedColumnWebhookManager(column.table_id, action);- relatedColumnWebhookManager.addOldColumn(column, action);- } else {- this.params.oldColumns.push(column);- }+ async addOldColumn(_column: any, _action?: WebhookActions) {return this;}- async addNewColumnById(columnId: string, action?: WebhookActions) {- // only valid on create column event- if ((action ?? this.params.action) !== WebhookActions.INSERT) {- return;- }-- // if column id exists, ignore- if (this.params.newColumns.find((col) => col.id === columnId)) {- return this;- }-- // needed to prevent circular dependencies- const columnsV3Service: IColumnsV3Service =- Noco.nestApp.get('IColumnsV3Service');- const newColumn = await columnsV3Service.columnGet(- this.context,- { columnId },- this.ncMeta,- );- await this.addNewColumn(newColumn, action);+ async addNewColumnById(_columnId: string, _action?: WebhookActions) {return this;}- async addNewColumn(column: any, action?: WebhookActions) {- // only valid on create column event- if ((action ?? this.params.action) !== WebhookActions.INSERT) {- return;- }- if (column.system) {- return this;- }-- // if column id exists, ignore- if (this.params.newColumns.find((col) => col.id === column.id)) {- return this;- }-- if (- // if table id is different- column.table_id !== this.params.modelId ||- // or if the action is somehow different- (action && action !== this.params.action)- ) {- const relatedColumnWebhookManager =- this.getOrCreateRelatedColumnWebhookManager(column.table_id, action);- relatedColumnWebhookManager.addOldColumn(column, action);- } else {- this.params.oldColumns.push(column);- }+ async addNewColumn(_column: any, _action?: WebhookActions) {return this;}- isOldAndNewEqual(oldColumn, newColumn) {- const compareOld = {- ...oldColumn,- created_at: null,- updated_at: null,- };- const compareNew = {- ...newColumn,- created_at: null,- updated_at: null,- };- return (- JSON.stringify(objRemoveEmptyStringProps(compareOld)) ===- JSON.stringify(objRemoveEmptyStringProps(compareNew))- );- }-- getDiffCols(oldColumns: any[], newColumns: any[]) {- const resultOldColumns: any[] = [];- const resultNewColumns: any[] = [];- // designed for the old and new columns to have same index- for (let i = 0; i < oldColumns.length; i++) {- const oldColumn = oldColumns[i];- const newColumn = newColumns[i];- if (!this.isOldAndNewEqual(oldColumn, newColumn)) {- resultOldColumns.push(oldColumn);- resultNewColumns.push(newColumn);- }- }- return {- oldColumns: resultOldColumns,- newColumns: resultNewColumns,- };- }-- emitRelated() {- for (const [_action, actionMap] of this.relatedColumnWebhookManager) {- for (const [_tableId, relatedManager] of actionMap) {- relatedManager.emit();- }- }- }-- emit() {- // if modelWebhookManager exists, we do not emit- if (!this.emitted && !this.params.modelWebhookManager) {- // if no changes on the column, do not emit- const emitData = {- prevData: this.params.oldColumns,- newData: this.params.newColumns,- };- if (this.params.action === WebhookActions.UPDATE) {- const { newColumns, oldColumns } = this.getDiffCols(- this.params.oldColumns,- this.params.newColumns,- );- emitData.prevData = oldColumns;- emitData.newData = newColumns;- }- if (!emitData.prevData?.length && !emitData.newData?.length) {- return;- }-- Noco.eventEmitter.emit(HANDLE_WEBHOOK, {- context: this.context,- hookName: `${WebhookEvents.FIELD}.${this.params.action}`,- ...emitData,- user: this.context.user,- modelId: this.params.modelId,- });- this.emitted = true;- }- }+ emit() {}}