#Is it possible to map nested properties directly in the query?

1 messages · Page 1 of 1 (latest)

crimson ivy
#

I have three models Organisation, Application and ApiKey. An organisation can have many applications, an application can have many api-keys. When looking for an api-key I want to include the id of the application AND the id of the organisation. Since the api-key is owned by an application and an application is owned by the organisation I have to work with an include on the application field which leads to the organisation_id being nested in the application object.

Is it possible, without adding an extra relationship between Organisation and ApiKey, to get the organisation_id in the main object?

My current object:

{
  id: 'd61536c3-2a68-4d98-b319-9efaffc020bf',
  code: '5df07f13-86d7-4d67-b56e-6c9668dd6bca',
  is_active: true,
  application_id: '68e7e353-3def-4b94-b3f1-9735b45a1ebf',
  application: {
    id: '68e7e353-3def-4b94-b3f1-9735b45a1ebf',
    name: 'Test app',
    url: null,
    organisation_id: '06531a5d-b9db-43a9-ac98-a9b61f83eb98',
  }
}

What I would like:

{
  id: 'd61536c3-2a68-4d98-b319-9efaffc020bf',
  code: '5df07f13-86d7-4d67-b56e-6c9668dd6bca',
  is_active: true,
  application_id: '68e7e353-3def-4b94-b3f1-9735b45a1ebf',
  organisation_id: '06531a5d-b9db-43a9-ac98-a9b61f83eb98'
}

prisma.schema

model Organisation {
  id   String           @id @default(uuid())
  name String           @unique

  applications Application[]

  @@map("organisations")
}

model Application {
  id   String           @id @default(uuid())
  name String           

  organisation   Organisation @relation(fields: [organisation_id], references: [id])
  organisation_id String

  api_keys ApiKey[]

  @@map("applications")
}

model ApiKey {
  id   String           @id @default(uuid())
  code String @unique
  is_active Boolean @default(true)

  application   Application @relation(fields: [application_id], references: [id])
  application_id String

  @@map("api_keys")
}
silent hinge
#

Hey @crimson ivy 👋
If I understand correctly, you only want to get the organisation_id column from the application relation and not the entire application object, right?

crimson ivy
#

Hi @silent hinge ,

Well it's a two part question. Indeed I only want the organisation_id of the application object. If I'm not mistaken this can be done by using select.
The second part is if it's possible to reshape the object in the prisma query so that the organisation_id can be moved up from the nested application object to the main object

So

{
    id: '56867'
    organisation_id: '34785',
    application_id: '76567',
}

instead of

{
    id: '56867'
    application_id: '76567',
    application: {
        organisation_id: '34785',
    }
}