nest-knexjs 0.0.7 → 0.0.10
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/.github/workflows/codeql-analysis.yml +4 -4
- package/.husky/commit-msg +4 -0
- package/.husky/pre-commit +4 -0
- package/README.md +2 -2
- package/dist/common/index.d.ts +2 -0
- package/dist/common/index.js +18 -0
- package/dist/common/knex.decorators.d.ts +3 -0
- package/dist/common/knex.decorators.js +11 -0
- package/dist/common/knex.utils.d.ts +20 -0
- package/dist/common/knex.utils.js +65 -0
- package/dist/entities-metadata.storage.d.ts +8 -0
- package/dist/entities-metadata.storage.js +31 -0
- package/dist/exceptions/circular-dependency.exception.d.ts +3 -0
- package/dist/exceptions/circular-dependency.exception.js +10 -0
- package/dist/exceptions/index.d.ts +1 -0
- package/dist/exceptions/index.js +17 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/dist/interfaces/index.d.ts +3 -0
- package/dist/interfaces/index.js +19 -0
- package/dist/interfaces/knex-module-async-options.interface.d.ts +10 -0
- package/dist/interfaces/knex-module-async-options.interface.js +2 -0
- package/dist/interfaces/knex-options-factory.interface.d.ts +4 -0
- package/dist/interfaces/knex-options-factory.interface.js +2 -0
- package/dist/interfaces/knex-options.interface.d.ts +7 -0
- package/dist/interfaces/knex-options.interface.js +2 -0
- package/dist/knex-core.module.d.ts +14 -0
- package/dist/knex-core.module.js +107 -0
- package/dist/knex.constants.d.ts +3 -0
- package/dist/knex.constants.js +6 -0
- package/dist/knex.module.d.ts +6 -0
- package/dist/knex.module.js +30 -0
- package/package.json +29 -34
- package/tests/jest-e2e.json +1 -2
- package/tsconfig.build.json +1 -2
|
@@ -15,17 +15,17 @@ jobs:
|
|
|
15
15
|
|
|
16
16
|
steps:
|
|
17
17
|
- name: Checkout repository
|
|
18
|
-
uses: actions/checkout@
|
|
18
|
+
uses: actions/checkout@v3
|
|
19
19
|
with:
|
|
20
20
|
fetch-depth: 0
|
|
21
21
|
|
|
22
22
|
- name: Initialize CodeQL
|
|
23
|
-
uses: github/codeql-action/init@
|
|
23
|
+
uses: github/codeql-action/init@v2
|
|
24
24
|
with:
|
|
25
25
|
languages: javascript
|
|
26
26
|
|
|
27
27
|
- name: Autobuild
|
|
28
|
-
uses: github/codeql-action/autobuild@
|
|
28
|
+
uses: github/codeql-action/autobuild@v2
|
|
29
29
|
|
|
30
30
|
- name: Perform CodeQL Analysis
|
|
31
|
-
uses: github/codeql-action/analyze@
|
|
31
|
+
uses: github/codeql-action/analyze@v2
|
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/
|
|
2
|
+
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="200" alt="Nest Logo" /></a>
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
5
|
[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
|
|
@@ -134,7 +134,7 @@ export class UsersController {
|
|
|
134
134
|
constructor(private readonly usersService: UsersService) {}
|
|
135
135
|
|
|
136
136
|
@Get()
|
|
137
|
-
async
|
|
137
|
+
async getAllUsers() {
|
|
138
138
|
return await this.usersService.findAll();
|
|
139
139
|
}
|
|
140
140
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./knex.decorators"), exports);
|
|
18
|
+
__exportStar(require("./knex.utils"), exports);
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { KnexModuleOptions } from '../interfaces/knex-options.interface';
|
|
2
|
+
export declare const InjectModel: (connection?: string) => (target: object, key: string | symbol, index?: number) => void;
|
|
3
|
+
export declare const InjectConnection: (connection?: KnexModuleOptions | string) => ParameterDecorator;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InjectConnection = exports.InjectModel = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const knex_utils_1 = require("./knex.utils");
|
|
6
|
+
const InjectModel = (connection) => {
|
|
7
|
+
return (0, common_1.Inject)((0, knex_utils_1.getConnectionToken)(connection));
|
|
8
|
+
};
|
|
9
|
+
exports.InjectModel = InjectModel;
|
|
10
|
+
const InjectConnection = (connection) => (0, common_1.Inject)((0, knex_utils_1.getConnectionToken)(connection));
|
|
11
|
+
exports.InjectConnection = InjectConnection;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { KnexModuleOptions } from '../interfaces';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
/**
|
|
4
|
+
* This function generates an injection token for an Model
|
|
5
|
+
* @param {Function} This parameter can either be a Model
|
|
6
|
+
* @param {string} [connection='default'] Connection name
|
|
7
|
+
* @returns {string} The Entity injection token
|
|
8
|
+
*/
|
|
9
|
+
export declare function getModelToken(model: Function, connection?: KnexModuleOptions | string): string;
|
|
10
|
+
export declare function getConnectionToken(connection?: KnexModuleOptions | string): string | Function;
|
|
11
|
+
/**
|
|
12
|
+
* This function returns a Connection prefix based on the connection name
|
|
13
|
+
* @param {KnexModuleOptions | string} [connection='default'] This optional parameter is either
|
|
14
|
+
* a KnexModuleOptions or a string.
|
|
15
|
+
* @returns {string | Function} The Connection injection token.
|
|
16
|
+
*/
|
|
17
|
+
export declare function getConnectionPrefix(connection?: KnexModuleOptions | string): string;
|
|
18
|
+
export declare function handleRetry(retryAttempts?: number, retryDelay?: number): <T>(source: Observable<T>) => Observable<T>;
|
|
19
|
+
export declare function getConnectionName(options: KnexModuleOptions): string;
|
|
20
|
+
export declare const generateString: () => any;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateString = exports.getConnectionName = exports.handleRetry = exports.getConnectionPrefix = exports.getConnectionToken = exports.getModelToken = void 0;
|
|
4
|
+
const knex_constants_1 = require("../knex.constants");
|
|
5
|
+
const operators_1 = require("rxjs/operators");
|
|
6
|
+
const uuid_1 = require("uuid");
|
|
7
|
+
const common_1 = require("@nestjs/common");
|
|
8
|
+
const circular_dependency_exception_1 = require("../exceptions/circular-dependency.exception");
|
|
9
|
+
const logger = new common_1.Logger('KnexModule');
|
|
10
|
+
/**
|
|
11
|
+
* This function generates an injection token for an Model
|
|
12
|
+
* @param {Function} This parameter can either be a Model
|
|
13
|
+
* @param {string} [connection='default'] Connection name
|
|
14
|
+
* @returns {string} The Entity injection token
|
|
15
|
+
*/
|
|
16
|
+
function getModelToken(model, connection = knex_constants_1.DEFAULT_CONNECTION_NAME) {
|
|
17
|
+
if (model === null || model === undefined) {
|
|
18
|
+
throw new circular_dependency_exception_1.CircularDependencyException('@InjectModel()');
|
|
19
|
+
}
|
|
20
|
+
const connectionPrefix = getConnectionPrefix(connection);
|
|
21
|
+
return `${connectionPrefix}${model.name}`;
|
|
22
|
+
}
|
|
23
|
+
exports.getModelToken = getModelToken;
|
|
24
|
+
function getConnectionToken(connection = knex_constants_1.DEFAULT_CONNECTION_NAME) {
|
|
25
|
+
if (typeof connection === 'string') {
|
|
26
|
+
return connection;
|
|
27
|
+
}
|
|
28
|
+
return `${connection.name || knex_constants_1.DEFAULT_CONNECTION_NAME}`;
|
|
29
|
+
}
|
|
30
|
+
exports.getConnectionToken = getConnectionToken;
|
|
31
|
+
/**
|
|
32
|
+
* This function returns a Connection prefix based on the connection name
|
|
33
|
+
* @param {KnexModuleOptions | string} [connection='default'] This optional parameter is either
|
|
34
|
+
* a KnexModuleOptions or a string.
|
|
35
|
+
* @returns {string | Function} The Connection injection token.
|
|
36
|
+
*/
|
|
37
|
+
function getConnectionPrefix(connection = knex_constants_1.DEFAULT_CONNECTION_NAME) {
|
|
38
|
+
if (connection === knex_constants_1.DEFAULT_CONNECTION_NAME) {
|
|
39
|
+
return '';
|
|
40
|
+
}
|
|
41
|
+
if (typeof connection === 'string') {
|
|
42
|
+
return connection + '_';
|
|
43
|
+
}
|
|
44
|
+
if (connection.name === knex_constants_1.DEFAULT_CONNECTION_NAME || !connection.name) {
|
|
45
|
+
return '';
|
|
46
|
+
}
|
|
47
|
+
return connection.name + '_';
|
|
48
|
+
}
|
|
49
|
+
exports.getConnectionPrefix = getConnectionPrefix;
|
|
50
|
+
function handleRetry(retryAttempts = 9, retryDelay = 3000) {
|
|
51
|
+
return (source) => source.pipe((0, operators_1.retryWhen)((e) => e.pipe((0, operators_1.scan)((errorCount, error) => {
|
|
52
|
+
logger.error(`Unable to connect to the database. Retrying (${errorCount + 1})...`, error.stack);
|
|
53
|
+
if (errorCount + 1 >= retryAttempts) {
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
return errorCount + 1;
|
|
57
|
+
}, 0), (0, operators_1.delay)(retryDelay))));
|
|
58
|
+
}
|
|
59
|
+
exports.handleRetry = handleRetry;
|
|
60
|
+
function getConnectionName(options) {
|
|
61
|
+
return options && options.name ? options.name : knex_constants_1.DEFAULT_CONNECTION_NAME;
|
|
62
|
+
}
|
|
63
|
+
exports.getConnectionName = getConnectionName;
|
|
64
|
+
const generateString = () => (0, uuid_1.v4)();
|
|
65
|
+
exports.generateString = generateString;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { KnexModuleOptions } from './interfaces';
|
|
2
|
+
declare type ConnectionToken = KnexModuleOptions | string;
|
|
3
|
+
export declare class EntitiesMetadataStorage {
|
|
4
|
+
private static readonly storage;
|
|
5
|
+
static addEntitiesByConnection(connection: ConnectionToken, entities: Function[]): void;
|
|
6
|
+
static getEntitiesByConnection(connection: ConnectionToken): Function[];
|
|
7
|
+
}
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EntitiesMetadataStorage = void 0;
|
|
4
|
+
class EntitiesMetadataStorage {
|
|
5
|
+
static addEntitiesByConnection(connection, entities) {
|
|
6
|
+
const connectionToken = typeof connection === 'string' ? connection : connection.name;
|
|
7
|
+
if (!connectionToken) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
let collection = this.storage.get(connectionToken);
|
|
11
|
+
if (!collection) {
|
|
12
|
+
collection = [];
|
|
13
|
+
this.storage.set(connectionToken, collection);
|
|
14
|
+
}
|
|
15
|
+
entities.forEach(entity => {
|
|
16
|
+
if (collection.includes(entity)) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
collection.push(entity);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
static getEntitiesByConnection(connection) {
|
|
23
|
+
const connectionToken = typeof connection === 'string' ? connection : connection.name;
|
|
24
|
+
if (!connectionToken) {
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
return this.storage.get(connectionToken) || [];
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.EntitiesMetadataStorage = EntitiesMetadataStorage;
|
|
31
|
+
EntitiesMetadataStorage.storage = new Map();
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CircularDependencyException = void 0;
|
|
4
|
+
class CircularDependencyException extends Error {
|
|
5
|
+
constructor(context) {
|
|
6
|
+
const ctx = context ? ` inside ${context}` : ``;
|
|
7
|
+
super(`A circular dependency has been detected${ctx}. Please, make sure that each side of a bidirectional relationships are decorated with "forwardRef()". Also, try to eliminate barrel files because they can lead to an unexpected behavior too.`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.CircularDependencyException = CircularDependencyException;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './circular-dependency.exception';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./circular-dependency.exception"), exports);
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./common"), exports);
|
|
18
|
+
__exportStar(require("./interfaces"), exports);
|
|
19
|
+
__exportStar(require("./knex.module"), exports);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./knex-options.interface"), exports);
|
|
18
|
+
__exportStar(require("./knex-options-factory.interface"), exports);
|
|
19
|
+
__exportStar(require("./knex-module-async-options.interface"), exports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ModuleMetadata, Type } from "@nestjs/common/interfaces";
|
|
2
|
+
import { KnexModuleOptions } from './knex-options.interface';
|
|
3
|
+
import { KnexOptionsFactory } from './knex-options-factory.interface';
|
|
4
|
+
export interface KnexModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
5
|
+
name?: string;
|
|
6
|
+
inject?: any[];
|
|
7
|
+
useClass?: Type<KnexOptionsFactory>;
|
|
8
|
+
useExisting?: Type<KnexOptionsFactory>;
|
|
9
|
+
useFactory?: (...args: any[]) => Promise<KnexModuleOptions> | KnexModuleOptions;
|
|
10
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { DynamicModule, Provider, OnApplicationShutdown } from '@nestjs/common';
|
|
2
|
+
import { KnexModuleAsyncOptions, KnexModuleOptions } from './interfaces';
|
|
3
|
+
import { ModuleRef } from '@nestjs/core';
|
|
4
|
+
export declare class KnexCoreModule implements OnApplicationShutdown {
|
|
5
|
+
private readonly options;
|
|
6
|
+
private readonly moduleRef;
|
|
7
|
+
constructor(options: KnexModuleOptions, moduleRef: ModuleRef);
|
|
8
|
+
static forRoot(options: KnexModuleOptions, connection?: string): DynamicModule;
|
|
9
|
+
static forRootAsync(options: KnexModuleAsyncOptions, connection: string): DynamicModule;
|
|
10
|
+
onApplicationShutdown(): Promise<any>;
|
|
11
|
+
static createAsyncProviders(options: KnexModuleAsyncOptions): Provider[];
|
|
12
|
+
static createAsyncOptionsProvider(options: KnexModuleAsyncOptions): Provider;
|
|
13
|
+
private static createConnectionFactory;
|
|
14
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
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 __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var KnexCoreModule_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.KnexCoreModule = void 0;
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const knex_utils_1 = require("./common/knex.utils");
|
|
19
|
+
const knex_constants_1 = require("./knex.constants");
|
|
20
|
+
const knex_1 = require("knex");
|
|
21
|
+
const core_1 = require("@nestjs/core");
|
|
22
|
+
const rxjs_1 = require("rxjs");
|
|
23
|
+
let KnexCoreModule = KnexCoreModule_1 = class KnexCoreModule {
|
|
24
|
+
constructor(options, moduleRef) {
|
|
25
|
+
this.options = options;
|
|
26
|
+
this.moduleRef = moduleRef;
|
|
27
|
+
}
|
|
28
|
+
static forRoot(options, connection) {
|
|
29
|
+
const knexModuleOptions = {
|
|
30
|
+
provide: knex_constants_1.KNEX_MODULE_OPTIONS,
|
|
31
|
+
useValue: options,
|
|
32
|
+
};
|
|
33
|
+
const connectionProvider = {
|
|
34
|
+
provide: (0, knex_utils_1.getConnectionToken)(connection),
|
|
35
|
+
useFactory: async () => await this.createConnectionFactory(options),
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
module: KnexCoreModule_1,
|
|
39
|
+
providers: [connectionProvider, knexModuleOptions],
|
|
40
|
+
exports: [connectionProvider],
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
static forRootAsync(options, connection) {
|
|
44
|
+
const connectionProvider = {
|
|
45
|
+
provide: (0, knex_utils_1.getConnectionToken)(connection),
|
|
46
|
+
useFactory: async (options) => {
|
|
47
|
+
return await this.createConnectionFactory(options);
|
|
48
|
+
},
|
|
49
|
+
inject: [knex_constants_1.KNEX_MODULE_OPTIONS],
|
|
50
|
+
};
|
|
51
|
+
return {
|
|
52
|
+
module: KnexCoreModule_1,
|
|
53
|
+
imports: options.imports,
|
|
54
|
+
providers: [...this.createAsyncProviders(options), connectionProvider],
|
|
55
|
+
exports: [connectionProvider],
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
async onApplicationShutdown() {
|
|
59
|
+
const connection = this.moduleRef.get((0, knex_utils_1.getConnectionToken)(this.options));
|
|
60
|
+
connection && (await connection.destroy());
|
|
61
|
+
}
|
|
62
|
+
static createAsyncProviders(options) {
|
|
63
|
+
if (options.useExisting || options.useFactory) {
|
|
64
|
+
return [this.createAsyncOptionsProvider(options)];
|
|
65
|
+
}
|
|
66
|
+
const useClass = options.useClass;
|
|
67
|
+
return [
|
|
68
|
+
this.createAsyncOptionsProvider(options),
|
|
69
|
+
{
|
|
70
|
+
provide: useClass,
|
|
71
|
+
useClass,
|
|
72
|
+
},
|
|
73
|
+
];
|
|
74
|
+
}
|
|
75
|
+
static createAsyncOptionsProvider(options) {
|
|
76
|
+
if (options.useFactory) {
|
|
77
|
+
return {
|
|
78
|
+
provide: knex_constants_1.KNEX_MODULE_OPTIONS,
|
|
79
|
+
useFactory: options.useFactory,
|
|
80
|
+
inject: options.inject || [],
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
// `as Type<KnexOptionsFactory>` is a workaround for microsoft/TypeScript#31603
|
|
84
|
+
const inject = [
|
|
85
|
+
(options.useClass || options.useExisting),
|
|
86
|
+
];
|
|
87
|
+
return {
|
|
88
|
+
provide: knex_constants_1.KNEX_MODULE_OPTIONS,
|
|
89
|
+
useFactory: async (optionsFactory) => {
|
|
90
|
+
return await optionsFactory.createKnexOptions();
|
|
91
|
+
},
|
|
92
|
+
inject
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
static async createConnectionFactory(options) {
|
|
96
|
+
return (0, rxjs_1.lastValueFrom)((0, rxjs_1.defer)(async () => {
|
|
97
|
+
return (0, knex_1.knex)(options.config);
|
|
98
|
+
}).pipe((0, knex_utils_1.handleRetry)(options.retryAttempts, options.retryDelay)));
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
KnexCoreModule = KnexCoreModule_1 = __decorate([
|
|
102
|
+
(0, common_1.Global)(),
|
|
103
|
+
(0, common_1.Module)({}),
|
|
104
|
+
__param(0, (0, common_1.Inject)(knex_constants_1.KNEX_MODULE_OPTIONS)),
|
|
105
|
+
__metadata("design:paramtypes", [Object, core_1.ModuleRef])
|
|
106
|
+
], KnexCoreModule);
|
|
107
|
+
exports.KnexCoreModule = KnexCoreModule;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_CONNECTION_NAME = exports.KNEX_MODULE_ID = exports.KNEX_MODULE_OPTIONS = void 0;
|
|
4
|
+
exports.KNEX_MODULE_OPTIONS = 'KnexModuleOptions';
|
|
5
|
+
exports.KNEX_MODULE_ID = 'KnexModuleId';
|
|
6
|
+
exports.DEFAULT_CONNECTION_NAME = 'default';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { KnexModuleAsyncOptions, KnexModuleOptions } from './interfaces';
|
|
3
|
+
export declare class KnexModule {
|
|
4
|
+
static forRoot(options: KnexModuleOptions, connection?: string): DynamicModule;
|
|
5
|
+
static forRootAsync(options: KnexModuleAsyncOptions, connection?: string): DynamicModule;
|
|
6
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
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 KnexModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.KnexModule = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const knex_core_module_1 = require("./knex-core.module");
|
|
13
|
+
let KnexModule = KnexModule_1 = class KnexModule {
|
|
14
|
+
static forRoot(options, connection) {
|
|
15
|
+
return {
|
|
16
|
+
module: KnexModule_1,
|
|
17
|
+
imports: [knex_core_module_1.KnexCoreModule.forRoot(options, connection)],
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
static forRootAsync(options, connection) {
|
|
21
|
+
return {
|
|
22
|
+
module: KnexModule_1,
|
|
23
|
+
imports: [knex_core_module_1.KnexCoreModule.forRootAsync(options, connection)],
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
KnexModule = KnexModule_1 = __decorate([
|
|
28
|
+
(0, common_1.Module)({})
|
|
29
|
+
], KnexModule);
|
|
30
|
+
exports.KnexModule = KnexModule;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nest-knexjs",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"description": "Knexjs module for Nest framework (node.js) 😻",
|
|
5
5
|
"author": "Tony133",
|
|
6
6
|
"license": "MIT",
|
|
@@ -15,56 +15,51 @@
|
|
|
15
15
|
"test:e2e": "jest --config ./tests/jest-e2e.json --runInBand",
|
|
16
16
|
"test:e2e:dev": "jest --config ./tests/jest-e2e.json --runInBand --watch",
|
|
17
17
|
"prerelease": "npm run build",
|
|
18
|
-
"release": "release-it"
|
|
18
|
+
"release": "release-it",
|
|
19
|
+
"prepare": "husky install"
|
|
19
20
|
},
|
|
20
21
|
"devDependencies": {
|
|
21
|
-
"@commitlint/cli": "16.
|
|
22
|
-
"@commitlint/config-angular": "16.
|
|
23
|
-
"@nestjs/common": "8.
|
|
24
|
-
"@nestjs/core": "8.
|
|
25
|
-
"@nestjs/platform-express": "8.
|
|
26
|
-
"@nestjs/testing": "8.
|
|
22
|
+
"@commitlint/cli": "16.2.4",
|
|
23
|
+
"@commitlint/config-angular": "16.2.4",
|
|
24
|
+
"@nestjs/common": "8.4.4",
|
|
25
|
+
"@nestjs/core": "8.4.4",
|
|
26
|
+
"@nestjs/platform-express": "8.4.4",
|
|
27
|
+
"@nestjs/testing": "8.4.4",
|
|
27
28
|
"@types/express": "4.17.13",
|
|
28
|
-
"@types/node": "16.11.
|
|
29
|
-
"@types/jest": "27.
|
|
30
|
-
"@types/supertest": "2.0.
|
|
31
|
-
"@typescript-eslint/eslint-plugin": "5.
|
|
32
|
-
"@typescript-eslint/parser": "5.
|
|
33
|
-
"eslint": "8.
|
|
34
|
-
"eslint-config-prettier": "8.
|
|
35
|
-
"eslint-plugin-import": "2.
|
|
36
|
-
"knex": "
|
|
29
|
+
"@types/node": "16.11.34",
|
|
30
|
+
"@types/jest": "27.5.0",
|
|
31
|
+
"@types/supertest": "2.0.12",
|
|
32
|
+
"@typescript-eslint/eslint-plugin": "5.23.0",
|
|
33
|
+
"@typescript-eslint/parser": "5.23.0",
|
|
34
|
+
"eslint": "8.15.0",
|
|
35
|
+
"eslint-config-prettier": "8.5.0",
|
|
36
|
+
"eslint-plugin-import": "2.26.0",
|
|
37
|
+
"knex": "2.0.0",
|
|
37
38
|
"mysql": "2.18.1",
|
|
38
|
-
"husky": "
|
|
39
|
-
"prettier": "2.
|
|
40
|
-
"jest": "
|
|
39
|
+
"husky": "8.0.1",
|
|
40
|
+
"prettier": "2.6.2",
|
|
41
|
+
"jest": "28.1.0",
|
|
41
42
|
"reflect-metadata": "0.1.13",
|
|
42
|
-
"release-it": "
|
|
43
|
-
"supertest": "6.2.
|
|
44
|
-
"ts-node": "10.
|
|
45
|
-
"rxjs": "7.5.
|
|
46
|
-
"ts-jest": "
|
|
47
|
-
"tsconfig-paths": "
|
|
48
|
-
"typescript": "4.
|
|
43
|
+
"release-it": "15.0.0",
|
|
44
|
+
"supertest": "6.2.3",
|
|
45
|
+
"ts-node": "10.7.0",
|
|
46
|
+
"rxjs": "7.5.5",
|
|
47
|
+
"ts-jest": "28.0.2",
|
|
48
|
+
"tsconfig-paths": "4.0.0",
|
|
49
|
+
"typescript": "4.6.4"
|
|
49
50
|
},
|
|
50
51
|
"peerDependencies": {
|
|
51
52
|
"@nestjs/common": "^7.0.0 || ^8.0.0",
|
|
52
53
|
"@nestjs/core": "^7.0.0 || ^8.0.0",
|
|
53
54
|
"reflect-metadata": "^0.1.13",
|
|
54
55
|
"rxjs": "^6.6.3 || ^7.2.0",
|
|
55
|
-
"knex": "^0.21.17 || ^0.95.0 || ^1.0.0"
|
|
56
|
+
"knex": "^0.21.17 || ^0.95.0 || ^1.0.0 || ^2.0.0"
|
|
56
57
|
},
|
|
57
58
|
"lint-staged": {
|
|
58
59
|
"*.ts": [
|
|
59
60
|
"prettier --write"
|
|
60
61
|
]
|
|
61
62
|
},
|
|
62
|
-
"husky": {
|
|
63
|
-
"hooks": {
|
|
64
|
-
"commit-msg": "commitlint -c .commitlintrc.json -E HUSKY_GIT_PARAMS",
|
|
65
|
-
"pre-commit": "lint-staged"
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
63
|
"repository": {
|
|
69
64
|
"type": "git",
|
|
70
65
|
"url": "https://github.com/tony133/nestjs-knexjs"
|
package/tests/jest-e2e.json
CHANGED
package/tsconfig.build.json
CHANGED