0

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.

0

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

How do you do that using sequelize?
@IonicăBizău That's an ORM, right? They usually have some way to execute normal SQL statements.
Yes, I was wondering if there is a Sequelize-way to do that, but running a custom query will work too. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.