@knorm/postgres
Advanced tools
Comparing version 1.0.1 to 1.1.0
const { Knorm } = require('@knorm/knorm'); | ||
const { Pool } = require('pg'); | ||
const { parse } = require('pg-connection-string'); | ||
const sqlBricksPostgres = require('sql-bricks-postgres'); | ||
@@ -14,5 +15,3 @@ | ||
this.pool = new Pool( | ||
typeof connection === 'string' | ||
? { connectionString: connection } | ||
: connection | ||
typeof connection === 'string' ? parse(connection) : connection | ||
); | ||
@@ -19,0 +18,0 @@ |
{ | ||
"name": "@knorm/postgres", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Postgres plugin for knorm", | ||
@@ -57,4 +57,5 @@ "main": "index.js", | ||
"pg": "^7.4.2", | ||
"pg-connection-string": "^2.0.0", | ||
"sql-bricks-postgres": "^0.5.0" | ||
} | ||
} |
@@ -81,2 +81,14 @@ const knorm = require('@knorm/knorm'); | ||
it('supports options in the connection string', async () => { | ||
const knormPostgres = new KnormPostgres({ | ||
connection: | ||
'postgres://postgres@127.0.0.1:5432/postgres?max=5&idleTimeoutMillis=1000' | ||
}); | ||
// TODO: pg-connection-string should parseInt on the `max` option | ||
await expect(knormPostgres.pool.options.max, 'to be', '5'); | ||
const client = await knormPostgres.acquireClient(); | ||
await knormPostgres.releaseClient(client); | ||
}); | ||
it('uses postgres environment variables if no `connection` config is provided', async () => { | ||
@@ -1076,6 +1088,8 @@ process.env.PGHOST = '127.0.0.1'; | ||
let User; | ||
let plugin; | ||
let Transaction; | ||
before(async () => { | ||
const orm = knorm().use(knormPostgres({ connection })); | ||
plugin = knormPostgres({ connection }); | ||
const orm = knorm().use(plugin); | ||
@@ -1193,3 +1207,3 @@ Transaction = orm.Transaction; | ||
it('runs queries with one client', async () => { | ||
const spy = sinon.spy(Transaction.prototype, 'acquireClient'); | ||
const spy = sinon.spy(plugin, 'acquireClient'); | ||
await new Transaction(async function() { | ||
@@ -1204,3 +1218,3 @@ await this.models.User.insert([{ id: 1, name: 'foo' }]); | ||
it('runs queries with one client even with nested models', async () => { | ||
const spy = sinon.spy(Transaction.prototype, 'acquireClient'); | ||
const spy = sinon.spy(plugin, 'acquireClient'); | ||
await new Transaction(async function() { | ||
@@ -1427,3 +1441,3 @@ class FooUser extends this.models.User { | ||
const transaction = new Transaction(); | ||
const spy = sinon.spy(transaction, 'acquireClient'); | ||
const spy = sinon.spy(plugin, 'acquireClient'); | ||
await transaction.models.User.insert([{ id: 1, name: 'foo' }]); | ||
@@ -1433,2 +1447,3 @@ await transaction.models.User.insert([{ id: 2, name: 'bar' }]); | ||
await expect(spy, 'was called once'); | ||
spy.restore(); | ||
}); | ||
@@ -1438,3 +1453,3 @@ | ||
const transaction = new Transaction(); | ||
const spy = sinon.spy(transaction, 'acquireClient'); | ||
const spy = sinon.spy(plugin, 'acquireClient'); | ||
class FooUser extends transaction.models.User { | ||
@@ -1457,2 +1472,3 @@ async foo() { | ||
await expect(spy, 'was called once'); | ||
spy.restore(); | ||
}); | ||
@@ -1459,0 +1475,0 @@ |
72944
1923
5
+ Addedpg-connection-string@^2.0.0