slonik-utilities icon indicating copy to clipboard operation
slonik-utilities copied to clipboard

Add "dictionary" query method

Open gajus opened this issue 6 years ago • 3 comments

I find myself often repeating this pattern:

const dictionary = {};

const cinemaForeignSeatTypes = await connection.any(sql`
  SELECT fuid, id
  FROM cinema_foreign_seat_type
  WHERE cinema_id = ${cinemaId}
`);

for (const cinemaForeignSeatType of cinemaForeignSeatTypes) {
  dictionary[cinemaForeignSeatType.fuid] = cinemaForeignSeatType.id;
}

I think this pattern appears often enough to mandate for a convenience method dictionary, i.e.

const cinemaForeignSeatTypeDictionary = await connection.dictionary(sql`
  SELECT fuid, id
  FROM cinema_foreign_seat_type
  WHERE cinema_id = ${cinemaId}
`);

Object.keys(cinemaForeignSeatTypeDictionary); // `fuid` values
Object.values(cinemaForeignSeatTypeDictionary); // `id` values

gajus avatar Aug 09 '19 13:08 gajus

This should also throw an error if keys are duplicate.

gajus avatar Oct 22 '19 03:10 gajus

Maybe it's worth to make it more flexible by adding a formatter which formats the rows before returning them?

e.g.:

const formatter = rows => 
  rows.reduce(
    (obj, value) => {
      obj[value.id] = value
      return obj
    },
    {}
  )

const dictionary = await connection.any(
  sql`
    SELECT fuid, id
    FROM cinema_foreign_seat_type
    WHERE cinema_id = ${cinemaId}
  `,
  formatter
);

And giving a few formatter for few standard use cases like these { id: row } transformation or the more specialised use case if { firstColumn: secondColumn } transformation.

Sharaal avatar Oct 23 '19 11:10 Sharaal

The reason this has remained an open issue is because it feels like an overly specialised feature, that likely better belongs in an abstraction such as https://github.com/gajus/slonik-utilities

gajus avatar Oct 23 '19 11:10 gajus