Remove typeorm service (#14116)

## Context
To simplify the way we inject our default datasource, I've recently
removed the token injection that was confusion since we only had once
configured on the module level. Now I'm removing TypeORM service which
allows us to instantiate a new Datasource with the same parameters as
the default one, it was redundant and confusing.
This commit is contained in:
Weiko
2025-08-28 13:21:26 +02:00
committed by GitHub
parent f1706670f5
commit dec2239ae7
35 changed files with 240 additions and 399 deletions
@@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common';
import { InjectDataSource } from '@nestjs/typeorm';
import { type EntityManager } from 'typeorm';
import { DataSource, type EntityManager } from 'typeorm';
import { v4 } from 'uuid';
import {
@@ -15,12 +16,12 @@ import { type DistantTables } from 'src/engine/metadata-modules/remote-server/re
import { STRIPE_DISTANT_TABLES } from 'src/engine/metadata-modules/remote-server/remote-table/distant-table/utils/stripe-distant-tables.util';
import { type PostgresTableSchemaColumn } from 'src/engine/metadata-modules/remote-server/types/postgres-table-schema-column';
import { isQueryTimeoutError } from 'src/engine/utils/query-timeout.util';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
@Injectable()
export class DistantTableService {
constructor(
private readonly workspaceDataSourceService: WorkspaceDataSourceService,
@InjectDataSource()
private readonly coreDataSource: DataSource,
) {}
public async fetchDistantTables(
@@ -68,11 +69,8 @@ export class DistantTableService {
const tmpSchemaId = v4();
const tmpSchemaName = `${workspaceId}_${remoteServer.id}_${tmpSchemaId}`;
const mainDataSource =
await this.workspaceDataSourceService.connectToMainDataSource();
try {
const distantTables = await mainDataSource.transaction(
const distantTables = await this.coreDataSource.transaction(
async (entityManager: EntityManager) => {
await entityManager.query(`CREATE SCHEMA "${tmpSchemaName}"`);
@@ -1,4 +1,7 @@
import { Injectable } from '@nestjs/common';
import { InjectDataSource } from '@nestjs/typeorm';
import { DataSource } from 'typeorm';
import {
type RemoteServerEntity,
@@ -31,18 +34,17 @@ export class ForeignTableService {
private readonly workspaceMigrationRunnerService: WorkspaceMigrationRunnerService,
private readonly workspaceDataSourceService: WorkspaceDataSourceService,
private readonly workspaceMetadataVersionService: WorkspaceMetadataVersionService,
@InjectDataSource()
private readonly coreDataSource: DataSource,
) {}
public async fetchForeignTableNamesWithinWorkspace(
_workspaceId: string,
foreignDataWrapperId: string,
): Promise<string[]> {
const mainDataSource =
await this.workspaceDataSourceService.connectToMainDataSource();
return (
(
await mainDataSource.query(
await this.coreDataSource.query(
`SELECT foreign_table_name, foreign_server_name FROM information_schema.foreign_tables WHERE foreign_server_name = $1`,
[foreignDataWrapperId],
)
@@ -1,9 +1,9 @@
import { Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
import isEmpty from 'lodash.isempty';
import { plural } from 'pluralize';
import { Repository } from 'typeorm';
import { DataSource, Repository } from 'typeorm';
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import { type CreateFieldInput } from 'src/engine/metadata-modules/field-metadata/dtos/create-field.input';
@@ -63,6 +63,8 @@ export class RemoteTableService {
private readonly foreignTableService: ForeignTableService,
private readonly workspaceDataSourceService: WorkspaceDataSourceService,
private readonly remoteTableSchemaUpdateService: RemoteTableSchemaUpdateService,
@InjectDataSource()
private readonly coreDataSource: DataSource,
) {}
public async findDistantTablesWithStatus(
@@ -182,14 +184,11 @@ export class RemoteTableService {
workspaceId,
);
const mainDataSource =
await this.workspaceDataSourceService.connectToMainDataSource();
const { baseName: localTableBaseName, suffix: localTableSuffix } =
await getRemoteTableLocalName(
input.name,
dataSourceMetatada.schema,
mainDataSource,
this.coreDataSource,
);
const localTableName = localTableSuffix
@@ -17,11 +17,10 @@ type RemoteTableLocalName = {
const isNameAvailable = async (
tableName: string,
workspaceSchemaName: string,
workspaceDataSource: DataSource,
coreDataSource: DataSource,
) => {
// TO DO workspaceDataSource.query method is not allowed, this will throw
const numberOfTablesWithSameName = +(
await workspaceDataSource.query(
await coreDataSource.query(
`SELECT count(table_name) FROM information_schema.tables WHERE table_name LIKE '${tableName}' AND table_schema IN ('core', '${workspaceSchemaName}')`,
)
)[0].count;
@@ -32,13 +31,13 @@ const isNameAvailable = async (
export const getRemoteTableLocalName = async (
distantTableName: string,
workspaceSchemaName: string,
workspaceDataSource: DataSource,
coreDataSource: DataSource,
): Promise<RemoteTableLocalName> => {
const baseName = singular(camelCase(distantTableName));
const isBaseNameValid = await isNameAvailable(
baseName,
workspaceSchemaName,
workspaceDataSource,
coreDataSource,
);
if (isBaseNameValid) {
@@ -50,7 +49,7 @@ export const getRemoteTableLocalName = async (
const isNameWithSuffixValid = await isNameAvailable(
name,
workspaceSchemaName,
workspaceDataSource,
coreDataSource,
);
if (isNameWithSuffixValid) {