@vercube/storage 0.0.33 → 0.0.35
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 +30 -24
- package/dist/Drivers/MemoryStorage.d.mts +8 -2
- package/dist/Drivers/MemoryStorage.mjs +9 -2
- package/dist/Drivers/S3Storage.d.mts +109 -3
- package/dist/Drivers/S3Storage.mjs +84 -6
- package/dist/{Storage-DeF_6O7t.d.mts → Storage-LzARx0pF.d.mts} +10 -3
- package/dist/StorageError-Dl7jGeVM.mjs +30 -0
- package/dist/index.d.mts +38 -2
- package/dist/index.mjs +14 -2
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,34 +1,40 @@
|
|
|
1
1
|
<div align="center">
|
|
2
|
-
<
|
|
2
|
+
<img src="https://raw.githubusercontent.com/vercube/vercube/refs/heads/main/.github/assets/cover.png" width="100%" alt="Vercube - Unleash your server development." />
|
|
3
3
|
<br>
|
|
4
4
|
<br>
|
|
5
5
|
|
|
6
|
-
#
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
<a href="https://codecov.io/gh/vercube/vercube" target="_blank">
|
|
20
|
-
<img src="https://img.shields.io/codecov/c/github/vercube/vercube?style=for-the-badge&color=%23767eff" alt="Coverage"/>
|
|
21
|
-
</a>
|
|
22
|
-
<br/>
|
|
23
|
-
<br/>
|
|
6
|
+
# @vercube/storage
|
|
7
|
+
|
|
8
|
+
### Key-value storage for Vercube apps
|
|
9
|
+
|
|
10
|
+
[&labelColor=%23000&color=%232f2f2f>)](https://deepwiki.com/vercube/vercube)
|
|
11
|
+
&labelColor=%23000&color=%232e2e2e&link=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40vercube%2Fstorage>)
|
|
12
|
+
&labelColor=%23000&color=%232f2f2f>)
|
|
13
|
+
&labelColor=%23000&color=%232f2f2f>)
|
|
14
|
+
|
|
15
|
+
**Simple storage abstraction - cache data, manage sessions, whatever you need. Mount different backends, use them through one API.**
|
|
16
|
+
|
|
17
|
+
[Website](https://vercube.dev) • [Documentation](https://vercube.dev/docs/getting-started)
|
|
18
|
+
|
|
24
19
|
</div>
|
|
25
20
|
|
|
26
|
-
|
|
21
|
+
## ✨ Features
|
|
22
|
+
|
|
23
|
+
- **Unified API** - `getItem`, `setItem`, `deleteItem` - same interface everywhere
|
|
24
|
+
- **Multiple backends** - memory, filesystem, or plug in your own
|
|
25
|
+
- **Named instances** - mount as many storages as you need
|
|
26
|
+
- **Type-safe** - generic types for stored values
|
|
27
|
+
|
|
28
|
+
## 📦 Installation
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pnpm add @vercube/storage
|
|
32
|
+
```
|
|
27
33
|
|
|
28
|
-
##
|
|
34
|
+
## 📖 Usage
|
|
29
35
|
|
|
30
|
-
|
|
36
|
+
Check out the full [documentation](https://vercube.dev/docs/modules/storage/overview)
|
|
31
37
|
|
|
32
|
-
##
|
|
38
|
+
## 📜 License
|
|
33
39
|
|
|
34
|
-
|
|
40
|
+
[MIT](https://github.com/vercube/vercube/blob/main/LICENSE)
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { t as Storage } from "../Storage-
|
|
1
|
+
import { t as Storage } from "../Storage-LzARx0pF.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/Drivers/MemoryStorage.d.ts
|
|
4
|
-
|
|
5
4
|
/**
|
|
6
5
|
* In-memory storage implementation of the Storage interface
|
|
7
6
|
* Provides a simple key-value store that persists only for the duration of the application runtime
|
|
@@ -24,6 +23,13 @@ declare class MemoryStorage implements Storage {
|
|
|
24
23
|
* @returns {T} The stored value cast to type T
|
|
25
24
|
*/
|
|
26
25
|
getItem<T = unknown>(key: string): T;
|
|
26
|
+
/**
|
|
27
|
+
* Retrieves multiple items from memory storage by their keys
|
|
28
|
+
* @template T - Type of the stored value
|
|
29
|
+
* @param {string[]} keys - The keys to retrieve the values for
|
|
30
|
+
* @returns {T[]} Array of stored values, with undefined for missing keys
|
|
31
|
+
*/
|
|
32
|
+
getItems<T = unknown>(keys: string[]): T[];
|
|
27
33
|
/**
|
|
28
34
|
* Stores a value in memory storage with the specified key
|
|
29
35
|
* @template T - Type of the value to store
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import "../Storage-Yj1niSzz.mjs";
|
|
2
|
-
|
|
3
1
|
//#region src/Drivers/MemoryStorage.ts
|
|
4
2
|
/**
|
|
5
3
|
* In-memory storage implementation of the Storage interface
|
|
@@ -26,6 +24,15 @@ var MemoryStorage = class {
|
|
|
26
24
|
return this.storage.get(key);
|
|
27
25
|
}
|
|
28
26
|
/**
|
|
27
|
+
* Retrieves multiple items from memory storage by their keys
|
|
28
|
+
* @template T - Type of the stored value
|
|
29
|
+
* @param {string[]} keys - The keys to retrieve the values for
|
|
30
|
+
* @returns {T[]} Array of stored values, with undefined for missing keys
|
|
31
|
+
*/
|
|
32
|
+
getItems(keys) {
|
|
33
|
+
return keys.map((key) => this.storage.get(key));
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
29
36
|
* Stores a value in memory storage with the specified key
|
|
30
37
|
* @template T - Type of the value to store
|
|
31
38
|
* @template U - Type of the options object
|
|
@@ -1,17 +1,100 @@
|
|
|
1
|
-
import { t as Storage } from "../Storage-
|
|
1
|
+
import { t as Storage } from "../Storage-LzARx0pF.mjs";
|
|
2
|
+
import { Logger } from "@vercube/logger";
|
|
2
3
|
import { S3ClientConfig } from "@aws-sdk/client-s3";
|
|
3
4
|
|
|
4
5
|
//#region src/Drivers/S3Storage.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Configuration options for S3 storage.
|
|
8
|
+
* Extends AWS S3ClientConfig with required bucket name.
|
|
9
|
+
*
|
|
10
|
+
* @property {string} bucket - The S3 bucket name (required)
|
|
11
|
+
* @property {string} region - AWS region (e.g., 'us-east-1') (required)
|
|
12
|
+
* @property {object} credentials - AWS credentials (optional)
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* **Authentication Methods:**
|
|
16
|
+
*
|
|
17
|
+
* 1. **IAM Roles (Recommended for AWS environments):**
|
|
18
|
+
* Omit the credentials field to use IAM roles automatically.
|
|
19
|
+
* This is the most secure method for Lambda, EC2, ECS, etc.
|
|
20
|
+
* ```ts
|
|
21
|
+
* { bucket: 'my-bucket', region: 'us-east-1' }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* 2. **Explicit Credentials:**
|
|
25
|
+
* Provide credentials explicitly (use with AWS Secrets Manager for security).
|
|
26
|
+
* ```ts
|
|
27
|
+
* {
|
|
28
|
+
* bucket: 'my-bucket',
|
|
29
|
+
* region: 'us-east-1',
|
|
30
|
+
* credentials: {
|
|
31
|
+
* accessKeyId: 'YOUR_ACCESS_KEY',
|
|
32
|
+
* secretAccessKey: 'YOUR_SECRET_KEY'
|
|
33
|
+
* }
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* 3. **STS Temporary Credentials:**
|
|
38
|
+
* Use temporary credentials from AWS STS AssumeRole.
|
|
39
|
+
* ```ts
|
|
40
|
+
* {
|
|
41
|
+
* bucket: 'my-bucket',
|
|
42
|
+
* region: 'us-east-1',
|
|
43
|
+
* credentials: {
|
|
44
|
+
* accessKeyId: tempCreds.AccessKeyId,
|
|
45
|
+
* secretAccessKey: tempCreds.SecretAccessKey,
|
|
46
|
+
* sessionToken: tempCreds.SessionToken
|
|
47
|
+
* }
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* When credentials are not provided, AWS SDK uses the default credential provider chain:
|
|
52
|
+
* - IAM roles (Lambda execution role, EC2 instance profile, ECS task role)
|
|
53
|
+
* - Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
|
|
54
|
+
* - Shared credentials file (~/.aws/credentials)
|
|
55
|
+
* - ECS container credentials
|
|
56
|
+
* - EC2 instance metadata service
|
|
57
|
+
*/
|
|
5
58
|
interface S3BaseOptions extends S3ClientConfig {
|
|
6
59
|
bucket: string;
|
|
60
|
+
logger?: Logger;
|
|
7
61
|
}
|
|
8
62
|
/**
|
|
9
63
|
* S3 storage implementation of the Storage interface.
|
|
10
64
|
* Provides key-value operations backed by AWS S3.
|
|
11
65
|
*
|
|
66
|
+
* @remarks
|
|
67
|
+
* **Security Best Practices:**
|
|
68
|
+
* - Use IAM roles in AWS environments (Lambda, EC2, ECS) instead of explicit credentials
|
|
69
|
+
* - Store credentials in AWS Secrets Manager if explicit credentials are required
|
|
70
|
+
* - Never hardcode credentials in source code
|
|
71
|
+
* - Use environment-specific configurations (IAM roles for production, credentials for local dev)
|
|
72
|
+
* - Grant minimum required S3 permissions (principle of least privilege)
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* // Recommended: Using IAM roles (no credentials)
|
|
77
|
+
* const storage = new S3Storage();
|
|
78
|
+
* await storage.initialize({
|
|
79
|
+
* bucket: 'my-app-bucket',
|
|
80
|
+
* region: 'us-east-1'
|
|
81
|
+
* });
|
|
82
|
+
*
|
|
83
|
+
* // Alternative: Using explicit credentials from environment
|
|
84
|
+
* await storage.initialize({
|
|
85
|
+
* bucket: 'my-app-bucket',
|
|
86
|
+
* region: 'us-east-1',
|
|
87
|
+
* credentials: {
|
|
88
|
+
* accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
89
|
+
* secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
|
|
90
|
+
* }
|
|
91
|
+
* });
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
12
94
|
* @implements {Storage}
|
|
13
95
|
*/
|
|
14
96
|
declare class S3Storage implements Storage<S3BaseOptions> {
|
|
97
|
+
private logger?;
|
|
15
98
|
private s3;
|
|
16
99
|
private bucket;
|
|
17
100
|
/**
|
|
@@ -19,6 +102,20 @@ declare class S3Storage implements Storage<S3BaseOptions> {
|
|
|
19
102
|
*
|
|
20
103
|
* @param {S3BaseOptions} options - Configuration options for the S3 client.
|
|
21
104
|
* @returns {Promise<void>} A promise that resolves when initialization is complete.
|
|
105
|
+
*
|
|
106
|
+
* @remarks
|
|
107
|
+
* The AWS SDK automatically handles credential resolution when credentials are not explicitly provided.
|
|
108
|
+
* In AWS environments (Lambda, EC2, ECS), IAM roles are used automatically, providing enhanced security
|
|
109
|
+
* without manual credential management.
|
|
110
|
+
*
|
|
111
|
+
* Priority of credential sources (when credentials field is omitted):
|
|
112
|
+
* 1. IAM roles (Lambda execution role, EC2 instance profile, ECS task role)
|
|
113
|
+
* 2. Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
|
|
114
|
+
* 3. Shared credentials file (~/.aws/credentials)
|
|
115
|
+
* 4. ECS container credentials
|
|
116
|
+
* 5. EC2 instance metadata service
|
|
117
|
+
*
|
|
118
|
+
* When credentials are explicitly provided in options, they take precedence over all other sources.
|
|
22
119
|
*/
|
|
23
120
|
initialize(options: S3BaseOptions): Promise<void>;
|
|
24
121
|
/**
|
|
@@ -27,9 +124,18 @@ declare class S3Storage implements Storage<S3BaseOptions> {
|
|
|
27
124
|
* @template T
|
|
28
125
|
* @param {string} key - The key whose value should be retrieved.
|
|
29
126
|
* @returns {Promise<T | undefined>} The stored value parsed as type T, or undefined if the key does not exist.
|
|
30
|
-
* @throws
|
|
127
|
+
* @throws {StorageError} Wraps any S3 error except for missing key (`NoSuchKey`) with sanitized error information.
|
|
31
128
|
*/
|
|
32
129
|
getItem<T = unknown>(key: string): Promise<T>;
|
|
130
|
+
/**
|
|
131
|
+
* Retrieves and parses multiple values from S3 storage by their keys.
|
|
132
|
+
*
|
|
133
|
+
* @template T
|
|
134
|
+
* @param {string[]} keys - The keys whose values should be retrieved.
|
|
135
|
+
* @returns {Promise<T[]>} An array of stored values parsed as type T, with undefined for missing keys.
|
|
136
|
+
* @throws {StorageError} Wraps any S3 error except for missing keys (`NoSuchKey`) with sanitized error information.
|
|
137
|
+
*/
|
|
138
|
+
getItems<T = unknown>(keys: string[]): Promise<T[]>;
|
|
33
139
|
/**
|
|
34
140
|
* Stores a value in S3 storage under the specified key.
|
|
35
141
|
*
|
|
@@ -53,7 +159,7 @@ declare class S3Storage implements Storage<S3BaseOptions> {
|
|
|
53
159
|
*
|
|
54
160
|
* @param {string} key - The key to check.
|
|
55
161
|
* @returns {Promise<boolean>} True if the key exists, false otherwise.
|
|
56
|
-
* @throws
|
|
162
|
+
* @throws {StorageError} Wraps any S3 error except for missing key (`NoSuchKey`) with sanitized error information.
|
|
57
163
|
*/
|
|
58
164
|
hasItem(key: string): Promise<boolean>;
|
|
59
165
|
/**
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { t as StorageError } from "../StorageError-Dl7jGeVM.mjs";
|
|
1
2
|
import { DeleteObjectCommand, GetObjectCommand, ListObjectsV2Command, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
|
|
2
3
|
|
|
3
4
|
//#region src/Drivers/S3Storage.ts
|
|
@@ -5,9 +6,38 @@ import { DeleteObjectCommand, GetObjectCommand, ListObjectsV2Command, PutObjectC
|
|
|
5
6
|
* S3 storage implementation of the Storage interface.
|
|
6
7
|
* Provides key-value operations backed by AWS S3.
|
|
7
8
|
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* **Security Best Practices:**
|
|
11
|
+
* - Use IAM roles in AWS environments (Lambda, EC2, ECS) instead of explicit credentials
|
|
12
|
+
* - Store credentials in AWS Secrets Manager if explicit credentials are required
|
|
13
|
+
* - Never hardcode credentials in source code
|
|
14
|
+
* - Use environment-specific configurations (IAM roles for production, credentials for local dev)
|
|
15
|
+
* - Grant minimum required S3 permissions (principle of least privilege)
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* // Recommended: Using IAM roles (no credentials)
|
|
20
|
+
* const storage = new S3Storage();
|
|
21
|
+
* await storage.initialize({
|
|
22
|
+
* bucket: 'my-app-bucket',
|
|
23
|
+
* region: 'us-east-1'
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* // Alternative: Using explicit credentials from environment
|
|
27
|
+
* await storage.initialize({
|
|
28
|
+
* bucket: 'my-app-bucket',
|
|
29
|
+
* region: 'us-east-1',
|
|
30
|
+
* credentials: {
|
|
31
|
+
* accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
32
|
+
* secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
|
|
33
|
+
* }
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
8
37
|
* @implements {Storage}
|
|
9
38
|
*/
|
|
10
39
|
var S3Storage = class {
|
|
40
|
+
logger;
|
|
11
41
|
s3;
|
|
12
42
|
bucket;
|
|
13
43
|
/**
|
|
@@ -15,10 +45,26 @@ var S3Storage = class {
|
|
|
15
45
|
*
|
|
16
46
|
* @param {S3BaseOptions} options - Configuration options for the S3 client.
|
|
17
47
|
* @returns {Promise<void>} A promise that resolves when initialization is complete.
|
|
48
|
+
*
|
|
49
|
+
* @remarks
|
|
50
|
+
* The AWS SDK automatically handles credential resolution when credentials are not explicitly provided.
|
|
51
|
+
* In AWS environments (Lambda, EC2, ECS), IAM roles are used automatically, providing enhanced security
|
|
52
|
+
* without manual credential management.
|
|
53
|
+
*
|
|
54
|
+
* Priority of credential sources (when credentials field is omitted):
|
|
55
|
+
* 1. IAM roles (Lambda execution role, EC2 instance profile, ECS task role)
|
|
56
|
+
* 2. Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
|
|
57
|
+
* 3. Shared credentials file (~/.aws/credentials)
|
|
58
|
+
* 4. ECS container credentials
|
|
59
|
+
* 5. EC2 instance metadata service
|
|
60
|
+
*
|
|
61
|
+
* When credentials are explicitly provided in options, they take precedence over all other sources.
|
|
18
62
|
*/
|
|
19
63
|
async initialize(options) {
|
|
20
|
-
|
|
21
|
-
this.
|
|
64
|
+
const { bucket, logger, ...s3Options } = options;
|
|
65
|
+
this.s3 = new S3Client(s3Options);
|
|
66
|
+
this.bucket = bucket;
|
|
67
|
+
this.logger = logger;
|
|
22
68
|
}
|
|
23
69
|
/**
|
|
24
70
|
* Retrieves and parses a value from S3 storage by its key.
|
|
@@ -26,7 +72,7 @@ var S3Storage = class {
|
|
|
26
72
|
* @template T
|
|
27
73
|
* @param {string} key - The key whose value should be retrieved.
|
|
28
74
|
* @returns {Promise<T | undefined>} The stored value parsed as type T, or undefined if the key does not exist.
|
|
29
|
-
* @throws
|
|
75
|
+
* @throws {StorageError} Wraps any S3 error except for missing key (`NoSuchKey`) with sanitized error information.
|
|
30
76
|
*/
|
|
31
77
|
async getItem(key) {
|
|
32
78
|
try {
|
|
@@ -39,10 +85,32 @@ var S3Storage = class {
|
|
|
39
85
|
return JSON.parse(body);
|
|
40
86
|
} catch (error) {
|
|
41
87
|
if (error.name === "NoSuchKey") return;
|
|
42
|
-
|
|
88
|
+
this.logger?.error("S3Storage::getItem failed", {
|
|
89
|
+
operation: "getItem",
|
|
90
|
+
errorName: error.name,
|
|
91
|
+
bucket: this.bucket,
|
|
92
|
+
statusCode: error.$metadata?.httpStatusCode
|
|
93
|
+
});
|
|
94
|
+
throw new StorageError(`Failed to retrieve item from S3: ${error.name}`, "getItem", error, {
|
|
95
|
+
bucket: this.bucket,
|
|
96
|
+
errorName: error.name,
|
|
97
|
+
statusCode: error.$metadata?.httpStatusCode
|
|
98
|
+
});
|
|
43
99
|
}
|
|
44
100
|
}
|
|
45
101
|
/**
|
|
102
|
+
* Retrieves and parses multiple values from S3 storage by their keys.
|
|
103
|
+
*
|
|
104
|
+
* @template T
|
|
105
|
+
* @param {string[]} keys - The keys whose values should be retrieved.
|
|
106
|
+
* @returns {Promise<T[]>} An array of stored values parsed as type T, with undefined for missing keys.
|
|
107
|
+
* @throws {StorageError} Wraps any S3 error except for missing keys (`NoSuchKey`) with sanitized error information.
|
|
108
|
+
*/
|
|
109
|
+
async getItems(keys) {
|
|
110
|
+
if (keys.length === 0) return [];
|
|
111
|
+
return await Promise.all(keys.map((key) => this.getItem(key)));
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
46
114
|
* Stores a value in S3 storage under the specified key.
|
|
47
115
|
*
|
|
48
116
|
* @template T
|
|
@@ -77,7 +145,7 @@ var S3Storage = class {
|
|
|
77
145
|
*
|
|
78
146
|
* @param {string} key - The key to check.
|
|
79
147
|
* @returns {Promise<boolean>} True if the key exists, false otherwise.
|
|
80
|
-
* @throws
|
|
148
|
+
* @throws {StorageError} Wraps any S3 error except for missing key (`NoSuchKey`) with sanitized error information.
|
|
81
149
|
*/
|
|
82
150
|
async hasItem(key) {
|
|
83
151
|
try {
|
|
@@ -88,7 +156,17 @@ var S3Storage = class {
|
|
|
88
156
|
return true;
|
|
89
157
|
} catch (error) {
|
|
90
158
|
if (error.name === "NoSuchKey") return false;
|
|
91
|
-
|
|
159
|
+
this.logger?.error("S3Storage::hasItem failed", {
|
|
160
|
+
operation: "hasItem",
|
|
161
|
+
errorName: error.name,
|
|
162
|
+
bucket: this.bucket,
|
|
163
|
+
statusCode: error.$metadata?.httpStatusCode
|
|
164
|
+
});
|
|
165
|
+
throw new StorageError(`Failed to check item existence in S3: ${error.name}`, "hasItem", error, {
|
|
166
|
+
bucket: this.bucket,
|
|
167
|
+
errorName: error.name,
|
|
168
|
+
statusCode: error.$metadata?.httpStatusCode
|
|
169
|
+
});
|
|
92
170
|
}
|
|
93
171
|
}
|
|
94
172
|
/**
|
|
@@ -10,15 +10,15 @@
|
|
|
10
10
|
* It supports basic operations like get, set, delete, and querying storage state.
|
|
11
11
|
* Concrete implementations must provide the actual storage mechanism.
|
|
12
12
|
*/
|
|
13
|
-
declare abstract class Storage<
|
|
13
|
+
declare abstract class Storage<InitOptions = undefined> {
|
|
14
14
|
/**
|
|
15
15
|
* Initializes the storage implementation.
|
|
16
16
|
* Must be called before using any other storage operations.
|
|
17
17
|
*
|
|
18
|
-
* @param {
|
|
18
|
+
* @param {InitOptions} options - Initialization parameters. May be omitted if not required by the storage implementation.
|
|
19
19
|
* @returns {Promise<void>} A promise that resolves when initialization is complete.
|
|
20
20
|
*/
|
|
21
|
-
abstract initialize(options:
|
|
21
|
+
abstract initialize(options: InitOptions): void | Promise<void>;
|
|
22
22
|
/**
|
|
23
23
|
* Retrieves a value from storage by its key
|
|
24
24
|
* @template T - The type of the stored value
|
|
@@ -26,6 +26,13 @@ declare abstract class Storage<T = undefined> {
|
|
|
26
26
|
* @returns {Promise<T>} A promise that resolves with the stored value
|
|
27
27
|
*/
|
|
28
28
|
abstract getItem<T = unknown>(key: string): T | Promise<T>;
|
|
29
|
+
/**
|
|
30
|
+
* Retrieves multiple items from storage by their keys
|
|
31
|
+
* @template T - The type of the stored value
|
|
32
|
+
* @param {string[]} keys - The keys to retrieve the values for
|
|
33
|
+
* @returns {Promise<T[]>} A promise that resolves with the stored values or empty array if not found
|
|
34
|
+
*/
|
|
35
|
+
abstract getItems<T = unknown>(keys: string[]): T[] | Promise<T[]>;
|
|
29
36
|
/**
|
|
30
37
|
* Stores a value in storage with the specified key
|
|
31
38
|
* @template T - The type of the value to store
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//#region src/Errors/StorageError.ts
|
|
2
|
+
/**
|
|
3
|
+
* Custom error class for storage-related errors.
|
|
4
|
+
* Wraps underlying storage driver errors with standardized error messages.
|
|
5
|
+
*/
|
|
6
|
+
var StorageError = class StorageError extends Error {
|
|
7
|
+
/**
|
|
8
|
+
* The original error that caused this storage error
|
|
9
|
+
*/
|
|
10
|
+
cause;
|
|
11
|
+
/**
|
|
12
|
+
* The storage operation that failed
|
|
13
|
+
*/
|
|
14
|
+
operation;
|
|
15
|
+
/**
|
|
16
|
+
* Additional metadata about the error (non-sensitive)
|
|
17
|
+
*/
|
|
18
|
+
metadata;
|
|
19
|
+
constructor(message, operation, cause, metadata) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = "StorageError";
|
|
22
|
+
this.operation = operation;
|
|
23
|
+
this.cause = cause;
|
|
24
|
+
this.metadata = metadata;
|
|
25
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, StorageError);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
export { StorageError as t };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as Storage } from "./Storage-
|
|
1
|
+
import { t as Storage } from "./Storage-LzARx0pF.mjs";
|
|
2
2
|
import { IOC } from "@vercube/di";
|
|
3
3
|
|
|
4
4
|
//#region src/Types/StorageTypes.d.ts
|
|
@@ -21,6 +21,9 @@ declare namespace StorageTypes {
|
|
|
21
21
|
interface GetItem extends BaseOptions {
|
|
22
22
|
key: string;
|
|
23
23
|
}
|
|
24
|
+
interface GetItems extends BaseOptions {
|
|
25
|
+
keys: string[];
|
|
26
|
+
}
|
|
24
27
|
interface SetItem<T = unknown, U = unknown> extends BaseOptions {
|
|
25
28
|
key: string;
|
|
26
29
|
value: T;
|
|
@@ -80,6 +83,18 @@ declare class StorageManager {
|
|
|
80
83
|
storage,
|
|
81
84
|
key
|
|
82
85
|
}: StorageTypes.GetItem): Promise<T | null>;
|
|
86
|
+
/**
|
|
87
|
+
* Retrieves multiple items from the specified storage
|
|
88
|
+
* @template T - Type of the stored value
|
|
89
|
+
* @param {StorageTypes.GetItems} params - Parameters for retrieving multiple items
|
|
90
|
+
* @param {string} [params.storage] - Name of the storage to retrieve from, defaults to 'default'
|
|
91
|
+
* @param {string[]} params.keys - Keys of the items to retrieve
|
|
92
|
+
* @returns {Promise<T[]>} A promise that resolves with the stored values or empty array if not found
|
|
93
|
+
*/
|
|
94
|
+
getItems<T = unknown>({
|
|
95
|
+
storage,
|
|
96
|
+
keys
|
|
97
|
+
}: StorageTypes.GetItems): Promise<T[]>;
|
|
83
98
|
/**
|
|
84
99
|
* Stores an item in the specified storage
|
|
85
100
|
* @template T - Type of the value to store
|
|
@@ -152,4 +167,25 @@ declare class StorageManager {
|
|
|
152
167
|
private init;
|
|
153
168
|
}
|
|
154
169
|
//#endregion
|
|
155
|
-
|
|
170
|
+
//#region src/Errors/StorageError.d.ts
|
|
171
|
+
/**
|
|
172
|
+
* Custom error class for storage-related errors.
|
|
173
|
+
* Wraps underlying storage driver errors with standardized error messages.
|
|
174
|
+
*/
|
|
175
|
+
declare class StorageError extends Error {
|
|
176
|
+
/**
|
|
177
|
+
* The original error that caused this storage error
|
|
178
|
+
*/
|
|
179
|
+
readonly cause?: Error;
|
|
180
|
+
/**
|
|
181
|
+
* The storage operation that failed
|
|
182
|
+
*/
|
|
183
|
+
readonly operation: string;
|
|
184
|
+
/**
|
|
185
|
+
* Additional metadata about the error (non-sensitive)
|
|
186
|
+
*/
|
|
187
|
+
readonly metadata?: Record<string, unknown>;
|
|
188
|
+
constructor(message: string, operation: string, cause?: Error, metadata?: Record<string, unknown>);
|
|
189
|
+
}
|
|
190
|
+
//#endregion
|
|
191
|
+
export { Storage, StorageError, StorageManager, StorageTypes };
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { t as Storage } from "./Storage-Yj1niSzz.mjs";
|
|
2
|
+
import { t as StorageError } from "./StorageError-Dl7jGeVM.mjs";
|
|
2
3
|
import { Container, Init, Inject, InjectOptional } from "@vercube/di";
|
|
3
4
|
import { Logger } from "@vercube/logger";
|
|
4
5
|
|
|
5
|
-
//#region \0@oxc-project+runtime@0.
|
|
6
|
+
//#region \0@oxc-project+runtime@0.110.0/helpers/decorate.js
|
|
6
7
|
function __decorate(decorators, target, key, desc) {
|
|
7
8
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
8
9
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -57,6 +58,17 @@ var StorageManager = class {
|
|
|
57
58
|
return this.getStorage(storage)?.getItem(key) ?? null;
|
|
58
59
|
}
|
|
59
60
|
/**
|
|
61
|
+
* Retrieves multiple items from the specified storage
|
|
62
|
+
* @template T - Type of the stored value
|
|
63
|
+
* @param {StorageTypes.GetItems} params - Parameters for retrieving multiple items
|
|
64
|
+
* @param {string} [params.storage] - Name of the storage to retrieve from, defaults to 'default'
|
|
65
|
+
* @param {string[]} params.keys - Keys of the items to retrieve
|
|
66
|
+
* @returns {Promise<T[]>} A promise that resolves with the stored values or empty array if not found
|
|
67
|
+
*/
|
|
68
|
+
async getItems({ storage, keys }) {
|
|
69
|
+
return this.getStorage(storage)?.getItems(keys) ?? [];
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
60
72
|
* Stores an item in the specified storage
|
|
61
73
|
* @template T - Type of the value to store
|
|
62
74
|
* @param {StorageTypes.SetItem<T>} params - Parameters for storing an item
|
|
@@ -133,4 +145,4 @@ __decorate([InjectOptional(Logger)], StorageManager.prototype, "gLogger", void 0
|
|
|
133
145
|
__decorate([Init()], StorageManager.prototype, "init", null);
|
|
134
146
|
|
|
135
147
|
//#endregion
|
|
136
|
-
export { Storage, StorageManager };
|
|
148
|
+
export { Storage, StorageError, StorageManager };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vercube/storage",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.35",
|
|
4
4
|
"description": "Storage module for Vercube framework",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -24,16 +24,16 @@
|
|
|
24
24
|
"README.md"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@vercube/
|
|
28
|
-
"@vercube/
|
|
27
|
+
"@vercube/logger": "0.0.35",
|
|
28
|
+
"@vercube/di": "0.0.35"
|
|
29
29
|
},
|
|
30
30
|
"optionalDependencies": {
|
|
31
|
-
"@aws-sdk/client-s3": "3.
|
|
31
|
+
"@aws-sdk/client-s3": "3.975.0"
|
|
32
32
|
},
|
|
33
33
|
"publishConfig": {
|
|
34
34
|
"access": "public"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
|
-
"build": "tsdown --config tsdown.config.ts"
|
|
37
|
+
"build": "tsdown --config tsdown.config.ts --config-loader=unrun"
|
|
38
38
|
}
|
|
39
39
|
}
|