@sqb/nestjs 4.0.9 → 4.0.13
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/dist/index.d.ts +5 -0
- package/dist/index.js +10 -0
- package/dist/sqb-core.module.d.ts +14 -0
- package/dist/sqb-core.module.js +128 -0
- package/dist/sqb.constants.d.ts +2 -0
- package/dist/sqb.constants.js +5 -0
- package/dist/sqb.decorators.d.ts +1 -0
- package/dist/sqb.decorators.js +7 -0
- package/dist/sqb.interface.d.ts +50 -0
- package/dist/sqb.interface.js +2 -0
- package/dist/sqb.module.d.ts +6 -0
- package/dist/sqb.module.js +25 -0
- package/dist/sqb.utils.d.ts +11 -0
- package/dist/sqb.utils.js +40 -0
- package/package.json +2 -2
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqbClient = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
tslib_1.__exportStar(require("./sqb.decorators"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./sqb.interface"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./sqb.module"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./sqb.utils"), exports);
|
|
9
|
+
var connect_1 = require("@sqb/connect");
|
|
10
|
+
Object.defineProperty(exports, "SqbClient", { enumerable: true, get: function () { return connect_1.SqbClient; } });
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { DynamicModule, OnApplicationShutdown } from '@nestjs/common';
|
|
2
|
+
import { ModuleRef } from '@nestjs/core';
|
|
3
|
+
import { SqbModuleAsyncOptions, SqbModuleOptions } from './sqb.interface';
|
|
4
|
+
export declare class SqbCoreModule implements OnApplicationShutdown {
|
|
5
|
+
private readonly options;
|
|
6
|
+
private readonly moduleRef;
|
|
7
|
+
constructor(options: SqbModuleOptions, moduleRef: ModuleRef);
|
|
8
|
+
static forRoot(options?: SqbModuleOptions): DynamicModule;
|
|
9
|
+
static forRootAsync(options: SqbModuleAsyncOptions): DynamicModule;
|
|
10
|
+
onApplicationShutdown(): Promise<void>;
|
|
11
|
+
private static createAsyncProviders;
|
|
12
|
+
private static createAsyncOptionsProvider;
|
|
13
|
+
private static createConnection;
|
|
14
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var SqbCoreModule_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.SqbCoreModule = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const crypto = tslib_1.__importStar(require("crypto"));
|
|
7
|
+
const common_1 = require("@nestjs/common");
|
|
8
|
+
const core_1 = require("@nestjs/core");
|
|
9
|
+
const rxjs_1 = require("rxjs");
|
|
10
|
+
const rxjs = tslib_1.__importStar(require("rxjs"));
|
|
11
|
+
const connect_1 = require("@sqb/connect");
|
|
12
|
+
const sqb_utils_1 = require("./sqb.utils");
|
|
13
|
+
const sqb_constants_1 = require("./sqb.constants");
|
|
14
|
+
let SqbCoreModule = SqbCoreModule_1 = class SqbCoreModule {
|
|
15
|
+
constructor(options, moduleRef) {
|
|
16
|
+
this.options = options;
|
|
17
|
+
this.moduleRef = moduleRef;
|
|
18
|
+
}
|
|
19
|
+
static forRoot(options = {}) {
|
|
20
|
+
const optionsProvider = {
|
|
21
|
+
provide: sqb_constants_1.SQB_MODULE_OPTIONS,
|
|
22
|
+
useValue: options,
|
|
23
|
+
};
|
|
24
|
+
const connectionProvider = {
|
|
25
|
+
provide: (0, sqb_utils_1.getSQBToken)(options.name),
|
|
26
|
+
useFactory: () => this.createConnection(options),
|
|
27
|
+
};
|
|
28
|
+
return {
|
|
29
|
+
module: SqbCoreModule_1,
|
|
30
|
+
providers: [connectionProvider, optionsProvider],
|
|
31
|
+
exports: [connectionProvider],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
static forRootAsync(options) {
|
|
35
|
+
const connectionProvider = {
|
|
36
|
+
provide: (0, sqb_utils_1.getSQBToken)(options.name),
|
|
37
|
+
inject: [sqb_constants_1.SQB_MODULE_OPTIONS],
|
|
38
|
+
useFactory: async (sqbOptions) => {
|
|
39
|
+
const name = options.name || sqbOptions.name;
|
|
40
|
+
return this.createConnection({
|
|
41
|
+
...sqbOptions,
|
|
42
|
+
name
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const asyncProviders = this.createAsyncProviders(options);
|
|
47
|
+
return {
|
|
48
|
+
module: SqbCoreModule_1,
|
|
49
|
+
imports: options.imports,
|
|
50
|
+
providers: [
|
|
51
|
+
...asyncProviders,
|
|
52
|
+
connectionProvider,
|
|
53
|
+
{
|
|
54
|
+
provide: sqb_constants_1.SQB_MODULE_ID,
|
|
55
|
+
useValue: crypto.randomUUID(),
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
exports: [connectionProvider],
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
async onApplicationShutdown() {
|
|
62
|
+
const client = this.moduleRef.get((0, sqb_utils_1.getSQBToken)(this.options.name));
|
|
63
|
+
if (client)
|
|
64
|
+
await client.close(this.options.shutdownWaitMs);
|
|
65
|
+
}
|
|
66
|
+
static createAsyncProviders(options) {
|
|
67
|
+
if (options.useExisting || options.useFactory)
|
|
68
|
+
return [this.createAsyncOptionsProvider(options)];
|
|
69
|
+
if (options.useClass)
|
|
70
|
+
return [
|
|
71
|
+
this.createAsyncOptionsProvider(options),
|
|
72
|
+
{
|
|
73
|
+
provide: options.useClass,
|
|
74
|
+
useClass: options.useClass
|
|
75
|
+
}
|
|
76
|
+
];
|
|
77
|
+
throw new Error('Invalid configuration. Must provide useFactory, useClass or useExisting');
|
|
78
|
+
}
|
|
79
|
+
static createAsyncOptionsProvider(options) {
|
|
80
|
+
if (options.useFactory) {
|
|
81
|
+
return {
|
|
82
|
+
provide: sqb_constants_1.SQB_MODULE_OPTIONS,
|
|
83
|
+
useFactory: options.useFactory,
|
|
84
|
+
inject: options.inject || [],
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
const useClass = options.useClass || options.useExisting;
|
|
88
|
+
if (useClass) {
|
|
89
|
+
return {
|
|
90
|
+
provide: sqb_constants_1.SQB_MODULE_OPTIONS,
|
|
91
|
+
useFactory: (optionsFactory) => optionsFactory.createSqbOptions(options.name),
|
|
92
|
+
inject: [useClass],
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
throw new Error('Invalid configuration. Must provide useFactory, useClass or useExisting');
|
|
96
|
+
}
|
|
97
|
+
static async createConnection(options) {
|
|
98
|
+
const connectionToken = options.name;
|
|
99
|
+
// NestJS 8
|
|
100
|
+
// @ts-ignore
|
|
101
|
+
if (rxjs.lastValueFrom) {
|
|
102
|
+
// @ts-ignore
|
|
103
|
+
return await rxjs.lastValueFrom((0, rxjs_1.defer)(async () => {
|
|
104
|
+
const client = new connect_1.SqbClient(options);
|
|
105
|
+
await client.test();
|
|
106
|
+
return client;
|
|
107
|
+
}).pipe((0, sqb_utils_1.handleRetry)(connectionToken, options.retryAttempts, options.retryDelay, options.verboseRetryLog, options.toRetry)));
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
// NestJS 7
|
|
111
|
+
// @ts-ignore
|
|
112
|
+
return await (0, rxjs_1.defer)(async () => {
|
|
113
|
+
const client = new connect_1.SqbClient(options);
|
|
114
|
+
await client.test();
|
|
115
|
+
return client;
|
|
116
|
+
})
|
|
117
|
+
.pipe((0, sqb_utils_1.handleRetry)(connectionToken, options.retryAttempts, options.retryDelay, options.verboseRetryLog, options.toRetry))
|
|
118
|
+
.toPromise();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
SqbCoreModule = SqbCoreModule_1 = tslib_1.__decorate([
|
|
123
|
+
(0, common_1.Global)(),
|
|
124
|
+
(0, common_1.Module)({}),
|
|
125
|
+
tslib_1.__param(0, (0, common_1.Inject)(sqb_constants_1.SQB_MODULE_OPTIONS)),
|
|
126
|
+
tslib_1.__metadata("design:paramtypes", [Object, core_1.ModuleRef])
|
|
127
|
+
], SqbCoreModule);
|
|
128
|
+
exports.SqbCoreModule = SqbCoreModule;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const InjectSQB: (name?: string) => ParameterDecorator;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InjectSQB = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const sqb_utils_1 = require("./sqb.utils");
|
|
6
|
+
const InjectSQB = (name) => (0, common_1.Inject)((0, sqb_utils_1.getSQBToken)(name));
|
|
7
|
+
exports.InjectSQB = InjectSQB;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Type } from '@nestjs/common';
|
|
2
|
+
import { ModuleMetadata } from '@nestjs/common/interfaces';
|
|
3
|
+
import { ClientConfiguration } from '@sqb/connect';
|
|
4
|
+
export declare type SqbModuleOptions = {
|
|
5
|
+
/**
|
|
6
|
+
* Connection name
|
|
7
|
+
*/
|
|
8
|
+
name?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Number of times to retry connecting
|
|
11
|
+
* Default: 10
|
|
12
|
+
*/
|
|
13
|
+
retryAttempts?: number;
|
|
14
|
+
/**
|
|
15
|
+
* Delay between connection retry attempts (ms)
|
|
16
|
+
* Default: 3000
|
|
17
|
+
*/
|
|
18
|
+
retryDelay?: number;
|
|
19
|
+
/**
|
|
20
|
+
* Function that determines whether the module should
|
|
21
|
+
* attempt to connect upon failure.
|
|
22
|
+
*
|
|
23
|
+
* @param err error that was thrown
|
|
24
|
+
* @returns whether to retry connection or not
|
|
25
|
+
*/
|
|
26
|
+
toRetry?: (err: any) => boolean;
|
|
27
|
+
/**
|
|
28
|
+
* If `true`, connection will not be closed on application shutdown.
|
|
29
|
+
*/
|
|
30
|
+
keepConnectionAlive?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* If `true`, will show verbose error messages on each connection retry.
|
|
33
|
+
*/
|
|
34
|
+
verboseRetryLog?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Number of ms to wait closing connection on shutdown
|
|
37
|
+
* Default: 10
|
|
38
|
+
*/
|
|
39
|
+
shutdownWaitMs?: number;
|
|
40
|
+
} & ClientConfiguration;
|
|
41
|
+
export interface SqbOptionsFactory {
|
|
42
|
+
createSqbOptions(connectionName?: string): Promise<SqbModuleOptions> | SqbModuleOptions;
|
|
43
|
+
}
|
|
44
|
+
export interface SqbModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
45
|
+
name?: string;
|
|
46
|
+
useExisting?: Type<SqbOptionsFactory>;
|
|
47
|
+
useClass?: Type<SqbOptionsFactory>;
|
|
48
|
+
useFactory?: (...args: any[]) => Promise<SqbModuleOptions> | SqbModuleOptions;
|
|
49
|
+
inject?: any[];
|
|
50
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { SqbModuleAsyncOptions, SqbModuleOptions } from './sqb.interface';
|
|
3
|
+
export declare class SqbModule {
|
|
4
|
+
static forRoot(options?: SqbModuleOptions): DynamicModule;
|
|
5
|
+
static forRootAsync(options: SqbModuleAsyncOptions): DynamicModule;
|
|
6
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var SqbModule_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.SqbModule = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const common_1 = require("@nestjs/common");
|
|
7
|
+
const sqb_core_module_1 = require("./sqb-core.module");
|
|
8
|
+
let SqbModule = SqbModule_1 = class SqbModule {
|
|
9
|
+
static forRoot(options) {
|
|
10
|
+
return {
|
|
11
|
+
module: SqbModule_1,
|
|
12
|
+
imports: [sqb_core_module_1.SqbCoreModule.forRoot(options)]
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
static forRootAsync(options) {
|
|
16
|
+
return {
|
|
17
|
+
module: SqbModule_1,
|
|
18
|
+
imports: [sqb_core_module_1.SqbCoreModule.forRootAsync(options)]
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
SqbModule = SqbModule_1 = tslib_1.__decorate([
|
|
23
|
+
(0, common_1.Module)({})
|
|
24
|
+
], SqbModule);
|
|
25
|
+
exports.SqbModule = SqbModule;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Type } from '@nestjs/common';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import { SqbClient } from '@sqb/connect';
|
|
4
|
+
/**
|
|
5
|
+
* This function returns a Connection injection token for the given connection name.
|
|
6
|
+
* @param {string | symbol} [name=SQB_DEFAULT_CONNECTION] This optional parameter is either
|
|
7
|
+
* a SqbClient, or a ConnectionOptions or a string.
|
|
8
|
+
* @returns {string | symbol} The Connection injection token.
|
|
9
|
+
*/
|
|
10
|
+
export declare function getSQBToken(name?: string | symbol | Type<SqbClient>): string | symbol | Type<SqbClient>;
|
|
11
|
+
export declare function handleRetry(connectionName?: string | symbol | Type<SqbClient>, retryAttempts?: number, retryDelay?: number, verboseRetryLog?: boolean, toRetry?: (err: any) => boolean): <T>(source: Observable<T>) => Observable<T>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.handleRetry = exports.getSQBToken = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const operators_1 = require("rxjs/operators");
|
|
6
|
+
const connect_1 = require("@sqb/connect");
|
|
7
|
+
const logger = new common_1.Logger('SqbModule');
|
|
8
|
+
/**
|
|
9
|
+
* This function returns a Connection injection token for the given connection name.
|
|
10
|
+
* @param {string | symbol} [name=SQB_DEFAULT_CONNECTION] This optional parameter is either
|
|
11
|
+
* a SqbClient, or a ConnectionOptions or a string.
|
|
12
|
+
* @returns {string | symbol} The Connection injection token.
|
|
13
|
+
*/
|
|
14
|
+
function getSQBToken(name) {
|
|
15
|
+
if (!name)
|
|
16
|
+
return connect_1.SqbClient;
|
|
17
|
+
if (typeof name === 'symbol' || typeof name === 'function')
|
|
18
|
+
return name;
|
|
19
|
+
return `${name}_SqbConnection`;
|
|
20
|
+
}
|
|
21
|
+
exports.getSQBToken = getSQBToken;
|
|
22
|
+
function handleRetry(connectionName, retryAttempts = 9, retryDelay = 3000, verboseRetryLog = false, toRetry) {
|
|
23
|
+
return (source) => source.pipe((0, operators_1.retryWhen)((e) => e.pipe((0, operators_1.scan)((errorCount, error) => {
|
|
24
|
+
if (toRetry && !toRetry(error)) {
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
const connectionInfo = !connectionName || connectionName === connect_1.SqbClient
|
|
28
|
+
? 'default'
|
|
29
|
+
: ` (${String(connectionName)})`;
|
|
30
|
+
const verboseMessage = verboseRetryLog
|
|
31
|
+
? ` Message: ${error.message}.`
|
|
32
|
+
: '';
|
|
33
|
+
logger.error(`Unable to connect to the database ${connectionInfo}.${verboseMessage} Retrying (${errorCount + 1})...`, error.stack);
|
|
34
|
+
if (errorCount + 1 >= retryAttempts) {
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
return errorCount + 1;
|
|
38
|
+
}, 0), (0, operators_1.delay)(retryDelay))));
|
|
39
|
+
}
|
|
40
|
+
exports.handleRetry = handleRetry;
|
package/package.json
CHANGED