-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.js
67 lines (48 loc) · 1.33 KB
/
redis.js
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
import { Client, Entity, Repository, Schema } from "redis-om";
const client = new Client();
const connect = async () => {
if (!client.isOpen()) {
await client.open(process.env.NEXT_PUBLIC_REDIS_URL);
}
};
class Score extends Entity {}
const schema = new Schema(
Score,
{
name: { type: "string" },
address: { type: "string" },
score: { type: "number", sortable: true, textSearch: true },
},
{ dataStructure: "JSON" }
);
export const createScore = async (data) => {
await connect();
const repository = client?.fetchRepository(schema, client);
const score = repository.createEntity();
score.name = data.name;
score.score = data.score;
score.address = data.address;
const id = await repository.save(score);
const ttlInSeconds = 21600;
await repository.expire(id, ttlInSeconds);
await client.close();
return id;
};
export const createIndex = async () => {
await connect();
const repository = client.fetchRepository(schema, client);
await repository.createIndex();
await client.close();
};
createIndex();
export const searchScore = async () => {
await connect();
const repository = client.fetchRepository(schema, client);
const offset = 0;
const count = 3;
const scores = await repository
.search()
.sortDesc("score")
.return.page(offset, count);
return scores;
};