ForestAdmin / agent-nodejs

🌱 Node.js agent for Forest Admin
GNU General Public License v3.0
66 stars 8 forks source link

Missing field rewrite OneToMany relation targets #1181

Open christian-mibex opened 6 days ago

christian-mibex commented 6 days ago

Expected behavior

We migrated a while ago from the express-sequalize agent to the nodejs agent. During that migration we wanted to keep the camelCase naming in the UI facing schema to avoid rework. As suggested here we rename the fields and collections.

We expect the same behavior for added relations:

    licenses.addOneToManyRelation('comments', 'licenseComments', {
        originKey: 'license_id',
        originKeyTarget: 'license_name',
    })

Actual behavior

The schema generation fails with the following stack trace

TypeError: Cannot read properties of undefined (reading 'columnType')
    at Function.buildToManyRelationSchema (/node_modules/@forestadmin/agent/src/utils/forest-schema/generator-fields.ts:134:26)
    at Function.buildRelationSchema (/node_modules/@forestadmin/agent/src/utils/forest-schema/generator-fields.ts:229:38)
    at Function.buildSchema (/node_modules/@forestadmin/agent/src/utils/forest-schema/generator-fields.ts:44:40)
    at /node_modules/@forestadmin/agent/src/utils/forest-schema/generator-collection.ts:51:42
    at Array.map (<anonymous>)
    at Function.buildFields (/node_modules/@forestadmin/agent/src/utils/forest-schema/generator-collection.ts:51:8)
    at Function.buildSchema /node_modules/@forestadmin/agent/src/utils/forest-schema/generator-collection.ts:18:20)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

When I debug the code, I see that the relation in the schema generation code has the snake_case name for originKeyTarget which then results in an undefinded field and brakes the schema generation with the stack trace above.

{
  type: 'OneToMany',
  foreignCollection: 'licenseComments',
  originKey: 'licenseId',
  originKeyTarget: 'license_name'
}

Context

Thenkei commented 3 days ago

Hello @christian-mibex,

Sorry for the late response. We gonna have a look at this next week.

As suggested here we rename the fields and collections.

⚠️ This only rename collections not field.

We will start by reproducting your issue and then have a closer look on how it can be resolved.

Kind regards, Morgan

christian-mibex commented 11 hours ago

No worries, thanks for having a look!

What is then the proposed solution to migrate from express-sequelize (camelCase in code & forestadmin, but snake_case in database) to the nodejs agent without reworking the UI?

Best regards Christian