Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
@wmfs/hl-pg-client
Advanced tools
Provides a slightly higher level PostgreSQL client, that builds on pg-pool
Provides a slightly higher level PostgreSQL client, that builds on pg-pool
const HlPgClient = require('@hl-pg-client')
const client = new HlPgClient(connectionString)
To just run a simple query, just use the query
method:
const time = await client.query('SELECT NOW()')
const name = await client.query('select $1::text as name', ['brianc'])
console.log(name.rows[0].name, 'says hello at', time.rows[0].name)
You can also use a callback here if you'd like:
client.query('SELECT $1::text as name', ['brianc'], function (err, res) {
console.log(res.rows[0].name) // brianc
})
Internally, query
uses pg-pool.query which ensures that a connection is acquired from the pool
beforehand, and then properly released again afterwards.
To run multiple SQL statements within the same transaction, use the run
method:
await client.run(statementArray)
or using callbacks
client.run(statementArray, (err, res) => { ... })
Each item in the statement array should be an object with the form
{
sql: 'SQL statement to execute',
params: parameter array
// optional
preStatementHook: (item, ctx) => { ... },
// optional, called before the SQL is executed
postStatementHook: (result, ctx) => { ... },
// optional, called after the SQL is executed
action: (sql, params, client) => { ... }
// alternative action to perform instead of calling query
}
In the above the ctx
parameter is initially the empty object, {}
,
and it is passed to each postStatementHook in turn.
The return value of run
is ctx.returnValue
.
The run method ensures that a client is properly acquired from and released back to the pool, whether the transaction succeeds or fails. In the event of a statement failure, it will attempt to rollback the transaction.
const statements = [
{ "sql": "BEGIN;" },
{ "sql": "CREATE TEMP TABLE delete_supercopy_test_adults ON COMMIT DROP AS SELECT * FROM supercopy_test.adults WITH NO DATA;" },
{
"sql": "COPY delete_supercopy_test_adults(adult_no) FROM '/some/path/to/the/deletes/adults.csv' CSV HEADER;",
"action": copyStream
},
{ "sql": "DELETE FROM supercopy_test.adults WHERE (adult_no) IN (SELECT adult_no FROM delete_supercopy_test_adults);" },
{ "sql": "DROP TABLE delete_supercopy_test_adults;" }
{ "sql": "COMMIT;" }
]
await client.run(statements)
...
function copyStream(statement, params, client) {
// use pg-copy-stream to send local file to remote PostgreSQL server
return new Promise((resolve, reject) => {
const components = statement.match(/COPY (.*?) FROM '([^']*)'/)
const tableAndCols = components[1]
const filename = components[2]
const newStatement = `COPY ${tableAndCols} FROM STDIN CSV HEADER;`
const stream = client.query(
copyFrom(newStatement)
)
stream.on('end', function () {
resolve()
}).on('error', function (err) {
reject(err)
})
const fileStream = fs.createReadStream(filename)
fileStream.on('error', function (err) {
reject(err)
})
fileStream.pipe(stream)
})
} // copyStream
The runFile
method reads a file of SQL statements and executes them within a single transaction.
Ensure a PostgreSQL database is setup for testing and add a PG_CONNECTION_STRING environment variable when establishing a pool of PostgreSQL connections, for example:
PG_CONNECTION_STRING=postgres://postgres:postgres@localhost:5432/my_test_db
1.22.0 (2020-07-15)
FAQs
Provides a slightly higher level PostgreSQL client, that builds on pg-pool
The npm package @wmfs/hl-pg-client receives a total of 0 weekly downloads. As such, @wmfs/hl-pg-client popularity was classified as not popular.
We found that @wmfs/hl-pg-client demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.