Ionică Bizău
Ionică Bizău

Reputation: 113455

Aggregation query using Sequelize and SQLITE

Using Sequelize and SQLite, and having a table like:

A | B
a | 1
b | 2
c | 3

I would like to create a map using a query via Sequelize, giving the following result:

{
  a: 1,
  b: 2,
  c: 3
}

How can I do that? I can do a findAll and then a docs.reduce(...), but I would like to extract that directly from SQLite if possible.

Upvotes: 0

Views: 156

Answers (1)

Shawn
Shawn

Reputation: 52549

If your version of sqlite supports the JSON1 extension, you can use something like

SELECT json_group_object(A, B) FROM yourtable;

to get a single row holding a single column that's a string holding a JSON object. That should be trivial to convert to a javascript map.

db<>fiddle example

Upvotes: 1

Related Questions