Where to store SQL commands for execution

后端 未结 12 1466
后悔当初
后悔当初 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:36

    I prefer putting every bigger query in one file. This way you can have syntax highlighting and it's easy to load on server start. To structure this, i usually have one folder for all queries and inside that one folder for each model.

    # queries/mymodel/select.mymodel.sql
    SELECT * FROM mymodel;
    
    // in mymodel.js
    const fs = require('fs');
    const queries = {
      select: fs.readFileSync(__dirname + '/queries/mymodel/select.mymodel.sql', 'utf8')
    };
    

提交回复
热议问题