@vn.chemgio/nestjs-utilities 2.0.0

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,78 @@
1
+ # @vn.chemgio/nestjs-utilities
2
+
3
+ A collection of modular NestJS utility packages. Each module can be imported independently via sub-path exports so you only install what you need.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @vn.chemgio/nestjs-utilities
9
+ ```
10
+
11
+ ## Available modules
12
+
13
+ | Import path | Description | README |
14
+ |---|---|---|
15
+ | `@vn.chemgio/nestjs-utilities` | Version constant (`VERSION`) | — |
16
+ | `@vn.chemgio/nestjs-utilities/typeorm` | TypeORM configuration helpers (RDS PostgreSQL, MySQL) | [README](src/typeorm/README.md) |
17
+ | `@vn.chemgio/nestjs-utilities/cache` | Global cache module with Valkey / Redis backend | [README](src/cache/README.md) |
18
+ | `@vn.chemgio/nestjs-utilities/queue` | BullMQ config factory for Valkey / Redis backend | [README](src/queue/README.md) |
19
+
20
+ Click each README link above for detailed usage, environment variables, and options specific to that module.
21
+
22
+ ## Quick examples
23
+
24
+ ### TypeORM
25
+
26
+ ```typescript
27
+ import { TypeOrmModule } from '@nestjs/typeorm'
28
+ import { ConfigService } from '@nestjs/config'
29
+ import { configTypeOrmRDSPostgres } from '@vn.chemgio/nestjs-utilities/typeorm'
30
+
31
+ TypeOrmModule.forRootAsync({
32
+ inject: [ConfigService],
33
+ useFactory: (config: ConfigService) =>
34
+ configTypeOrmRDSPostgres(config, { poolSize: 20 }),
35
+ })
36
+ ```
37
+
38
+ ### Cache
39
+
40
+ ```typescript
41
+ import { CacheModule } from '@vn.chemgio/nestjs-utilities/cache'
42
+
43
+ @Module({
44
+ imports: [CacheModule.forRoot({ store: 'valkey' })],
45
+ })
46
+ export class AppModule {}
47
+ ```
48
+
49
+ ### Queue
50
+
51
+ ```typescript
52
+ import { BullModule } from '@nestjs/bullmq'
53
+ import { configBullMQ } from '@vn.chemgio/nestjs-utilities/queue'
54
+
55
+ BullModule.forRootAsync({
56
+ inject: [ConfigService],
57
+ useFactory: configBullMQ,
58
+ })
59
+ ```
60
+
61
+ ## Development
62
+
63
+ ```bash
64
+ npm install
65
+ npm run build
66
+ npm test
67
+ ```
68
+
69
+ ## Publishing
70
+
71
+ ```bash
72
+ npm login
73
+ npm publish --access public
74
+ ```
75
+
76
+ ## License
77
+
78
+ MIT
@@ -0,0 +1,91 @@
1
+ # @vn.chemgio/nestjs-utilities/cache
2
+
3
+ A global NestJS cache module using [Keyv](https://keyv.org/) + [Cacheable](https://github.com/jaredwray/cacheable) with Valkey or Redis backend.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @vn.chemgio/nestjs-utilities keyv cacheable
9
+ ```
10
+
11
+ Depending on your store:
12
+
13
+ - **Valkey**: `npm install @keyv/valkey`
14
+ - **Redis**: `npm install @keyv/redis`
15
+
16
+ ## Quick start
17
+
18
+ ### Valkey (reads `VALKEY_URL` from env)
19
+
20
+ ```typescript
21
+ // app.module.ts
22
+ import { CacheModule } from '@vn.chemgio/nestjs-utilities/cache'
23
+
24
+ @Module({
25
+ imports: [
26
+ CacheModule.forRoot({ store: 'valkey' }),
27
+ // Requires VALKEY_URL in your .env:
28
+ // VALKEY_URL=valkey://localhost:6379
29
+ ],
30
+ })
31
+ export class AppModule {}
32
+ ```
33
+
34
+ ### Redis (reads `REDIS_URL` from env)
35
+
36
+ ```typescript
37
+ CacheModule.forRoot({ store: 'redis' })
38
+ // Requires REDIS_URL in .env:
39
+ // REDIS_URL=redis://localhost:6379
40
+ ```
41
+
42
+ ### Direct URL (no env vars needed)
43
+
44
+ ```typescript
45
+ CacheModule.forRoot({
46
+ store: 'valkey',
47
+ url: 'valkey://user:pass@host:6379',
48
+ })
49
+ ```
50
+
51
+ ## Usage in a service
52
+
53
+ ```typescript
54
+ import { Inject } from '@nestjs/common'
55
+ import { CACHE_MANAGER } from '@vn.chemgio/nestjs-utilities/cache'
56
+ import { Cacheable } from 'cacheable'
57
+
58
+ @Injectable()
59
+ export class MyService {
60
+ constructor(
61
+ @Inject(CACHE_MANAGER) private readonly cache: Cacheable,
62
+ ) {}
63
+
64
+ async getData(key: string) {
65
+ return this.cache.get(key)
66
+ }
67
+
68
+ async setData(key: string, value: unknown, ttl?: number) {
69
+ await this.cache.set(key, value, ttl)
70
+ }
71
+ }
72
+ ```
73
+
74
+ ## CacheModuleOptions
75
+
76
+ | Option | Default | Description |
77
+ |---|---|---|
78
+ | `store` | `'valkey'` | Backend store: `'valkey'` or `'redis'` |
79
+ | `namespace` | `'{default}'` | Cache namespace prefix |
80
+ | `url` | — | Connection URL. If omitted, reads `VALKEY_URL` or `REDIS_URL` from env |
81
+
82
+ ## How it works
83
+
84
+ ```
85
+ Cacheable → Keyv → KeyvValkey / KeyvRedis → Valkey / Redis
86
+ ```
87
+
88
+ - `KeyvValkey` / `KeyvRedis` connects to the backend
89
+ - `Keyv` wraps it with a uniform API
90
+ - `Cacheable` adds TTL, namespace, and caching logic
91
+ - The module is `@Global()`, so `CACHE_MANAGER` is available everywhere
@@ -0,0 +1,10 @@
1
+ import { DynamicModule } from '@nestjs/common';
2
+ export declare const CACHE_MANAGER = "CACHE_MANAGER";
3
+ export interface CacheModuleOptions {
4
+ namespace?: string;
5
+ store?: 'valkey' | 'redis';
6
+ url?: string;
7
+ }
8
+ export declare class CacheModule {
9
+ static forRoot(options?: CacheModuleOptions): DynamicModule;
10
+ }
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ 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;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __importDefault = (this && this.__importDefault) || function (mod) {
9
+ return (mod && mod.__esModule) ? mod : { "default": mod };
10
+ };
11
+ var CacheModule_1;
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.CacheModule = exports.CACHE_MANAGER = void 0;
14
+ const common_1 = require("@nestjs/common");
15
+ const config_1 = require("@nestjs/config");
16
+ const keyv_1 = __importDefault(require("keyv"));
17
+ const cacheable_1 = require("cacheable");
18
+ exports.CACHE_MANAGER = 'CACHE_MANAGER';
19
+ async function loadAdapter(store, url) {
20
+ if (store === 'valkey') {
21
+ const { default: KeyvValkey } = await import('@keyv/valkey');
22
+ return new KeyvValkey(url);
23
+ }
24
+ const { default: KeyvRedis } = await import('@keyv/redis');
25
+ return new KeyvRedis(url);
26
+ }
27
+ function buildCacheable(adapter, namespace) {
28
+ const keyv = new keyv_1.default({ store: adapter });
29
+ return new cacheable_1.Cacheable({ primary: keyv, namespace });
30
+ }
31
+ let CacheModule = CacheModule_1 = class CacheModule {
32
+ static forRoot(options = {}) {
33
+ const { namespace = '{default}', store = 'valkey' } = options;
34
+ if (options.url) {
35
+ return {
36
+ module: CacheModule_1,
37
+ providers: [
38
+ {
39
+ provide: exports.CACHE_MANAGER,
40
+ useFactory: async () => {
41
+ const adapter = await loadAdapter(store, options.url);
42
+ return buildCacheable(adapter, namespace);
43
+ },
44
+ },
45
+ ],
46
+ exports: [exports.CACHE_MANAGER],
47
+ };
48
+ }
49
+ return {
50
+ module: CacheModule_1,
51
+ providers: [
52
+ {
53
+ provide: exports.CACHE_MANAGER,
54
+ inject: [config_1.ConfigService],
55
+ useFactory: async (configService) => {
56
+ const envKey = store === 'valkey' ? 'VALKEY_URL' : 'REDIS_URL';
57
+ const connectionUrl = configService.get(envKey);
58
+ if (!connectionUrl) {
59
+ throw new Error(`Missing ${envKey} environment variable. Set it or pass "url" to forRoot().`);
60
+ }
61
+ const adapter = await loadAdapter(store, connectionUrl);
62
+ return buildCacheable(adapter, namespace);
63
+ },
64
+ },
65
+ ],
66
+ exports: [exports.CACHE_MANAGER],
67
+ };
68
+ }
69
+ };
70
+ exports.CacheModule = CacheModule;
71
+ exports.CacheModule = CacheModule = CacheModule_1 = __decorate([
72
+ (0, common_1.Global)(),
73
+ (0, common_1.Module)({})
74
+ ], CacheModule);
75
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/cache/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,2CAA+D;AAC/D,2CAA+C;AAC/C,gDAAwB;AACxB,yCAAsC;AAEzB,QAAA,aAAa,GAAG,eAAe,CAAC;AAQ7C,KAAK,UAAU,WAAW,CAAC,KAAyB,EAAE,GAAW;IAC/D,IAAI,KAAK,KAAK,QAAQ,EAAE,CAAC;QACvB,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,GAAG,MAAM,MAAM,CAAC,cAAc,CAAC,CAAC;QAC7D,OAAO,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;IAC7B,CAAC;IACD,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,GAAG,MAAM,MAAM,CAAC,aAAa,CAAC,CAAC;IAC3D,OAAO,IAAI,SAAS,CAAC,GAAG,CAAC,CAAC;AAC5B,CAAC;AAED,SAAS,cAAc,CAAC,OAAgB,EAAE,SAAiB;IACzD,MAAM,IAAI,GAAG,IAAI,cAAI,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;IAC1C,OAAO,IAAI,qBAAS,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC;AACrD,CAAC;AAIM,IAAM,WAAW,mBAAjB,MAAM,WAAW;IACtB,MAAM,CAAC,OAAO,CAAC,UAA8B,EAAE;QAC7C,MAAM,EAAE,SAAS,GAAG,WAAW,EAAE,KAAK,GAAG,QAAQ,EAAE,GAAG,OAAO,CAAC;QAE9D,IA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@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VERSION = void 0;
4
+ exports.VERSION = '2.0.0';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAa,QAAA,OAAO,GAAG,OAAO,CAAC"}
@@ -0,0 +1,82 @@
1
+ # @vn.chemgio/nestjs-utilities/queue
2
+
3
+ RDS-style config factory for [BullMQ](https://bullmq.io) (via `@nestjs/bullmq`).
4
+
5
+ Works with either Valkey (`VALKEY_URL`) or Redis (`REDIS_URL`) environment variables.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @vn.chemgio/nestjs-utilities @nestjs/bullmq bullmq
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```typescript
16
+ import { BullModule } from '@nestjs/bullmq';
17
+ import { configBullMQ } from '@vn.chemgio/nestjs-utilities/queue';
18
+
19
+ @Module({
20
+ imports: [
21
+ BullModule.forRootAsync({
22
+ useFactory: configBullMQ,
23
+ inject: [ConfigService],
24
+ }),
25
+ BullModule.registerQueue({ name: 'email' }),
26
+ ],
27
+ })
28
+ export class AppModule {}
29
+ ```
30
+
31
+ ### Valkey (reads `VALKEY_URL` from env)
32
+
33
+ ```typescript
34
+ BullModule.forRootAsync({
35
+ // Requires VALKEY_URL in .env:
36
+ // VALKEY_URL=valkey://localhost:6379
37
+ useFactory: configBullMQ,
38
+ inject: [ConfigService],
39
+ })
40
+ ```
41
+
42
+ ### Redis (reads `REDIS_URL` from env)
43
+
44
+ ```typescript
45
+ BullModule.forRootAsync({
46
+ useFactory: (config: ConfigService) =>
47
+ configBullMQ(config, { store: 'redis' }),
48
+ inject: [ConfigService],
49
+ })
50
+ ```
51
+
52
+ ### With custom prefix and default job options
53
+
54
+ ```typescript
55
+ BullModule.forRootAsync({
56
+ useFactory: (config: ConfigService) =>
57
+ configBullMQ(config, {
58
+ prefix: '{myapp}',
59
+ defaultJobOptions: { attempts: 3, removeOnComplete: 100 },
60
+ }),
61
+ inject: [ConfigService],
62
+ })
63
+ ```
64
+
65
+ ### Direct URL (no env vars)
66
+
67
+ ```typescript
68
+ BullModule.forRootAsync({
69
+ useFactory: () => ({
70
+ connection: { url: 'redis://user:pass@host:6379' },
71
+ prefix: '{default}',
72
+ }),
73
+ })
74
+ ```
75
+
76
+ ## QueueModuleOptions
77
+
78
+ | Option | Default | Description |
79
+ |---|---|---|
80
+ | `store` | `'valkey'` | Backend store: `'valkey'` or `'redis'` |
81
+ | `prefix` | `'{default}'` | BullMQ key prefix |
82
+ | `defaultJobOptions` | — | Default job options for all queues (e.g. `{ attempts: 3 }`) |
@@ -0,0 +1,14 @@
1
+ import type { ConfigService } from '@nestjs/config';
2
+ export interface QueueModuleOptions {
3
+ store?: 'valkey' | 'redis';
4
+ prefix?: string;
5
+ defaultJobOptions?: Record<string, unknown>;
6
+ }
7
+ export interface BullMQModuleOptions {
8
+ connection: {
9
+ url: string;
10
+ };
11
+ prefix: string;
12
+ defaultJobOptions?: Record<string, unknown>;
13
+ }
14
+ export declare function configBullMQ(configService: ConfigService, options?: QueueModuleOptions): BullMQModuleOptions;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.configBullMQ = configBullMQ;
4
+ function configBullMQ(configService, options) {
5
+ const { store = 'valkey', prefix, defaultJobOptions } = options ?? {};
6
+ const envKey = store === 'valkey' ? 'VALKEY_URL' : 'REDIS_URL';
7
+ const url = configService.get(envKey);
8
+ if (!url) {
9
+ throw new Error(`Missing ${envKey} environment variable.`);
10
+ }
11
+ return {
12
+ connection: { url },
13
+ prefix: prefix ?? '{default}',
14
+ defaultJobOptions,
15
+ };
16
+ }
17
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/queue/index.ts"],"names":[],"mappings":";;AAcA,oCAeC;AAfD,SAAgB,YAAY,CAC1B,aAA4B,EAC5B,OAA4B;IAE5B,MAAM,EAAE,KAAK,GAAG,QAAQ,EAAE,MAAM,EAAE,iBAAiB,EAAE,GAAG,OAAO,IAAI,EAAE,CAAC;IACtE,MAAM,MAAM,GAAG,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,WAAW,CAAC;IAC/D,MAAM,GAAG,GAAG,aAAa,CAAC,GAAG,CAAS,MAAM,CAAC,CAAC;IAC9C,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,MAAM,IAAI,KAAK,CAAC,WAAW,MAAM,wBAAwB,CAAC,CAAC;IAC7D,CAAC;IACD,OAAO;QACL,UAAU,EAAE,EAAE,GAAG,EAAE;QACnB,MAAM,EAAE,MAAM,IAAI,WAAW;QAC7B,iBAAiB;KAClB,CAAC;AACJ,CAAC"}