简体   繁体   English

用sequelize链接查询

[英]Chaining queries with sequelize

In sequelize I want to do something like chaining findAll function calls. 在续集中,我想做一些类似的链接findAll函数调用。

I would like to make a findAll on a model like 我想在像这样的模型上进行一次findAll

const newYorkers = await People.findAll({ where: { city: 'New York' } });

and then use newYorkers as parameter for a function, that makes another findAll on that 然后使用newYorkers作为函数的参数,从而在该函数上进行另一个findAll

const get5ThAvenueInhabitants = (citiesInhabitants) => {
  return await citiesInhabitants.findAll({ where: { street: '5th Avenue' } });
};

get5ThAvenueInhabitants(newYorkers);

That won't work, because after the first findAll, the result isn't a model anymore, but an array of results. 那将行不通,因为在第一个findAll之后,结果不再是模型,而是一系列结果。

Is it possible to achieve this with sequelize? 是否可以通过续集来实现?

If all you want to do is findAll People who live in New York and 5th Avenue , why don't you use and operator? 如果你想要做的是的findAll People谁住在New York5th Avenue ,你为什么不使用and操作?

It would be as simple as 就这么简单



const newYorkers = People.findAll({ 
  where: { 
       city: 'New York',
       street: '5th Avenue',
      },
    ],
  },
});

First besides what you asking, I don't understand the logic behind the function get5ThAvenueInhabitants() . 首先,除了您要询问的内容外,我不了解get5ThAvenueInhabitants()函数背后的逻辑。 As the previous answer, you can filter directly using where: { city: 'New York', street: '5th Avenue'} . 作为上一个答案,您可以使用以下where: { city: 'New York', street: '5th Avenue'}直接过滤where: { city: 'New York', street: '5th Avenue'} There is no need to query first 'New York' and then '5th Avenue'. 无需先查询“纽约”,再查询“第五大道”。

Now coming back on what you really ask, about chaining the request you can use async/await like this: 现在回到您真正要问的问题,关于链接请求,您可以像这样使用async / await:

const mainFunction = async () => {
    const newYorkers = await People.findAll({ where: { city: 'New York' } });
    get5ThAvenueInhabitants(newYorkers);
}

This way, the newYorkers will wait untill all the data is fetch and then proceed to get5ThAvenueInhabitants() . 这样, newYorkers将等待直到所有数据都被提取,然后继续进行get5ThAvenueInhabitants()

As you mention, People.findAll({ where: { city: 'New York' } }); 如您所述, People.findAll({ where: { city: 'New York' } }); returns an array of instances and not a model . 返回instances数组,而不是model数组。 The instance object does expose methods like destroy , update , etc., and so you can, in a sense, do a kind of query "chaining", by using the methods on a returned instance, like this: instance对象确实公开了诸如destroyupdate等之类的方法,因此,从某种意义上讲,您可以通过在返回的实例上使用这些方法来执行某种查询“链接”,如下所示:

People.findAll({ where: { city: 'New York' } }).then(people => {
     people.forEach(person => {
         person.destroy();
     })
 });

To filter the returned collection in your application code, as opposed to using a DB query, you could do something like this: 要在应用程序代码中过滤返回的集合,而不是使用数据库查询,可以执行以下操作:

 People.findAll({ where: { city: 'New York' } }).then(people => {
     people.filter(person => {
        if(person.street === '5th Avenue'){
            return person;
        }
     })
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM