Skip to content

Commit

Permalink
Remove acronymes
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Trompette committed Mar 25, 2024
1 parent d344cf5 commit ea27f6c
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 62 deletions.

This file was deleted.

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"`);
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
import { Injectable } from '@nestjs/common';

import {
FdwOptions,
ForeignDataWrapperOptions,
RemoteServerType,
UserMappingOptions,
} from 'src/engine/metadata-modules/remote-server/remote-server.entity';

@Injectable()
export class ForeignDataWrapperQueryFactory {
createFDW(
fdwId: string,
fdwType: RemoteServerType,
fdwOptions: FdwOptions<RemoteServerType>,
createForeignDataWrapper(
foreignDataWrapperId: string,
foreignDataWrapperType: RemoteServerType,
foreignDataWrapperOptions: ForeignDataWrapperOptions<RemoteServerType>,
) {
let fdwName = '';
let options = '';
const [name, options] = this.buildNameAndOptionsFromType(
foreignDataWrapperType,
foreignDataWrapperOptions,
);

switch (fdwType) {
case RemoteServerType.POSTGRES_FDW:
fdwName = 'postgres_fdw';
options = this.buildPostgresFDWQueryOptions(fdwOptions);
break;
default:
throw new Error('FDW type not supported');
}
return `CREATE SERVER "${foreignDataWrapperId}" FOREIGN DATA WRAPPER ${name} OPTIONS (${options})`;
}

return `CREATE SERVER IF NOT EXISTS "${fdwId}" FOREIGN DATA WRAPPER ${fdwName} 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}')`;
}

createUserMapping(fdwId: string, userMappingOptions: UserMappingOptions) {
return `CREATE USER MAPPING IF NOT EXISTS FOR ${userMappingOptions.username} SERVER "${fdwId}" 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(
fdwOptions: FdwOptions<RemoteServerType>,
foreignDataWrapperOptions: ForeignDataWrapperOptions<RemoteServerType>,
) {
return `dbname '${fdwOptions.dbname}', host '${fdwOptions.host}', port '${fdwOptions.port}'`;
return `dbname '${foreignDataWrapperOptions.dbname}', host '${foreignDataWrapperOptions.host}', port '${foreignDataWrapperOptions.port}'`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { TimelineMessagingModule } from 'src/engine/core-modules/messaging/timel
import { TimelineCalendarEventModule } from 'src/engine/core-modules/calendar/timeline-calendar-event.module';
import { BillingModule } from 'src/engine/core-modules/billing/billing.module';
import { HealthModule } from 'src/engine/core-modules/health/health.module';
import { RemoteServerModule } from 'src/engine/metadata-modules/remote-server/remote-server.module';

import { AnalyticsModule } from './analytics/analytics.module';
import { FileModule } from './file/file.module';
Expand All @@ -30,6 +31,7 @@ import { ClientConfigModule } from './client-config/client-config.module';
TimelineCalendarEventModule,
UserModule,
WorkspaceModule,
RemoteServerModule,
],
exports: [
AnalyticsModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import { IsOptional } from 'class-validator';
import GraphQLJSON from 'graphql-type-json';

import {
FdwOptions,
ForeignDataWrapperOptions,
RemoteServerType,
UserMappingOptions,
} from 'src/engine/metadata-modules/remote-server/remote-server.entity';

@InputType()
export class CreateRemoteServerInput<T extends RemoteServerType> {
@Field(() => String)
fdwType: T;
foreignDataWrapperType: T;

@IsOptional()
@Field(() => GraphQLJSON)
fdwOptions: FdwOptions<T>;
foreignDataWrapperOptions: ForeignDataWrapperOptions<T>;

@IsOptional()
@Field(() => GraphQLJSON, { nullable: true })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import { RemoteServerType } from 'src/engine/metadata-modules/remote-server/remo
export class RemoteServerTypeInput<T extends RemoteServerType> {
@Field(() => String)
@IsString()
fdwType!: T;
foreignDataWrapperType!: T;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IsOptional } from 'class-validator';
import GraphQLJSON from 'graphql-type-json';

import {
FdwOptions,
ForeignDataWrapperOptions,
RemoteServerType,
} from 'src/engine/metadata-modules/remote-server/remote-server.entity';

Expand All @@ -14,14 +14,14 @@ export class RemoteServerDTO<T extends RemoteServerType> {
id: string;

@Field(() => ID)
fwdId: string;
foreignDataWrapperId: string;

@Field(() => String)
fdwType: T;
foreignDataWrapperType: T;

@IsOptional()
@Field(() => GraphQLJSON, { nullable: true })
fdwOptions?: FdwOptions<T>;
foreignDataWrapperOptions?: ForeignDataWrapperOptions<T>;

@HideField()
workspaceId: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ export enum RemoteServerType {
POSTGRES_FDW = 'postgres_fdw',
}

type PostgresFdwOptions = {
type PostgresForeignDataWrapperOptions = {
host: string;
port: number;
dbname: string;
};

export type FdwOptions<T extends RemoteServerType> =
T extends RemoteServerType.POSTGRES_FDW ? PostgresFdwOptions : never;
export type ForeignDataWrapperOptions<T extends RemoteServerType> =
T extends RemoteServerType.POSTGRES_FDW
? PostgresForeignDataWrapperOptions
: never;

export type UserMappingOptions = {
username: string;
Expand All @@ -35,13 +37,13 @@ export class RemoteServerEntity<T extends RemoteServerType> {

@Column()
@Generated('uuid')
fdwId: string;
foreignDataWrapperId: string;

@Column({ nullable: true })
fdwType: T;
foreignDataWrapperType: T;

@Column({ nullable: true, type: 'jsonb' })
fdwOptions: FdwOptions<T>;
foreignDataWrapperOptions: ForeignDataWrapperOptions<T>;

@Column({ nullable: true, type: 'jsonb' })
userMappingOptions: UserMappingOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ export class RemoteServerResolver {

@Query(() => [RemoteServerDTO])
async findManyRemoteServersByType(
@Args('input') { fdwType }: RemoteServerTypeInput<RemoteServerType>,
@Args('input')
{ foreignDataWrapperType }: RemoteServerTypeInput<RemoteServerType>,
@AuthWorkspace() { id: workspaceId }: Workspace,
) {
return this.remoteServerService.findManyByTypeWithinWorkspace(
fdwType,
foreignDataWrapperType,
workspaceId,
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';

import { v4 } from 'uuid';
import { Repository } from 'typeorm';

import { TypeORMService } from 'src/database/typeorm/typeorm.service';
Expand Down Expand Up @@ -33,17 +34,19 @@ export class RemoteServerService<T extends RemoteServerType> {
remoteServerInput: CreateRemoteServerInput<T>,
workspaceId: string,
): Promise<RemoteServerEntity<RemoteServerType>> {
validateObject(remoteServerInput.fdwOptions);
validateObject(remoteServerInput.foreignDataWrapperOptions);

if (remoteServerInput.userMappingOptions) {
validateObject(remoteServerInput.userMappingOptions);
}

const mainDatasource = this.typeORMService.getMainDataSource();
const foreignDataWrapperId = v4();

let remoteServerToCreate = {
...remoteServerInput,
workspaceId,
foreignDataWrapperId,
};

if (remoteServerInput.userMappingOptions) {
Expand All @@ -67,18 +70,19 @@ export class RemoteServerService<T extends RemoteServerType> {
const createdRemoteServer =
await this.remoteServerRepository.create(remoteServerToCreate);

const fdwQuery = this.foreignDataWrapperQueryFactory.createFDW(
createdRemoteServer.fdwId,
remoteServerInput.fdwType,
remoteServerInput.fdwOptions,
);
const foreignDataWrapperQuery =
this.foreignDataWrapperQueryFactory.createForeignDataWrapper(
createdRemoteServer.foreignDataWrapperId,
remoteServerInput.foreignDataWrapperType,
remoteServerInput.foreignDataWrapperOptions,
);

await mainDatasource.query(fdwQuery);
await mainDatasource.query(foreignDataWrapperQuery);

if (remoteServerInput.userMappingOptions) {
const userMappingQuery =
this.foreignDataWrapperQueryFactory.createUserMapping(
createdRemoteServer.fdwId,
createdRemoteServer.foreignDataWrapperId,
remoteServerInput.userMappingOptions,
);

Expand Down Expand Up @@ -109,7 +113,9 @@ export class RemoteServerService<T extends RemoteServerType> {

const mainDatasource = this.typeORMService.getMainDataSource();

await mainDatasource.query(`DROP SERVER "${remoteServer.fdwId}" CASCADE`);
await mainDatasource.query(
`DROP SERVER "${remoteServer.foreignDataWrapperId}" CASCADE`,
);
await this.remoteServerRepository.delete(id);

return remoteServer;
Expand All @@ -125,12 +131,12 @@ export class RemoteServerService<T extends RemoteServerType> {
}

public async findManyByTypeWithinWorkspace<T extends RemoteServerType>(
fdwType: T,
foreignDataWrapperType: T,
workspaceId: string,
) {
return this.remoteServerRepository.find({
where: {
fdwType,
foreignDataWrapperType,
workspaceId,
},
});
Expand Down

0 comments on commit ea27f6c

Please sign in to comment.