guardflux
Version:
A light callable lib to keep your API alive
26 lines (18 loc) • 1.23 kB
text/typescript
import { Entity, PrimaryKey, Property, Index } from '@mikro-orm/core';
import { rateLimitTableName } from './constants';
import { randomUUID } from 'crypto';
// Define the RateLimit entity class with MikroORM annotations
({ tableName: rateLimitTableName }) // This decorator specifies that the class is an entity with a custom table name
export class RateLimit {
() // Marks this field as the primary key for the entity
_id: string = randomUUID(); // Automatically generates a unique ID for each record using UUID
() // Creates an index for this property, which improves query performance when searching by userId
() // Marks this as a column in the database
userId!: string; // Represents the user identifier, which could also be an IP address or API key
() // Marks this as a column in the database
route!: string; // The specific API route being accessed
() // Marks this as a column in the database
requestCount: number = 0; // Tracks the number of requests made by the user to this route
() // Marks this as a column in the database
lastRequest: Date = new Date(); // Records the timestamp of the last request made
}