feathersjs / feathers

The API and real-time application framework
https://feathersjs.com
MIT License
15.03k stars 750 forks source link

[schema] Composite key #3016

Closed mdartic closed 1 year ago

mdartic commented 1 year ago

I'm trying to port feathers-objection and discover in the AdapterServiceOptions that id can only be a string

https://github.com/feathersjs/feathers/blob/dove/packages/adapter-commons/src/declarations.ts#L30

How do we manage composite keys ?

For example, in an association table, where the primary key is composed of the two foreign tables.

Actually, I transform the id property of service options in a string | string[] and rewrite some parts of the _find methods like this

    const idColumns = Array.isArray(this.id) ? this.id : [this.id]
    const fullIdColumns = idColumns.map((idc: string) => `${this.table}.${idc}`)

    // provide default sorting if its not set
    if (!filters.$sort) {
      builder.orderBy(
        fullIdColumns.map((fic: string) => ({
          column: fic,
          order: 'asc',
        })) as ColumnRefOrOrderByDescriptor[],
      )
    }

And also for the count request.

Do you think this is the "right" way to do it, or maybe there is another way ?