Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document use of query files #2

Open
gajus opened this issue Feb 15, 2019 · 2 comments
Open

Document use of query files #2

gajus opened this issue Feb 15, 2019 · 2 comments

Comments

@gajus
Copy link
Owner

gajus commented Feb 15, 2019

pg-promise documentation features a concept of query files.

This is effectively a helper function that is used to prepare a statement using pg-promise, e.g.

const path = require('path');

// Helper for linking to external query files:
function sql(file) {
    const fullPath = path.join(__dirname, file);
    return new pgp.QueryFile(fullPath, {minify: true});
}

// Create a QueryFile globally, once per file:
const sqlFindUser = sql('./sql/findUser.sql');

db.one(sqlFindUser, {id: 123})
    .then(user => {
        console.log(user);
    })
    .catch(error => {
        if (error instanceof pgp.errors.QueryFileError) {
            // => the error is related to our QueryFile
        }
    });

In case of Slonik, this would be best implement using sql.raw, e.g.

const getQuery = (queryName, parameters = []) => {
  return sql`${sql.raw(
    fs.readFileSync(path.resolve(__dirname, 'queries', queryName + '.sql'), parameters)
  )}`;
};

connection.query(getQuery('foo', [1, 2, 3]));

I don't see an immediate need to either add helper for loading queries or to advertise this as a distinct feature.

What abstraction could Slonik offer here and what value would it add?

@vitaly-t
Copy link

vitaly-t commented Feb 25, 2019

b.t.w., when using {minify: true}, it also minifies the SQL, by removing comments, and compressing the query, using module pg-minify that I wrote while I was working on pg-promise.

Without it, working with large SQL files becomes awkward. Some large files may contain whole revisions commented out, plus a lot of inline documentation.

return sql${sql.raw( fs.readFileSync(path.resolve(__dirname, 'queries', queryName + '.sql'), parameters) )};

I'm not sure how this can work at all. The entire ES6 string template concept is based on evaluation of the inner expressions. Which means you cannot format any values?

That is the reason I never seriously considered ES6 template strings for query formatting. See also.

@hollowcat
Copy link

Would be helpful for setup & teardown in integration tests.

Might add a dev mode flag, such that a database definition file is supplied to Slonik as configuration and if in dev mode, it sets things up before running first query. Another flag to indicate if tables need to be recreated on each run, and if not, inline a SQL procedure for resetting them. Also inline a snapshot SQL procedure that remembers current table contents.

Then while playing with the app being developed one could use psql to snapshot and reset to states, and in integration tests this could be used as well.

@gajus gajus transferred this issue from gajus/slonik Nov 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants