-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
167 lines (147 loc) · 5.36 KB
/
Copy pathindex.ts
File metadata and controls
167 lines (147 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import type { KeyValueAdapter, IAdminForth } from "adminforth";
import { Filters, Sorts, afLogger } from 'adminforth';
import type { AdapterOptions } from "./types.js";
export default class ResourceKeyValueAdapter implements KeyValueAdapter {
adminForth: IAdminForth | undefined;
options: AdapterOptions;
constructor(options: AdapterOptions) {
this.options = options;
}
protected registerAdminForthIfNeeded() {
if (!this.adminForth) {
this.adminForth = global.adminforth as IAdminForth;
}
}
protected getResource() {
this.registerAdminForthIfNeeded();
return this.adminForth!.resource(this.options.resourceId);
}
protected getActualKey(key: string, collection?: string): string {
if (collection) {
return `${collection}:${key}`;
}
return key;
}
protected getExpireAt(record: Record<string, any>): number | null {
if (!this.options.expireField) {
return null;
}
const raw = record[this.options.expireField];
if (raw === null || raw === undefined || raw === '') {
return null;
}
let ts: number;
if (raw instanceof Date) {
ts = raw.getTime();
} else if (typeof raw === 'number') {
ts = raw;
} else {
const asNumber = Number(raw);
ts = Number.isNaN(asNumber) ? new Date(raw).getTime() : asNumber;
}
return Number.isNaN(ts) ? null : ts;
}
protected isExpired(record: Record<string, any>): boolean {
const expireAt = this.getExpireAt(record);
const expired = expireAt !== null && Date.now() > expireAt;
return expired;
}
protected async cleanupExpired(actualPrefix: string): Promise<void> {
if (!this.options.expireField) {
return;
}
const resource = this.getResource();
const expiredRecords = await resource.list(
Filters.AND(
Filters.LIKE(this.options.keyField, `${actualPrefix}%`),
Filters.LT(this.options.expireField, Date.now()),
),
);
for (const record of expiredRecords) {
await resource.delete(record[this.options.keyField]);
}
}
async get(key: string, collection?: string): Promise<string | null> {
const resource = this.getResource();
const actualKey = this.getActualKey(key, collection);
const record = await resource.get(Filters.EQ(this.options.keyField, actualKey));
if (record) {
if (this.isExpired(record)) {
await resource.delete(actualKey);
return null;
}
return record[this.options.valueField];
} else {
afLogger.error(`ResourceKeyValueAdapter: Key not found: ${actualKey} in collection: ${collection}`);
}
}
async set(key: string, value: any, expiresInSeconds?: number, collection?: string): Promise<void> {
const resource = this.getResource();
if (expiresInSeconds && !this.options.expireField) {
afLogger.error(`ResourceKeyValueAdapter: expiresInSeconds is set but no expireField is configured. Ignoring expiry for key: ${key} and collection: ${collection}`);
}
const actualKey = this.getActualKey(key, collection);
const expireAt = expiresInSeconds ? Date.now() + expiresInSeconds * 1000 : null;
const existing = await resource.get(Filters.EQ(this.options.keyField, actualKey));
if (existing) {
const updatePayload: Record<string, any> = {
[this.options.valueField]: value,
};
if (this.options.expireField) {
updatePayload[this.options.expireField] = expireAt;
}
await resource.update(actualKey, updatePayload);
} else {
const createPayload: Record<string, any> = {
[this.options.keyField]: actualKey,
[this.options.valueField]: value,
};
if (collection) {
createPayload[this.options.collectionField] = collection;
}
if (this.options.expireField) {
createPayload[this.options.expireField] = expireAt;
}
await resource.create(createPayload);
}
}
async delete(key: string, collection?: string): Promise<void> {
const resource = this.getResource();
const actualKey = this.getActualKey(key, collection);
await resource.delete(actualKey);
}
async listByPrefix(prefix: string, limit?: number, collection?: string): Promise<Record<string, string>[]> {
const resource = this.getResource();
const actualPrefix = this.getActualKey(prefix, collection);
await this.cleanupExpired(actualPrefix);
const list = await resource.list(Filters.LIKE(this.options.keyField, `${actualPrefix}%`), limit, 0, Sorts.ASC(this.options.keyField));
const keyValuePairs: Record<string, string>[] = list.map((record) => {
const key = record[this.options.keyField];
const value = record[this.options.valueField];
return { [key]: value };
});
const keyValuePairsFiltered = keyValuePairs.filter((pair) => {
const key = Object.keys(pair)[0];
if (collection) {
return key.startsWith(`${collection}:`);
}
return true;
})
keyValuePairsFiltered.forEach((pair) => {
const key = Object.keys(pair)[0];
if (collection) {
const keyWithoutCollection = key.replace(`${collection}:`, '');
pair[keyWithoutCollection] = pair[key];
delete pair[key];
}
})
const result = keyValuePairsFiltered.filter((pair) => {
const key = Object.keys(pair)[0];
if (key.startsWith(`${prefix}`)) {
return true;
}
return false;
})
return result;
}
}