Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(postgres): add optional schema name parameter for postgres #227

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions lib/RateLimiterPostgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
* storeClient: postgresClient,
* storeType: 'knex', // required only for Knex instance
* tableName: 'string',
* schemaName: 'string', // optional
* }
*/
constructor(opts, cb = null) {
Expand All @@ -23,6 +24,7 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
this.clientType = opts.storeType;

this.tableName = opts.tableName;
this.schemaName = opts.schemaName;

this.clearExpiredByTimeout = opts.clearExpiredByTimeout;

Expand Down Expand Up @@ -52,11 +54,15 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
}
}

_getTableIdentifier() {
return this.schemaName ? `"${this.schemaName}"."${this.tableName}"` : `"${this.tableName}"`;
}

clearExpired(expire) {
return new Promise((resolve) => {
const q = {
name: 'rlflx-clear-expired',
text: `DELETE FROM "${this.tableName}" WHERE expire < $1`,
text: `DELETE FROM ${this._getTableIdentifier()} WHERE expire < $1`,
values: [expire],
};
this._query(q)
Expand Down Expand Up @@ -150,7 +156,7 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
}

_getCreateTableStmt() {
return `CREATE TABLE IF NOT EXISTS "${this.tableName}" (
return `CREATE TABLE IF NOT EXISTS ${this._getTableIdentifier()} (
key varchar(255) PRIMARY KEY,
points integer NOT NULL DEFAULT 0,
expire bigint
Expand Down Expand Up @@ -190,8 +196,16 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
this._tableName = typeof value === 'undefined' ? this.keyPrefix : value;
}

get schemaName() {
return this._schemaName;
}

set schemaName(value) {
this._schemaName = value;
}

get tableCreated() {
return this._tableCreated
return this._tableCreated;
}

set tableCreated(value) {
Expand Down Expand Up @@ -252,18 +266,18 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
const expireQ = forceExpire
? ' $3 '
: ` CASE
WHEN "${this.tableName}".expire <= $4 THEN $3
ELSE "${this.tableName}".expire
WHEN ${this._getTableIdentifier()}.expire <= $4 THEN $3
ELSE ${this._getTableIdentifier()}.expire
END `;

return this._query({
name: forceExpire ? 'rlflx-upsert-force' : 'rlflx-upsert',
text: `
INSERT INTO "${this.tableName}" VALUES ($1, $2, $3)
INSERT INTO ${this._getTableIdentifier()} VALUES ($1, $2, $3)
ON CONFLICT(key) DO UPDATE SET
points = CASE
WHEN ("${this.tableName}".expire <= $4 OR 1=${forceExpire ? 1 : 0}) THEN $2
ELSE "${this.tableName}".points + ($2)
WHEN (${this._getTableIdentifier()}.expire <= $4 OR 1=${forceExpire ? 1 : 0}) THEN $2
ELSE ${this._getTableIdentifier()}.points + ($2)
END,
expire = ${expireQ}
RETURNING points, expire;`,
Expand All @@ -280,7 +294,7 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
this._query({
name: 'rlflx-get',
text: `
SELECT points, expire FROM "${this.tableName}" WHERE key = $1 AND (expire > $2 OR expire IS NULL);`,
SELECT points, expire FROM ${this._getTableIdentifier()} WHERE key = $1 AND (expire > $2 OR expire IS NULL);`,
values: [rlKey, Date.now()],
})
.then((res) => {
Expand All @@ -302,7 +316,7 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {

return this._query({
name: 'rlflx-delete',
text: `DELETE FROM "${this.tableName}" WHERE key = $1`,
text: `DELETE FROM ${this._getTableIdentifier()} WHERE key = $1`,
values: [rlKey],
})
.then(res => res.rowCount > 0);
Expand Down
16 changes: 10 additions & 6 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,14 @@ interface IRateLimiterMongoOptions extends IRateLimiterStoreOptions {
};
}

interface IRateLimiterPostgresOptions extends IRateLimiterStoreNoAutoExpiryOptions {
schemaName?: string;
}

interface IRateLimiterRedisOptions extends IRateLimiterStoreOptions {
rejectIfRedisNotReady?: boolean;
useRedisPackage?: boolean;
useRedis3AndLowerPackage?: boolean;
rejectIfRedisNotReady?: boolean;
useRedisPackage?: boolean;
useRedis3AndLowerPackage?: boolean;
}

interface ICallbackReady {
Expand Down Expand Up @@ -283,7 +287,7 @@ export class RateLimiterClusterMasterPM2 {
}

export class RateLimiterRedis extends RateLimiterStoreAbstract {
constructor(opts: IRateLimiterRedisOptions);
constructor(opts: IRateLimiterRedisOptions);
}

export interface IRateLimiterMongoFunctionOptions {
Expand Down Expand Up @@ -342,10 +346,10 @@ export class RateLimiterMySQL extends RateLimiterStoreAbstract {
}

export class RateLimiterPostgres extends RateLimiterStoreAbstract {
constructor(opts: IRateLimiterStoreNoAutoExpiryOptions, cb?: ICallbackReady);
constructor(opts: IRateLimiterPostgresOptions, cb?: ICallbackReady);
}

export class RateLimiterMemcache extends RateLimiterStoreAbstract {}
export class RateLimiterMemcache extends RateLimiterStoreAbstract { }

export class RateLimiterUnion {
constructor(...limiters: RateLimiterAbstract[]);
Expand Down