-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable-module.ts
131 lines (102 loc) · 3.3 KB
/
table-module.ts
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
import { PrettyError } from "@infinite-fansub/logger";
import { Client, Model, ModelOptions } from "../src";
/*
This module allows you to define tables to use with normal redis operations
However it doesn't affect the `Search` functionality since it doesn't have suffix constrains
Keep in mind that this could be way simpler but it is written this way to try and showcase
the different features and some other possibilities
*/
class Table<T extends Model<any>> {
readonly #oldSuffix: ModelOptions["suffix"];
constructor(name: string, public model: T) {
const { suffix } = model.options;
this.#oldSuffix = suffix;
if (typeof suffix !== "undefined") {
if (typeof suffix === "function") throw new PrettyError("Using tables is not allowed with dynamic suffixes");
model.options = {
suffix: `${suffix}:${name}`
}
} else {
model.options = {
suffix: name
}
}
}
/** @internal */
public _cleanUp(): void {
this.model.options = {
suffix: this.#oldSuffix
}
}
}
type TableFunction = ((table: Table<any>) => Table<any> | Promise<Table<any>>) | Table<any>;
const client = new Client({
base: {
schema: {
methods: {
withTable: async function (name: string, table: TableFunction) {
if (typeof table === "function") (await table(new Table(name, <never>this)))._cleanUp();
else table._cleanUp();
}
}
}
}
});
// IIFE
(async () => {
await client.connect().then(() => console.log("Connected!"));
const simpleSchema = client.schema({
name: "string",
age: "number"
})
const exampleModel = client.model("test", simpleSchema);
await exampleModel.createIndex();
await exampleModel.createAndSave({
$id: 1,
name: "DidaS",
age: 18
});
//#region No Tables
const result1 = await exampleModel.get(1);
console.log(result1)
/*
Expected Redis log:
"JSON.GET" "Nekdis:V1:test:1"
Expected result:
JSONDocument {
'$id': '1',
name: 'DidaS',
age: 18
}
*/
//#endregion No Tables
//#region With Tables
// For some reason typescript is not passing generics around so this is a temporary solution until i figure out how to get generics working
type ExampleTable = Table<typeof exampleModel>;
await exampleModel.withTable("table1", async (table: ExampleTable) => {
await table.model.createAndSave({
$id: 1,
name: "DidaS",
age: 21
})
return table;
});
// This could be done in the same `withTable` call but its just to show that you can do it anywhere in the project
await exampleModel.withTable("table1", async (table: ExampleTable) => {
const result2 = await table.model.get(1);
console.log(result2)
/*
Expected Redis log:
"JSON.GET" "Nekdis:V1:test:table1:1"
Expected result:
JSONDocument {
'$id': '1',
name: undefined,
age: 21
}
*/
return table;
});
//#endregion With Tables
await client.disconnect();
})()