question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Flatten result from .findAll

See original GitHub issue

Issue Description

Is your feature request related to a problem? Please describe.

I usually need to get the value list of a certain column instead of the whole row data. To do so, currently, I call .findAll to get a list of objects then .map to have the final result. Basically need to do 2 steps and create an intermediate variable. This happens quite frequently in my code.

Django has an option to flatten the result https://docs.djangoproject.com/en/2.2/ref/models/querysets/#values-list

Describe the solution you’d like

Add .flat() or .flatten() function

const restaurantIds = await Restaurant.findAll({
    where: {
        status: "active"
    },
    attributes: ["id"]
}).flat();

Why should this be in Sequelize

If someone passes 1 column name to attributes, most likely the next step will be transforming the result to a list of single-column values.

And, the above code is elegant and more convenient compared with the following:

const restaurants = await Restaurant.findAll({
    where: {
        status: "active"
    },
    attributes: ["id"]
});
const restaurantIds = restaurants.map((r) => r.id);

// OR:
const restaurantIds = (await Restaurant.findAll({
    where: {
        status: "active"
    },
    attributes: ["id"]
})).map((r) => r.id);

// OR:
const restaurantIds = await Restaurant.findAll({
    where: {
        status: "active"
    },
    attributes: ["id"]
}).then((result) => result.map((r) => r.id));

Describe alternatives/workarounds you’ve considered

Yes, the solutions I show above. It’s ok but not super convenient.

Additional context

I can work on this

Issue Template Checklist

Is this issue dialect-specific?

  • No. This issue is relevant to Sequelize as a whole.
  • Yes. This issue only applies to the following dialect(s): XXX, YYY, ZZZ

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don’t know how to start, I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
papbcommented, Oct 23, 2019

Hello! I think this is unnecessary complexity to be added in the core library. Calling .map(r => r.id) seems easy enough to me. In fact, if we had this .flat() or even a { flat: true } option, readers would have to check our docs to really discover what it is. If you are worried about performance, consider using { raw: true } in your query. What do you think?

0reactions
github-actions[bot]commented, Nov 6, 2021

This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the “stale” label. 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

Return flat object from sequelize with association
I tried to use different modules to flatten the objects (inside a loop, each object individually), but I always got an error telling...
Read more >
str(array) -> re.findall speedy solution for Flatten a List by pavlik
He categorized a series of numbers and as the result of his efforts, a simple sequence of numbers became a deeply-nested list.
Read more >
Flatten List in Python - Javatpoint
In this function, we have used the for loop where the elements from the nested list are appended to the empty list we...
Read more >
Collection (Groovy JDK enhancements)
Flatten a Collection. ... Returns: true if this collection changed as a result of the call. Since: 1.7.2 ... public Collection findAll(Closure closure)....
Read more >
drakkarski's solution for Flatten Array in Groovy on Exercism
Iteration 1 · 1. class FlattenArray { · 2. static List flatten(List l) { · 3. l.flatten().findAll {it != null} · 4. }...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found