@vercube/storage 0.0.1-alpha.15

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.
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Abstract base class for storage implementations
3
+ * Provides a common interface for different storage providers
4
+ *
5
+ * @abstract
6
+ * @class Storage
7
+ * @description
8
+ * The Storage class defines a standard interface for storing and retrieving data.
9
+ * It supports basic operations like get, set, delete, and querying storage state.
10
+ * Concrete implementations must provide the actual storage mechanism.
11
+ */
12
+ export declare abstract class Storage {
13
+ /**
14
+ * Initializes the storage implementation
15
+ * Must be called before using any other storage operations
16
+ * @param {unknown} [options] - Optional initialization parameters
17
+ * @returns {Promise<void>} A promise that resolves when initialization is complete
18
+ */
19
+ abstract initialize(options?: unknown): void | Promise<void>;
20
+ /**
21
+ * Retrieves a value from storage by its key
22
+ * @template T - The type of the stored value
23
+ * @param {string} key - The key to retrieve the value for
24
+ * @returns {Promise<T>} A promise that resolves with the stored value
25
+ */
26
+ abstract getItem<T = unknown>(key: string): T | Promise<T>;
27
+ /**
28
+ * Stores a value in storage with the specified key
29
+ * @template T - The type of the value to store
30
+ * @template U - The type of the optional options object
31
+ * @param {string} key - The key under which to store the value
32
+ * @param {T} value - The value to store
33
+ * @returns {Promise<void>} A promise that resolves when the value is stored
34
+ */
35
+ abstract setItem<
36
+ T = unknown,
37
+ U = unknown
38
+ >(key: string, value: T, options?: U): void | Promise<void>;
39
+ /**
40
+ * Removes a value from storage by its key
41
+ * @param {string} key - The key of the value to delete
42
+ * @returns {Promise<void>} A promise that resolves when the value is deleted
43
+ */
44
+ abstract deleteItem(key: string): void | Promise<void>;
45
+ /**
46
+ * Checks if a value exists in storage for the given key
47
+ * @param {string} key - The key to check
48
+ * @returns {Promise<boolean>} A promise that resolves to true if the key exists, false otherwise
49
+ */
50
+ abstract hasItem(key: string): boolean | Promise<boolean>;
51
+ /**
52
+ * Retrieves all keys currently stored in storage
53
+ * @returns {Promise<string[]>} A promise that resolves with an array of all stored keys
54
+ */
55
+ abstract getKeys(): string[] | Promise<string[]>;
56
+ /**
57
+ * Removes all stored values from storage
58
+ * @returns {Promise<void>} A promise that resolves when all values are cleared
59
+ */
60
+ abstract clear(): void | Promise<void>;
61
+ /**
62
+ * Gets the number of key-value pairs stored in storage
63
+ * @returns {Promise<number>} A promise that resolves with the number of stored items
64
+ */
65
+ abstract size(): number | Promise<number>;
66
+ }
@@ -0,0 +1,94 @@
1
+ import { Storage } from "./Storage.js";
2
+ import { StorageTypes } from "../Types/StorageTypes.js";
3
+ /**
4
+ * Manages multiple storage instances and provides a unified interface for storage operations.
5
+ * Each storage instance is identified by a unique name and implements the Storage interface.
6
+ * This class handles initialization, registration, and delegation of storage operations.
7
+ */
8
+ export declare class StorageManager {
9
+ private gContainer;
10
+ private gLogger;
11
+ /**
12
+ * Map of registered storage instances indexed by their names
13
+ */
14
+ private fStorages;
15
+ /**
16
+ * Mounts a new storage instance with the specified name
17
+ * @param {StorageTypes.Mount} params - Mount parameters containing name and storage implementation
18
+ * @param {string} [params.name] - Optional name for the storage instance, defaults to 'default'
19
+ * @param {IOC.Newable<Storage>} params.storage - Storage implementation to mount
20
+ * @returns {Promise<void>} A promise that resolves when mounting is complete
21
+ */
22
+ mount({ name, storage, initOptions }: StorageTypes.Mount): Promise<void>;
23
+ /**
24
+ * Retrieves a registered storage instance by name
25
+ * @param {string} name - Name of the storage instance to retrieve
26
+ * @returns {Storage | undefined} The storage instance if found, undefined otherwise
27
+ */
28
+ getStorage(name?: string): Storage | undefined;
29
+ /**
30
+ * Retrieves an item from the specified storage
31
+ * @template T - Type of the stored value
32
+ * @param {StorageTypes.GetItem} params - Parameters for retrieving an item
33
+ * @param {string} [params.storage] - Name of the storage to retrieve from, defaults to 'default'
34
+ * @param {string} params.key - Key of the item to retrieve
35
+ * @returns {Promise<T | null>} A promise that resolves with the stored value or null if not found
36
+ */
37
+ getItem<T = unknown>({ storage, key }: StorageTypes.GetItem): Promise<T | null>;
38
+ /**
39
+ * Stores an item in the specified storage
40
+ * @template T - Type of the value to store
41
+ * @param {StorageTypes.SetItem<T>} params - Parameters for storing an item
42
+ * @param {string} [params.storage] - Name of the storage to store in, defaults to 'default'
43
+ * @param {string} params.key - Key under which to store the value
44
+ * @param {T} params.value - Value to store
45
+ * @returns {Promise<void>} A promise that resolves when the value is stored
46
+ */
47
+ setItem<
48
+ T = unknown,
49
+ U = unknown
50
+ >({ storage, key, value, options }: StorageTypes.SetItem<T, U>): Promise<void>;
51
+ /**
52
+ * Deletes an item from the specified storage
53
+ * @param {StorageTypes.DeleteItem} params - Parameters for deleting an item
54
+ * @param {string} [params.storage] - Name of the storage to delete from, defaults to 'default'
55
+ * @param {string} params.key - Key of the item to delete
56
+ * @returns {Promise<void>} A promise that resolves when the item is deleted
57
+ */
58
+ deleteItem({ storage, key }: StorageTypes.DeleteItem): Promise<void>;
59
+ /**
60
+ * Checks if an item exists in the specified storage
61
+ * @param {StorageTypes.HasItem} params - Parameters for checking item existence
62
+ * @param {string} [params.storage] - Name of the storage to check, defaults to 'default'
63
+ * @param {string} params.key - Key to check for
64
+ * @returns {Promise<boolean>} A promise that resolves to true if the item exists, false otherwise
65
+ */
66
+ hasItem({ storage, key }: StorageTypes.HasItem): Promise<boolean>;
67
+ /**
68
+ * Retrieves all keys from the specified storage
69
+ * @param {StorageTypes.GetKeys} params - Parameters for retrieving keys
70
+ * @param {string} [params.storage] - Name of the storage to get keys from, defaults to 'default'
71
+ * @returns {Promise<string[]>} A promise that resolves with an array of all keys
72
+ */
73
+ getKeys({ storage }: StorageTypes.GetKeys): Promise<string[]>;
74
+ /**
75
+ * Clears all items from the specified storage
76
+ * @param {StorageTypes.Clear} params - Parameters for clearing storage
77
+ * @param {string} [params.storage] - Name of the storage to clear, defaults to 'default'
78
+ * @returns {Promise<void>} A promise that resolves when the storage is cleared
79
+ */
80
+ clear({ storage }: StorageTypes.Clear): Promise<void>;
81
+ /**
82
+ * Gets the number of items in the specified storage
83
+ * @param {StorageTypes.Size} params - Parameters for getting storage size
84
+ * @param {string} [params.storage] - Name of the storage to get size of, defaults to 'default'
85
+ * @returns {Promise<number>} A promise that resolves with the number of items
86
+ */
87
+ size({ storage }: StorageTypes.Size): Promise<number>;
88
+ /**
89
+ * Initializes all registered storage instances
90
+ * Called automatically with @Init() decorator
91
+ * @returns {Promise<void>} A promise that resolves when all storages are initialized
92
+ */
93
+ private init;
94
+ }
@@ -0,0 +1,60 @@
1
+ import { Storage } from "../Service/Storage.js";
2
+ /**
3
+ * In-memory storage implementation of the Storage interface
4
+ * Provides a simple key-value store that persists only for the duration of the application runtime
5
+ * Useful for testing, caching, and scenarios where temporary storage is needed
6
+ *
7
+ * @implements {Storage}
8
+ */
9
+ export declare class MemoryStorage implements Storage {
10
+ /** Internal storage map to hold key-value pairs */
11
+ private storage;
12
+ /**
13
+ * Initializes the memory storage
14
+ * No initialization is needed for in-memory storage as the Map is created on instantiation
15
+ */
16
+ initialize(): void;
17
+ /**
18
+ * Retrieves a value from memory storage by its key
19
+ * @template T - Type of the stored value
20
+ * @param {string} key - The key to retrieve the value for
21
+ * @returns {T} The stored value cast to type T
22
+ */
23
+ getItem<T = unknown>(key: string): T;
24
+ /**
25
+ * Stores a value in memory storage with the specified key
26
+ * @template T - Type of the value to store
27
+ * @template U - Type of the options object
28
+ * @param {string} key - The key under which to store the value
29
+ * @param {T} value - The value to store
30
+ */
31
+ setItem<
32
+ T = unknown,
33
+ U = unknown
34
+ >(key: string, value: T, options?: U): void;
35
+ /**
36
+ * Removes a value from memory storage by its key
37
+ * @param {string} key - The key of the value to delete
38
+ */
39
+ deleteItem(key: string): void;
40
+ /**
41
+ * Checks if a value exists in memory storage for the given key
42
+ * @param {string} key - The key to check
43
+ * @returns {boolean} True if the key exists, false otherwise
44
+ */
45
+ hasItem(key: string): boolean;
46
+ /**
47
+ * Retrieves all keys currently stored in memory storage
48
+ * @returns {string[]} Array of all stored keys
49
+ */
50
+ getKeys(): string[];
51
+ /**
52
+ * Removes all stored values from memory storage
53
+ */
54
+ clear(): void;
55
+ /**
56
+ * Gets the number of key-value pairs stored in memory storage
57
+ * @returns {number} The number of stored items
58
+ */
59
+ size(): number;
60
+ }
@@ -0,0 +1,36 @@
1
+ import type { IOC } from "@vercube/di";
2
+ import { Storage } from "../Service/Storage.js";
3
+ export declare namespace StorageTypes {
4
+ interface BaseOptions {
5
+ storage?: string;
6
+ }
7
+ interface Mount {
8
+ name?: string;
9
+ storage: IOC.Newable<Storage>;
10
+ initOptions?: unknown;
11
+ }
12
+ interface Storages<T = unknown> {
13
+ storage: Storage;
14
+ initOptions?: T;
15
+ }
16
+ interface GetItem extends BaseOptions {
17
+ key: string;
18
+ }
19
+ interface SetItem<
20
+ T = unknown,
21
+ U = unknown
22
+ > extends BaseOptions {
23
+ key: string;
24
+ value: T;
25
+ options?: U;
26
+ }
27
+ interface DeleteItem extends BaseOptions {
28
+ key: string;
29
+ }
30
+ interface HasItem extends BaseOptions {
31
+ key: string;
32
+ }
33
+ interface GetKeys extends BaseOptions {}
34
+ interface Clear extends BaseOptions {}
35
+ interface Size extends BaseOptions {}
36
+ }
package/dist/index.cjs ADDED
@@ -0,0 +1,239 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __commonJS = (cb, mod) => function() {
9
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
13
+ key = keys[i];
14
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
15
+ get: ((k) => from[k]).bind(null, key),
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ }
19
+ return to;
20
+ };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
22
+ value: mod,
23
+ enumerable: true
24
+ }) : target, mod));
25
+
26
+ //#endregion
27
+ const __vercube_di = __toESM(require("@vercube/di"));
28
+ const __vercube_logger = __toESM(require("@vercube/logger"));
29
+
30
+ //#region node_modules/.pnpm/@oxc-project+runtime@0.62.0/node_modules/@oxc-project/runtime/src/helpers/decorate.js
31
+ var require_decorate = __commonJS({ "node_modules/.pnpm/@oxc-project+runtime@0.62.0/node_modules/@oxc-project/runtime/src/helpers/decorate.js"(exports, module) {
32
+ function __decorate(decorators, target, key, desc) {
33
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
34
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
35
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
36
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
37
+ }
38
+ module.exports = __decorate, module.exports.__esModule = true, module.exports["default"] = module.exports;
39
+ } });
40
+ var import_decorate = __toESM(require_decorate(), 1);
41
+
42
+ //#endregion
43
+ //#region packages/storage/src/Service/StorageManager.ts
44
+ var StorageManager = class {
45
+ gContainer;
46
+ gLogger;
47
+ /**
48
+ * Map of registered storage instances indexed by their names
49
+ */
50
+ fStorages = new Map();
51
+ /**
52
+ * Mounts a new storage instance with the specified name
53
+ * @param {StorageTypes.Mount} params - Mount parameters containing name and storage implementation
54
+ * @param {string} [params.name] - Optional name for the storage instance, defaults to 'default'
55
+ * @param {IOC.Newable<Storage>} params.storage - Storage implementation to mount
56
+ * @returns {Promise<void>} A promise that resolves when mounting is complete
57
+ */
58
+ async mount({ name, storage, initOptions }) {
59
+ this.fStorages.set(name ?? "default", {
60
+ storage: this.gContainer.resolve(storage),
61
+ initOptions
62
+ });
63
+ }
64
+ /**
65
+ * Retrieves a registered storage instance by name
66
+ * @param {string} name - Name of the storage instance to retrieve
67
+ * @returns {Storage | undefined} The storage instance if found, undefined otherwise
68
+ */
69
+ getStorage(name = "default") {
70
+ return this.fStorages.get(name)?.storage ?? void 0;
71
+ }
72
+ /**
73
+ * Retrieves an item from the specified storage
74
+ * @template T - Type of the stored value
75
+ * @param {StorageTypes.GetItem} params - Parameters for retrieving an item
76
+ * @param {string} [params.storage] - Name of the storage to retrieve from, defaults to 'default'
77
+ * @param {string} params.key - Key of the item to retrieve
78
+ * @returns {Promise<T | null>} A promise that resolves with the stored value or null if not found
79
+ */
80
+ async getItem({ storage, key }) {
81
+ const storageInstance = this.getStorage(storage);
82
+ return storageInstance?.getItem(key) ?? null;
83
+ }
84
+ /**
85
+ * Stores an item in the specified storage
86
+ * @template T - Type of the value to store
87
+ * @param {StorageTypes.SetItem<T>} params - Parameters for storing an item
88
+ * @param {string} [params.storage] - Name of the storage to store in, defaults to 'default'
89
+ * @param {string} params.key - Key under which to store the value
90
+ * @param {T} params.value - Value to store
91
+ * @returns {Promise<void>} A promise that resolves when the value is stored
92
+ */
93
+ async setItem({ storage, key, value, options }) {
94
+ const storageInstance = this.getStorage(storage);
95
+ storageInstance?.setItem(key, value, options);
96
+ }
97
+ /**
98
+ * Deletes an item from the specified storage
99
+ * @param {StorageTypes.DeleteItem} params - Parameters for deleting an item
100
+ * @param {string} [params.storage] - Name of the storage to delete from, defaults to 'default'
101
+ * @param {string} params.key - Key of the item to delete
102
+ * @returns {Promise<void>} A promise that resolves when the item is deleted
103
+ */
104
+ async deleteItem({ storage, key }) {
105
+ const storageInstance = this.getStorage(storage);
106
+ storageInstance?.deleteItem(key);
107
+ }
108
+ /**
109
+ * Checks if an item exists in the specified storage
110
+ * @param {StorageTypes.HasItem} params - Parameters for checking item existence
111
+ * @param {string} [params.storage] - Name of the storage to check, defaults to 'default'
112
+ * @param {string} params.key - Key to check for
113
+ * @returns {Promise<boolean>} A promise that resolves to true if the item exists, false otherwise
114
+ */
115
+ async hasItem({ storage, key }) {
116
+ const storageInstance = this.getStorage(storage);
117
+ return storageInstance?.hasItem(key) ?? false;
118
+ }
119
+ /**
120
+ * Retrieves all keys from the specified storage
121
+ * @param {StorageTypes.GetKeys} params - Parameters for retrieving keys
122
+ * @param {string} [params.storage] - Name of the storage to get keys from, defaults to 'default'
123
+ * @returns {Promise<string[]>} A promise that resolves with an array of all keys
124
+ */
125
+ async getKeys({ storage }) {
126
+ const storageInstance = this.getStorage(storage);
127
+ return storageInstance?.getKeys() ?? [];
128
+ }
129
+ /**
130
+ * Clears all items from the specified storage
131
+ * @param {StorageTypes.Clear} params - Parameters for clearing storage
132
+ * @param {string} [params.storage] - Name of the storage to clear, defaults to 'default'
133
+ * @returns {Promise<void>} A promise that resolves when the storage is cleared
134
+ */
135
+ async clear({ storage }) {
136
+ const storageInstance = this.getStorage(storage);
137
+ storageInstance?.clear();
138
+ }
139
+ /**
140
+ * Gets the number of items in the specified storage
141
+ * @param {StorageTypes.Size} params - Parameters for getting storage size
142
+ * @param {string} [params.storage] - Name of the storage to get size of, defaults to 'default'
143
+ * @returns {Promise<number>} A promise that resolves with the number of items
144
+ */
145
+ async size({ storage }) {
146
+ const storageInstance = this.getStorage(storage);
147
+ return storageInstance?.size() ?? 0;
148
+ }
149
+ /**
150
+ * Initializes all registered storage instances
151
+ * Called automatically with @Init() decorator
152
+ * @returns {Promise<void>} A promise that resolves when all storages are initialized
153
+ */
154
+ async init() {
155
+ for (const { storage, initOptions } of this.fStorages.values()) try {
156
+ await storage?.initialize(initOptions);
157
+ } catch (error) {
158
+ this.gLogger?.error("Vercube/StorageManager::init", error);
159
+ }
160
+ }
161
+ };
162
+ (0, import_decorate.default)([(0, __vercube_di.Inject)(__vercube_di.Container)], StorageManager.prototype, "gContainer", void 0);
163
+ (0, import_decorate.default)([(0, __vercube_di.InjectOptional)(__vercube_logger.Logger)], StorageManager.prototype, "gLogger", void 0);
164
+ (0, import_decorate.default)([(0, __vercube_di.Init)()], StorageManager.prototype, "init", null);
165
+
166
+ //#endregion
167
+ //#region packages/storage/src/Service/Storage.ts
168
+ var Storage = class {};
169
+
170
+ //#endregion
171
+ //#region packages/storage/src/Storages/MemoryStorage.ts
172
+ var MemoryStorage = class {
173
+ /** Internal storage map to hold key-value pairs */
174
+ storage = new Map();
175
+ /**
176
+ * Initializes the memory storage
177
+ * No initialization is needed for in-memory storage as the Map is created on instantiation
178
+ */
179
+ initialize() {}
180
+ /**
181
+ * Retrieves a value from memory storage by its key
182
+ * @template T - Type of the stored value
183
+ * @param {string} key - The key to retrieve the value for
184
+ * @returns {T} The stored value cast to type T
185
+ */
186
+ getItem(key) {
187
+ return this.storage.get(key);
188
+ }
189
+ /**
190
+ * Stores a value in memory storage with the specified key
191
+ * @template T - Type of the value to store
192
+ * @template U - Type of the options object
193
+ * @param {string} key - The key under which to store the value
194
+ * @param {T} value - The value to store
195
+ */
196
+ setItem(key, value, options) {
197
+ this.storage.set(key, value);
198
+ }
199
+ /**
200
+ * Removes a value from memory storage by its key
201
+ * @param {string} key - The key of the value to delete
202
+ */
203
+ deleteItem(key) {
204
+ this.storage.delete(key);
205
+ }
206
+ /**
207
+ * Checks if a value exists in memory storage for the given key
208
+ * @param {string} key - The key to check
209
+ * @returns {boolean} True if the key exists, false otherwise
210
+ */
211
+ hasItem(key) {
212
+ return this.storage.has(key);
213
+ }
214
+ /**
215
+ * Retrieves all keys currently stored in memory storage
216
+ * @returns {string[]} Array of all stored keys
217
+ */
218
+ getKeys() {
219
+ return [...this.storage.keys()];
220
+ }
221
+ /**
222
+ * Removes all stored values from memory storage
223
+ */
224
+ clear() {
225
+ this.storage.clear();
226
+ }
227
+ /**
228
+ * Gets the number of key-value pairs stored in memory storage
229
+ * @returns {number} The number of stored items
230
+ */
231
+ size() {
232
+ return this.storage.size;
233
+ }
234
+ };
235
+
236
+ //#endregion
237
+ exports.MemoryStorage = MemoryStorage
238
+ exports.Storage = Storage
239
+ exports.StorageManager = StorageManager
@@ -0,0 +1,4 @@
1
+ export * from "./Service/StorageManager.js";
2
+ export * from "./Service/Storage.js";
3
+ export * from "./Storages/MemoryStorage.js";
4
+ export * from "./Types/StorageTypes.js";
package/dist/index.mjs ADDED
@@ -0,0 +1,207 @@
1
+ import { Container, Init, Inject, InjectOptional } from "@vercube/di";
2
+ import { Logger } from "@vercube/logger";
3
+
4
+ //#region node_modules/.pnpm/@oxc-project+runtime@0.62.0/node_modules/@oxc-project/runtime/src/helpers/esm/decorate.js
5
+ function __decorate(decorators, target, key, desc) {
6
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
7
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
8
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
9
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
10
+ }
11
+
12
+ //#endregion
13
+ //#region packages/storage/src/Service/StorageManager.ts
14
+ var StorageManager = class {
15
+ gContainer;
16
+ gLogger;
17
+ /**
18
+ * Map of registered storage instances indexed by their names
19
+ */
20
+ fStorages = new Map();
21
+ /**
22
+ * Mounts a new storage instance with the specified name
23
+ * @param {StorageTypes.Mount} params - Mount parameters containing name and storage implementation
24
+ * @param {string} [params.name] - Optional name for the storage instance, defaults to 'default'
25
+ * @param {IOC.Newable<Storage>} params.storage - Storage implementation to mount
26
+ * @returns {Promise<void>} A promise that resolves when mounting is complete
27
+ */
28
+ async mount({ name, storage, initOptions }) {
29
+ this.fStorages.set(name ?? "default", {
30
+ storage: this.gContainer.resolve(storage),
31
+ initOptions
32
+ });
33
+ }
34
+ /**
35
+ * Retrieves a registered storage instance by name
36
+ * @param {string} name - Name of the storage instance to retrieve
37
+ * @returns {Storage | undefined} The storage instance if found, undefined otherwise
38
+ */
39
+ getStorage(name = "default") {
40
+ return this.fStorages.get(name)?.storage ?? void 0;
41
+ }
42
+ /**
43
+ * Retrieves an item from the specified storage
44
+ * @template T - Type of the stored value
45
+ * @param {StorageTypes.GetItem} params - Parameters for retrieving an item
46
+ * @param {string} [params.storage] - Name of the storage to retrieve from, defaults to 'default'
47
+ * @param {string} params.key - Key of the item to retrieve
48
+ * @returns {Promise<T | null>} A promise that resolves with the stored value or null if not found
49
+ */
50
+ async getItem({ storage, key }) {
51
+ const storageInstance = this.getStorage(storage);
52
+ return storageInstance?.getItem(key) ?? null;
53
+ }
54
+ /**
55
+ * Stores an item in the specified storage
56
+ * @template T - Type of the value to store
57
+ * @param {StorageTypes.SetItem<T>} params - Parameters for storing an item
58
+ * @param {string} [params.storage] - Name of the storage to store in, defaults to 'default'
59
+ * @param {string} params.key - Key under which to store the value
60
+ * @param {T} params.value - Value to store
61
+ * @returns {Promise<void>} A promise that resolves when the value is stored
62
+ */
63
+ async setItem({ storage, key, value, options }) {
64
+ const storageInstance = this.getStorage(storage);
65
+ storageInstance?.setItem(key, value, options);
66
+ }
67
+ /**
68
+ * Deletes an item from the specified storage
69
+ * @param {StorageTypes.DeleteItem} params - Parameters for deleting an item
70
+ * @param {string} [params.storage] - Name of the storage to delete from, defaults to 'default'
71
+ * @param {string} params.key - Key of the item to delete
72
+ * @returns {Promise<void>} A promise that resolves when the item is deleted
73
+ */
74
+ async deleteItem({ storage, key }) {
75
+ const storageInstance = this.getStorage(storage);
76
+ storageInstance?.deleteItem(key);
77
+ }
78
+ /**
79
+ * Checks if an item exists in the specified storage
80
+ * @param {StorageTypes.HasItem} params - Parameters for checking item existence
81
+ * @param {string} [params.storage] - Name of the storage to check, defaults to 'default'
82
+ * @param {string} params.key - Key to check for
83
+ * @returns {Promise<boolean>} A promise that resolves to true if the item exists, false otherwise
84
+ */
85
+ async hasItem({ storage, key }) {
86
+ const storageInstance = this.getStorage(storage);
87
+ return storageInstance?.hasItem(key) ?? false;
88
+ }
89
+ /**
90
+ * Retrieves all keys from the specified storage
91
+ * @param {StorageTypes.GetKeys} params - Parameters for retrieving keys
92
+ * @param {string} [params.storage] - Name of the storage to get keys from, defaults to 'default'
93
+ * @returns {Promise<string[]>} A promise that resolves with an array of all keys
94
+ */
95
+ async getKeys({ storage }) {
96
+ const storageInstance = this.getStorage(storage);
97
+ return storageInstance?.getKeys() ?? [];
98
+ }
99
+ /**
100
+ * Clears all items from the specified storage
101
+ * @param {StorageTypes.Clear} params - Parameters for clearing storage
102
+ * @param {string} [params.storage] - Name of the storage to clear, defaults to 'default'
103
+ * @returns {Promise<void>} A promise that resolves when the storage is cleared
104
+ */
105
+ async clear({ storage }) {
106
+ const storageInstance = this.getStorage(storage);
107
+ storageInstance?.clear();
108
+ }
109
+ /**
110
+ * Gets the number of items in the specified storage
111
+ * @param {StorageTypes.Size} params - Parameters for getting storage size
112
+ * @param {string} [params.storage] - Name of the storage to get size of, defaults to 'default'
113
+ * @returns {Promise<number>} A promise that resolves with the number of items
114
+ */
115
+ async size({ storage }) {
116
+ const storageInstance = this.getStorage(storage);
117
+ return storageInstance?.size() ?? 0;
118
+ }
119
+ /**
120
+ * Initializes all registered storage instances
121
+ * Called automatically with @Init() decorator
122
+ * @returns {Promise<void>} A promise that resolves when all storages are initialized
123
+ */
124
+ async init() {
125
+ for (const { storage, initOptions } of this.fStorages.values()) try {
126
+ await storage?.initialize(initOptions);
127
+ } catch (error) {
128
+ this.gLogger?.error("Vercube/StorageManager::init", error);
129
+ }
130
+ }
131
+ };
132
+ __decorate([Inject(Container)], StorageManager.prototype, "gContainer", void 0);
133
+ __decorate([InjectOptional(Logger)], StorageManager.prototype, "gLogger", void 0);
134
+ __decorate([Init()], StorageManager.prototype, "init", null);
135
+
136
+ //#endregion
137
+ //#region packages/storage/src/Service/Storage.ts
138
+ var Storage = class {};
139
+
140
+ //#endregion
141
+ //#region packages/storage/src/Storages/MemoryStorage.ts
142
+ var MemoryStorage = class {
143
+ /** Internal storage map to hold key-value pairs */
144
+ storage = new Map();
145
+ /**
146
+ * Initializes the memory storage
147
+ * No initialization is needed for in-memory storage as the Map is created on instantiation
148
+ */
149
+ initialize() {}
150
+ /**
151
+ * Retrieves a value from memory storage by its key
152
+ * @template T - Type of the stored value
153
+ * @param {string} key - The key to retrieve the value for
154
+ * @returns {T} The stored value cast to type T
155
+ */
156
+ getItem(key) {
157
+ return this.storage.get(key);
158
+ }
159
+ /**
160
+ * Stores a value in memory storage with the specified key
161
+ * @template T - Type of the value to store
162
+ * @template U - Type of the options object
163
+ * @param {string} key - The key under which to store the value
164
+ * @param {T} value - The value to store
165
+ */
166
+ setItem(key, value, options) {
167
+ this.storage.set(key, value);
168
+ }
169
+ /**
170
+ * Removes a value from memory storage by its key
171
+ * @param {string} key - The key of the value to delete
172
+ */
173
+ deleteItem(key) {
174
+ this.storage.delete(key);
175
+ }
176
+ /**
177
+ * Checks if a value exists in memory storage for the given key
178
+ * @param {string} key - The key to check
179
+ * @returns {boolean} True if the key exists, false otherwise
180
+ */
181
+ hasItem(key) {
182
+ return this.storage.has(key);
183
+ }
184
+ /**
185
+ * Retrieves all keys currently stored in memory storage
186
+ * @returns {string[]} Array of all stored keys
187
+ */
188
+ getKeys() {
189
+ return [...this.storage.keys()];
190
+ }
191
+ /**
192
+ * Removes all stored values from memory storage
193
+ */
194
+ clear() {
195
+ this.storage.clear();
196
+ }
197
+ /**
198
+ * Gets the number of key-value pairs stored in memory storage
199
+ * @returns {number} The number of stored items
200
+ */
201
+ size() {
202
+ return this.storage.size;
203
+ }
204
+ };
205
+
206
+ //#endregion
207
+ export { MemoryStorage, Storage, StorageManager };
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@vercube/storage",
3
+ "version": "0.0.1-alpha.15",
4
+ "description": "Storage module for Vercube framework",
5
+ "repository": "@vercube/storage",
6
+ "license": "MIT",
7
+ "sideEffects": false,
8
+ "type": "module",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.mjs",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "main": "./dist/index.cjs",
17
+ "module": "./dist/index.mjs",
18
+ "types": "./dist/index.d.ts",
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "dependencies": {
23
+ "@vercube/di": "0.0.1-alpha.15",
24
+ "@vercube/logger": "0.0.1-alpha.15"
25
+ },
26
+ "devDependencies": {},
27
+ "publishConfig": {
28
+ "access": "public"
29
+ }
30
+ }