montycat 1.0.2

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/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # Montycat Node.js Client
2
+
3
+ Montycat Node.js Client is the official JavaScript/TypeScript client for **Montycat**, a high-performance, distributed NoSQL store built on cutting-edge Data Mesh architecture. This package enables Node.js developers to easily connect, query, and manage data within Montycat’s decentralized, scalable environment.
4
+
5
+ ## Key Features
6
+ - ⚡ **High Performance**: Leverages Montycat’s architecture for extremely fast reads and writes.
7
+ - 🏆 **Best of Both Worlds**: Combines the flexibility of NoSQL with familiar SQL-like features.
8
+ - 💾 **In-Memory & Persistent Storage**: Choose between ultra-fast in-memory operations or durable persistent storage — or mix both.
9
+ - 🗂️ **Data Mesh Design**: Supports decentralized data ownership, allowing teams to manage their own domains.
10
+ - 🔄 **Asynchronous Operations**: Built for non-blocking I/O with async/await, perfect for real-time and high-concurrency workloads.
11
+ - 🛡️ **Robust & Stable**: Utilizes Node.js’s networking stack (net module) for reliable connections and secure data handling.
12
+ - 📊 **Smart Data Governance**: Integrates governance features to ensure data integrity and compliance.
13
+ - 🤝 **Simple API**: Designed for easy integration into Node.js applications with minimal setup.
14
+ - 📚 **Well-Documented**: Clear and comprehensive documentation to get you up and running fast.
15
+
16
+ Installation
17
+ You can install the Node.js client for Montycat using `npm`:
18
+
19
+ ```bash
20
+ npm install montycat
@@ -0,0 +1,162 @@
1
+ import { inspect } from 'util';
2
+ /**
3
+ * GenericKV class that provides a base for key-value store operations.
4
+ * It includes methods for inserting, updating, retrieving, and deleting keys and values.
5
+ */
6
+ declare class GenericKV {
7
+ static store: string;
8
+ static command: string;
9
+ static persistent: boolean;
10
+ static keyspace: string;
11
+ static username: string;
12
+ static password: string;
13
+ keyspace: string;
14
+ username: string;
15
+ password: string;
16
+ constructor(options: {
17
+ keyspace: string;
18
+ username: string;
19
+ password: string;
20
+ });
21
+ static [inspect.custom](): string;
22
+ static connectEngine(engine: any): void;
23
+ /**
24
+ * Get value from a store
25
+ * @param key - The key for the value to retrieve.
26
+ * @param customKey - The custom key for the value to retrieve.
27
+ * @param withPointers - Whether to include pointers in the retrieved value.
28
+ * @return A promise that resolves with the retrieved value.
29
+ */
30
+ static getValue({ key, customKey, withPointers }?: {
31
+ key?: string;
32
+ customKey?: string | null;
33
+ withPointers?: boolean;
34
+ }): Promise<any>;
35
+ /**
36
+ * List all depending keys for a given key or custom key.
37
+ * @param key - The key for which to list depending keys.
38
+ * @param customKey - The custom key for which to list depending keys.
39
+ * @returns A promise that resolves with the list of depending keys.
40
+ * */
41
+ static listAllDependingKeys({ key, customKey }?: {
42
+ key?: string;
43
+ customKey?: string | null;
44
+ }): Promise<any>;
45
+ static listAllSchemasInKeyspace(): Promise<any>;
46
+ /**
47
+ * Deletes a key from the keyspace.
48
+ * @param key - The key to delete.
49
+ * @param customKey - The custom key to delete.
50
+ * @return A promise that resolves with the result of the deletion.
51
+ * */
52
+ static deleteKey({ key, customKey }?: {
53
+ key?: string;
54
+ customKey?: string | null;
55
+ }): Promise<any>;
56
+ /**
57
+ * Deletes a bulk of keys from the keyspace.
58
+ * @param bulkKeys - An array of keys to delete.
59
+ * @param bulkCustomKeys - An array of custom keys to delete.
60
+ * @return A promise that resolves with the result of the deletion.
61
+ * */
62
+ static deleteBulk({ bulkKeys, bulkCustomKeys }?: {
63
+ bulkKeys?: string[];
64
+ bulkCustomKeys?: string[];
65
+ }): Promise<any>;
66
+ /**
67
+ * Gets bulk values from the keyspace.
68
+ * @param bulkKeys - An array of keys to retrieve.
69
+ * @param bulkCustomKeys - An array of custom keys to retrieve.
70
+ * @param limitOutput - An object specifying the start and stop indices for limiting output.
71
+ * @param withPointers - Whether to include pointers in the retrieved values.
72
+ * @returns A promise that resolves with the retrieved keys.
73
+ */
74
+ static getBulk({ bulkKeys, bulkCustomKeys, limitOutput, withPointers }?: {
75
+ bulkKeys?: string[];
76
+ bulkCustomKeys?: string[];
77
+ limitOutput?: {
78
+ start: number;
79
+ stop: number;
80
+ };
81
+ withPointers?: boolean;
82
+ }): Promise<any>;
83
+ /**
84
+ * Updates a bulk of keys and values in the keyspace.
85
+ * @param bulkKeysValues - An object where keys are the keys to update and values are the new values.
86
+ * @param bulkCustomKeysValues - An object where custom keys are the keys to update and values are the new values.
87
+ * @return A promise that resolves with the result of the update.
88
+ */
89
+ static updateBulk({ bulkKeysValues, bulkCustomKeysValues }?: {
90
+ bulkKeysValues?: {
91
+ [key: string]: any;
92
+ };
93
+ bulkCustomKeysValues?: {
94
+ [key: string]: any;
95
+ };
96
+ }): Promise<any>;
97
+ /**
98
+ * Looks up keys based on search criteria.
99
+ * @param searchCriteria - An object containing the search criteria.
100
+ * @param limitOutput - An object specifying the start and stop indices for limiting output.
101
+ * @param schema - The schema to use for the lookup.
102
+ * @returns A promise that resolves with the result of the lookup.
103
+ */
104
+ static lookupKeysWhere({ searchCriteria, limitOutput, schema }?: {
105
+ searchCriteria?: {
106
+ [key: string]: any;
107
+ };
108
+ limitOutput?: {
109
+ start: number;
110
+ stop: number;
111
+ };
112
+ schema?: any;
113
+ }): Promise<any>;
114
+ /**
115
+ * Looks up values based on search criteria.
116
+ * @param searchCriteria - An object containing the search criteria.
117
+ * @param limitOutput - An object specifying the start and stop indices for limiting output.
118
+ * @param withPointers - Whether to include pointers in the retrieved values.
119
+ * @param schema - The schema to use for the lookup.
120
+ * @return A promise that resolves with the result of the lookup.
121
+ */
122
+ static lookupValuesWhere({ searchCriteria, limitOutput, withPointers, schema }?: {
123
+ searchCriteria?: {
124
+ [key: string]: any;
125
+ };
126
+ limitOutput?: {
127
+ start: number;
128
+ stop: number;
129
+ };
130
+ withPointers?: boolean;
131
+ schema?: any;
132
+ }): Promise<any>;
133
+ /**
134
+ * Gets the length of the keyspace.
135
+ * @returns A promise that resolves with the length of the keyspace.
136
+ */
137
+ static getLen(): Promise<any>;
138
+ /**
139
+ * Removes the keyspace from the store.
140
+ * @returns A promise that resolves with the result of the keyspace removal.
141
+ * */
142
+ static removeKeyspace(): Promise<any>;
143
+ /**
144
+ * Enforces a schema on the store.
145
+ * @param schema - The schema to enforce. Should be an instance of the Schema class.
146
+ * @throws TypeError if the schema is not an instance of Schema.
147
+ * @returns A promise that resolves with the result of the schema enforcement.
148
+ */
149
+ static enforceSchema(schema: any): Promise<any>;
150
+ /**
151
+ * Removes an enforced schema from the store.
152
+ * @param schema - The name of the schema to remove.
153
+ * @returns A promise that resolves with the result of the schema removal.
154
+ */
155
+ static removeEnforcedSchema(schema: any): Promise<any>;
156
+ /**
157
+ * Shows the properties of the store.
158
+ * @returns The current instance of GenericKV.
159
+ */
160
+ showStoreProperties(): this;
161
+ }
162
+ export default GenericKV;
@@ -0,0 +1,274 @@
1
+ import { convertCustomKey, convertCustomKeys, convertCustomKeysValues, convertToBinaryQuery, runQuery, } from '../functions/storeGenericFunctions.js';
2
+ import { inspect } from 'util';
3
+ /**
4
+ * GenericKV class that provides a base for key-value store operations.
5
+ * It includes methods for inserting, updating, retrieving, and deleting keys and values.
6
+ */
7
+ class GenericKV {
8
+ static store = "";
9
+ static command = "";
10
+ static persistent = false;
11
+ static keyspace;
12
+ static username;
13
+ static password;
14
+ keyspace;
15
+ username;
16
+ password;
17
+ constructor(options) {
18
+ this.keyspace = options.keyspace;
19
+ this.username = options.username;
20
+ this.password = options.password;
21
+ }
22
+ static [inspect.custom]() {
23
+ return this.name;
24
+ }
25
+ static connectEngine(engine) {
26
+ Object.assign(this, engine);
27
+ }
28
+ /**
29
+ * Get value from a store
30
+ * @param key - The key for the value to retrieve.
31
+ * @param customKey - The custom key for the value to retrieve.
32
+ * @param withPointers - Whether to include pointers in the retrieved value.
33
+ * @return A promise that resolves with the retrieved value.
34
+ */
35
+ static async getValue({ key = "", customKey = null, withPointers = false } = {}) {
36
+ try {
37
+ if (customKey)
38
+ key = convertCustomKey(customKey);
39
+ if (!key) {
40
+ throw new Error("No key provided");
41
+ }
42
+ this.command = "get_value";
43
+ const query = convertToBinaryQuery(this, { key, withPointers });
44
+ return runQuery(this, query);
45
+ }
46
+ catch (err) {
47
+ throw err;
48
+ }
49
+ }
50
+ /**
51
+ * List all depending keys for a given key or custom key.
52
+ * @param key - The key for which to list depending keys.
53
+ * @param customKey - The custom key for which to list depending keys.
54
+ * @returns A promise that resolves with the list of depending keys.
55
+ * */
56
+ static async listAllDependingKeys({ key = "", customKey = null } = {}) {
57
+ try {
58
+ if (customKey)
59
+ key = convertCustomKey(customKey);
60
+ if (!key) {
61
+ throw new Error("No key provided");
62
+ }
63
+ this.command = "list_all_depending_keys";
64
+ const query = convertToBinaryQuery(this, { key });
65
+ return runQuery(this, query);
66
+ }
67
+ catch (err) {
68
+ throw err;
69
+ }
70
+ }
71
+ static async listAllSchemasInKeyspace() {
72
+ this.command = "list_all_schemas_in_keyspace";
73
+ const query = convertToBinaryQuery(this, {});
74
+ return runQuery(this, query);
75
+ }
76
+ /**
77
+ * Deletes a key from the keyspace.
78
+ * @param key - The key to delete.
79
+ * @param customKey - The custom key to delete.
80
+ * @return A promise that resolves with the result of the deletion.
81
+ * */
82
+ static async deleteKey({ key = "", customKey = null } = {}) {
83
+ try {
84
+ if (customKey)
85
+ key = convertCustomKey(customKey);
86
+ if (!key) {
87
+ throw new Error("No key provided");
88
+ }
89
+ this.command = "delete_key";
90
+ const query = convertToBinaryQuery(this, { key });
91
+ return runQuery(this, query);
92
+ }
93
+ catch (err) {
94
+ throw err;
95
+ }
96
+ }
97
+ /**
98
+ * Deletes a bulk of keys from the keyspace.
99
+ * @param bulkKeys - An array of keys to delete.
100
+ * @param bulkCustomKeys - An array of custom keys to delete.
101
+ * @return A promise that resolves with the result of the deletion.
102
+ * */
103
+ static async deleteBulk({ bulkKeys = [], bulkCustomKeys = [] } = {}) {
104
+ try {
105
+ if (bulkCustomKeys.length)
106
+ bulkKeys = bulkKeys.concat(convertCustomKeys(bulkCustomKeys));
107
+ if (!bulkKeys || bulkKeys.length === 0) {
108
+ throw new Error("No keys provided");
109
+ }
110
+ this.command = "delete_bulk";
111
+ const query = convertToBinaryQuery(this, { bulkKeys });
112
+ return runQuery(this, query);
113
+ }
114
+ catch (err) {
115
+ throw err;
116
+ }
117
+ }
118
+ /**
119
+ * Gets bulk values from the keyspace.
120
+ * @param bulkKeys - An array of keys to retrieve.
121
+ * @param bulkCustomKeys - An array of custom keys to retrieve.
122
+ * @param limitOutput - An object specifying the start and stop indices for limiting output.
123
+ * @param withPointers - Whether to include pointers in the retrieved values.
124
+ * @returns A promise that resolves with the retrieved keys.
125
+ */
126
+ static async getBulk({ bulkKeys = [], bulkCustomKeys = [], limitOutput = { start: 0, stop: 0 }, withPointers = false } = {}) {
127
+ try {
128
+ if (bulkCustomKeys.length)
129
+ bulkKeys = bulkKeys.concat(convertCustomKeys(bulkCustomKeys));
130
+ if (!bulkKeys || bulkKeys.length === 0) {
131
+ throw new Error("No keys provided");
132
+ }
133
+ this.command = "get_bulk";
134
+ const query = convertToBinaryQuery(this, { bulkKeys, limitOutput, withPointers });
135
+ return runQuery(this, query);
136
+ }
137
+ catch (err) {
138
+ throw err;
139
+ }
140
+ }
141
+ /**
142
+ * Updates a bulk of keys and values in the keyspace.
143
+ * @param bulkKeysValues - An object where keys are the keys to update and values are the new values.
144
+ * @param bulkCustomKeysValues - An object where custom keys are the keys to update and values are the new values.
145
+ * @return A promise that resolves with the result of the update.
146
+ */
147
+ static async updateBulk({ bulkKeysValues = {}, bulkCustomKeysValues = {} } = {}) {
148
+ try {
149
+ if (Object.keys(bulkCustomKeysValues).length) {
150
+ bulkKeysValues = { ...bulkKeysValues, ...convertCustomKeysValues(bulkCustomKeysValues) };
151
+ }
152
+ if (Object.keys(bulkKeysValues).length === 0) {
153
+ throw new Error("No keys provided");
154
+ }
155
+ this.command = "update_bulk";
156
+ const query = convertToBinaryQuery(this, { bulkKeysValues });
157
+ return runQuery(this, query);
158
+ }
159
+ catch (err) {
160
+ throw err;
161
+ }
162
+ }
163
+ /**
164
+ * Looks up keys based on search criteria.
165
+ * @param searchCriteria - An object containing the search criteria.
166
+ * @param limitOutput - An object specifying the start and stop indices for limiting output.
167
+ * @param schema - The schema to use for the lookup.
168
+ * @returns A promise that resolves with the result of the lookup.
169
+ */
170
+ static async lookupKeysWhere({ searchCriteria = {}, limitOutput = { start: 0, stop: 0 }, schema = null } = {}) {
171
+ try {
172
+ this.command = "lookup_keys";
173
+ const query = convertToBinaryQuery(this, { searchCriteria, limitOutput, schema });
174
+ return runQuery(this, query);
175
+ }
176
+ catch (err) {
177
+ throw err;
178
+ }
179
+ }
180
+ /**
181
+ * Looks up values based on search criteria.
182
+ * @param searchCriteria - An object containing the search criteria.
183
+ * @param limitOutput - An object specifying the start and stop indices for limiting output.
184
+ * @param withPointers - Whether to include pointers in the retrieved values.
185
+ * @param schema - The schema to use for the lookup.
186
+ * @return A promise that resolves with the result of the lookup.
187
+ */
188
+ static async lookupValuesWhere({ searchCriteria = {}, limitOutput = { start: 0, stop: 0 }, withPointers = false, schema = null } = {}) {
189
+ try {
190
+ this.command = "lookup_values";
191
+ const query = convertToBinaryQuery(this, { searchCriteria, limitOutput, withPointers, schema });
192
+ return runQuery(this, query);
193
+ }
194
+ catch (err) {
195
+ throw err;
196
+ }
197
+ }
198
+ /**
199
+ * Gets the length of the keyspace.
200
+ * @returns A promise that resolves with the length of the keyspace.
201
+ */
202
+ static async getLen() {
203
+ this.command = "get_len";
204
+ const query = convertToBinaryQuery(this, {});
205
+ return runQuery(this, query);
206
+ }
207
+ /**
208
+ * Removes the keyspace from the store.
209
+ * @returns A promise that resolves with the result of the keyspace removal.
210
+ * */
211
+ static async removeKeyspace() {
212
+ const query = {
213
+ raw: ["remove-keyspace", "store", this.store, "keyspace", this.keyspace],
214
+ credentials: [this.username, this.password],
215
+ };
216
+ return await runQuery(this, JSON.stringify(query));
217
+ }
218
+ // how to make it available for TS only ?
219
+ /**
220
+ * Enforces a schema on the store.
221
+ * @param schema - The schema to enforce. Should be an instance of the Schema class.
222
+ * @throws TypeError if the schema is not an instance of Schema.
223
+ * @returns A promise that resolves with the result of the schema enforcement.
224
+ */
225
+ static async enforceSchema(schema) {
226
+ function parseType(fieldType) {
227
+ switch (fieldType) {
228
+ case "String": return "String";
229
+ case "Number": return "Number";
230
+ case "Boolean": return "Boolean";
231
+ case "Array": return "Array";
232
+ case "Object": return "Object";
233
+ case "Pointer": return "Pointer";
234
+ case "Timestamp": return "Timestamp";
235
+ default: throw new TypeError(`Unsupported field type: ${fieldType}`);
236
+ }
237
+ }
238
+ let schemaTypes = {};
239
+ if (!schema.metadata) {
240
+ throw new TypeError("Schema must have metadata");
241
+ }
242
+ for (const [field, fieldType] of Object.entries(schema.metadata)) {
243
+ schemaTypes[field] = parseType(fieldType);
244
+ }
245
+ const query = {
246
+ raw: ["enforce-schema", "store", this.store, "keyspace", this.keyspace, "persistent", this.persistent ? "y" : "n", "schema_name", schema.name, "schema_content", `${JSON.stringify(schemaTypes)}`],
247
+ credentials: [this.username, this.password],
248
+ };
249
+ return await runQuery(this, JSON.stringify(query));
250
+ }
251
+ /**
252
+ * Removes an enforced schema from the store.
253
+ * @param schema - The name of the schema to remove.
254
+ * @returns A promise that resolves with the result of the schema removal.
255
+ */
256
+ static async removeEnforcedSchema(schema) {
257
+ if (!schema) {
258
+ throw new TypeError("Schema must be provided");
259
+ }
260
+ const query = {
261
+ raw: ["remove-enforced-schema", "store", this.store, "keyspace", this.keyspace, "persistent", this.persistent ? "y" : "n", "schema_name", schema.name],
262
+ credentials: [this.username, this.password],
263
+ };
264
+ return await runQuery(this, JSON.stringify(query));
265
+ }
266
+ /**
267
+ * Shows the properties of the store.
268
+ * @returns The current instance of GenericKV.
269
+ */
270
+ showStoreProperties() {
271
+ return this;
272
+ }
273
+ }
274
+ export default GenericKV;
@@ -0,0 +1,113 @@
1
+ import GenericKV from '../classes/generic.js';
2
+ /** * Interface for engine configuration options.
3
+ * @interface
4
+ * */
5
+ interface EngineConfig {
6
+ host?: string | null;
7
+ port?: number | null;
8
+ username?: string | null;
9
+ password?: string | null;
10
+ store?: string | null;
11
+ }
12
+ /** * Enum for valid permissions.
13
+ * @enum
14
+ * */
15
+ declare enum ValidPermissions {
16
+ READ = "read",
17
+ WRITE = "write",
18
+ ALL = "all"
19
+ }
20
+ /**
21
+ * Represents the configuration and connection details for a communication engine.
22
+ * This class allows you to connect to a MontyCat server and perform operations such as creating stores, managing owners, and granting/revoking permissions.
23
+ * @class
24
+ * @param {EngineConfig} config - The configuration for the engine, including host, port, username, password, and store.
25
+ * @example
26
+ *
27
+ * const engine = new Engine({
28
+ * host: 'localhost',
29
+ * port: 3000,
30
+ * username: 'admin',
31
+ * password: 'admin',
32
+ * store: 'test_store',
33
+ * });
34
+ *
35
+ */
36
+ declare class Engine {
37
+ private host;
38
+ private port;
39
+ private username;
40
+ private password;
41
+ private store;
42
+ constructor({ host, port, username, password, store }?: EngineConfig);
43
+ /**
44
+ * Creates an Engine instance from a URI string in the format:
45
+ * montycat://host/port/username/password[/store]
46
+ */
47
+ static fromUri(uri: string): Engine;
48
+ /**
49
+ * Creates a store with the specified persistence option.
50
+ * @param {Object} options - Options for creating the store.
51
+ * @param {boolean} [options.persistent=false] - Whether to create a persistent store.
52
+ * @returns {Promise<unknown>} A promise that resolves with the result of the store creation.
53
+ */
54
+ createStore({ persistent }?: {
55
+ persistent?: boolean;
56
+ }): Promise<unknown>;
57
+ /**
58
+ * Removes a store with the specified persistence option.
59
+ * @param {Object} options - Options for removing the store.
60
+ * @param {boolean} [options.persistent=false] - Whether to remove a persistent store.
61
+ * @returns {Promise<unknown>} A promise that resolves with the result of the store removal.
62
+ */
63
+ removeStore({ persistent }?: {
64
+ persistent?: boolean;
65
+ }): Promise<unknown>;
66
+ /**
67
+ * Creates an owner with the specified username and password.
68
+ * @param {string} owner - The username of the owner to create.
69
+ * @param {string} password - The password for the owner.
70
+ * @returns {Promise<unknown>} A promise that resolves with the result of the owner creation.
71
+ */
72
+ createOwner(owner: string, password: string): Promise<unknown>;
73
+ removeOwner(owner: string): Promise<unknown>;
74
+ /**
75
+ * Lists all owners in the store.
76
+ * @returns {Promise<unknown>} A promise that resolves with the list of owners.
77
+ */
78
+ listOwners(): Promise<unknown>;
79
+ /**
80
+ * Grants a permission to an owner for specified keyspaces.
81
+ * @param {string} owner - The username of the owner to grant permission to.
82
+ * @param {ValidPermissions} permission - The permission to grant (read, write, all).
83
+ * @param {string | GenericKV[] | string[] | { keyspace: string }} [keyspaces] - The keyspaces to grant permission for.
84
+ * @returns {Promise<unknown>} A promise that resolves with the result of the grant operation.
85
+ */
86
+ grantTo(owner: string, permission: ValidPermissions, keyspaces?: string | GenericKV[] | string[] | {
87
+ keyspace: string;
88
+ }): Promise<unknown>;
89
+ /**
90
+ * Revokes a permission from an owner for specified keyspaces.
91
+ * @param {string} owner - The username of the owner to revoke permission from.
92
+ * @param {ValidPermissions} permission - The permission to revoke (read, write, all).
93
+ * @param {string | GenericKV[] | string[] | { keyspace: string }} [keyspaces] - The keyspaces to revoke permission for.
94
+ * @returns {Promise<unknown>} A promise that resolves with the result of the revoke operation.
95
+ */
96
+ revokeFrom(owner: string, permission: ValidPermissions, keyspaces?: string | string[] | GenericKV[] | {
97
+ keyspace: string;
98
+ }): Promise<unknown>;
99
+ /**
100
+ * Retrieves the structure of the store.
101
+ * @returns {Promise<unknown>} A promise that resolves with the structure of the store
102
+ * */
103
+ getStructureAvailable(): Promise<unknown>;
104
+ }
105
+ /**
106
+ * Sends a string to the specified host and port, and returns the parsed response.
107
+ * @param {string} host - The host to connect to.
108
+ * @param {number} port - The port to connect to.
109
+ * @param {string} string - The string to send.
110
+ * @returns {Promise<unknown>} A promise that resolves with the parsed response.
111
+ */
112
+ declare function sendData(host: string, port: number, string: string): Promise<unknown>;
113
+ export { Engine, EngineConfig, sendData, ValidPermissions };