Where to store SQL commands for execution

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

    I suggest you store your queries in .sql files away from your js code. This will separate the concerns and make both code & queries much more readable. You should have different directories with nested structure based on your business.

    eg:

    queries
    ├── global.sql
    ├── products
    │   └── select.sql
    └── users
        └── select.sql
    

    Now, you just need to require all these files at application startup. You can either do it manually or use some logic. The code below will read all the files (sync) and produce an object with the same hierarchy as the folder above

    var glob = require('glob')
    var _  = require('lodash')
    var fs = require('fs')
    
    // directory containing all queries (in nested folders)
    var queriesDirectory = 'queries'
    
    // get all sql files in dir and sub dirs
    var files = glob.sync(queriesDirectory + '/**/*.sql', {})
    
    // create object to store all queries
    var queries = {}
    
    _.each(files, function(file){
        // 1. read file text
        var queryText = fs.readFileSync(__dirname + '/' + file, 'utf8')
    
        // 2. store into object
        // create regex for directory name
        var directoryNameReg = new RegExp("^" + queriesDirectory + "/")
    
        // get the property path to set in the final object, eg: model.queryName
        var queryPath = file
            // remove directory name
            .replace(directoryNameReg,'')
            // remove extension
            .replace(/\.sql/,'')
            // replace '/' with '.'
            .replace(/\//g, '.')
    
        //  use lodash to set the nested properties
        _.set(queries, queryPath, queryText)
    })
    
    // final object with all queries according to nested folder structure
    console.log(queries)
    

    log output

    {
        global: '-- global query if needed\n',
        products: {
            select: 'select * from products\n'
        },
    
        users: {
            select: 'select * from users\n'
        }
    }
    

    so you can access all queries like this queries.users.select

提交回复
热议问题