Repository navigation
Expand file tree
/
Copy pathlib.ts
More file actions
152 lines (141 loc) · 4.22 KB
/
Copy pathlib.ts
File metadata and controls
152 lines (141 loc) · 4.22 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
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { Redis } from 'ioredis';
import assert from 'assert';
export function newRedis() {
const client = new Redis({
port: Number(process.env.REDIS_PORT),
host: process.env.REDIS_HOST,
password: process.env.REDIS_PASSWORD,
});
return client;
}
// 取得物件中所有的property以及其原型鏈上的property
function getAllNonFunctionKeys(obj: object): string[] {
const keys: string[] = [];
for (
let current = obj;
current !== null;
current = Object.getPrototypeOf(current)
) {
const ownKeys = Object.getOwnPropertyNames(current);
for (const key of ownKeys) {
// @ts-expect-error
if (key !== '__proto__' && typeof current[key] !== 'function') {
keys.push(key);
}
}
}
return keys;
}
type Constructor<U> = new () => U;
export class BaseEntity {
protected static _redis?: Redis;
protected static REDIS_ROOT: string;
private static PROPERTIES: string[];
public static init<U extends BaseEntity>(this: Constructor<U>, redis: Redis) {
// setup redis of derived class
(this as unknown as typeof BaseEntity)._redis = redis;
(this as unknown as typeof BaseEntity).PROPERTIES = getAllNonFunctionKeys(
new this(),
);
}
public static async set<U extends BaseEntity>(
this: Constructor<U>,
where: string | number,
value: Property<U>,
expireTime?: Partial<KeyToType<Property<U>, number>>,
) {
const this_ = this as unknown as typeof BaseEntity;
assert(this_._redis);
const transaction = this_._redis.multi();
for (const [key, val] of Object.entries(value)) {
if (typeof val === 'string' || typeof val === 'number') {
transaction.set(
`${this_.REDIS_ROOT}:${where}:${key}`,
val,
'EX',
// @ts-expect-error Missing type information for expireTime
expireTime?.[key] ?? 86400,
); // expires in a day
} else {
transaction.set(
`${this_.REDIS_ROOT}:${where}:${key}`,
JSON.stringify(val),
'EX',
// @ts-expect-error Missing type information for expireTime
expireTime?.[key] ?? 86400,
); // expires in a day
}
}
const execStatus = await transaction.exec();
if (execStatus === null) {
return;
}
for (const status of execStatus) {
if (status[0] !== null) {
throw status[0];
}
}
}
public static async get<U extends BaseEntity>(
this: Constructor<U>,
where: string | number,
): Promise<U | undefined> {
const this_ = this as unknown as typeof BaseEntity;
assert(this_._redis);
const object = {};
for (const key of this_.PROPERTIES) {
// @ts-expect-error
if (typeof this_[key] === 'string' || typeof this_[key] === 'number') {
const retrieved = await this_._redis.get(
`${this_.REDIS_ROOT}:${where}:${key}`,
);
if (retrieved === null) {
return undefined;
}
// @ts-expect-error
object[key] = retrieved;
} else {
const unknownValue = await this_._redis.get(
`${this_.REDIS_ROOT}:${where}:${key}`,
);
if (unknownValue === null) return undefined;
try {
// @ts-expect-error
object[key] = JSON.parse(unknownValue);
} catch (err) {
// @ts-expect-error
object[key] = unknownValue;
}
}
}
return object as U;
}
public static async del<U extends BaseEntity>(
this: Constructor<U>,
where: string | number,
) {
const this_ = this as unknown as typeof BaseEntity;
assert(this_._redis);
const transaction = this_._redis.multi();
for (const key of this_.PROPERTIES) {
transaction.del(`${this_.REDIS_ROOT}:${where}:${key}`);
}
const execStatus = await transaction.exec();
if (execStatus === null) {
return;
}
for (const status of execStatus) {
if (status[0] !== null) {
throw status[0];
}
}
}
}
type BaseEntityConstructor = new () => BaseEntity;
export function init(...entities: BaseEntityConstructor[]) {
const redis = newRedis();
entities.forEach((entity) =>
(entity as unknown as typeof BaseEntity).init(redis),
);
}