montycat 1.1.3 → 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.
@@ -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;
@@ -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) {
@@ -132,19 +138,15 @@ class GenericKV {
132
138
  * @returns A promise that resolves with the retrieved keys.
133
139
  */
134
140
  static async getBulk({ bulkKeys = [], bulkCustomKeys = [], limitOutput = { start: 0, stop: 0 }, withPointers = false, keyIncluded = false, pointersMetadata = false, volumes = [], latestVolume = false } = {}) {
135
- if (pointersMetadata && withPointers) {
136
- throw new Error("You select both pointers value and pointers metadata. Choose one");
137
- }
138
141
  try {
139
142
  if (bulkCustomKeys.length)
140
143
  bulkKeys = bulkKeys.concat(convertCustomKeys(bulkCustomKeys));
141
144
  const selectedOptions = [
142
145
  bulkKeys.length > 0,
143
- volumes.length > 0,
144
- latestVolume
146
+ volumes.length > 0 || latestVolume || (limitOutput.start !== 0 || limitOutput.stop !== 0),
145
147
  ].filter(Boolean).length;
146
148
  if (selectedOptions !== 1) {
147
- throw new Error("Multiple conflicting options provided. Please provide exactly one of the following: keys, volumes, or latest volume.");
149
+ throw new Error("Please provide keys or volumes/latest volume or limit.");
148
150
  }
149
151
  this.command = "get_bulk";
150
152
  const query = convertToBinaryQuery(this, { bulkKeys, limitOutput, withPointers, keyIncluded, pointersMetadata, volumes, latestVolume });
@@ -204,9 +206,6 @@ class GenericKV {
204
206
  * @return A promise that resolves with the result of the lookup.
205
207
  */
206
208
  static async lookupValuesWhere({ searchCriteria = {}, limitOutput = { start: 0, stop: 0 }, withPointers = false, schema = null, keyIncluded = false, pointersMetadata = false } = {}) {
207
- if (pointersMetadata && withPointers) {
208
- throw new Error("You select both pointers value and pointers metadata. Choose one");
209
- }
210
209
  try {
211
210
  this.command = "lookup_values";
212
211
  const query = convertToBinaryQuery(this, { searchCriteria, limitOutput, withPointers, schema, keyIncluded, pointersMetadata });
@@ -308,5 +307,29 @@ class GenericKV {
308
307
  showStoreProperties() {
309
308
  return this;
310
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
+ }
311
334
  }
312
335
  export default GenericKV;
@@ -151,8 +151,8 @@ class InMemory extends GenericKV {
151
151
  * @returns A promise that resolves with the retrieved keys.
152
152
  */
153
153
  static async getKeys({ volumes = [], latestVolume = false } = {}) {
154
- if (latestVolume && volumes.length > 0) {
155
- throw new Error("Select either latest volume or volumes list, not both.");
154
+ if ((!volumes || volumes.length === 0) && !latestVolume) {
155
+ throw new Error("Please provide volumes/latest volume.");
156
156
  }
157
157
  this.command = "get_keys";
158
158
  const query = convertToBinaryQuery(this, { volumes, latestVolume });
@@ -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,11 +13,6 @@ declare class Persistent extends GenericKV {
15
13
  password: string;
16
14
  [key: string]: any;
17
15
  });
18
- static subscribe({ callback, key, customKey }: {
19
- callback?: (data: any) => void;
20
- key?: string;
21
- customKey?: string;
22
- }): Promise<any>;
23
16
  /**
24
17
  * Inserts a value into the persistent store.
25
18
  * @param value - The value to insert.
@@ -29,9 +22,8 @@ declare class Persistent extends GenericKV {
29
22
  value?: any;
30
23
  }): Promise<any>;
31
24
  /**
32
- * Inserts a key-value pair into the persistent store.
33
- * @param key - The key for the value.
34
- * @param value - The value to insert.
25
+ * Inserts a custom key into the persistent store.
26
+ * @param customKey - The custom key to insert.
35
27
  * @return A promise that resolves with the result of the insertion.
36
28
  */
37
29
  static insertCustomKey({ customKey }: {
@@ -60,19 +52,19 @@ declare class Persistent extends GenericKV {
60
52
  value?: any;
61
53
  }): Promise<any>;
62
54
  /**
63
- * Retrieves a value from the persistent store.
64
- * @param key - The key for the value to retrieve.
65
- * @param customKey - The custom key for the value to retrieve.
66
- * @return A promise that resolves with the retrieved value.
55
+ * Inserts multiple key-value pairs into the persistent store in bulk.
56
+ * @param bulk - An array of key-value pairs to insert.
57
+ * @return A promise that resolves with the result of the bulk insertion.
67
58
  */
68
59
  static insertBulk({ bulk }?: {
69
60
  bulk?: any[];
70
61
  }): Promise<any>;
71
62
  /**
72
- * Retrieves a value from the persistent store.
73
- * @param key - The key for the value to retrieve.
74
- * @param customKey - The custom key for the value to retrieve.
75
- * @return A promise that resolves with the retrieved value.
63
+ * Retrieves keys from the persistent store based on provided options.
64
+ * @param limitOutput - An object specifying the start and stop limits for output.
65
+ * @param latestVolume - A boolean indicating whether to retrieve keys from the latest volume.
66
+ * @param volumes - An array of volume names to retrieve keys from.
67
+ * @returns A promise that resolves with the retrieved keys.
76
68
  */
77
69
  static getKeys({ limitOutput, latestVolume, volumes }?: {
78
70
  limitOutput?: {
@@ -84,16 +76,22 @@ declare class Persistent extends GenericKV {
84
76
  }): Promise<any>;
85
77
  /**
86
78
  * Updates the cache and compression settings for the persistent store.
87
- * @param cache - The cache settings to apply.
88
- * @param compression - Whether to enable compression.
79
+ * @param options - Optional cache (number) and compression (boolean) settings.
89
80
  * @return A promise that resolves with the result of the update.
90
81
  */
91
- static updateCacheAndCompression(): Promise<any>;
82
+ static updateCacheAndCompression({ cache, compression }?: {
83
+ cache?: number;
84
+ compression?: boolean;
85
+ }): Promise<any>;
92
86
  /**
93
87
  * Creates a keyspace in the persistent store.
88
+ * @param options - Optional cache (number) and compression (boolean) settings.
94
89
  * @return A promise that resolves with the result of the keyspace creation.
95
90
  */
96
- static createKeyspace(): Promise<any>;
91
+ static createKeyspace({ cache, compression }?: {
92
+ cache?: number;
93
+ compression?: boolean;
94
+ }): Promise<any>;
97
95
  }
98
96
  /**
99
97
  * PersistentDistributed class that extends Persistent for distributed persistent keyspace operations.
@@ -7,26 +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
- static async subscribe({ callback, key, customKey }) {
18
- const effectiveKey = customKey ? convertCustomKey(customKey) : (key || null);
19
- const queryObj = {
20
- subscribe: true,
21
- key: effectiveKey,
22
- keyspace: this.keyspace,
23
- store: this.store,
24
- username: this.username,
25
- password: this.password
26
- };
27
- const query = JSON.stringify(queryObj);
28
- return await runQuery(this, query, callback, true);
29
- }
30
15
  /**
31
16
  * Inserts a value into the persistent store.
32
17
  * @param value - The value to insert.
@@ -38,9 +23,8 @@ class Persistent extends GenericKV {
38
23
  return runQuery(this, query);
39
24
  }
40
25
  /**
41
- * Inserts a key-value pair into the persistent store.
42
- * @param key - The key for the value.
43
- * @param value - The value to insert.
26
+ * Inserts a custom key into the persistent store.
27
+ * @param customKey - The custom key to insert.
44
28
  * @return A promise that resolves with the result of the insertion.
45
29
  */
46
30
  static async insertCustomKey({ customKey }) {
@@ -82,10 +66,9 @@ class Persistent extends GenericKV {
82
66
  return runQuery(this, query);
83
67
  }
84
68
  /**
85
- * Retrieves a value from the persistent store.
86
- * @param key - The key for the value to retrieve.
87
- * @param customKey - The custom key for the value to retrieve.
88
- * @return A promise that resolves with the retrieved value.
69
+ * Inserts multiple key-value pairs into the persistent store in bulk.
70
+ * @param bulk - An array of key-value pairs to insert.
71
+ * @return A promise that resolves with the result of the bulk insertion.
89
72
  */
90
73
  static async insertBulk({ bulk = [] } = {}) {
91
74
  if (!bulk || bulk.length === 0) {
@@ -96,14 +79,15 @@ class Persistent extends GenericKV {
96
79
  return runQuery(this, query);
97
80
  }
98
81
  /**
99
- * Retrieves a value from the persistent store.
100
- * @param key - The key for the value to retrieve.
101
- * @param customKey - The custom key for the value to retrieve.
102
- * @return A promise that resolves with the retrieved value.
82
+ * Retrieves keys from the persistent store based on provided options.
83
+ * @param limitOutput - An object specifying the start and stop limits for output.
84
+ * @param latestVolume - A boolean indicating whether to retrieve keys from the latest volume.
85
+ * @param volumes - An array of volume names to retrieve keys from.
86
+ * @returns A promise that resolves with the retrieved keys.
103
87
  */
104
88
  static async getKeys({ limitOutput = { start: 0, stop: 0 }, latestVolume = false, volumes = [] } = {}) {
105
- if (latestVolume && volumes.length > 0) {
106
- throw new Error("Select either latest volume or volumes list, not both.");
89
+ if ((!volumes || volumes.length === 0) && !latestVolume && (!limitOutput || (limitOutput.start === 0 && limitOutput.stop === 0))) {
90
+ throw new Error("Please provide volumes/latest volume or limit.");
107
91
  }
108
92
  this.command = "get_keys";
109
93
  const query = convertToBinaryQuery(this, { limitOutput, latestVolume, volumes });
@@ -111,21 +95,22 @@ class Persistent extends GenericKV {
111
95
  }
112
96
  /**
113
97
  * Updates the cache and compression settings for the persistent store.
114
- * @param cache - The cache settings to apply.
115
- * @param compression - Whether to enable compression.
98
+ * @param options - Optional cache (number) and compression (boolean) settings.
116
99
  * @return A promise that resolves with the result of the update.
117
100
  */
118
- static async updateCacheAndCompression() {
101
+ static async updateCacheAndCompression({ cache, compression } = {}) {
119
102
  if (!this.persistent) {
120
103
  throw new Error("Cache and compression settings can only be updated for persistent keyspaces.");
121
104
  }
105
+ const cacheValue = cache !== undefined ? cache.toString() : "0";
106
+ const compressionValue = compression === true ? "y" : "n";
122
107
  const query = {
123
108
  raw: [
124
109
  "update-cache-compression",
125
110
  "store", this.store,
126
111
  "keyspace", this.keyspace,
127
- "cache", this.cache ? this.cache : "0",
128
- "compression", this.compression ? "y" : "n"
112
+ "cache", cacheValue,
113
+ "compression", compressionValue
129
114
  ],
130
115
  credentials: [this.username, this.password],
131
116
  };
@@ -133,9 +118,12 @@ class Persistent extends GenericKV {
133
118
  }
134
119
  /**
135
120
  * Creates a keyspace in the persistent store.
121
+ * @param options - Optional cache (number) and compression (boolean) settings.
136
122
  * @return A promise that resolves with the result of the keyspace creation.
137
123
  */
138
- static async createKeyspace() {
124
+ static async createKeyspace({ cache, compression } = {}) {
125
+ const cacheValue = cache !== undefined ? cache.toString() : "0";
126
+ const compressionValue = compression === true ? "y" : "n";
139
127
  const query = {
140
128
  raw: [
141
129
  "create-keyspace",
@@ -143,8 +131,8 @@ class Persistent extends GenericKV {
143
131
  "keyspace", this.keyspace,
144
132
  "persistent", this.persistent ? "y" : "n",
145
133
  "distributed", this.distributed ? "y" : "n",
146
- "cache", this.cache ? this.cache : "0",
147
- "compression", this.compression ? "y" : "n"
134
+ "cache", cacheValue,
135
+ "compression", compressionValue
148
136
  ],
149
137
  credentials: [this.username, this.password],
150
138
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "montycat",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "description": "A Node.js client for Montycat, NoSQL database utilizing Data Mesh architecture.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",