constpostache=require("postache")// this is a pg config object, check the offical documentation: https://node-postgres.com/features/connecting#programmaticconstpgConfig={connectionString: "postgres://localhost:5432/mydb",ssl: {ca: fs.readFileSync("ca.pem").toString(),}}constdb=postache(postache.loadDir(path.join(__dirname,"queries")),{articlesTable: "articles",},pgConfig)Note: Older version of this library used custom connection strings which is no longer supported. You can pass a pg config object to the
postachefunction instead.constpostache=require("postache")constconnectionString="postgres://localhost:5432/mydb"constpgConfig={ssl: {ca: fs.readFileSync("ca.pem").toString(),}}constdb=postache(postache.loadDir(path.join(__dirname,"queries")),{articlesTable: "articles",},connectionString,pgConfig// <--- this is optional parameter that will override pg config created from connection string)
Instead of
db.query("select * from articles offset $1 limit $2",[params.offset,params.limit])and in the queries fold your have a file getArticles.sql:
select*from {{articlesTable}}
{{> _paging}}and a file _paging.sql which contains:
offset $offset
limit $limitThis is compiled to:
select*from articles
offset $offset
limit $limitWhich you call like this:
db.getArticles({offset: 0,limit: 10,})