@dismissible/nestjs-storage 0.0.1

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,148 @@
1
+ # @dismissible/nestjs-storage
2
+
3
+ Storage interface and in-memory adapter for the Dismissible system.
4
+
5
+ > **Part of the Dismissible API** - This library is part of the [Dismissible API](https://dismissible.io) ecosystem. Visit [dismissible.io](https://dismissible.io) for more information and documentation.
6
+
7
+ ## Overview
8
+
9
+ This library provides:
10
+
11
+ - `IDismissibleStorage` - Interface that all storage adapters must implement
12
+ - `MemoryStorageAdapter` - In-memory storage implementation (useful for development and testing)
13
+ - Storage abstraction layer for the Dismissible system
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @dismissible/nestjs-storage
19
+ ```
20
+
21
+ ## Getting Started
22
+
23
+ ### Using In-Memory Storage
24
+
25
+ The in-memory storage adapter is useful for development, testing, or when you don't need persistence:
26
+
27
+ ```typescript
28
+ import { Module } from '@nestjs/common';
29
+ import { StorageModule, MemoryStorageAdapter } from '@dismissible/nestjs-storage';
30
+ import { LoggerModule } from '@dismissible/nestjs-logger';
31
+ import { DismissibleItemModule } from '@dismissible/nestjs-dismissible-item';
32
+
33
+ @Module({
34
+ imports: [
35
+ LoggerModule.forRoot({}),
36
+ DismissibleItemModule,
37
+ StorageModule.forRoot({
38
+ adapter: MemoryStorageAdapter,
39
+ }),
40
+ ],
41
+ })
42
+ export class AppModule {}
43
+ ```
44
+
45
+ ### Implementing a Custom Storage Adapter
46
+
47
+ You can implement your own storage adapter by implementing the `IDismissibleStorage` interface:
48
+
49
+ ```typescript
50
+ import { Injectable } from '@nestjs/common';
51
+ import { IDismissibleStorage, DISMISSIBLE_STORAGE_ADAPTER } from '@dismissible/nestjs-storage';
52
+ import { DismissibleItemDto } from '@dismissible/nestjs-dismissible-item';
53
+ import { DISMISSIBLE_LOGGER, IDismissibleLogger } from '@dismissible/nestjs-logger';
54
+
55
+ @Injectable()
56
+ export class MyCustomStorageAdapter implements IDismissibleStorage {
57
+ constructor(@Inject(DISMISSIBLE_LOGGER) private readonly logger: IDismissibleLogger) {}
58
+
59
+ async get(userId: string, itemId: string): Promise<DismissibleItemDto | null> {
60
+ // Your implementation
61
+ this.logger.debug('Getting item', { userId, itemId });
62
+ // ...
63
+ }
64
+
65
+ async create(item: DismissibleItemDto): Promise<DismissibleItemDto> {
66
+ // Your implementation
67
+ this.logger.debug('Creating item', { itemId: item.id });
68
+ // ...
69
+ }
70
+
71
+ async update(item: DismissibleItemDto): Promise<DismissibleItemDto> {
72
+ // Your implementation
73
+ this.logger.debug('Updating item', { itemId: item.id });
74
+ // ...
75
+ }
76
+ }
77
+
78
+ // Register your adapter
79
+ @Module({
80
+ providers: [
81
+ MyCustomStorageAdapter,
82
+ {
83
+ provide: DISMISSIBLE_STORAGE_ADAPTER,
84
+ useExisting: MyCustomStorageAdapter,
85
+ },
86
+ ],
87
+ exports: [DISMISSIBLE_STORAGE_ADAPTER],
88
+ })
89
+ export class MyStorageModule {}
90
+ ```
91
+
92
+ ## API Reference
93
+
94
+ ### IDismissibleStorage Interface
95
+
96
+ ```typescript
97
+ interface IDismissibleStorage {
98
+ get(userId: string, itemId: string): Promise<DismissibleItemDto | null>;
99
+ create(item: DismissibleItemDto): Promise<DismissibleItemDto>;
100
+ update(item: DismissibleItemDto): Promise<DismissibleItemDto>;
101
+ }
102
+ ```
103
+
104
+ ### MemoryStorageAdapter
105
+
106
+ An in-memory implementation of `IDismissibleStorage`. Data is stored in a `Map` and will be lost when the application restarts.
107
+
108
+ **Note:** This adapter is suitable for development and testing only. For production use, consider using `@dismissible/nestjs-postgres-storage` or implementing your own persistent storage adapter.
109
+
110
+ ### StorageModule
111
+
112
+ #### `StorageModule.forRoot(options)`
113
+
114
+ Configures the storage module with the provided adapter.
115
+
116
+ **Options:**
117
+
118
+ - `adapter: Type<IDismissibleStorage>` - The storage adapter class to use
119
+
120
+ **Returns:** `DynamicModule`
121
+
122
+ ## Injection Token
123
+
124
+ The storage adapter is provided via the `DISMISSIBLE_STORAGE_ADAPTER` injection token:
125
+
126
+ ```typescript
127
+ import { Inject } from '@nestjs/common';
128
+ import { DISMISSIBLE_STORAGE_ADAPTER, IDismissibleStorage } from '@dismissible/nestjs-storage';
129
+
130
+ @Injectable()
131
+ export class MyService {
132
+ constructor(
133
+ @Inject(DISMISSIBLE_STORAGE_ADAPTER)
134
+ private readonly storage: IDismissibleStorage,
135
+ ) {}
136
+ }
137
+ ```
138
+
139
+ ## Related Packages
140
+
141
+ - `@dismissible/nestjs-dismissible` - Main dismissible service (uses storage adapters)
142
+ - `@dismissible/nestjs-postgres-storage` - PostgreSQL storage adapter implementation
143
+ - `@dismissible/nestjs-dismissible-item` - Data models used by storage adapters
144
+ - `@dismissible/nestjs-logger` - Logging used by storage adapters
145
+
146
+ ## License
147
+
148
+ MIT
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@dismissible/nestjs-storage",
3
+ "version": "0.0.1",
4
+ "description": "In-memory storage adapter for Dismissible",
5
+ "main": "./src/index.js",
6
+ "types": "./src/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./src/index.mjs",
10
+ "require": "./src/index.js",
11
+ "types": "./src/index.d.ts"
12
+ }
13
+ },
14
+ "files": [
15
+ "src",
16
+ "README.md"
17
+ ],
18
+ "dependencies": {
19
+ "@dismissible/nestjs-logger": "^0.0.1",
20
+ "@dismissible/nestjs-dismissible-item": "^0.0.1"
21
+ },
22
+ "peerDependencies": {
23
+ "@nestjs/common": "^11.0.0"
24
+ },
25
+ "peerDependenciesMeta": {
26
+ "@nestjs/common": {
27
+ "optional": false
28
+ }
29
+ },
30
+ "keywords": [
31
+ "nestjs",
32
+ "dismissible",
33
+ "storage",
34
+ "storage",
35
+ "adapter"
36
+ ],
37
+ "author": "",
38
+ "license": "MIT",
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "https://github.com/DismissibleIo/dismissible-api"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "type": "commonjs"
47
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './memory-storage.adapter';
2
+ export * from './storage.interface';
3
+ export * from './storage.module';
package/src/index.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./memory-storage.adapter"), exports);
5
+ tslib_1.__exportStar(require("./storage.interface"), exports);
6
+ tslib_1.__exportStar(require("./storage.module"), exports);
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../libs/storage/src/index.ts"],"names":[],"mappings":";;;AAAA,mEAAyC;AACzC,8DAAoC;AACpC,2DAAiC"}
@@ -0,0 +1,29 @@
1
+ import { IDismissibleLogger } from '@dismissible/nestjs-logger';
2
+ import { IDismissibleStorage } from './storage.interface';
3
+ import { DismissibleItemDto } from '@dismissible/nestjs-dismissible-item';
4
+ /**
5
+ * In-memory storage provider for dismissible items.
6
+ * Suitable for development and testing; not for production use.
7
+ */
8
+ export declare class MemoryStorageAdapter implements IDismissibleStorage {
9
+ private readonly logger;
10
+ private readonly storage;
11
+ constructor(logger: IDismissibleLogger);
12
+ /**
13
+ * Create a storage key from userId and itemId.
14
+ */
15
+ private createKey;
16
+ get(userId: string, itemId: string): Promise<DismissibleItemDto | null>;
17
+ create(item: DismissibleItemDto): Promise<DismissibleItemDto>;
18
+ update(item: DismissibleItemDto): Promise<DismissibleItemDto>;
19
+ /**
20
+ * Clear all stored items.
21
+ * Useful for testing.
22
+ */
23
+ clear(): void;
24
+ /**
25
+ * Get the number of stored items.
26
+ * Useful for testing/debugging.
27
+ */
28
+ get size(): number;
29
+ }
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MemoryStorageAdapter = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const common_1 = require("@nestjs/common");
6
+ const nestjs_logger_1 = require("@dismissible/nestjs-logger");
7
+ /**
8
+ * In-memory storage provider for dismissible items.
9
+ * Suitable for development and testing; not for production use.
10
+ */
11
+ let MemoryStorageAdapter = class MemoryStorageAdapter {
12
+ constructor(logger) {
13
+ this.logger = logger;
14
+ this.storage = new Map();
15
+ }
16
+ /**
17
+ * Create a storage key from userId and itemId.
18
+ */
19
+ createKey(userId, itemId) {
20
+ return `${userId}:${itemId}`;
21
+ }
22
+ async get(userId, itemId) {
23
+ const key = this.createKey(userId, itemId);
24
+ this.logger.debug(`Storage get`, { userId, itemId, key });
25
+ const item = this.storage.get(key);
26
+ if (!item) {
27
+ this.logger.debug(`Storage miss`, { userId, itemId });
28
+ return null;
29
+ }
30
+ this.logger.debug(`Storage hit`, { userId, itemId });
31
+ return item;
32
+ }
33
+ async create(item) {
34
+ const key = this.createKey(item.userId, item.id);
35
+ this.logger.debug(`Storage create`, { userId: item.userId, itemId: item.id, key });
36
+ this.storage.set(key, item);
37
+ return item;
38
+ }
39
+ async update(item) {
40
+ const key = this.createKey(item.userId, item.id);
41
+ this.logger.debug(`Storage update`, { userId: item.userId, itemId: item.id, key });
42
+ this.storage.set(key, item);
43
+ return item;
44
+ }
45
+ /**
46
+ * Clear all stored items.
47
+ * Useful for testing.
48
+ */
49
+ clear() {
50
+ this.logger.debug(`Storage clear`, { previousSize: this.storage.size });
51
+ this.storage.clear();
52
+ }
53
+ /**
54
+ * Get the number of stored items.
55
+ * Useful for testing/debugging.
56
+ */
57
+ get size() {
58
+ return this.storage.size;
59
+ }
60
+ };
61
+ exports.MemoryStorageAdapter = MemoryStorageAdapter;
62
+ exports.MemoryStorageAdapter = MemoryStorageAdapter = tslib_1.__decorate([
63
+ (0, common_1.Injectable)(),
64
+ tslib_1.__param(0, (0, common_1.Inject)(nestjs_logger_1.DISMISSIBLE_LOGGER)),
65
+ tslib_1.__metadata("design:paramtypes", [Object])
66
+ ], MemoryStorageAdapter);
67
+ //# sourceMappingURL=memory-storage.adapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory-storage.adapter.js","sourceRoot":"","sources":["../../../../libs/storage/src/memory-storage.adapter.ts"],"names":[],"mappings":";;;;AAAA,2CAAoD;AACpD,8DAAoF;AAIpF;;;GAGG;AAEI,IAAM,oBAAoB,GAA1B,MAAM,oBAAoB;IAG/B,YAAwC,MAA2C;QAA1B,WAAM,GAAN,MAAM,CAAoB;QAFlE,YAAO,GAAG,IAAI,GAAG,EAA8B,CAAC;IAEqB,CAAC;IAEvF;;OAEG;IACK,SAAS,CAAC,MAAc,EAAE,MAAc;QAC9C,OAAO,GAAG,MAAM,IAAI,MAAM,EAAE,CAAC;IAC/B,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,MAAc,EAAE,MAAc;QACtC,MAAM,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC3C,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,aAAa,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;QAE1D,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAEnC,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,cAAc,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;YACtD,OAAO,IAAI,CAAC;QACd,CAAC;QAED,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,aAAa,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;QACrD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,IAAwB;QACnC,MAAM,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;QACjD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,gBAAgB,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;QACnF,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;QAC5B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,IAAwB;QACnC,MAAM,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;QACjD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,gBAAgB,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;QACnF,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;QAC5B,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;OAGG;IACH,KAAK;QACH,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,eAAe,EAAE,EAAE,YAAY,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;QACxE,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;IAED;;;OAGG;IACH,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC;IAC3B,CAAC;CACF,CAAA;AAzDY,oDAAoB;+BAApB,oBAAoB;IADhC,IAAA,mBAAU,GAAE;IAIE,mBAAA,IAAA,eAAM,EAAC,kCAAkB,CAAC,CAAA;;GAH5B,oBAAoB,CAyDhC"}
@@ -0,0 +1,30 @@
1
+ import { DismissibleItemDto } from '@dismissible/nestjs-dismissible-item';
2
+ /**
3
+ * Injection token for the storage provider.
4
+ */
5
+ export declare const DISMISSIBLE_STORAGE_ADAPTER: unique symbol;
6
+ /**
7
+ * Interface for storage providers.
8
+ * Implementations handle the persistence of dismissible items.
9
+ */
10
+ export interface IDismissibleStorage {
11
+ /**
12
+ * Retrieve an item by user ID and item ID.
13
+ * @param userId The user identifier
14
+ * @param itemId The item identifier
15
+ * @returns The item or null if not found
16
+ */
17
+ get(userId: string, itemId: string): Promise<DismissibleItemDto | null>;
18
+ /**
19
+ * Create a new item.
20
+ * @param item The item to create (contains userId and id)
21
+ * @returns The created item
22
+ */
23
+ create(item: DismissibleItemDto): Promise<DismissibleItemDto>;
24
+ /**
25
+ * Update an existing item.
26
+ * @param item The item to update (contains userId and id)
27
+ * @returns The updated item
28
+ */
29
+ update(item: DismissibleItemDto): Promise<DismissibleItemDto>;
30
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DISMISSIBLE_STORAGE_ADAPTER = void 0;
4
+ /**
5
+ * Injection token for the storage provider.
6
+ */
7
+ exports.DISMISSIBLE_STORAGE_ADAPTER = Symbol('DISMISSIBLE_STORAGE_ADAPTER');
8
+ //# sourceMappingURL=storage.interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.interface.js","sourceRoot":"","sources":["../../../../libs/storage/src/storage.interface.ts"],"names":[],"mappings":";;;AAEA;;GAEG;AACU,QAAA,2BAA2B,GAAG,MAAM,CAAC,6BAA6B,CAAC,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { DynamicModule, Type } from '@nestjs/common';
2
+ export type IDismissibleStorageModuleOptions = {
3
+ storage?: Type<any> | DynamicModule;
4
+ };
5
+ export declare class StorageModule {
6
+ }
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StorageModule = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const common_1 = require("@nestjs/common");
6
+ const storage_interface_1 = require("./storage.interface");
7
+ const memory_storage_adapter_1 = require("./memory-storage.adapter");
8
+ let StorageModule = class StorageModule {
9
+ };
10
+ exports.StorageModule = StorageModule;
11
+ exports.StorageModule = StorageModule = tslib_1.__decorate([
12
+ (0, common_1.Module)({
13
+ providers: [
14
+ {
15
+ provide: storage_interface_1.DISMISSIBLE_STORAGE_ADAPTER,
16
+ useClass: memory_storage_adapter_1.MemoryStorageAdapter,
17
+ },
18
+ ],
19
+ exports: [storage_interface_1.DISMISSIBLE_STORAGE_ADAPTER],
20
+ })
21
+ ], StorageModule);
22
+ //# sourceMappingURL=storage.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.module.js","sourceRoot":"","sources":["../../../../libs/storage/src/storage.module.ts"],"names":[],"mappings":";;;;AAAA,2CAA6D;AAC7D,2DAAkE;AAClE,qEAAgE;AAezD,IAAM,aAAa,GAAnB,MAAM,aAAa;CAAG,CAAA;AAAhB,sCAAa;wBAAb,aAAa;IATzB,IAAA,eAAM,EAAC;QACN,SAAS,EAAE;YACT;gBACE,OAAO,EAAE,+CAA2B;gBACpC,QAAQ,EAAE,6CAAoB;aAC/B;SACF;QACD,OAAO,EAAE,CAAC,+CAA2B,CAAC;KACvC,CAAC;GACW,aAAa,CAAG"}