π₯ π₯ π₯ A Free & Self-hostable Airtable Alternative
TypeScript airtableairtable-alternativeautomatic-apihacktoberfestlow-codeno-codeno-code-databaseno-code-platformpostgresqlrest-apirestful-apispreadsheetsqliteswagger
WIP
Fendy Heryantof5b4fe587247
diff --git a/packages/nocodb/src/modules/noco.module.ts b/packages/nocodb/src/modules/noco.module.tsindex c0f4335a51..71d1f193ae 100644--- a/packages/nocodb/src/modules/noco.module.ts+++ b/packages/nocodb/src/modules/noco.module.ts@@ -331,6 +331,10 @@ export const nocoModuleMetadata = {provide: 'IViewsV3Service',useClass: ViewsV3Service,},+ {+ provide: 'IColumnsV3Service',+ useClass: ColumnsV3Service,+ },DataAttachmentV3Service,// use custom provider to avoid circular dependencydiff --git a/packages/nocodb/src/services/v3/columns-v3.types.ts b/packages/nocodb/src/services/v3/columns-v3.types.tsnew file mode 100644index 0000000000..40ee542bee--- /dev/null+++ b/packages/nocodb/src/services/v3/columns-v3.types.ts@@ -0,0 +1,10 @@+import type { NcContext } from 'nocodb-sdk';+import type { MetaService } from '~/meta/meta.service';++export interface IColumnsV3Service {+ columnGet(+ context: NcContext,+ param: { columnId: string },+ ncMeta?: MetaService,+ ): Promise<any>;+}diff --git a/packages/nocodb/src/utils/field-webhook-manager.ts b/packages/nocodb/src/utils/field-webhook-manager.tsnew file mode 100644index 0000000000..258523faab--- /dev/null+++ b/packages/nocodb/src/utils/field-webhook-manager.ts@@ -0,0 +1,207 @@+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;+ }+ }+}