Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

include table_id as response to column

Fendy Heryanto08b53a48c736
diff --git a/packages/nocodb/src/utils/api-v3-data-transformation.builder.ts b/packages/nocodb/src/utils/api-v3-data-transformation.builder.ts
index 456b8dc1e9..1a5be79eb7 100644
--- a/packages/nocodb/src/utils/api-v3-data-transformation.builder.ts
+++ b/packages/nocodb/src/utils/api-v3-data-transformation.builder.ts
@@ -361,11 +361,13 @@ export const columnBuilder = builderGenerator<Column | ColumnType, FieldV3Type>(
'description',
'meta',
'colOptions',
+ 'fk_model_id',
],
mappings: {
uidt: 'type',
cdf: 'default_value',
meta: 'options',
+ fk_model_id: 'table_id',
},
excludeNullProps: true,
meta: {
diff --git a/packages/nocodb/src/utils/column-webhook-manager.ts b/packages/nocodb/src/utils/column-webhook-manager.ts
new file mode 100644
index 0000000000..79366b8378
--- /dev/null
+++ b/packages/nocodb/src/utils/column-webhook-manager.ts
@@ -0,0 +1,335 @@
+import {
+ objRemoveEmptyStringProps,
+ WebhookActions,
+ WebhookEvents,
+} 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';
+
+export class ColumnWebhookManagerBuilder {
+ constructor(
+ protected readonly context: NcContext,
+ protected readonly ncMeta?: MetaService,
+ ) {}
+ modelWebhookManager?: ModelWebhookManager;
+ modelId?: string;
+ oldColumns: any[] = [];
+
+ withModelManager(modelWebhookManager: ModelWebhookManager) {
+ this.modelWebhookManager = modelWebhookManager;
+ return this;
+ }
+ withModel(model: Model) {
+ this.modelId = model.id;
+ return this;
+ }
+ async withModelId(modelId: string) {
+ this.modelId = modelId;
+ return this;
+ }
+ async addColumnById(columnId: string) {
+ // needed to prevent circular dependencies
+ const columnsV3Service: IColumnsV3Service =
+ Noco.nestApp.get('IColumnsV3Service');
+ this.oldColumns.push(
+ await columnsV3Service.columnGet(this.context, { columnId }, this.ncMeta),
+ );
+ return this;
+ }
+
+ forCreate() {
+ if (!this.modelId) {
+ NcError.get(this.context).internalServerError(
+ `Need to call 'withModel' before running 'forCreate'`,
+ );
+ }
+ return new ColumnWebhookManager(this.context, {
+ action: WebhookActions.INSERT,
+ modelId: this.modelId!,
+ modelWebhookManager: this.modelWebhookManager,
+ oldColumns: this.oldColumns,
+ });
+ }
+
+ forUpdate() {
+ if (!this.modelId) {
+ NcError.get(this.context).internalServerError(
+ `Need to call 'withModel' before running 'forUpdate'`,
+ );
+ }
+ if (!this.oldColumns.length) {
+ NcError.get(this.context).internalServerError(
+ `Need to call 'withView' before running 'forUpdate'`,
+ );
+ }
+ return new ColumnWebhookManager(this.context, {
+ action: WebhookActions.UPDATE,
+ modelId: this.modelId!,
+ oldColumns: this.oldColumns,
+ modelWebhookManager: this.modelWebhookManager,
+ });
+ }
+
+ forDelete() {
+ if (!this.modelId) {
+ NcError.get(this.context).internalServerError(
+ `Need to call 'withModel' before running 'forDelete'`,
+ );
+ }
+ if (!this.oldColumns.length) {
+ NcError.get(this.context).internalServerError(
+ `Need to call 'withView' before running 'forDelete'`,
+ );
+ }
+ return new ColumnWebhookManager(
+ this.context,
+ {
+ action: WebhookActions.DELETE,
+ modelId: this.modelId!,
+ oldColumns: this.oldColumns,
+ modelWebhookManager: this.modelWebhookManager,
+ },
+ this.ncMeta,
+ );
+ }
+}
+
+export class ColumnWebhookManager {
+ constructor(
+ protected readonly context: NcContext,
+ protected readonly params: {
+ action: WebhookActions;
+ modelId: string;
+ oldColumns: any[];
+ newColumns?: any[];
+ modelWebhookManager?: ModelWebhookManager;
+ },
+ protected readonly ncMeta?: MetaService,
+ ) {
+ 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);
+ 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 (
+ // 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);
+ }
+ 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);
+ return this;
+ }
+ async addNewColumn(column: any, 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 === 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);
+ }
+ 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;
+ }
+ 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;
+ }
+ }
+}
diff --git a/packages/nocodb/src/utils/field-webhook-manager.ts b/packages/nocodb/src/utils/field-webhook-manager.ts
deleted file mode 100644
index 258523faab..0000000000
--- a/packages/nocodb/src/utils/field-webhook-manager.ts
+++ /dev/null
@@ -1,207 +0,0 @@
-import {
- objRemoveEmptyStringProps,
- WebhookActions,
- WebhookEvents,
-} from 'nocodb-sdk';
-import type { IColumnsV3Service } from 'src/services/v3/columns-v3.types';
-import type { ModelWebhookManager } from '~/utils/model-webhook-manager';
-import type { NcContext, NcRequest } from 'nocodb-sdk';
-import type { IViewsV3Service } from '~/services/v3/views-v3.types';
-import type { MetaService } from '~/meta/meta.service';
-import Noco from '~/Noco';
-import { Column, Model } from '~/models';
-import { NcError } from '~/helpers/ncError';
-import { HANDLE_WEBHOOK } from '~/services/hook-handler.service';
-
-export class FieldWebhookManagerBuilder {
- constructor(
- protected readonly context: NcContext,
- protected readonly ncMeta?: MetaService,
- ) {}
- modelWebhookManager?: ModelWebhookManager;
- model?: Model;
- modelId?: string;
- oldColumns: any[] = [];
-
- withModelManager(modelWebhookManager: ModelWebhookManager) {
- this.modelWebhookManager = modelWebhookManager;
- return this;
- }
- withModel(model: Model) {
- this.model = model;
- this.modelId = model.id;
- return this;
- }
- async withModelId(modelId: string) {
- this.model = await Model.getByIdOrName(
- this.context,
- {
- id: modelId,
- },
- this.ncMeta,
- );
- this.modelId = modelId;
- return this;
- }
- async addColumnById(columnId: string) {
- // needed to prevent circular dependencies
- const columnsV3Service: IColumnsV3Service =
- Noco.nestApp.get('IColumnsV3Service');
- this.oldColumns.push(
- await columnsV3Service.columnGet(this.context, { columnId }, this.ncMeta),
- );
- return this;
- }
-
- forCreate() {
- if (!this.model) {
- NcError.get(this.context).internalServerError(
- `Need to call 'withModel' before running 'forCreate'`,
- );
- }
- return new FieldWebhookManager(this.context, {
- action: WebhookActions.INSERT,
- model: this.model!,
- modelId: this.modelId!,
- modelWebhookManager: this.modelWebhookManager,
- oldColumns: this.oldColumns,
- });
- }
-
- forUpdate() {
- if (!this.model) {
- NcError.get(this.context).internalServerError(
- `Need to call 'withModel' before running 'forUpdate'`,
- );
- }
- if (!this.oldColumns.length) {
- NcError.get(this.context).internalServerError(
- `Need to call 'withView' before running 'forUpdate'`,
- );
- }
- return new FieldWebhookManager(this.context, {
- action: WebhookActions.UPDATE,
- modelId: this.modelId!,
- oldColumns: this.oldColumns,
- modelWebhookManager: this.modelWebhookManager,
- });
- }
-
- forDelete() {
- if (!this.model) {
- NcError.get(this.context).internalServerError(
- `Need to call 'withModel' before running 'forDelete'`,
- );
- }
- if (!this.oldColumns.length) {
- NcError.get(this.context).internalServerError(
- `Need to call 'withView' before running 'forDelete'`,
- );
- }
- return new FieldWebhookManager(
- this.context,
- {
- action: WebhookActions.DELETE,
- modelId: this.modelId!,
- oldColumns: this.oldColumns,
- modelWebhookManager: this.modelWebhookManager,
- },
- this.ncMeta,
- );
- }
-}
-
-export class FieldWebhookManager {
- constructor(
- protected readonly context: NcContext,
- protected readonly params: {
- action: WebhookActions;
- modelId: string;
- oldColumns: any[];
- newColumns?: any[];
- modelWebhookManager?: ModelWebhookManager;
- },
- protected readonly ncMeta?: MetaService,
- ) {
- this.params.newColumns = this.params.newColumns ?? [];
- }
-
- relatedFieldWebhookManager: Map<string, FieldWebhookManager> = new Map();
-
- protected emitted = false;
-
- async populateNewColumns() {
- if (
- this.params.action !== WebhookActions.UPDATE ||
- !this.params.oldColumns.length
- ) {
- 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;
- return this;
- }
- async addNewColumnById(columnId: string) {
- // needed to prevent circular dependencies
- const columnsV3Service: IColumnsV3Service =
- Noco.nestApp.get('IColumnsV3Service');
- const newColumn = await columnsV3Service.columnGet(
- this.context,
- { columnId },
- this.ncMeta,
- );
- this.params.newColumns.push(newColumn);
- return this;
- }
-
- isOldAndNewEqual(oldView, newView) {
- const compareOld = {
- ...oldView,
- created_at: null,
- updated_at: null,
- };
- const compareNew = {
- ...newView,
- created_at: null,
- updated_at: null,
- };
- return (
- JSON.stringify(objRemoveEmptyStringProps(compareOld)) ===
- JSON.stringify(objRemoveEmptyStringProps(compareNew))
- );
- }
-
- emit() {
- // if modelWebhookManager exists, we do not emit
- if (!this.emitted && !this.params.modelWebhookManager) {
- // if no changes on the view, do not emit
- if (
- this.params.action === WebhookActions.UPDATE &&
- this.isOldAndNewEqual(this.params.oldView, this.params.newView)
- ) {
- return;
- }
- Noco.eventEmitter.emit(HANDLE_WEBHOOK, {
- context: this.context,
- hookName: `${WebhookEvents.FIELD}.${this.params.action}`,
- prevData: this.params.oldView,
- newData: this.params.newView,
- user: this.context.user,
- modelId: this.params.modelId,
- });
- this.emitted = true;
- }
- }
-}