Skip to content

Commit 804fdd7

Browse files
Added two different types of stores: individual IP and aggregated IP (default).
Cleaned up code base. Moved redundant code to util class. Linted database migrations.
1 parent ca14975 commit 804fdd7

File tree

10 files changed

+541
-294
lines changed

10 files changed

+541
-294
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"scripts": {
2323
"test": "echo \"Error: no test specified\" && exit 1",
2424
"clean": "rm -rf ./dist",
25-
"build": "npm run clean && npm run build:esm && npm run build:cjs && cp source/db/migrations/ dist/ -r",
25+
"build": "npm run clean && npm run build:esm && npm run build:cjs && cp source/migrations/ dist/ -r",
2626
"build:esm": "tsc -p ./configs/tsconfig.esm.json && mv dist/esm/index.js dist/esm/index.mjs",
2727
"build:cjs": "tsc -p ./configs/tsconfig.cjs.json",
2828
"prepack": "npm run build",

source/db/migrations/1-init.sql

Lines changed: 0 additions & 16 deletions
This file was deleted.

source/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
export { default } from './lib.js'
1+
import PostgresStoreIndividualIP from './stores/individual_ip/store_individual_ip'
2+
import PostgresStoreAggregatedIP from './stores/aggregated_ip/store_aggregated_ip'
3+
4+
export { PostgresStoreAggregatedIP as PostgresStore, PostgresStoreIndividualIP }

source/lib.ts

Lines changed: 0 additions & 276 deletions
This file was deleted.

source/migrations/1-init.sql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
2+
3+
CREATE SCHEMA IF NOT EXISTS rate_limit;
4+
5+
CREATE TABLE IF NOT EXISTS rate_limit.sessions (
6+
id uuid DEFAULT uuid_generate_v1() PRIMARY KEY,
7+
name_ text UNIQUE,
8+
type_ text,
9+
registered_at timestamptz DEFAULT now(),
10+
expires_at timestamptz
11+
);
12+
13+
14+
CREATE TABLE IF NOT EXISTS rate_limit.records_aggregated (
15+
key text PRIMARY KEY,
16+
session_id uuid REFERENCES rate_limit.sessions (id) ON DELETE CASCADE,
17+
count integer DEFAULT 1
18+
);
19+
20+
CREATE TABLE IF NOT EXISTS rate_limit.individual_records (
21+
id uuid DEFAULT uuid_generate_v1() PRIMARY KEY,
22+
key text,
23+
event_time timestamptz DEFAULT now(),
24+
session_id uuid REFERENCES rate_limit.sessions (id) ON DELETE CASCADE
25+
);
26+
27+
ALTER TABLE rate_limit.records_aggregated
28+
ADD CONSTRAINT unique_session_key UNIQUE (key, session_id);

source/models/session.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export type Session = {
2+
id: string
3+
name: string
4+
type: string
5+
expires_at: Date | undefined
6+
}

0 commit comments

Comments
 (0)