Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 35 additions & 25 deletions packages/agent/src/agent/utils/forest-schema/generator-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,37 @@ export default class SchemaGeneratorFields {
foreignCollection: Collection,
baseSchema: ForestServerField,
): ForestServerField {
let key: string;
let keySchema: ColumnSchema;
let targetName: string;
let targetField: ColumnSchema;
let isReadOnly: boolean;

if (relation.type === 'OneToMany') {
key = relation.originKeyTarget;
keySchema = collection.schema.fields[key] as ColumnSchema;
targetName = relation.originKeyTarget;
targetField = collection.schema.fields[targetName] as ColumnSchema;

const originKey = foreignCollection.schema.fields[relation.originKey] as ColumnSchema;
isReadOnly = originKey.isReadOnly;
} else {
key = relation.foreignKeyTarget;
keySchema = foreignCollection.schema.fields[key] as ColumnSchema;
targetName = relation.foreignKeyTarget;
targetField = foreignCollection.schema.fields[targetName] as ColumnSchema;

const throughSchema = collection.dataSource.getCollection(relation.throughCollection).schema;
const foreignKey = throughSchema.fields[relation.foreignKey] as ColumnSchema;
const originKey = throughSchema.fields[relation.originKey] as ColumnSchema;
isReadOnly = originKey.isReadOnly || foreignKey.isReadOnly;
}

return {
type: [keySchema.columnType as PrimitiveTypes],
...baseSchema,
type: [targetField.columnType as PrimitiveTypes],
defaultValue: null,
isFilterable: false,
isPrimaryKey: false,
isRequired: false,
isReadOnly: Boolean(isReadOnly),
isSortable: false,
validations: [],
reference: `${foreignCollection.name}.${key}`,
...baseSchema,
reference: `${foreignCollection.name}.${targetName}`,
};
}

Expand All @@ -133,19 +143,20 @@ export default class SchemaGeneratorFields {
foreignCollection: Collection,
baseSchema: ForestServerField,
): ForestServerField {
const key = relation.originKeyTarget;
const keySchema = collection.schema.fields[key] as ColumnSchema;
const targetField = collection.schema.fields[relation.originKeyTarget] as ColumnSchema;
const keyField = foreignCollection.schema.fields[relation.originKey] as ColumnSchema;

return {
type: keySchema.columnType as PrimitiveTypes,
...baseSchema,
type: keyField.columnType as PrimitiveTypes,
defaultValue: null,
isFilterable: SchemaGeneratorFields.isForeignCollectionFilterable(foreignCollection),
isPrimaryKey: false,
isRequired: false,
isSortable: Boolean(keySchema.isSortable),
isReadOnly: Boolean(keyField.isReadOnly),
isSortable: Boolean(targetField.isSortable),
validations: [],
reference: `${foreignCollection.name}.${key}`,
...baseSchema,
reference: `${foreignCollection.name}.${relation.originKeyTarget}`,
};
}

Expand All @@ -155,19 +166,19 @@ export default class SchemaGeneratorFields {
foreignCollection: Collection,
baseSchema: ForestServerField,
): ForestServerField {
const key = relation.foreignKey;
const keySchema = collection.schema.fields[key] as ColumnSchema;
const keyField = collection.schema.fields[relation.foreignKey] as ColumnSchema;

return {
type: keySchema.columnType as PrimitiveTypes,
defaultValue: keySchema.defaultValue ?? null,
...baseSchema,
type: keyField.columnType as PrimitiveTypes,
defaultValue: keyField.defaultValue ?? null,
isFilterable: SchemaGeneratorFields.isForeignCollectionFilterable(foreignCollection),
isPrimaryKey: Boolean(keySchema.isPrimaryKey),
isRequired: keySchema.validation?.some(v => v.operator === 'Present') ?? false,
isSortable: Boolean(keySchema.isSortable),
validations: FrontendValidationUtils.convertValidationList(keySchema.validation),
isPrimaryKey: Boolean(keyField.isPrimaryKey),
isRequired: keyField.validation?.some(v => v.operator === 'Present') ?? false,
isReadOnly: Boolean(keyField.isReadOnly),
isSortable: Boolean(keyField.isSortable),
validations: FrontendValidationUtils.convertValidationList(keyField.validation),
reference: `${foreignCollection.name}.${relation.foreignKeyTarget}`,
...baseSchema,
};
}

Expand All @@ -179,7 +190,6 @@ export default class SchemaGeneratorFields {
field: name,
enums: null,
integration: null,
isReadOnly: false,
isVirtual: false,
inverseOf: CollectionUtils.getInverseRelation(collection, name),
relationship: SchemaGeneratorFields.relationMap[relation.type],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ describe('SchemaGeneratorFields > Many to Many', () => {
fields: {
bookId: factories.columnSchema
.isPrimaryKey()
.build({ validation: [{ operator: 'Present' }] }),
.build({ isReadOnly: true, validation: [{ operator: 'Present' }] }),
book: factories.manyToOneSchema.build({
foreignCollection: 'books',
foreignKey: 'bookId',
foreignKeyTarget: 'booksPk',
}),
personId: factories.columnSchema
.isPrimaryKey()
.build({ validation: [{ operator: 'Present' }] }),
.build({ isReadOnly: true, validation: [{ operator: 'Present' }] }),
person: factories.manyToOneSchema.build({
foreignCollection: 'person',
foreignKey: 'personId',
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('SchemaGeneratorFields > Many to Many', () => {
integration: null,
isFilterable: false,
isPrimaryKey: false,
isReadOnly: false,
isReadOnly: true,
isRequired: false,
isSortable: false,
isVirtual: false,
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('SchemaGeneratorFields > Many to Many', () => {
defaultValue: null,
enums: null,
integration: null,
isReadOnly: false,
isReadOnly: true,
isSortable: false,
isVirtual: false,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('SchemaGeneratorFields > One to Many', () => {
}),
authorId: factories.columnSchema.build({
columnType: 'Uuid',
isReadOnly: true,
}),
},
}),
Expand Down Expand Up @@ -47,6 +48,7 @@ describe('SchemaGeneratorFields > One to Many', () => {
inverseOf: 'writtenBooks',
reference: 'persons.personsPk',
relationship: 'BelongsTo',
isReadOnly: true,
type: 'Uuid',
});
});
Expand All @@ -69,7 +71,7 @@ describe('SchemaGeneratorFields > One to Many', () => {
integration: null,
isFilterable: false,
isPrimaryKey: false,
isReadOnly: false,
isReadOnly: true,
isRequired: false,
isSortable: false,
isVirtual: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('SchemaGeneratorFields > One to One', () => {
}),
authorId: factories.columnSchema.build({
columnType: 'String',
isReadOnly: true,
isSortable: true,
}),
author: factories.manyToOneSchema.build({
Expand Down Expand Up @@ -62,7 +63,7 @@ describe('SchemaGeneratorFields > One to One', () => {
integration: null,
isFilterable: false,
isPrimaryKey: false,
isReadOnly: false,
isReadOnly: true,
isRequired: false,
isSortable: false,
isVirtual: false,
Expand All @@ -89,7 +90,7 @@ describe('SchemaGeneratorFields > One to One', () => {
integration: null,
isFilterable: false,
isPrimaryKey: false,
isReadOnly: false,
isReadOnly: true,
isRequired: false,
isVirtual: false,
validations: [],
Expand Down