Skip to content

Idea: Unique keys #32

Description

@jmsv

At the moment, (where food is equal to the README example)

const oatyFood = new OatyArray(food, { keys: ['id', 'type'] })
const idOne = oatyFood.get("fruit", 1)

idOne has the following type:

const idOne: {
    id: number;
    name: string;
    type: string;
}[] | undefined

which is annoying for two reasons:

  1. Need code to check if idOne is defined
  2. Need to check array length is 1

That second reason is particularly annoying if you know that id is unique the source data.

Would be cool if oaty supported something like:

const oatyFood = new OatyArray(food, {
  keys: [
    { key: "id", unique: true },
    { key: "type" }
  ],
});

and could even make this backwards-compatible, e.g.

const oatyFood = new OatyArray(food, {
  keys: [{ key: "id", unique: true }, "type"],
});

and then the type for oatyFood.get("id", 1) should become

const idOne: {
    id: number;
    name: string;
    type: string;
} | undefined

which still requires a (necessary and not that annoying) check for undefined, but means you don't need to check the array (not necessary and very annoying)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions