The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix: nested relations not working and relations not prefixed (#2782)
Jérémy M474db1e1420a
diff --git a/server/src/metadata/field-metadata/field-metadata.service.ts b/server/src/metadata/field-metadata/field-metadata.service.tsindex 574fb494a78..0fd4c7d6630 100644--- a/server/src/metadata/field-metadata/field-metadata.service.ts+++ b/server/src/metadata/field-metadata/field-metadata.service.ts@@ -7,7 +7,7 @@ import {import { InjectRepository } from '@nestjs/typeorm';import { v4 as uuidV4 } from 'uuid';-import { Repository } from 'typeorm';+import { FindOneOptions, Repository } from 'typeorm';import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';import { WorkspaceMigrationRunnerService } from 'src/workspace/workspace-migration-runner/workspace-migration-runner.service';@@ -47,8 +47,12 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit): Promise<FieldMetadataEntity> {const objectMetadata =await this.objectMetadataService.findOneWithinWorkspace(- record.objectMetadataId,record.workspaceId,+ {+ where: {+ id: record.objectMetadataId,+ },+ },);if (!objectMetadata) {@@ -156,8 +160,12 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntitconst objectMetadata =await this.objectMetadataService.findOneWithinWorkspace(- existingFieldMetadata?.objectMetadataId,record.workspaceId,+ {+ where: {+ id: existingFieldMetadata?.objectMetadataId,+ },+ },);if (!objectMetadata) {@@ -200,11 +208,15 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit}public async findOneWithinWorkspace(- fieldMetadataId: string,workspaceId: string,+ options: FindOneOptions<FieldMetadataEntity>,) {return this.fieldMetadataRepository.findOne({- where: { id: fieldMetadataId, workspaceId },+ ...options,+ where: {+ ...options.where,+ workspaceId,+ },});}diff --git a/server/src/metadata/field-metadata/hooks/before-delete-one-field.hook.ts b/server/src/metadata/field-metadata/hooks/before-delete-one-field.hook.tsindex e7beb1ef421..1e3ae4dce74 100644--- a/server/src/metadata/field-metadata/hooks/before-delete-one-field.hook.ts+++ b/server/src/metadata/field-metadata/hooks/before-delete-one-field.hook.ts@@ -27,10 +27,11 @@ export class BeforeDeleteOneField implements BeforeDeleteOneHook<any> {}const fieldMetadata =- await this.fieldMetadataService.findOneWithinWorkspace(- instance.id.toString(),- workspaceId,- );+ await this.fieldMetadataService.findOneWithinWorkspace(workspaceId, {+ where: {+ id: instance.id.toString(),+ },+ });if (!fieldMetadata) {throw new BadRequestException('Field does not exist');diff --git a/server/src/metadata/field-metadata/hooks/before-update-one-field.hook.ts b/server/src/metadata/field-metadata/hooks/before-update-one-field.hook.tsindex d2d39cafe16..8efe56f3a7b 100644--- a/server/src/metadata/field-metadata/hooks/before-update-one-field.hook.ts+++ b/server/src/metadata/field-metadata/hooks/before-update-one-field.hook.ts@@ -31,10 +31,11 @@ export class BeforeUpdateOneField<T extends UpdateFieldInput>}const fieldMetadata =- await this.fieldMetadataService.findOneWithinWorkspace(- instance.id.toString(),- workspaceId,- );+ await this.fieldMetadataService.findOneWithinWorkspace(workspaceId, {+ where: {+ id: instance.id.toString(),+ },+ });if (!fieldMetadata) {throw new BadRequestException('Field does not exist');diff --git a/server/src/metadata/field-metadata/interfaces/field-metadata.interface.ts b/server/src/metadata/field-metadata/interfaces/field-metadata.interface.tsindex 23e01227ba6..80413a6a80c 100644--- a/server/src/metadata/field-metadata/interfaces/field-metadata.interface.ts+++ b/server/src/metadata/field-metadata/interfaces/field-metadata.interface.ts@@ -16,6 +16,7 @@ export interface FieldMetadataInterface<defaultValue?: FieldMetadataDefaultValue<T>;options?: FieldMetadataOptions<T>;objectMetadataId: string;+ workspaceId?: string;description?: string;isNullable?: boolean;fromRelationMetadata?: RelationMetadataEntity;diff --git a/server/src/metadata/field-metadata/utils/generate-target-column-map.util.ts b/server/src/metadata/field-metadata/utils/generate-target-column-map.util.tsindex 5822e5c2dc0..69ddb21d022 100644--- a/server/src/metadata/field-metadata/utils/generate-target-column-map.util.ts+++ b/server/src/metadata/field-metadata/utils/generate-target-column-map.util.ts@@ -3,6 +3,7 @@ import { BadRequestException } from '@nestjs/common';import { FieldMetadataTargetColumnMap } from 'src/metadata/field-metadata/interfaces/field-metadata-target-column-map.interface';import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity';+import { createCustomColumnName } from 'src/metadata/utils/create-custom-column-name.util';/*** Generate a target column map for a given type, this is used to map the field to the correct column(s) in the database.@@ -16,7 +17,9 @@ export function generateTargetColumnMap(isCustomField: boolean,fieldName: string,): FieldMetadataTargetColumnMap {- const columnName = isCustomField ? `_${fieldName}` : fieldName;+ const columnName = isCustomField+ ? createCustomColumnName(fieldName)+ : fieldName;switch (type) {case FieldMetadataType.UUID:diff --git a/server/src/metadata/object-metadata/hooks/before-delete-one-object.hook.ts b/server/src/metadata/object-metadata/hooks/before-delete-one-object.hook.tsindex 643c4ab7c2d..69bbe7cf464 100644--- a/server/src/metadata/object-metadata/hooks/before-delete-one-object.hook.ts+++ b/server/src/metadata/object-metadata/hooks/before-delete-one-object.hook.ts@@ -26,10 +26,11 @@ export class BeforeDeleteOneObject implements BeforeDeleteOneHook<any> {}const objectMetadata =- await this.objectMetadataService.findOneWithinWorkspace(- instance.id.toString(),- workspaceId,- );+ await this.objectMetadataService.findOneWithinWorkspace(workspaceId, {+ where: {+ id: instance.id.toString(),+ },+ });if (!objectMetadata) {throw new BadRequestException('Object does not exist');diff --git a/server/src/metadata/object-metadata/hooks/before-update-one-object.hook.ts b/server/src/metadata/object-metadata/hooks/before-update-one-object.hook.tsindex a6e89c02e30..e6fd71aa97c 100644--- a/server/src/metadata/object-metadata/hooks/before-update-one-object.hook.ts+++ b/server/src/metadata/object-metadata/hooks/before-update-one-object.hook.ts@@ -39,10 +39,11 @@ export class BeforeUpdateOneObject<T extends UpdateObjectInput>}const objectMetadata =- await this.objectMetadataService.findOneWithinWorkspace(- instance.id.toString(),- workspaceId,- );+ await this.objectMetadataService.findOneWithinWorkspace(workspaceId, {+ where: {+ id: instance.id.toString(),+ },+ });if (!objectMetadata) {throw new BadRequestException('Object does not exist');diff --git a/server/src/metadata/object-metadata/object-metadata.service.ts b/server/src/metadata/object-metadata/object-metadata.service.tsindex 5597ce3fc59..84a1a8e0230 100644--- a/server/src/metadata/object-metadata/object-metadata.service.ts+++ b/server/src/metadata/object-metadata/object-metadata.service.ts@@ -1,7 +1,7 @@import { Injectable } from '@nestjs/common';import { InjectRepository } from '@nestjs/typeorm';-import { Equal, In, Repository } from 'typeorm';+import { FindManyOptions, FindOneOptions, Repository } from 'typeorm';import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';import { WorkspaceMigrationService } from 'src/metadata/workspace-migration/workspace-migration.service';@@ -21,6 +21,7 @@ import {RelationMetadataEntity,RelationMetadataType,} from 'src/metadata/relation-metadata/relation-metadata.entity';+import { createCustomColumnName } from 'src/metadata/utils/create-custom-column-name.util';import { ObjectMetadataEntity } from './object-metadata.entity';@@ -63,7 +64,7 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEntconst createdObjectMetadata = await super.createOne({...record,dataSourceId: lastDataSourceMetadata.id,- targetTableName: `_${record.nameSingular}`,+ targetTableName: createCustomColumnName(record.nameSingular),isActive: true,isCustom: true,isSystem: false,@@ -298,39 +299,39 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEntreturn createdObjectMetadata;}- public async getObjectMetadataFromWorkspaceId(workspaceId: string) {- return this.objectMetadataRepository.find({- where: { workspaceId },+ public async findOneWithinWorkspace(+ workspaceId: string,+ options: FindOneOptions<ObjectMetadataEntity>,+ ): Promise<ObjectMetadataEntity | null> {+ return this.objectMetadataRepository.findOne({+ ...options,+ where: {+ ...options.where,+ workspaceId,+ },relations: ['fields','fields.fromRelationMetadata',- 'fields.fromRelationMetadata.fromObjectMetadata',- 'fields.fromRelationMetadata.toObjectMetadata',- 'fields.fromRelationMetadata.toObjectMetadata.fields','fields.toRelationMetadata',- 'fields.toRelationMetadata.fromObjectMetadata',- 'fields.toRelationMetadata.fromObjectMetadata.fields',- 'fields.toRelationMetadata.toObjectMetadata',],});}- public async findOneWithinWorkspace(- objectMetadataId: string,- workspaceId: string,- ) {- return this.objectMetadataRepository.findOne({- where: { id: objectMetadataId, workspaceId },- });- }-public async findManyWithinWorkspace(- objectMetadataIds: string[],workspaceId: string,+ options?: FindManyOptions<ObjectMetadataEntity>,) {- return this.objectMetadataRepository.findBy({- id: In(objectMetadataIds),- workspaceId: Equal(workspaceId),+ return this.objectMetadataRepository.find({+ ...options,+ where: {+ ...options?.where,+ workspaceId,+ },+ relations: [+ 'fields',+ 'fields.fromRelationMetadata',+ 'fields.toRelationMetadata',+ ],});}diff --git a/server/src/metadata/relation-metadata/hooks/before-delete-one-field.hook.ts b/server/src/metadata/relation-metadata/hooks/before-delete-one-field.hook.tsindex 6b62f6db323..457796aedba 100644--- a/server/src/metadata/relation-metadata/hooks/before-delete-one-field.hook.ts+++ b/server/src/metadata/relation-metadata/hooks/before-delete-one-field.hook.ts@@ -26,10 +26,11 @@ export class BeforeDeleteOneRelation implements BeforeDeleteOneHook<any> {}const relationMetadata =- await this.relationMetadataService.findOneWithinWorkspace(- instance.id.toString(),- workspaceId,- );+ await this.relationMetadataService.findOneWithinWorkspace(workspaceId, {+ where: {+ id: instance.id.toString(),+ },+ });if (!relationMetadata) {throw new BadRequestException('Relation does not exist');diff --git a/server/src/metadata/relation-metadata/relation-metadata.service.ts b/server/src/metadata/relation-metadata/relation-metadata.service.tsindex 7df62eedb27..51be1bda6ad 100644--- a/server/src/metadata/relation-metadata/relation-metadata.service.ts+++ b/server/src/metadata/relation-metadata/relation-metadata.service.ts@@ -6,7 +6,7 @@ import {import { InjectRepository } from '@nestjs/typeorm';import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';-import { Repository } from 'typeorm';+import { FindOneOptions, In, Repository } from 'typeorm';import camelCase from 'lodash.camelcase';import { ObjectMetadataService } from 'src/metadata/object-metadata/object-metadata.service';@@ -16,6 +16,8 @@ import { WorkspaceMigrationRunnerService } from 'src/workspace/workspace-migratiimport { WorkspaceMigrationService } from 'src/metadata/workspace-migration/workspace-migration.service';import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity';import { WorkspaceMigrationColumnActionType } from 'src/metadata/workspace-migration/workspace-migration.entity';+import { ObjectMetadataEntity } from 'src/metadata/object-metadata/object-metadata.entity';+import { createCustomColumnName } from 'src/metadata/utils/create-custom-column-name.util';import {RelationMetadataEntity,@@ -85,14 +87,18 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadatconst objectMetadataEntries =await this.objectMetadataService.findManyWithinWorkspace(- [record.fromObjectMetadataId, record.toObjectMetadataId],record.workspaceId,+ {+ where: {+ id: In([record.fromObjectMetadataId, record.toObjectMetadataId]),+ },+ },);const objectMetadataMap = objectMetadataEntries.reduce((acc, curr) => {acc[curr.id] = curr;return acc;- }, {});+ }, {} as { [key: string]: ObjectMetadataEntity });if (objectMetadataMap[record.fromObjectMetadataId] === undefined ||@@ -103,7 +109,11 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat);}- const foreignKeyColumnName = `${camelCase(record.toName)}Id`;+ const baseColumnName = `${camelCase(record.toName)}Id`;+ const isToCustom = objectMetadataMap[record.toObjectMetadataId].isCustom;+ const foreignKeyColumnName = isToCustom+ ? createCustomColumnName(baseColumnName)+ : baseColumnName;const createdFields = await this.fieldMetadataService.createMany([// FROM@@ -126,7 +136,11 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadatdescription: record.toDescription,icon: record.toIcon,isCustom: true,- targetColumnMap: {},+ targetColumnMap: isToCustom+ ? {+ value: createCustomColumnName(record.toName),+ }+ : {},isActive: true,type: FieldMetadataType.RELATION,objectMetadataId: record.toObjectMetadataId,@@ -134,12 +148,14 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat},// FOREIGN KEY{- name: foreignKeyColumnName,+ name: baseColumnName,label: `${record.toLabel} Foreign Key`,- description: undefined,+ description: `${record.toDescription} Foreign Key`,icon: undefined,isCustom: true,- targetColumnMap: {},+ targetColumnMap: {+ value: foreignKeyColumnName,+ },isActive: true,// Should not be visible on the front sideisSystem: true,@@ -203,11 +219,15 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat}public async findOneWithinWorkspace(- relationMetadataId: string,workspaceId: string,+ options: FindOneOptions<RelationMetadataEntity>,) {return this.relationMetadataRepository.findOne({- where: { id: relationMetadataId, workspaceId },+ ...options,+ where: {+ ...options.where,+ workspaceId,+ },relations: ['fromFieldMetadata', 'toFieldMetadata'],});}diff --git a/server/src/metadata/utils/create-custom-column-name.util.ts b/server/src/metadata/utils/create-custom-column-name.util.tsnew file mode 100644index 00000000000..2eb4674e44b--- /dev/null+++ b/server/src/metadata/utils/create-custom-column-name.util.ts@@ -0,0 +1,3 @@+export const createCustomColumnName = (name: string) => {+ return `_${name}`;+};diff --git a/server/src/workspace/workspace-query-builder/factories/create-many-query.factory.ts b/server/src/workspace/workspace-query-builder/factories/create-many-query.factory.tsindex 5ef332b55ae..54d3d30171e 100644--- a/server/src/workspace/workspace-query-builder/factories/create-many-query.factory.ts+++ b/server/src/workspace/workspace-query-builder/factories/create-many-query.factory.ts@@ -20,11 +20,11 @@ export class CreateManyQueryFactory {private readonly argsAliasFactory: ArgsAliasFactory,) {}- create<Record extends IRecord = IRecord>(+ async create<Record extends IRecord = IRecord>(args: CreateManyResolverArgs<Record>,options: WorkspaceQueryBuilderOptions,) {- const fieldsString = this.fieldsStringFactory.create(+ const fieldsString = await this.fieldsStringFactory.create(options.info,options.fieldMetadataCollection,);diff --git a/server/src/workspace/workspace-query-builder/factories/delete-many-query.factory.ts b/server/src/workspace/workspace-query-builder/factories/delete-many-query.factory.tsindex 276b73e5f8d..d51a078bb10 100644--- a/server/src/workspace/workspace-query-builder/factories/delete-many-query.factory.ts+++ b/server/src/workspace/workspace-query-builder/factories/delete-many-query.factory.ts@@ -11,8 +11,11 @@ import { FieldsStringFactory } from './fields-string.factory';export class DeleteManyQueryFactory {constructor(private readonly fieldsStringFactory: FieldsStringFactory) {}- create(args: DeleteManyResolverArgs, options: WorkspaceQueryBuilderOptions) {- const fieldsString = this.fieldsStringFactory.create(+ async create(+ args: DeleteManyResolverArgs,+ options: WorkspaceQueryBuilderOptions,+ ) {+ const fieldsString = await this.fieldsStringFactory.create(options.info,options.fieldMetadataCollection,);diff --git a/server/src/workspace/workspace-query-builder/factories/delete-one-query.factory.ts b/server/src/workspace/workspace-query-builder/factories/delete-one-query.factory.tsindex 799de88d994..e55531dbd08 100644--- a/server/src/workspace/workspace-query-builder/factories/delete-one-query.factory.ts+++ b/server/src/workspace/workspace-query-builder/factories/delete-one-query.factory.ts@@ -11,8 +11,11 @@ export class DeleteOneQueryFactory {constructor(private readonly fieldsStringFactory: FieldsStringFactory) {}- create(args: DeleteOneResolverArgs, options: WorkspaceQueryBuilderOptions) {- const fieldsString = this.fieldsStringFactory.create(+ async create(+ args: DeleteOneResolverArgs,+ options: WorkspaceQueryBuilderOptions,+ ) {+ const fieldsString = await this.fieldsStringFactory.create(options.info,options.fieldMetadataCollection,);diff --git a/server/src/workspace/workspace-query-builder/factories/fields-string.factory.ts b/server/src/workspace/workspace-query-builder/factories/fields-string.factory.tsindex 5cfffa68639..8faa3dc5f51 100644--- a/server/src/workspace/workspace-query-builder/factories/fields-string.factory.ts+++ b/server/src/workspace/workspace-query-builder/factories/fields-string.factory.ts@@ -23,7 +23,7 @@ export class FieldsStringFactory {create(info: GraphQLResolveInfo,fieldMetadataCollection: FieldMetadataInterface[],- ) {+ ): Promise<string> {const selectedFields: Record<string, any> = graphqlFields(info);return this.createFieldsStringRecursive(@@ -33,12 +33,12 @@ export class FieldsStringFactory {);}- createFieldsStringRecursive(+ async createFieldsStringRecursive(info: GraphQLResolveInfo,selectedFields: Record<string, any>,fieldMetadataCollection: FieldMetadataInterface[],accumulator = '',- ): string {+ ): Promise<string> {const fieldMetadataMap = new Map(fieldMetadataCollection.map((metadata) => [metadata.name, metadata]),);@@ -54,7 +54,7 @@ export class FieldsStringFactory {// If the field is a relation field, we need to create a special aliasif (isRelationFieldMetadataType(fieldMetadata.type)) {- const alias = this.relationFieldAliasFactory.create(+ const alias = await this.relationFieldAliasFactory.create(fieldKey,fieldValue,fieldMetadata,@@ -80,7 +80,7 @@ export class FieldsStringFactory {!isEmpty(fieldValue)) {accumulator += `${fieldKey} {\n`;- accumulator = this.createFieldsStringRecursive(+ accumulator = await this.createFieldsStringRecursive(info,fieldValue,fieldMetadataCollection,diff --git a/server/src/workspace/workspace-query-builder/factories/find-many-query.factory.ts b/server/src/workspace/workspace-query-builder/factories/find-many-query.factory.tsindex 8554a329c5c..c3f19c87c43 100644--- a/server/src/workspace/workspace-query-builder/factories/find-many-query.factory.ts+++ b/server/src/workspace/workspace-query-builder/factories/find-many-query.factory.ts@@ -19,14 +19,14 @@ export class FindManyQueryFactory {private readonly argsStringFactory: ArgsStringFactory,) {}- create<+ async create<Filter extends RecordFilter = RecordFilter,OrderBy extends RecordOrderBy = RecordOrderBy,>(args: FindManyResolverArgs<Filter, OrderBy>,options: WorkspaceQueryBuilderOptions,) {- const fieldsString = this.fieldsStringFactory.create(+ const fieldsString = await this.fieldsStringFactory.create(options.info,options.fieldMetadataCollection,);diff --git a/server/src/workspace/workspace-query-builder/factories/find-one-query.factory.ts b/server/src/workspace/workspace-query-builder/factories/find-one-query.factory.tsindex 98f27fb0679..94b07992ef0 100644--- a/server/src/workspace/workspace-query-builder/factories/find-one-query.factory.ts+++ b/server/src/workspace/workspace-query-builder/factories/find-one-query.factory.ts@@ -16,11 +16,11 @@ export class FindOneQueryFactory {private readonly argsStringFactory: ArgsStringFactory,) {}- create<Filter extends RecordFilter = RecordFilter>(+ async create<Filter extends RecordFilter = RecordFilter>(args: FindOneResolverArgs<Filter>,options: WorkspaceQueryBuilderOptions,) {- const fieldsString = this.fieldsStringFactory.create(+ const fieldsString = await this.fieldsStringFactory.create(options.info,options.fieldMetadataCollection,);diff --git a/server/src/workspace/workspace-query-builder/factories/relation-field-alias.factory.ts b/server/src/workspace/workspace-query-builder/factories/relation-field-alias.factory.tsindex 79e6c9266da..d17e42abcfc 100644--- a/server/src/workspace/workspace-query-builder/factories/relation-field-alias.factory.ts+++ b/server/src/workspace/workspace-query-builder/factories/relation-field-alias.factory.ts@@ -11,6 +11,7 @@ import {RelationDirection,} from 'src/workspace/utils/deduce-relation-direction.util';import { getFieldArgumentsByKey } from 'src/workspace/workspace-query-builder/utils/get-field-arguments-by-key.util';+import { ObjectMetadataService } from 'src/metadata/object-metadata/object-metadata.service';import { FieldsStringFactory } from './fields-string.factory';import { ArgsStringFactory } from './args-string.factory';@@ -23,6 +24,7 @@ export class RelationFieldAliasFactory {@Inject(forwardRef(() => FieldsStringFactory))private readonly fieldsStringFactory: FieldsStringFactory,private readonly argsStringFactory: ArgsStringFactory,+ private readonly objectMetadataService: ObjectMetadataService,) {}create(@@ -30,7 +32,7 @@ export class RelationFieldAliasFactory {fieldValue: any,fieldMetadata: FieldMetadataInterface,info: GraphQLResolveInfo,- ) {+ ): Promise<string> {if (!isRelationFieldMetadataType(fieldMetadata.type)) {throw new Error(`Field ${fieldMetadata.name} is not a relation field`);}@@ -38,12 +40,12 @@ export class RelationFieldAliasFactory {return this.createRelationAlias(fieldKey, fieldValue, fieldMetadata, info);}- private createRelationAlias(+ private async createRelationAlias(fieldKey: string,fieldValue: any,fieldMetadata: FieldMetadataInterface,info: GraphQLResolveInfo,- ) {+ ): Promise<string> {const relationMetadata =fieldMetadata.fromRelationMetadata ?? fieldMetadata.toRelationMetadata;@@ -53,14 +55,36 @@ export class RelationFieldAliasFactory {);}+ if (!fieldMetadata.workspaceId) {+ throw new Error(+ `Workspace id not found for field ${fieldMetadata.name} in object metadata ${fieldMetadata.objectMetadataId}`,+ );+ }+const relationDirection = deduceRelationDirection(fieldMetadata.objectMetadataId,relationMetadata,);+ // Retrieve the referenced object metadata based on the relation direction+ // Mandatory to handle n+n relationsconst referencedObjectMetadata =- relationDirection == RelationDirection.TO- ? relationMetadata.fromObjectMetadata- : relationMetadata.toObjectMetadata;+ await this.objectMetadataService.findOneWithinWorkspace(+ fieldMetadata.workspaceId,+ {+ where: {+ id:+ relationDirection == RelationDirection.TO+ ? relationMetadata.fromObjectMetadataId+ : relationMetadata.toObjectMetadataId,+ },+ },+ );++ if (!referencedObjectMetadata) {+ throw new Error(+ `Referenced object metadata not found for relation ${relationMetadata.id}`,+ );+ }// If it's a relation destination is of kind MANY, we need to add the collection suffix and extract the argsif (@@ -70,21 +94,26 @@ export class RelationFieldAliasFactory {const args = getFieldArgumentsByKey(info, fieldKey);const argsString = this.argsStringFactory.create(args,- relationMetadata.toObjectMetadata.fields ?? [],+ referencedObjectMetadata.fields ?? [],);+ const fieldsString =+ await this.fieldsStringFactory.createFieldsStringRecursive(+ info,+ fieldValue,+ referencedObjectMetadata.fields ?? [],+ );+return `${fieldKey}: ${referencedObjectMetadata.targetTableName}Collection${argsString ? `(${argsString})` : ''} {- ${this.fieldsStringFactory.createFieldsStringRecursive(- info,- fieldValue,- relationMetadata.toObjectMetadata.fields ?? [],- )}+ ${fieldsString}}`;}- let relationAlias = fieldMetadata.isCustom ? `_${fieldKey}` : fieldKey;+ let relationAlias = fieldMetadata.isCustom+ ? `${fieldKey}: ${fieldMetadata.targetColumnMap.value}`+ : fieldKey;// For one to one relations, pg_graphql use the targetTableName on the side that is not storing the foreign key// so we need to alias it to the field key@@ -94,15 +123,17 @@ export class RelationFieldAliasFactory {) {relationAlias = `${fieldKey}: ${referencedObjectMetadata.targetTableName}`;}+ const fieldsString =+ await this.fieldsStringFactory.createFieldsStringRecursive(+ info,+ fieldValue,+ referencedObjectMetadata.fields ?? [],+ );// Otherwise it means it's a relation destination is of kind ONEreturn `${relationAlias} {- ${this.fieldsStringFactory.createFieldsStringRecursive(- info,- fieldValue,- referencedObjectMetadata.fields ?? [],- )}+ ${fieldsString}}`;}diff --git a/server/src/workspace/workspace-query-builder/factories/update-many-query.factory.ts b/server/src/workspace/workspace-query-builder/factories/update-many-query.factory.tsindex 46d963607a6..148f77a5455 100644--- a/server/src/workspace/workspace-query-builder/factories/update-many-query.factory.ts+++ b/server/src/workspace/workspace-query-builder/factories/update-many-query.factory.ts@@ -18,14 +18,14 @@ export class UpdateManyQueryFactory {private readonly argsAliasFactory: ArgsAliasFactory,) {}- create<+ async create<Record extends IRecord = IRecord,Filter extends RecordFilter = RecordFilter,>(args: UpdateManyResolverArgs<Record, Filter>,options: WorkspaceQueryBuilderOptions,) {- const fieldsString = this.fieldsStringFactory.create(+ const fieldsString = await this.fieldsStringFactory.create(options.info,options.fieldMetadataCollection,);diff --git a/server/src/workspace/workspace-query-builder/factories/update-one-query.factory.ts b/server/src/workspace/workspace-query-builder/factories/update-one-query.factory.tsindex eb43812a956..bf52cdabf97 100644--- a/server/src/workspace/workspace-query-builder/factories/update-one-query.factory.ts+++ b/server/src/workspace/workspace-query-builder/factories/update-one-query.factory.ts@@ -18,11 +18,11 @@ export class UpdateOneQueryFactory {private readonly argsAliasFactory: ArgsAliasFactory,) {}- create<Record extends IRecord = IRecord>(+ async create<Record extends IRecord = IRecord>(args: UpdateOneResolverArgs<Record>,options: WorkspaceQueryBuilderOptions,) {- const fieldsString = this.fieldsStringFactory.create(+ const fieldsString = await this.fieldsStringFactory.create(options.info,options.fieldMetadataCollection,);diff --git a/server/src/workspace/workspace-query-builder/workspace-query-builder.factory.ts b/server/src/workspace/workspace-query-builder/workspace-query-builder.factory.tsindex 0f706edbfb4..1d76e99704e 100644--- a/server/src/workspace/workspace-query-builder/workspace-query-builder.factory.ts+++ b/server/src/workspace/workspace-query-builder/workspace-query-builder.factory.ts@@ -44,35 +44,35 @@ export class WorkspaceQueryBuilderFactory {>(args: FindManyResolverArgs<Filter, OrderBy>,options: WorkspaceQueryBuilderOptions,- ): string {+ ): Promise<string> {return this.findManyQueryFactory.create<Filter, OrderBy>(args, options);}findOne<Filter extends RecordFilter = RecordFilter>(args: FindOneResolverArgs<Filter>,options: WorkspaceQueryBuilderOptions,- ): string {+ ): Promise<string> {return this.findOneQueryFactory.create<Filter>(args, options);}createMany<Record extends IRecord = IRecord>(args: CreateManyResolverArgs<Record>,options: WorkspaceQueryBuilderOptions,- ): string {+ ): Promise<string> {return this.createManyQueryFactory.create<Record>(args, options);}updateOne<Record extends IRecord = IRecord>(initialArgs: UpdateOneResolverArgs<Record>,options: WorkspaceQueryBuilderOptions,- ): string {+ ): Promise<string> {return this.updateOneQueryFactory.create<Record>(initialArgs, options);}deleteOne(args: DeleteOneResolverArgs,options: WorkspaceQueryBuilderOptions,- ): string {+ ): Promise<string> {return this.deleteOneQueryFactory.create(args, options);}@@ -82,14 +82,14 @@ export class WorkspaceQueryBuilderFactory {>(args: UpdateManyResolverArgs<Record, Filter>,options: WorkspaceQueryBuilderOptions,- ): string {+ ): Promise<string> {return this.updateManyQueryFactory.create(args, options);}deleteMany<Filter extends RecordFilter = RecordFilter>(args: DeleteManyResolverArgs<Filter>,options: WorkspaceQueryBuilderOptions,- ): string {+ ): Promise<string> {return this.deleteManyQueryFactory.create(args, options);}}diff --git a/server/src/workspace/workspace-query-builder/workspace-query-builder.module.ts b/server/src/workspace/workspace-query-builder/workspace-query-builder.module.tsindex 5edb493735e..7b25351a781 100644--- a/server/src/workspace/workspace-query-builder/workspace-query-builder.module.ts+++ b/server/src/workspace/workspace-query-builder/workspace-query-builder.module.ts@@ -1,11 +1,13 @@import { Module } from '@nestjs/common';+import { ObjectMetadataModule } from 'src/metadata/object-metadata/object-metadata.module';+import { WorkspaceQueryBuilderFactory } from './workspace-query-builder.factory';import { workspaceQueryBuilderFactories } from './factories/factories';@Module({- imports: [],+ imports: [ObjectMetadataModule],providers: [...workspaceQueryBuilderFactories, WorkspaceQueryBuilderFactory],exports: [WorkspaceQueryBuilderFactory],})diff --git a/server/src/workspace/workspace-query-runner/workspace-query-runner.service.ts b/server/src/workspace/workspace-query-runner/workspace-query-runner.service.tsindex e0956029be4..3b40a839c4e 100644--- a/server/src/workspace/workspace-query-runner/workspace-query-runner.service.ts+++ b/server/src/workspace/workspace-query-runner/workspace-query-runner.service.ts@@ -45,7 +45,10 @@ export class WorkspaceQueryRunnerService {options: WorkspaceQueryRunnerOptions,): Promise<IConnection<Record> | undefined> {const { workspaceId, targetTableName } = options;- const query = this.workspaceQueryBuilderFactory.findMany(args, options);+ const query = await this.workspaceQueryBuilderFactory.findMany(+ args,+ options,+ );const result = await this.execute(query, workspaceId);return this.parseResult<IConnection<Record>>(result, targetTableName, '');@@ -62,7 +65,10 @@ export class WorkspaceQueryRunnerService {throw new BadRequestException('Missing filter argument');}const { workspaceId, targetTableName } = options;- const query = this.workspaceQueryBuilderFactory.findOne(args, options);+ const query = await this.workspaceQueryBuilderFactory.findOne(+ args,+ options,+ );const result = await this.execute(query, workspaceId);const parsedResult = this.parseResult<IConnection<Record>>(result,@@ -78,7 +84,10 @@ export class WorkspaceQueryRunnerService {options: WorkspaceQueryRunnerOptions,): Promise<Record[] | undefined> {const { workspaceId, targetTableName } = options;- const query = this.workspaceQueryBuilderFactory.createMany(args, options);+ const query = await this.workspaceQueryBuilderFactory.createMany(+ args,+ options,+ );const result = await this.execute(query, workspaceId);return this.parseResult<PGGraphQLMutation<Record>>(@@ -102,9 +111,10 @@ export class WorkspaceQueryRunnerService {options: WorkspaceQueryRunnerOptions,): Promise<Record | undefined> {const { workspaceId, targetTableName } = options;-- const query = this.workspaceQueryBuilderFactory.updateOne(args, options);-+ const query = await this.workspaceQueryBuilderFactory.updateOne(+ args,+ options,+ );const result = await this.execute(query, workspaceId);return this.parseResult<PGGraphQLMutation<Record>>(@@ -119,7 +129,10 @@ export class WorkspaceQueryRunnerService {options: WorkspaceQueryRunnerOptions,): Promise<Record | undefined> {const { workspaceId, targetTableName } = options;- const query = this.workspaceQueryBuilderFactory.deleteOne(args, options);+ const query = await this.workspaceQueryBuilderFactory.deleteOne(+ args,+ options,+ );const result = await this.execute(query, workspaceId);return this.parseResult<PGGraphQLMutation<Record>>(@@ -134,9 +147,10 @@ export class WorkspaceQueryRunnerService {options: WorkspaceQueryRunnerOptions,): Promise<Record[] | undefined> {const { workspaceId, targetTableName } = options;-- const query = this.workspaceQueryBuilderFactory.updateMany(args, options);-+ const query = await this.workspaceQueryBuilderFactory.updateMany(+ args,+ options,+ );const result = await this.execute(query, workspaceId);return this.parseResult<PGGraphQLMutation<Record>>(@@ -154,9 +168,10 @@ export class WorkspaceQueryRunnerService {options: WorkspaceQueryRunnerOptions,): Promise<Record[] | undefined> {const { workspaceId, targetTableName } = options;-- const query = this.workspaceQueryBuilderFactory.deleteMany(args, options);-+ const query = await this.workspaceQueryBuilderFactory.deleteMany(+ args,+ options,+ );const result = await this.execute(query, workspaceId);return this.parseResult<PGGraphQLMutation<Record>>(diff --git a/server/src/workspace/workspace-schema-builder/factories/args.factory.ts b/server/src/workspace/workspace-schema-builder/factories/args.factory.tsindex d3fdb87f138..646f31ce073 100644--- a/server/src/workspace/workspace-schema-builder/factories/args.factory.ts+++ b/server/src/workspace/workspace-schema-builder/factories/args.factory.ts@@ -18,7 +18,7 @@ export class ArgsFactory {) {}public create(- { args, objectMetadata }: ArgsMetadata,+ { args, objectMetadataId }: ArgsMetadata,options: WorkspaceBuildSchemaOptions,): GraphQLFieldConfigArgumentMap {const fieldConfigMap: GraphQLFieldConfigArgumentMap = {};@@ -65,21 +65,21 @@ export class ArgsFactory {// Argument is an input typeif (arg.kind) {const inputType = this.typeDefinitionsStorage.getInputTypeByKey(- objectMetadata.id,+ objectMetadataId,arg.kind,);if (!inputType) {this.logger.error(- `Could not find a GraphQL input type for ${objectMetadata.id}`,+ `Could not find a GraphQL input type for ${objectMetadataId}`,{- objectMetadata,+ objectMetadataId,options,},);throw new Error(- `Could not find a GraphQL input type for ${objectMetadata.id}`,+ `Could not find a GraphQL input type for ${objectMetadataId}`,);}diff --git a/server/src/workspace/workspace-schema-builder/factories/extend-object-type-definition.factory.ts b/server/src/workspace/workspace-schema-builder/factories/extend-object-type-definition.factory.tsindex d4c852e4417..60b9847394d 100644--- a/server/src/workspace/workspace-schema-builder/factories/extend-object-type-definition.factory.ts+++ b/server/src/workspace/workspace-schema-builder/factories/extend-object-type-definition.factory.ts@@ -155,7 +155,7 @@ export class ExtendObjectTypeDefinitionFactory {argsType = this.argsFactory.create({args,- objectMetadata: relationMetadata.toObjectMetadata,+ objectMetadataId: relationMetadata.toObjectMetadataId,},options,);diff --git a/server/src/workspace/workspace-schema-builder/factories/root-type.factory.ts b/server/src/workspace/workspace-schema-builder/factories/root-type.factory.tsindex 88927e84ad2..b15e1e7b31e 100644--- a/server/src/workspace/workspace-schema-builder/factories/root-type.factory.ts+++ b/server/src/workspace/workspace-schema-builder/factories/root-type.factory.ts@@ -81,7 +81,7 @@ export class RootTypeFactory {const argsType = this.argsFactory.create({args,- objectMetadata,+ objectMetadataId: objectMetadata.id,},options,);diff --git a/server/src/workspace/workspace-schema-builder/interfaces/param-metadata.interface.ts b/server/src/workspace/workspace-schema-builder/interfaces/param-metadata.interface.tsindex 2b9f94eb357..12a1ac18153 100644--- a/server/src/workspace/workspace-schema-builder/interfaces/param-metadata.interface.ts+++ b/server/src/workspace/workspace-schema-builder/interfaces/param-metadata.interface.ts@@ -1,5 +1,3 @@-import { ObjectMetadataInterface } from 'src/metadata/field-metadata/interfaces/object-metadata.interface';-import { InputTypeDefinitionKind } from 'src/workspace/workspace-schema-builder/factories/input-type-definition.factory';import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity';@@ -15,5 +13,5 @@ export interface ArgsMetadata {args: {[key: string]: ArgMetadata;};- objectMetadata: ObjectMetadataInterface;+ objectMetadataId: string;}diff --git a/server/src/workspace/workspace.factory.ts b/server/src/workspace/workspace.factory.tsindex fd3aec1a386..88e1b0f79b7 100644--- a/server/src/workspace/workspace.factory.ts+++ b/server/src/workspace/workspace.factory.ts@@ -51,9 +51,7 @@ export class WorkspaceFactory {// If object metadata is not cached, get it from the databaseif (!objectMetadataCollection) {objectMetadataCollection =- await this.objectMetadataService.getObjectMetadataFromWorkspaceId(- workspaceId,- );+ await this.objectMetadataService.findManyWithinWorkspace(workspaceId);await this.workspaceSchemaStorageService.setObjectMetadata(workspaceId,