-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add endpoints to create and delete remote server (#4606)
* Build remote server * Add getters * Migrate to json inputs * Use extendable type * Use regex validation * Remove acronymes --------- Co-authored-by: Thomas Trompette <[email protected]>
- Loading branch information
Showing
17 changed files
with
496 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...nty-server/src/database/typeorm/metadata/migrations/1711374137222-addRemoteServerTable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class AddRemoteServerTable1711374137222 implements MigrationInterface { | ||
name = 'AddRemoteServerTable1711374137222'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`CREATE TABLE "metadata"."remoteServer" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "foreignDataWrapperId" uuid NOT NULL DEFAULT uuid_generate_v4(), "foreignDataWrapperType" character varying, "foreignDataWrapperOptions" jsonb, "userMappingOptions" jsonb, "workspaceId" uuid NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_8e5d208498fa2c9710bb934023a" PRIMARY KEY ("id"))`, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP TABLE "metadata"."remoteServer"`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...ngine/api/graphql/workspace-query-builder/factories/foreign-data-wrapper-query.factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { | ||
ForeignDataWrapperOptions, | ||
RemoteServerType, | ||
UserMappingOptions, | ||
} from 'src/engine/metadata-modules/remote-server/remote-server.entity'; | ||
|
||
@Injectable() | ||
export class ForeignDataWrapperQueryFactory { | ||
createForeignDataWrapper( | ||
foreignDataWrapperId: string, | ||
foreignDataWrapperType: RemoteServerType, | ||
foreignDataWrapperOptions: ForeignDataWrapperOptions<RemoteServerType>, | ||
) { | ||
const [name, options] = this.buildNameAndOptionsFromType( | ||
foreignDataWrapperType, | ||
foreignDataWrapperOptions, | ||
); | ||
|
||
return `CREATE SERVER "${foreignDataWrapperId}" FOREIGN DATA WRAPPER ${name} OPTIONS (${options})`; | ||
} | ||
|
||
createUserMapping( | ||
foreignDataWrapperId: string, | ||
userMappingOptions: UserMappingOptions, | ||
) { | ||
return `CREATE USER MAPPING IF NOT EXISTS FOR ${userMappingOptions.username} SERVER "${foreignDataWrapperId}" OPTIONS (user '${userMappingOptions.username}', password '${userMappingOptions.password}')`; | ||
} | ||
|
||
private buildNameAndOptionsFromType( | ||
type: RemoteServerType, | ||
options: ForeignDataWrapperOptions<RemoteServerType>, | ||
) { | ||
switch (type) { | ||
case RemoteServerType.POSTGRES_FDW: | ||
return ['postgres_fdw', this.buildPostgresFDWQueryOptions(options)]; | ||
default: | ||
throw new Error('Foreign data wrapper type not supported'); | ||
} | ||
} | ||
|
||
private buildPostgresFDWQueryOptions( | ||
foreignDataWrapperOptions: ForeignDataWrapperOptions<RemoteServerType>, | ||
) { | ||
return `dbname '${foreignDataWrapperOptions.dbname}', host '${foreignDataWrapperOptions.host}', port '${foreignDataWrapperOptions.port}'`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...wenty-server/src/engine/metadata-modules/remote-server/dtos/create-remote-server.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Field, InputType } from '@nestjs/graphql'; | ||
|
||
import { IsOptional } from 'class-validator'; | ||
import GraphQLJSON from 'graphql-type-json'; | ||
|
||
import { | ||
ForeignDataWrapperOptions, | ||
RemoteServerType, | ||
UserMappingOptions, | ||
} from 'src/engine/metadata-modules/remote-server/remote-server.entity'; | ||
|
||
@InputType() | ||
export class CreateRemoteServerInput<T extends RemoteServerType> { | ||
@Field(() => String) | ||
foreignDataWrapperType: T; | ||
|
||
@IsOptional() | ||
@Field(() => GraphQLJSON) | ||
foreignDataWrapperOptions: ForeignDataWrapperOptions<T>; | ||
|
||
@IsOptional() | ||
@Field(() => GraphQLJSON, { nullable: true }) | ||
userMappingOptions?: UserMappingOptions; | ||
} |
9 changes: 9 additions & 0 deletions
9
...es/twenty-server/src/engine/metadata-modules/remote-server/dtos/remote-server-id.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { InputType, ID } from '@nestjs/graphql'; | ||
|
||
import { IDField } from '@ptc-org/nestjs-query-graphql'; | ||
|
||
@InputType() | ||
export class RemoteServerIdInput { | ||
@IDField(() => ID, { description: 'The id of the record.' }) | ||
id!: string; | ||
} |
12 changes: 12 additions & 0 deletions
12
.../twenty-server/src/engine/metadata-modules/remote-server/dtos/remote-server-type.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { InputType, Field } from '@nestjs/graphql'; | ||
|
||
import { IsString } from 'class-validator'; | ||
|
||
import { RemoteServerType } from 'src/engine/metadata-modules/remote-server/remote-server.entity'; | ||
|
||
@InputType() | ||
export class RemoteServerTypeInput<T extends RemoteServerType> { | ||
@Field(() => String) | ||
@IsString() | ||
foreignDataWrapperType!: T; | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/twenty-server/src/engine/metadata-modules/remote-server/dtos/remote-server.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { ObjectType, Field, HideField, ID } from '@nestjs/graphql'; | ||
|
||
import { IsOptional } from 'class-validator'; | ||
import GraphQLJSON from 'graphql-type-json'; | ||
|
||
import { | ||
ForeignDataWrapperOptions, | ||
RemoteServerType, | ||
} from 'src/engine/metadata-modules/remote-server/remote-server.entity'; | ||
|
||
@ObjectType('RemoteServer') | ||
export class RemoteServerDTO<T extends RemoteServerType> { | ||
@Field(() => ID) | ||
id: string; | ||
|
||
@Field(() => ID) | ||
foreignDataWrapperId: string; | ||
|
||
@Field(() => String) | ||
foreignDataWrapperType: T; | ||
|
||
@IsOptional() | ||
@Field(() => GraphQLJSON, { nullable: true }) | ||
foreignDataWrapperOptions?: ForeignDataWrapperOptions<T>; | ||
|
||
@HideField() | ||
workspaceId: string; | ||
|
||
@Field() | ||
createdAt: Date; | ||
|
||
@Field() | ||
updatedAt: Date; | ||
} |
59 changes: 59 additions & 0 deletions
59
packages/twenty-server/src/engine/metadata-modules/remote-server/remote-server.entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { ObjectType } from '@nestjs/graphql'; | ||
|
||
import { | ||
Column, | ||
CreateDateColumn, | ||
Entity, | ||
Generated, | ||
PrimaryGeneratedColumn, | ||
UpdateDateColumn, | ||
} from 'typeorm'; | ||
|
||
export enum RemoteServerType { | ||
POSTGRES_FDW = 'postgres_fdw', | ||
} | ||
|
||
type PostgresForeignDataWrapperOptions = { | ||
host: string; | ||
port: number; | ||
dbname: string; | ||
}; | ||
|
||
export type ForeignDataWrapperOptions<T extends RemoteServerType> = | ||
T extends RemoteServerType.POSTGRES_FDW | ||
? PostgresForeignDataWrapperOptions | ||
: never; | ||
|
||
export type UserMappingOptions = { | ||
username: string; | ||
password: string; | ||
}; | ||
|
||
@Entity('remoteServer') | ||
@ObjectType('RemoteServer') | ||
export class RemoteServerEntity<T extends RemoteServerType> { | ||
@PrimaryGeneratedColumn('uuid') | ||
id: string; | ||
|
||
@Column() | ||
@Generated('uuid') | ||
foreignDataWrapperId: string; | ||
|
||
@Column({ nullable: true }) | ||
foreignDataWrapperType: T; | ||
|
||
@Column({ nullable: true, type: 'jsonb' }) | ||
foreignDataWrapperOptions: ForeignDataWrapperOptions<T>; | ||
|
||
@Column({ nullable: true, type: 'jsonb' }) | ||
userMappingOptions: UserMappingOptions; | ||
|
||
@Column({ nullable: false, type: 'uuid' }) | ||
workspaceId: string; | ||
|
||
@CreateDateColumn() | ||
createdAt: Date; | ||
|
||
@UpdateDateColumn() | ||
updatedAt: Date; | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/twenty-server/src/engine/metadata-modules/remote-server/remote-server.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
|
||
import { TypeORMModule } from 'src/database/typeorm/typeorm.module'; | ||
import { ForeignDataWrapperQueryFactory } from 'src/engine/api/graphql/workspace-query-builder/factories/foreign-data-wrapper-query.factory'; | ||
import { RemoteServerEntity } from 'src/engine/metadata-modules/remote-server/remote-server.entity'; | ||
import { RemoteServerResolver } from 'src/engine/metadata-modules/remote-server/remote-server.resolver'; | ||
import { RemoteServerService } from 'src/engine/metadata-modules/remote-server/remote-server.service'; | ||
|
||
@Module({ | ||
imports: [ | ||
TypeORMModule, | ||
TypeOrmModule.forFeature([RemoteServerEntity], 'metadata'), | ||
], | ||
providers: [ | ||
RemoteServerService, | ||
RemoteServerResolver, | ||
ForeignDataWrapperQueryFactory, | ||
], | ||
exports: [RemoteServerService], | ||
}) | ||
export class RemoteServerModule {} |
Oops, something went wrong.