montycat 1.1.4 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/classes/generic.d.ts +13 -0
- package/dist/classes/generic.js +39 -9
- package/dist/core/persistent.d.ts +10 -40
- package/dist/core/persistent.js +12 -52
- package/package.json +1 -1
|
@@ -173,5 +173,18 @@ declare class GenericKV {
|
|
|
173
173
|
* @returns The current instance of GenericKV.
|
|
174
174
|
*/
|
|
175
175
|
showStoreProperties(): this;
|
|
176
|
+
/**
|
|
177
|
+
* Subscribes to changes in the keyspace.
|
|
178
|
+
* Inherited by InMemory and Persistent - `this.persistent` resolves correctly per subclass.
|
|
179
|
+
* @param callback - A callback function to handle incoming data.
|
|
180
|
+
* @param key - A specific key to subscribe to.
|
|
181
|
+
* @param customKey - A custom key to subscribe to (will be hashed with XXH32).
|
|
182
|
+
* @returns A promise that resolves with a subscription handle ({ stop() }).
|
|
183
|
+
*/
|
|
184
|
+
static subscribe({ callback, key, customKey }: {
|
|
185
|
+
callback?: (data: any) => void;
|
|
186
|
+
key?: string;
|
|
187
|
+
customKey?: string;
|
|
188
|
+
}): Promise<any>;
|
|
176
189
|
}
|
|
177
190
|
export default GenericKV;
|
package/dist/classes/generic.js
CHANGED
|
@@ -34,15 +34,15 @@ class GenericKV {
|
|
|
34
34
|
* @return A promise that resolves with the retrieved value.
|
|
35
35
|
*/
|
|
36
36
|
static async getValue({ key = "", customKey = null, withPointers = false, keyIncluded = false, pointersMetadata = false } = {}) {
|
|
37
|
-
if (pointersMetadata && withPointers) {
|
|
38
|
-
throw new Error("You select both pointers value and pointers metadata. Choose one");
|
|
39
|
-
}
|
|
40
37
|
try {
|
|
38
|
+
if (key && customKey) {
|
|
39
|
+
throw new Error("Provide either 'key' or 'customKey', not both.");
|
|
40
|
+
}
|
|
41
|
+
if (!key && !customKey) {
|
|
42
|
+
throw new Error("Provide either 'key' or 'customKey'.");
|
|
43
|
+
}
|
|
41
44
|
if (customKey)
|
|
42
45
|
key = convertCustomKey(customKey);
|
|
43
|
-
if (!key) {
|
|
44
|
-
throw new Error("No key provided");
|
|
45
|
-
}
|
|
46
46
|
this.command = "get_value";
|
|
47
47
|
const query = convertToBinaryQuery(this, { key, withPointers, keyIncluded, pointersMetadata });
|
|
48
48
|
return runQuery(this, query);
|
|
@@ -59,11 +59,14 @@ class GenericKV {
|
|
|
59
59
|
* */
|
|
60
60
|
static async listAllDependingKeys({ key = "", customKey = null } = {}) {
|
|
61
61
|
try {
|
|
62
|
+
if (key && customKey) {
|
|
63
|
+
throw new Error("Provide either 'key' or 'customKey', not both.");
|
|
64
|
+
}
|
|
65
|
+
if (!key && !customKey) {
|
|
66
|
+
throw new Error("Provide either 'key' or 'customKey'.");
|
|
67
|
+
}
|
|
62
68
|
if (customKey)
|
|
63
69
|
key = convertCustomKey(customKey);
|
|
64
|
-
if (!key) {
|
|
65
|
-
throw new Error("No key provided");
|
|
66
|
-
}
|
|
67
70
|
this.command = "list_all_depending_keys";
|
|
68
71
|
const query = convertToBinaryQuery(this, { key });
|
|
69
72
|
return runQuery(this, query);
|
|
@@ -85,6 +88,9 @@ class GenericKV {
|
|
|
85
88
|
* */
|
|
86
89
|
static async deleteKey({ key = "", customKey = null } = {}) {
|
|
87
90
|
try {
|
|
91
|
+
if (key && customKey) {
|
|
92
|
+
throw new Error("Provide either 'key' or 'customKey', not both.");
|
|
93
|
+
}
|
|
88
94
|
if (customKey)
|
|
89
95
|
key = convertCustomKey(customKey);
|
|
90
96
|
if (!key) {
|
|
@@ -301,5 +307,29 @@ class GenericKV {
|
|
|
301
307
|
showStoreProperties() {
|
|
302
308
|
return this;
|
|
303
309
|
}
|
|
310
|
+
/**
|
|
311
|
+
* Subscribes to changes in the keyspace.
|
|
312
|
+
* Inherited by InMemory and Persistent - `this.persistent` resolves correctly per subclass.
|
|
313
|
+
* @param callback - A callback function to handle incoming data.
|
|
314
|
+
* @param key - A specific key to subscribe to.
|
|
315
|
+
* @param customKey - A custom key to subscribe to (will be hashed with XXH32).
|
|
316
|
+
* @returns A promise that resolves with a subscription handle ({ stop() }).
|
|
317
|
+
*/
|
|
318
|
+
static async subscribe({ callback, key, customKey }) {
|
|
319
|
+
if (key && customKey) {
|
|
320
|
+
throw new Error("Provide either 'key' or 'customKey', not both.");
|
|
321
|
+
}
|
|
322
|
+
const effectiveKey = customKey ? convertCustomKey(customKey) : (key || null);
|
|
323
|
+
const queryObj = {
|
|
324
|
+
subscribe: true,
|
|
325
|
+
store: this.store,
|
|
326
|
+
keyspace: this.keyspace,
|
|
327
|
+
username: this.username,
|
|
328
|
+
password: this.password,
|
|
329
|
+
persistent: this.persistent,
|
|
330
|
+
key: effectiveKey,
|
|
331
|
+
};
|
|
332
|
+
return await runQuery(this, JSON.stringify(queryObj), callback, true);
|
|
333
|
+
}
|
|
304
334
|
}
|
|
305
335
|
export default GenericKV;
|
|
@@ -6,8 +6,6 @@ import GenericKV from '../classes/generic.js';
|
|
|
6
6
|
declare class Persistent extends GenericKV {
|
|
7
7
|
static persistent: boolean;
|
|
8
8
|
static distributed: boolean;
|
|
9
|
-
static cache: number | null;
|
|
10
|
-
static compression: boolean;
|
|
11
9
|
keyspace: string;
|
|
12
10
|
constructor(options: {
|
|
13
11
|
keyspace: string;
|
|
@@ -15,40 +13,6 @@ declare class Persistent extends GenericKV {
|
|
|
15
13
|
password: string;
|
|
16
14
|
[key: string]: any;
|
|
17
15
|
});
|
|
18
|
-
/**
|
|
19
|
-
* Subscribes to changes in the persistent store based on the provided options.
|
|
20
|
-
* @param callback - A callback function to handle incoming data.
|
|
21
|
-
* @param key - A specific key to subscribe to.
|
|
22
|
-
* @param customKey - A custom key to subscribe to.
|
|
23
|
-
* @return A promise that resolves with the result of the subscription.
|
|
24
|
-
* Note: Subscriptions are only allowed on non-persistent keyspaces, so this method will throw an error if called on a persistent keyspace.
|
|
25
|
-
* The effective key for the subscription is determined by the presence of a custom key or a regular key, with the custom key taking precedence if both are provided.
|
|
26
|
-
* The subscription query is constructed with the appropriate parameters and sent to the server using the runQuery function, with the callback function passed to handle incoming data.
|
|
27
|
-
* If the subscription is successful, the promise will resolve with the result of the subscription; otherwise, it will reject with an error.
|
|
28
|
-
* Error handling is implemented to ensure that subscriptions are not allowed on persistent keyspaces, and that a key is provided for the subscription.
|
|
29
|
-
* The method is designed to facilitate real-time updates and notifications for changes in the keyspace, allowing clients to react to data changes as they occur.
|
|
30
|
-
* Overall, this method provides a mechanism for clients to stay informed about changes in the persistent store, while enforcing the constraints of the keyspace type and ensuring that necessary parameters are provided for the subscription.
|
|
31
|
-
* @throws Will throw an error if subscriptions are attempted on a persistent keyspace or if no key is provided for the subscription.
|
|
32
|
-
* @example
|
|
33
|
-
* Persistent.subscribe({
|
|
34
|
-
* callback: (data) => {
|
|
35
|
-
* console.log("Received data:", data);
|
|
36
|
-
* },
|
|
37
|
-
* key: "myKey"
|
|
38
|
-
* });
|
|
39
|
-
* @example
|
|
40
|
-
* Persistent.subscribe({
|
|
41
|
-
* callback: (data) => {
|
|
42
|
-
* console.log("Received data for custom key:", data);
|
|
43
|
-
* },
|
|
44
|
-
* customKey: "myCustomKey"
|
|
45
|
-
* });
|
|
46
|
-
*/
|
|
47
|
-
static subscribe({ callback, key, customKey }: {
|
|
48
|
-
callback?: (data: any) => void;
|
|
49
|
-
key?: string;
|
|
50
|
-
customKey?: string;
|
|
51
|
-
}): Promise<any>;
|
|
52
16
|
/**
|
|
53
17
|
* Inserts a value into the persistent store.
|
|
54
18
|
* @param value - The value to insert.
|
|
@@ -112,16 +76,22 @@ declare class Persistent extends GenericKV {
|
|
|
112
76
|
}): Promise<any>;
|
|
113
77
|
/**
|
|
114
78
|
* Updates the cache and compression settings for the persistent store.
|
|
115
|
-
* @param
|
|
116
|
-
* @param compression - Whether to enable compression.
|
|
79
|
+
* @param options - Optional cache (number) and compression (boolean) settings.
|
|
117
80
|
* @return A promise that resolves with the result of the update.
|
|
118
81
|
*/
|
|
119
|
-
static updateCacheAndCompression(
|
|
82
|
+
static updateCacheAndCompression({ cache, compression }?: {
|
|
83
|
+
cache?: number;
|
|
84
|
+
compression?: boolean;
|
|
85
|
+
}): Promise<any>;
|
|
120
86
|
/**
|
|
121
87
|
* Creates a keyspace in the persistent store.
|
|
88
|
+
* @param options - Optional cache (number) and compression (boolean) settings.
|
|
122
89
|
* @return A promise that resolves with the result of the keyspace creation.
|
|
123
90
|
*/
|
|
124
|
-
static createKeyspace(
|
|
91
|
+
static createKeyspace({ cache, compression }?: {
|
|
92
|
+
cache?: number;
|
|
93
|
+
compression?: boolean;
|
|
94
|
+
}): Promise<any>;
|
|
125
95
|
}
|
|
126
96
|
/**
|
|
127
97
|
* PersistentDistributed class that extends Persistent for distributed persistent keyspace operations.
|
package/dist/core/persistent.js
CHANGED
|
@@ -7,55 +7,11 @@ import GenericKV from '../classes/generic.js';
|
|
|
7
7
|
class Persistent extends GenericKV {
|
|
8
8
|
static persistent = true;
|
|
9
9
|
static distributed = false;
|
|
10
|
-
static cache = null;
|
|
11
|
-
static compression = false;
|
|
12
10
|
keyspace;
|
|
13
11
|
constructor(options) {
|
|
14
12
|
super(options);
|
|
15
13
|
this.keyspace = options.keyspace;
|
|
16
14
|
}
|
|
17
|
-
/**
|
|
18
|
-
* Subscribes to changes in the persistent store based on the provided options.
|
|
19
|
-
* @param callback - A callback function to handle incoming data.
|
|
20
|
-
* @param key - A specific key to subscribe to.
|
|
21
|
-
* @param customKey - A custom key to subscribe to.
|
|
22
|
-
* @return A promise that resolves with the result of the subscription.
|
|
23
|
-
* Note: Subscriptions are only allowed on non-persistent keyspaces, so this method will throw an error if called on a persistent keyspace.
|
|
24
|
-
* The effective key for the subscription is determined by the presence of a custom key or a regular key, with the custom key taking precedence if both are provided.
|
|
25
|
-
* The subscription query is constructed with the appropriate parameters and sent to the server using the runQuery function, with the callback function passed to handle incoming data.
|
|
26
|
-
* If the subscription is successful, the promise will resolve with the result of the subscription; otherwise, it will reject with an error.
|
|
27
|
-
* Error handling is implemented to ensure that subscriptions are not allowed on persistent keyspaces, and that a key is provided for the subscription.
|
|
28
|
-
* The method is designed to facilitate real-time updates and notifications for changes in the keyspace, allowing clients to react to data changes as they occur.
|
|
29
|
-
* Overall, this method provides a mechanism for clients to stay informed about changes in the persistent store, while enforcing the constraints of the keyspace type and ensuring that necessary parameters are provided for the subscription.
|
|
30
|
-
* @throws Will throw an error if subscriptions are attempted on a persistent keyspace or if no key is provided for the subscription.
|
|
31
|
-
* @example
|
|
32
|
-
* Persistent.subscribe({
|
|
33
|
-
* callback: (data) => {
|
|
34
|
-
* console.log("Received data:", data);
|
|
35
|
-
* },
|
|
36
|
-
* key: "myKey"
|
|
37
|
-
* });
|
|
38
|
-
* @example
|
|
39
|
-
* Persistent.subscribe({
|
|
40
|
-
* callback: (data) => {
|
|
41
|
-
* console.log("Received data for custom key:", data);
|
|
42
|
-
* },
|
|
43
|
-
* customKey: "myCustomKey"
|
|
44
|
-
* });
|
|
45
|
-
*/
|
|
46
|
-
static async subscribe({ callback, key, customKey }) {
|
|
47
|
-
const effectiveKey = customKey ? convertCustomKey(customKey) : (key || null);
|
|
48
|
-
const queryObj = {
|
|
49
|
-
subscribe: true,
|
|
50
|
-
key: effectiveKey,
|
|
51
|
-
keyspace: this.keyspace,
|
|
52
|
-
store: this.store,
|
|
53
|
-
username: this.username,
|
|
54
|
-
password: this.password
|
|
55
|
-
};
|
|
56
|
-
const query = JSON.stringify(queryObj);
|
|
57
|
-
return await runQuery(this, query, callback, true);
|
|
58
|
-
}
|
|
59
15
|
/**
|
|
60
16
|
* Inserts a value into the persistent store.
|
|
61
17
|
* @param value - The value to insert.
|
|
@@ -139,21 +95,22 @@ class Persistent extends GenericKV {
|
|
|
139
95
|
}
|
|
140
96
|
/**
|
|
141
97
|
* Updates the cache and compression settings for the persistent store.
|
|
142
|
-
* @param
|
|
143
|
-
* @param compression - Whether to enable compression.
|
|
98
|
+
* @param options - Optional cache (number) and compression (boolean) settings.
|
|
144
99
|
* @return A promise that resolves with the result of the update.
|
|
145
100
|
*/
|
|
146
|
-
static async updateCacheAndCompression() {
|
|
101
|
+
static async updateCacheAndCompression({ cache, compression } = {}) {
|
|
147
102
|
if (!this.persistent) {
|
|
148
103
|
throw new Error("Cache and compression settings can only be updated for persistent keyspaces.");
|
|
149
104
|
}
|
|
105
|
+
const cacheValue = cache !== undefined ? cache.toString() : "0";
|
|
106
|
+
const compressionValue = compression === true ? "y" : "n";
|
|
150
107
|
const query = {
|
|
151
108
|
raw: [
|
|
152
109
|
"update-cache-compression",
|
|
153
110
|
"store", this.store,
|
|
154
111
|
"keyspace", this.keyspace,
|
|
155
|
-
"cache",
|
|
156
|
-
"compression",
|
|
112
|
+
"cache", cacheValue,
|
|
113
|
+
"compression", compressionValue
|
|
157
114
|
],
|
|
158
115
|
credentials: [this.username, this.password],
|
|
159
116
|
};
|
|
@@ -161,9 +118,12 @@ class Persistent extends GenericKV {
|
|
|
161
118
|
}
|
|
162
119
|
/**
|
|
163
120
|
* Creates a keyspace in the persistent store.
|
|
121
|
+
* @param options - Optional cache (number) and compression (boolean) settings.
|
|
164
122
|
* @return A promise that resolves with the result of the keyspace creation.
|
|
165
123
|
*/
|
|
166
|
-
static async createKeyspace() {
|
|
124
|
+
static async createKeyspace({ cache, compression } = {}) {
|
|
125
|
+
const cacheValue = cache !== undefined ? cache.toString() : "0";
|
|
126
|
+
const compressionValue = compression === true ? "y" : "n";
|
|
167
127
|
const query = {
|
|
168
128
|
raw: [
|
|
169
129
|
"create-keyspace",
|
|
@@ -171,8 +131,8 @@ class Persistent extends GenericKV {
|
|
|
171
131
|
"keyspace", this.keyspace,
|
|
172
132
|
"persistent", this.persistent ? "y" : "n",
|
|
173
133
|
"distributed", this.distributed ? "y" : "n",
|
|
174
|
-
"cache",
|
|
175
|
-
"compression",
|
|
134
|
+
"cache", cacheValue,
|
|
135
|
+
"compression", compressionValue
|
|
176
136
|
],
|
|
177
137
|
credentials: [this.username, this.password],
|
|
178
138
|
};
|