Skip to content

Commit

Permalink
Merge 163dd9f into b66f34f
Browse files Browse the repository at this point in the history
  • Loading branch information
seromenho committed Jan 10, 2021
2 parents b66f34f + 163dd9f commit 21e2a3e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ It works with _Redis_, process _Memory_, _Cluster_ or _PM2_, _Memcached_, _Mongo

**Ready for growth.** It provides unified API for all limiters. Whenever your application grows, it is ready. Prepare your limiters in minutes.

**Friendly.** No matter which node package you prefer: `redis` or `ioredis`, `sequelize` or `knex`, `memcached`, native driver or `mongoose`. It works with all of them.
**Friendly.** No matter which node package you prefer: `redis` or `ioredis`, `sequelize`/`typeorm` or `knex`, `memcached`, native driver or `mongoose`. It works with all of them.

**In memory blocks.** Avoid extra requests to store with [inmemoryBlockOnConsumed](https://github.com/animir/node-rate-limiter-flexible/wiki/Options#inmemoryblockonconsumed).

Expand Down Expand Up @@ -122,7 +122,7 @@ Some copy/paste examples on Wiki:
* [RateLimiterMemcache](https://github.com/animir/node-rate-limiter-flexible/wiki/Memcache)
* [RateLimiterMongo](https://github.com/animir/node-rate-limiter-flexible/wiki/Mongo) (with [sharding support](https://github.com/animir/node-rate-limiter-flexible/wiki/Mongo#mongodb-sharding-options))
* [RateLimiterMySQL](https://github.com/animir/node-rate-limiter-flexible/wiki/MySQL) (support Sequelize and Knex)
* [RateLimiterPostgres](https://github.com/animir/node-rate-limiter-flexible/wiki/PostgreSQL) (support Sequelize and Knex)
* [RateLimiterPostgres](https://github.com/animir/node-rate-limiter-flexible/wiki/PostgreSQL) (support Sequelize, TypeORM and Knex)
* [RateLimiterCluster](https://github.com/animir/node-rate-limiter-flexible/wiki/Cluster) ([PM2 cluster docs read here](https://github.com/animir/node-rate-limiter-flexible/wiki/PM2-cluster))
* [RateLimiterMemory](https://github.com/animir/node-rate-limiter-flexible/wiki/Memory)
* [RateLimiterUnion](https://github.com/animir/node-rate-limiter-flexible/wiki/RateLimiterUnion) Combine 2 or more limiters to act as single
Expand Down
4 changes: 4 additions & 0 deletions lib/RateLimiterPostgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
return this.client.connectionManager.getConnection();
case 'knex':
return this.client.client.acquireConnection();
case 'typeorm':
return Promise.resolve(this.client.driver.master);
default:
return Promise.resolve(this.client);
}
Expand All @@ -114,6 +116,8 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
return this.client.connectionManager.releaseConnection(conn);
case 'knex':
return this.client.client.releaseConnection(conn);
case 'typeorm':
return true;
default:
return true;
}
Expand Down
41 changes: 41 additions & 0 deletions test/RateLimiterPostgres.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,28 @@ describe('RateLimiterPostgres with fixed window', function RateLimiterPostgresTe
});
});

it('private _getConnection returns client for TypeORM', (done) => {
class Pool {
Pool() {}
query() {}
}

const typeORMConnection = {
driver: { master: new Pool()}
}

const rateLimiter = new RateLimiterPostgres({
storeClient: typeORMConnection,
storeType: 'typeorm',
}, () => {
rateLimiter._getConnection()
.then((conn) => {
expect(conn).to.equal(typeORMConnection.driver.master);
done();
});
});
});

it('Pool does not require specific connection releasing', (done) => {
class Pool {
Pool() {}
Expand Down Expand Up @@ -508,6 +530,25 @@ describe('RateLimiterPostgres with fixed window', function RateLimiterPostgresTe
});
});

it('TypeORM does not require specific connection releasing', (done) => {
class Pool {
Pool() {}
query() {}
}

const typeORMConnection = {
driver: { master: new Pool()}
}

const rateLimiter = new RateLimiterPostgres({
storeClient: typeORMConnection,
storeType: 'typeorm',
}, () => {
expect(rateLimiter._releaseConnection()).to.equal(true);
done();
});
});

it('does not expire key if duration set to 0', (done) => {
const testKey = 'neverexpire';
const rateLimiter = new RateLimiterPostgres({
Expand Down

0 comments on commit 21e2a3e

Please sign in to comment.