Where to store SQL commands for execution

后端 未结 12 1456
后悔当初
后悔当初 2021-02-05 16:00

We face code quality issues because of inline mysql queries. Having self-written mysql queries really clutters the code and also increases code base etc.

Our code is clu

12条回答
  •  隐瞒了意图╮
    2021-02-05 16:58

    You could create a completely new npm module let's assume the custom-queries module and put all your complex queries in there.

    Then you can categorize all your queries by resource and by action. For example, the dir structure can be:

    /index.js -> it will bootstrap all the resources
    /queries
    /queries/sc (random name)
    /queries/psc (random name)
    /queries/complex (random name)
    

    The following query can live under the /queries/complex directory in its own file and the file will have a descriptive name (let's assume retrieveDistance)

    // You can define some placeholders within this var because possibly you would like to be a bit configurable and reuseable in different parts of your code.
    /* jshint ignore:start */
    var sql = "SELECT *"
    +" ,DATE_ADD(sc.created_at,INTERVAL 14 DAY) AS duedate"
    +" ,distance_mail(?,?,lat,lon) as distance,count(pks.skill_id) c1"
    +" ,count(ps.profile_id) c2"
    +" FROM TABLE sc"
    +" JOIN "
    +" PACKAGE_V psc on sc.id = psc.s_id "
    +" JOIN "
    +" PACKAGE_SKILL pks on pks.package_id = psc.package_id  "
    +" LEFT JOIN PROFILE_SKILL ps on ps.skill_id = pks.skill_id and ps.profile_id = ?"
    +" WHERE sc.type in "
    +" ('a',"
    +" 'b',"
    +" 'c' ,"
    +" 'd',"
    +" 'e',"
    +" 'f',"
    +" 'g',"
    +" 'h')"
    +" AND sc.status = 'open'"
    +" AND sc.crowd_type = ?"
    +" AND sc.created_at < DATE_SUB(NOW(),INTERVAL 10 MINUTE) "
    +" AND sc.created_at > DATE_SUB(NOW(),INTERVAL 14 DAY)"
    +" AND distance_mail(?, ?,lat,lon) < 500"
    +" GROUP BY sc.id"
    +" HAVING c1 = c2 "
    +" ORDER BY distance;";
    /* jshint ignore:end */
    
    module.exports = sql;
    

    The top level index.js will export an object with all the complex queries. An example can be:

    var sc = require('./queries/sc');
    var psc = require('./queries/psc');
    var complex = require('./queries/complex');
    
    // Quite important because you want to ensure that no one will touch the queries outside of
    // the scope of this module. Be careful, because the Object.freeze is freezing only the top
    // level elements of the object and it is not recursively freezing the nested objects.
    var queries = Object.freeze({
      sc: sc,
      psc: psc,
      complex: complex
    });
    
    module.exports = queries;
    

    Finally, on your main code you can use the module like that:

    var cq = require('custom-queries');
    var retrieveDistanceQuery = cq.complex.retrieveDistance;
    // @todo: replace the placeholders if they exist
    

    Doing something like that you will move all the noise of the string concatenation to another place that you would expect and you will be able to find quite easily in one place all your complex queries.

提交回复
热议问题