-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathkvstore.ts
More file actions
135 lines (126 loc) · 5.25 KB
/
Copy pathkvstore.ts
File metadata and controls
135 lines (126 loc) · 5.25 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
import { TFClient } from "../clients/tf-grid/client";
import { GridClientConfig } from "../config";
import { expose } from "../helpers/expose";
import { validateInput } from "../helpers/validator";
import { KVStoreBatchRemoveModel, KVStoreGetModel, KVStoreRemoveModel, KVStoreSetModel } from "./models";
import { checkBalance } from "./utils";
class KVStore {
client: TFClient;
/**
* Represents a `key-value` store class that interacts with the `TFClient` for performing `CRUD operations`.
*
* This class provides methods for `setting`, `getting`, `listing`, `removing`, and `batch removing` `key-value` pairs.
*
* It enforces `validation on input` parameters and `checks the balance` before executing certain operations.
*
* @class KVStore
* @param {GridClientConfig} config - The configuration object for initializing the client.
*/
constructor(public config: GridClientConfig) {
this.client = config.tfclient;
}
/**
* Asynchronously sets a `key-value` pair using the provided options.
*
* This method interacts with the `TFClient` to set a `key-value` pair based on the specified options.
*
* @param {KVStoreSetModel} options - The options object containing the key, value, and optional encryption flag.
* @returns {Promise<void>} A promise that resolves once the `key-value` pair is successfully set.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
* - `@checkBalance`: Checks the balance to ensure there are enough funds available.
*/
@expose
@validateInput
@checkBalance
async set(options: KVStoreSetModel): Promise<string> {
return (await this.client.kvStore.set(options)).apply();
}
/**
* Asynchronously retrieves a `key-value` pair based on the provided options.
*
* This method interacts with the `TFClient` to get a `key-value` pair using the specified options.
*
* @param {KVStoreGetModel} options - The options object containing the `key` and an optional `decryption` flag.
* @returns {Promise<string>} A promise that resolves with the retrieved `value` of the `options.key`.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
* - `@checkBalance`: Checks the balance to ensure there are enough funds available.
*/
@expose
@validateInput
async get(options: KVStoreGetModel): Promise<string> {
return await this.client.kvStore.get(options);
}
/**
* Asynchronously retrieves a list of all `keys`.
*
* This method interacts with the `TFClient` to retrieve a list of all stored `keys`.
*
* @returns {Promise<string[]>} A promise that resolves with the list of all `keys`.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input parameters.
*/
@expose
@validateInput
async list(): Promise<string[]> {
return await this.client.kvStore.list();
}
/**
* Asynchronously removes a `key-value` pair based on the provided options.
*
* This method interacts with the `TFClient` to delete a `key-value` pair using the specified options.
*
* @param {KVStoreRemoveModel} options - The options object containing the `key` to be removed.
* @returns {Promise<KVStoreSetOptions>} A promise that resolves once the `key-value` pair is successfully removed.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
* - `@checkBalance`: Checks the balance to ensure there are enough funds available.
*/
@expose
@validateInput
@checkBalance
async remove(options: KVStoreRemoveModel): Promise<string> {
return (await this.client.kvStore.delete(options)).apply();
}
/**
* Asynchronously removes all `key-value` pairs from the `key-value` store.
*
* This method interacts with the TFClient to delete all `key-value` pairs stored in the `key-value` store.
*
* @returns {Promise<string[]>} A promise that resolves with a list of keys that were successfully removed.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input parameters.
* - `@checkBalance`: Checks the balance to ensure there are enough funds available.
*/
@expose
@validateInput
@checkBalance
async removeAll(): Promise<string[]> {
return this.client.kvStore.deleteAll();
}
/**
* Asynchronously removes a batch of `key-value` pairs based on the provided options.
*
* This method interacts with the `TFClient` to delete a batch of `key-value` pairs using the specified options.
*
* @param {KVStoreBatchRemoveModel} options - The options object containing an array of keys to be removed.
* @returns {Promise<string[]>} A promise that resolves with a list of keys that were successfully removed.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
* - `@checkBalance`: Checks the balance to ensure there are enough funds available.
*/
@expose
@validateInput
@checkBalance
async batchRemove(options: KVStoreBatchRemoveModel): Promise<string[]> {
return await this.client.kvStore.batchRemove(options);
}
}
export { KVStore as kvstore };