Code Snippet - Sequelize

mail@pastecode.io avatar
unknown
javascript
a year ago
2.1 kB
5
Indexable
Never
/*
select pm.product_name, ft.farm_type_name, fgi.farm_name, facg.product_cultivation_info_id, count(facg.product_cultivation_info_id) as total_batch
FROM farm_actual_crops_grown facg
inner join product_cultivation_master pcm
on facg.product_cultivation_info_id = pcm.id
inner join farm_general_info fgi 
on facg.farm_id = fgi.id
inner join farm_types ft 
on pcm.farm_type_id = ft.id
inner join products_master pm 
on pcm.product_id = pm.id 
where facg.farm_id = 1
group by pcm.farm_type_id, fgi.farm_name, ft.id, pm.id, facg.product_cultivation_info_id
order by pm.product_name 
limit 10 offset 0;
*/

FarmActualCropsGrown.findAll({
        attributes: [
            'product_cultivation_info_id',
            [sequelize.fn('COUNT', sequelize.col('product_cultivation_info_id'), 'total_batch')]
        ],
        where: {
            farm_id: 1
        },
        include: [
            {
                model: ProductCultivationMaster,
                as: 'product_cultivation_master',
                required: true,
                include: [
                    {
                        model: FarmTypes,
                        as: 'farm_types',
                        attributes: ['farm_type_name'],
                        required: true
                    },
                    {
                        model: ProductsMaster,
                        as: 'products_master',
                        attributes: ['product_name'],
                        required: true
                    }
                ]
            },
            {
                model: FarmGeneralInfo,
                as: 'farm_general_info',
                attributes: ['farm_name'],
                required: true
            }
        ],
        order: 'product_name',
        group: 'product_cultivation_info_id',
        limit: 10,
        offset: undefined
    })


/*
select *
from farm_actual_crops_grown facg
where product_cultivation_info_id = 12 and farm_id = 1;
*/ 

FarmActualCropsGrown.findAll({
        where: {
            farm_id: 1,
            product_cultivation_info_id: 12
        }
    })