guardflux
Version:
A light callable lib to keep your API alive
55 lines (54 loc) • 2.63 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RateLimit = void 0;
const core_1 = require("@mikro-orm/core");
const constants_1 = require("./constants");
const crypto_1 = require("crypto");
// Define the RateLimit entity class with MikroORM annotations
let RateLimit = class RateLimit {
constructor() {
this._id = (0, crypto_1.randomUUID)(); // Automatically generates a unique ID for each record using UUID
this.requestCount = 0; // Tracks the number of requests made by the user to this route
this.lastRequest = new Date(); // Records the timestamp of the last request made
}
};
exports.RateLimit = RateLimit;
__decorate([
(0, core_1.PrimaryKey)() // Marks this field as the primary key for the entity
,
__metadata("design:type", String)
], RateLimit.prototype, "_id", void 0);
__decorate([
(0, core_1.Index)() // Creates an index for this property, which improves query performance when searching by userId
,
(0, core_1.Property)() // Marks this as a column in the database
,
__metadata("design:type", String)
], RateLimit.prototype, "userId", void 0);
__decorate([
(0, core_1.Property)() // Marks this as a column in the database
,
__metadata("design:type", String)
], RateLimit.prototype, "route", void 0);
__decorate([
(0, core_1.Property)() // Marks this as a column in the database
,
__metadata("design:type", Number)
], RateLimit.prototype, "requestCount", void 0);
__decorate([
(0, core_1.Property)() // Marks this as a column in the database
,
__metadata("design:type", Date)
], RateLimit.prototype, "lastRequest", void 0);
exports.RateLimit = RateLimit = __decorate([
(0, core_1.Entity)({ tableName: constants_1.rateLimitTableName }) // This decorator specifies that the class is an entity with a custom table name
], RateLimit);