Structuring data from DB

Asked

Viewed 22 times

1

Hello fellow developers,

I have a query that brings geographical areas that are contained in larger units of area; these larger units are received via params (id) and may be one or more, without limitation.

PS. Before the geographical calculations I look for the polygons that represent these units (by the id received)

Important to say that for each received unit a filter is generated using sequelize.Where and sequelize.fn which is then injected into the query:

TIME OF FILTER CREATION:

if (query.length > 1) { 
    polygons.map(p => intersectionUnitsFilter.push(this.intersectsUnits(p.coordinates, p.type)));

FUNCTION THAT EFFECTIVELY CREATES THE FILTER (Intersectunits):

intersectsUnits(encoded: string, type: string) { // mudar esse encoded
    const coords: any = encoded
    
    let wkt = wktParser.convert({
      type: type,
      coordinates: coords,
    });
    
    const polygon = Sequelize.fn('ST_GeomFromText', wkt, 4326);
    const intersects = Sequelize.fn('ST_Intersects', Sequelize.fn('ST_SetSRID', Sequelize.col('polygon'), 4326), polygon);
    const intersectionFilter = Sequelize.where(intersects, true); // maybe i can add some code here to change the query and add some column

    return intersectionFilter;
  }

Just to contextualize all this, this is the moment of the final query => The code that inserts the filter generated from units is injected into the Filters variable that is used within the final query:

filters = { 
            ...filters,
            [Op.and]: {
              [Op.or]: {
                ...intersectionUnitsFilter
              }
            }
          }
        }
        
      }

      if (subfilters.length) {
        filters = {
          ...filters,
          [Op.or]: subfilters
        }
      }

      const limit = params.limit ? params.limit : 1000;
      let areas = await Area.findAll({
        where: filters,
        include: relationships,
        offset: params.page ? params.page * limit : 0, 
        limit: limit
      });

      return areas;
    
     

THE PROBLEM:

If I get more than 1 unit, all the filtered areas come mixed together, there is no distinction about the filter that was applied in a specific area, that is, I wouldn’t know which unit belongs to a certain area, I can only say that all the results are contained in one area or another and this makes some conditions difficult there at the frontend.

ATTEMPTS TO RESOLVE:

1 - First I tried to iterate through the filters and for each filter set a filter variable and inject in the query. In this way I would make queries separately, receiving for each iteration an array with areas belonging to ONE unit only. I could for example apply a Object.assign and build objects identifying which unit that array belongs to. I had difficulties because the await does not work inside callbacks and I was confused to make a Promise that was a wrapper for the whole query.

2 - I searched insistently for sequelize commands and types of queries that could do this work directly in the database, without success.

There is an easy and quick solution to the problem?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.