Is there a way to destringify a subarray in order to use it as a Sequelize operation in my query filter?

Viewed 14

I am attempting to built query parameters for a route that returns all of the "spot" records in my Sequelize database. I am close to getting this to work; my only problem that I am running into now, is that my operations, which I have nested as sub-arrays in an array, (e.g [[Op.lt], [Op.gt]]) must be stringified (['[Op.lt]', '[Op.gt]']) in order to prevent a "Cannot convert a Symbol value to a string" error. However, if I stringify these sub-arrays, I receive an obvious error due to the stringified array being an invalid data type.

Here is my current code:

let { minLat, maxLat, minLng, maxLng, minPrice, maxPrice } = req.query;  
let queryParams = [minLat, maxLat, minLng, maxLng, minPrice, maxPrice];
let queryParamNames = ['lat', 'lat', 'lng', 'lng', 'price', 'price'];
let operations = ['[Op.lt]', '[Op.gt]'];

// Empty object to assign where clauses and operations to 
const where = {};

for (let i = 0; i < queryParams.length; i++) {
    // I am iterating over my queryParams array as well as my queryName array in order to assign
    // proper key names to my 'where' object that map to the column names in my database
    let query = queryParams[i];
    let queryName = queryParamNames[i];
    // Grabbing the desired operations at appropriate indices from my operations array
    let lt = operations[0];
    let gt = operations[1];

    // If the query is not undefined and I do not already have the query inside of my 'where' object
    // assign a key to my object as the name of the appropriate column and the operation as the value
    // in order to perform the operation on the desired column
    if (query !== undefined && where[queryName] === undefined && query === minLat) where[queryName] = {[gt]: Number(query)};

    // Can be ignored
    if (query !== undefined && where[queryName] === undefined && query === maxLat) where[queryName] = {[lt]: Number(query)};
    if (query !== undefined && where[queryName] === undefined && query === minLng) where[queryName] = {[gt]: Number(query)};
    if (query !== undefined && where[queryName] === undefined && query === maxLng) where[queryName] = {[lt]: Number(query)};
    if (query !== undefined && where[queryName] === undefined && query === minPrice) where[queryName] = {[gt]: Number(query)};
    if (query !== undefined && where[queryName] === undefined && query === maxPrice) where[queryName] = {[lt]: Number(query)};
};

This is my current output when logging my 'where' object: { lat: { '[Op.gt]': 40 } }

Which is great, except I need '[Op.gt]' to be [Op.gt], as such: { lat: { [Op.gt]: 40 } }

This seems like an easy fix, but I have attempted several different approaches to no avail. Any insight into how I can destringify my sub-arrays would be greatly appreciated.

0 Answers
Related