@rsdk/db.knex 2.5.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/.env +3 -0
- package/CHANGELOG.md +155 -0
- package/README.md +11 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/inject-knex.decorator.d.ts +2 -0
- package/dist/inject-knex.decorator.js +8 -0
- package/dist/inject-knex.decorator.js.map +1 -0
- package/dist/internal/context.d.ts +77 -0
- package/dist/internal/context.js +220 -0
- package/dist/internal/context.js.map +1 -0
- package/dist/internal/exceptions.d.ts +65 -0
- package/dist/internal/exceptions.js +94 -0
- package/dist/internal/exceptions.js.map +1 -0
- package/dist/internal/initializer.d.ts +9 -0
- package/dist/internal/initializer.js +42 -0
- package/dist/internal/initializer.js.map +1 -0
- package/dist/internal/strategy.d.ts +18 -0
- package/dist/internal/strategy.js +75 -0
- package/dist/internal/strategy.js.map +1 -0
- package/dist/internal/test/strategy.test.e2e.d.ts +1 -0
- package/dist/internal/test/strategy.test.e2e.js +762 -0
- package/dist/internal/test/strategy.test.e2e.js.map +1 -0
- package/dist/internal/test/utils.d.ts +17 -0
- package/dist/internal/test/utils.js +21 -0
- package/dist/internal/test/utils.js.map +1 -0
- package/dist/knex.module.d.ts +5 -0
- package/dist/knex.module.js +34 -0
- package/dist/knex.module.js.map +1 -0
- package/dist/knex.plugin.d.ts +10 -0
- package/dist/knex.plugin.js +20 -0
- package/dist/knex.plugin.js.map +1 -0
- package/dist/providers/index.d.ts +4 -0
- package/dist/providers/index.js +21 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/knex-logger.adapter.d.ts +10 -0
- package/dist/providers/knex-logger.adapter.js +45 -0
- package/dist/providers/knex-logger.adapter.js.map +1 -0
- package/dist/providers/knex.config.d.ts +10 -0
- package/dist/providers/knex.config.js +76 -0
- package/dist/providers/knex.config.js.map +1 -0
- package/dist/providers/knex.factory.d.ts +13 -0
- package/dist/providers/knex.factory.js +96 -0
- package/dist/providers/knex.factory.js.map +1 -0
- package/dist/providers/knex.indicator.d.ts +8 -0
- package/dist/providers/knex.indicator.js +41 -0
- package/dist/providers/knex.indicator.js.map +1 -0
- package/dist/types.d.ts +12 -0
- package/dist/types.js +20 -0
- package/dist/types.js.map +1 -0
- package/jest.config.js +8 -0
- package/package.json +26 -0
- package/src/index.ts +10 -0
- package/src/inject-knex.decorator.ts +6 -0
- package/src/internal/context.ts +284 -0
- package/src/internal/exceptions.ts +91 -0
- package/src/internal/initializer.ts +43 -0
- package/src/internal/strategy.ts +116 -0
- package/src/internal/test/strategy.test.e2e.ts +1026 -0
- package/src/internal/test/utils.ts +26 -0
- package/src/knex.module.ts +28 -0
- package/src/knex.plugin.ts +21 -0
- package/src/providers/index.ts +4 -0
- package/src/providers/knex-logger.adapter.ts +32 -0
- package/src/providers/knex.config.ts +57 -0
- package/src/providers/knex.factory.ts +83 -0
- package/src/providers/knex.indicator.ts +22 -0
- package/src/types.ts +21 -0
- package/tsconfig.json +11 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Initializer } from '../initializer';
|
|
2
|
+
|
|
3
|
+
export const contextTransactionMustNotInitialized = (): void => {
|
|
4
|
+
const context = Initializer.storage.getContext();
|
|
5
|
+
|
|
6
|
+
expect(context?.transactionInitialized).toBeFalsy();
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const contextTransactionMustBeInitialized = (): void => {
|
|
10
|
+
const context = Initializer.storage.getContext();
|
|
11
|
+
|
|
12
|
+
expect(context?.transactionInitialized).toBeTruthy();
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export class SyntheticException extends Error {
|
|
16
|
+
override name = this.constructor.name;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const cat = { id: 1, name: 'Pushistik' };
|
|
20
|
+
export const secondCat = { id: 2, name: 'Barsik' };
|
|
21
|
+
|
|
22
|
+
export interface Cat {
|
|
23
|
+
id: number;
|
|
24
|
+
|
|
25
|
+
name: string;
|
|
26
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { DynamicModule, FactoryProvider } from '@nestjs/common';
|
|
2
|
+
import { Module } from '@nestjs/common';
|
|
3
|
+
import { PlatformConfigModule } from '@rsdk/core';
|
|
4
|
+
import type { Knex } from 'knex';
|
|
5
|
+
|
|
6
|
+
import { KNEX_INJECTION_TOKEN } from './inject-knex.decorator';
|
|
7
|
+
import { KnexFactory, KnexIndicator, KnexPluginConfig } from './providers';
|
|
8
|
+
import type { KnexPluginOptions } from './types';
|
|
9
|
+
|
|
10
|
+
@Module({})
|
|
11
|
+
export class KnexModule {
|
|
12
|
+
static forRoot(options: KnexPluginOptions): DynamicModule {
|
|
13
|
+
const instanceProvider: FactoryProvider = {
|
|
14
|
+
inject: [KnexFactory],
|
|
15
|
+
provide: KNEX_INJECTION_TOKEN,
|
|
16
|
+
useFactory: async (factory: KnexFactory): Promise<Knex> =>
|
|
17
|
+
await factory.create(options.driver),
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
module: KnexModule,
|
|
22
|
+
imports: [PlatformConfigModule.forFeature(KnexPluginConfig)],
|
|
23
|
+
providers: [KnexIndicator, KnexFactory, instanceProvider],
|
|
24
|
+
exports: [instanceProvider],
|
|
25
|
+
global: true,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import type { Constructor } from '@rsdk/common';
|
|
3
|
+
import type { AppropriateTransports, PlatformAppPlugin } from '@rsdk/core';
|
|
4
|
+
|
|
5
|
+
import { Initializer } from './internal/initializer';
|
|
6
|
+
import { KnexModule } from './knex.module';
|
|
7
|
+
import type { KnexPluginOptions } from './types';
|
|
8
|
+
|
|
9
|
+
export class KnexPlugin implements PlatformAppPlugin {
|
|
10
|
+
constructor(private readonly options: KnexPluginOptions) {
|
|
11
|
+
Initializer.initialize();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
forTransports(): AppropriateTransports {
|
|
15
|
+
return 'any';
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
modules(): (Constructor | DynamicModule)[] {
|
|
19
|
+
return [KnexModule.forRoot(this.options)];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { InjectLogger } from '@rsdk/core';
|
|
2
|
+
import { ILogger } from '@rsdk/logging';
|
|
3
|
+
import type { Knex } from 'knex';
|
|
4
|
+
|
|
5
|
+
export class KnexLoggerAdapter implements Knex.Logger {
|
|
6
|
+
constructor(
|
|
7
|
+
@InjectLogger(KnexLoggerAdapter) private readonly platformLogger: ILogger,
|
|
8
|
+
) {
|
|
9
|
+
// TODO [issue: https://github.com/knex/knex/pull/5585]: Because knex takes it out of context before calling
|
|
10
|
+
this.warn = this.warn.bind(this);
|
|
11
|
+
this.error = this.error.bind(this);
|
|
12
|
+
this.debug = this.debug.bind(this);
|
|
13
|
+
this.deprecate = this.deprecate.bind(this);
|
|
14
|
+
this.deprecate = this.deprecate.bind(this);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
warn(message: any): void {
|
|
18
|
+
this.platformLogger.warn(message);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
error(message: any): void {
|
|
22
|
+
this.platformLogger.error(message);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
debug(message: any): void {
|
|
26
|
+
this.platformLogger.debug(message);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
deprecate(method: string, alternative: string): void {
|
|
30
|
+
this.platformLogger.warn(`deprecate: ${method}, use: ${alternative}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { text, Timespan } from '@rsdk/common';
|
|
2
|
+
import {
|
|
3
|
+
Config,
|
|
4
|
+
ConfigSection,
|
|
5
|
+
IntParser,
|
|
6
|
+
Property,
|
|
7
|
+
StringParser,
|
|
8
|
+
TimespanParser,
|
|
9
|
+
UrlParser,
|
|
10
|
+
} from '@rsdk/core';
|
|
11
|
+
|
|
12
|
+
@ConfigSection()
|
|
13
|
+
export class KnexPluginConfig extends Config {
|
|
14
|
+
@Property('DB_URL', new UrlParser(), {
|
|
15
|
+
description: 'database server url',
|
|
16
|
+
})
|
|
17
|
+
url!: URL;
|
|
18
|
+
|
|
19
|
+
@Property('DB_SCHEMA', new StringParser(), {
|
|
20
|
+
description: text`
|
|
21
|
+
Use schema for all entities.
|
|
22
|
+
Not all databases supports this!
|
|
23
|
+
`,
|
|
24
|
+
defaultValue: '',
|
|
25
|
+
})
|
|
26
|
+
schema!: string;
|
|
27
|
+
|
|
28
|
+
@Property('DB_POOL_MIN', new IntParser(), {
|
|
29
|
+
defaultValue: 1,
|
|
30
|
+
description: text`
|
|
31
|
+
Minimum connection pool size.
|
|
32
|
+
See: https://knexjs.org/guide/#pool
|
|
33
|
+
`,
|
|
34
|
+
})
|
|
35
|
+
poolMin!: number;
|
|
36
|
+
|
|
37
|
+
@Property('DB_POOL_MAX', new IntParser(), {
|
|
38
|
+
defaultValue: 10,
|
|
39
|
+
description: text`
|
|
40
|
+
Maximum connection pool size.
|
|
41
|
+
See: https://knexjs.org/guide/#pool
|
|
42
|
+
`,
|
|
43
|
+
})
|
|
44
|
+
poolMax!: number;
|
|
45
|
+
|
|
46
|
+
@Property('DB_RECONNECT_ATTEMPTS', new IntParser(), {
|
|
47
|
+
defaultValue: Number.POSITIVE_INFINITY,
|
|
48
|
+
description: 'Retry attempt count before give up',
|
|
49
|
+
})
|
|
50
|
+
reconnectAttempts!: number;
|
|
51
|
+
|
|
52
|
+
@Property('DB_RECONNECT_INTERVAL', new TimespanParser(), {
|
|
53
|
+
defaultValue: new Timespan(1, 's'),
|
|
54
|
+
description: 'Delay between retries',
|
|
55
|
+
})
|
|
56
|
+
reconnectInterval!: Timespan;
|
|
57
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { OnApplicationShutdown } from '@nestjs/common';
|
|
2
|
+
import { Injectable } from '@nestjs/common';
|
|
3
|
+
import { AppName, InjectLogger } from '@rsdk/core';
|
|
4
|
+
import { ILogger } from '@rsdk/logging';
|
|
5
|
+
import type { Knex } from 'knex';
|
|
6
|
+
import { knex } from 'knex';
|
|
7
|
+
import { setTimeout } from 'node:timers/promises';
|
|
8
|
+
|
|
9
|
+
import { KnexPluginConfig } from './knex.config';
|
|
10
|
+
import { KnexLoggerAdapter } from './knex-logger.adapter';
|
|
11
|
+
|
|
12
|
+
@Injectable()
|
|
13
|
+
export class KnexFactory implements OnApplicationShutdown {
|
|
14
|
+
private static instance: Knex | null = null;
|
|
15
|
+
|
|
16
|
+
constructor(
|
|
17
|
+
@AppName() private readonly appName: string,
|
|
18
|
+
@InjectLogger(KnexFactory) private readonly logger: ILogger,
|
|
19
|
+
private readonly config: KnexPluginConfig,
|
|
20
|
+
) {}
|
|
21
|
+
|
|
22
|
+
public async create(driver: string): Promise<Knex> {
|
|
23
|
+
if (KnexFactory.instance) {
|
|
24
|
+
return KnexFactory.instance;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const knexInstance = knex({
|
|
28
|
+
client: driver,
|
|
29
|
+
log: new KnexLoggerAdapter(this.logger),
|
|
30
|
+
connection: this.config.url.toString(),
|
|
31
|
+
...(this.config.schema && {
|
|
32
|
+
searchPath: [this.config.schema],
|
|
33
|
+
}),
|
|
34
|
+
pool: {
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
+
afterCreate: async (connection: any, done: any) => {
|
|
37
|
+
const queries = [
|
|
38
|
+
'SET timezone="UTC";',
|
|
39
|
+
`SET application_name='${this.appName}:knex';`,
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
await connection.query(queries.join('\n'));
|
|
43
|
+
done(null, connection);
|
|
44
|
+
},
|
|
45
|
+
max: this.config.poolMax,
|
|
46
|
+
min: this.config.poolMin,
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* This cycle guarantees, that application will not be bootstrapped
|
|
52
|
+
* if database is offline or URL is wrong.
|
|
53
|
+
*
|
|
54
|
+
* NOTE: When application is running and database goes offline "bad"
|
|
55
|
+
* connection will be removed from connection pool. And each subsequent
|
|
56
|
+
* call to database (including 'SELECT 1' from healthcheck system) will
|
|
57
|
+
* actually trigger "reconnection attempt".
|
|
58
|
+
*
|
|
59
|
+
* Healthchecks are infinite, thus we effectively get
|
|
60
|
+
* "infinite reconnect"
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
let attempt = 1;
|
|
64
|
+
|
|
65
|
+
while (attempt < this.config.reconnectAttempts) {
|
|
66
|
+
try {
|
|
67
|
+
await knexInstance.raw('SELECT 1');
|
|
68
|
+
break;
|
|
69
|
+
} catch (error) {
|
|
70
|
+
this.logger.error(`try: ${attempt++} - not connected`, error as Error);
|
|
71
|
+
await setTimeout(this.config.reconnectInterval.millis());
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return knexInstance;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public async onApplicationShutdown(): Promise<void> {
|
|
79
|
+
if (KnexFactory.instance) {
|
|
80
|
+
await KnexFactory.instance.destroy();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
|
2
|
+
import type { HealthIndicator } from '@rsdk/core';
|
|
3
|
+
import { CheckResult, Indicator } from '@rsdk/core';
|
|
4
|
+
import { Knex } from 'knex';
|
|
5
|
+
|
|
6
|
+
import { InjectKnex } from '../inject-knex.decorator';
|
|
7
|
+
|
|
8
|
+
@Injectable()
|
|
9
|
+
@Indicator('knex')
|
|
10
|
+
export class KnexIndicator implements HealthIndicator {
|
|
11
|
+
constructor(@InjectKnex() private readonly knex: Knex) {}
|
|
12
|
+
|
|
13
|
+
public async check(): Promise<CheckResult> {
|
|
14
|
+
try {
|
|
15
|
+
await this.knex.raw('SELECT 1');
|
|
16
|
+
|
|
17
|
+
return CheckResult.up({});
|
|
18
|
+
} catch (error: unknown) {
|
|
19
|
+
return CheckResult.down({ error: (error as Error).message });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface KnexPluginOptions {
|
|
2
|
+
driver: string;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export enum IsolationLevel {
|
|
6
|
+
READ_UNCOMMITTED = 'read uncommitted',
|
|
7
|
+
READ_COMMITTED = 'read committed',
|
|
8
|
+
SNAPSHOT = 'snapshot',
|
|
9
|
+
REPEATABLE_READ = 'repeatable read',
|
|
10
|
+
SERIALIZABLE = 'serializable',
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const isolationLevels: readonly IsolationLevel[] = [
|
|
14
|
+
IsolationLevel.READ_UNCOMMITTED,
|
|
15
|
+
IsolationLevel.READ_COMMITTED,
|
|
16
|
+
IsolationLevel.SNAPSHOT,
|
|
17
|
+
IsolationLevel.REPEATABLE_READ,
|
|
18
|
+
IsolationLevel.SERIALIZABLE,
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
export const DEFAULT_ISOLATION_LEVEL = IsolationLevel.READ_UNCOMMITTED;
|
package/tsconfig.json
ADDED