agrs-sequelize-sdk 1.2.89 → 1.2.91

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,121 @@
1
+ // models/TokenRotationState.js
2
+ module.exports = (sequelize, DataTypes) => {
3
+ const TokenRotationState = sequelize.define(
4
+ "TokenRotationState",
5
+ {
6
+ id: {
7
+ type: DataTypes.INTEGER,
8
+ primaryKey: true,
9
+ autoIncrement: true,
10
+ },
11
+ tokenType: {
12
+ type: DataTypes.STRING,
13
+ allowNull: false,
14
+ unique: true,
15
+ defaultValue: "FACEBOOK_ACCESS_TOKEN",
16
+ comment: "Type of token being rotated (e.g., FACEBOOK_ACCESS_TOKEN)",
17
+ },
18
+ currentIndex: {
19
+ type: DataTypes.INTEGER,
20
+ allowNull: false,
21
+ defaultValue: 0,
22
+ comment: "Current index in the token rotation (0-based)",
23
+ },
24
+ totalTokens: {
25
+ type: DataTypes.INTEGER,
26
+ allowNull: false,
27
+ defaultValue: 10,
28
+ comment: "Total number of tokens in rotation",
29
+ },
30
+ lastUsedAt: {
31
+ type: DataTypes.DATE,
32
+ allowNull: true,
33
+ comment: "Timestamp of last token usage",
34
+ },
35
+ totalUsageCount: {
36
+ type: DataTypes.BIGINT,
37
+ allowNull: false,
38
+ defaultValue: 0,
39
+ comment: "Total number of times tokens have been used",
40
+ },
41
+ rotationStrategy: {
42
+ type: DataTypes.ENUM("round_robin", "least_used", "random"),
43
+ allowNull: false,
44
+ defaultValue: "round_robin",
45
+ comment: "Strategy for token rotation",
46
+ },
47
+ metadata: {
48
+ type: DataTypes.JSONB,
49
+ allowNull: true,
50
+ comment: "Additional metadata (per-token usage stats, health, etc.)",
51
+ },
52
+ },
53
+ {
54
+ tableName: "TokenRotationState",
55
+ timestamps: true, // Adds createdAt and updatedAt
56
+ indexes: [
57
+ {
58
+ fields: ["tokenType"],
59
+ unique: true,
60
+ name: "idx_token_rotation_type",
61
+ },
62
+ {
63
+ fields: ["lastUsedAt"],
64
+ name: "idx_token_rotation_last_used",
65
+ },
66
+ ],
67
+ }
68
+ );
69
+
70
+ // Instance methods
71
+ TokenRotationState.prototype.incrementUsage = async function () {
72
+ this.totalUsageCount = (
73
+ BigInt(this.totalUsageCount) + BigInt(1)
74
+ ).toString();
75
+ this.lastUsedAt = new Date();
76
+ await this.save();
77
+ };
78
+
79
+ TokenRotationState.prototype.rotateToNext = async function () {
80
+ this.currentIndex = (this.currentIndex + 1) % this.totalTokens;
81
+ await this.save();
82
+ };
83
+
84
+ TokenRotationState.prototype.getCurrentTokenNumber = function () {
85
+ return this.currentIndex + 1; // Return 1-based index
86
+ };
87
+
88
+ // Static methods
89
+ TokenRotationState.getOrCreate = async function (
90
+ tokenType = "FACEBOOK_ACCESS_TOKEN"
91
+ ) {
92
+ const [state, created] = await this.findOrCreate({
93
+ where: { tokenType },
94
+ defaults: {
95
+ tokenType,
96
+ currentIndex: 0,
97
+ totalTokens: 10,
98
+ rotationStrategy: "round_robin",
99
+ },
100
+ });
101
+ return state;
102
+ };
103
+
104
+ TokenRotationState.getStats = async function (
105
+ tokenType = "FACEBOOK_ACCESS_TOKEN"
106
+ ) {
107
+ const state = await this.findOne({ where: { tokenType } });
108
+ if (!state) return null;
109
+
110
+ return {
111
+ currentTokenNumber: state.getCurrentTokenNumber(),
112
+ totalTokens: state.totalTokens,
113
+ totalUsageCount: state.totalUsageCount.toString(),
114
+ lastUsedAt: state.lastUsedAt,
115
+ rotationStrategy: state.rotationStrategy,
116
+ metadata: state.metadata,
117
+ };
118
+ };
119
+
120
+ return TokenRotationState;
121
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agrs-sequelize-sdk",
3
- "version": "1.2.89",
3
+ "version": "1.2.91",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "start": "node index.js",