Skip to content

Repository files navigation

Postache

Configuration

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 postache function 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)

Usage

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 $limit

This is compiled to:

select*from articles
offset $offset
limit $limit

Which you call like this:

db.getArticles({offset: 0,limit: 10,})

About

Pg-then + mustache + object queries = profit

Resources

Stars

2 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages