Skip to content

Commit

Permalink
Merge branch 'nestjs_8' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorobkov committed Feb 19, 2022
2 parents 129b78d + f84d174 commit 06ca8cf
Show file tree
Hide file tree
Showing 5 changed files with 12,989 additions and 3,244 deletions.
4 changes: 2 additions & 2 deletions lib/interfaces/slonik-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Type, ModuleMetadata } from '@nestjs/common';
import { ClientConfigurationInputType } from 'slonik';
import { ClientConfigurationInput } from 'slonik';

export interface SlonikOptions {
connectionUri: string;
clientConfigurationInput?: ClientConfigurationInputType;
clientConfigurationInput?: ClientConfigurationInput;
}

export interface SlonikModuleOptions extends SlonikOptions {
Expand Down
36 changes: 17 additions & 19 deletions lib/slonik-core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
Logger,
} from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { defer } from 'rxjs';
import { createPool, DatabasePoolType } from 'slonik';
import { defer, lastValueFrom } from 'rxjs';
import { createPool, DatabasePool } from 'slonik';
import {
generateString,
getPoolName,
Expand Down Expand Up @@ -84,15 +84,13 @@ export class SlonikCoreModule implements OnApplicationShutdown {
}

async onApplicationShutdown(): Promise<void> {
const pool = this.moduleRef.get<DatabasePoolType>(
getPoolToken(this.options),
);
const pool = this.moduleRef.get<DatabasePool>(getPoolToken(this.options));
try {
// https://github.com/gajus/slonik#end-connection-pool
// The result of pool.end() is a promise that is resolved when all connections are ended.
// Note: pool.end() does not terminate active connections/ transactions.
await pool?.end();
} catch (e) {
} catch (e: any) {
this.logger.error(e?.message);
}
}
Expand Down Expand Up @@ -137,29 +135,29 @@ export class SlonikCoreModule implements OnApplicationShutdown {

private static async createPoolFactory(
options: SlonikModuleOptions,
): Promise<DatabasePoolType> {
): Promise<DatabasePool> {
const poolToken = getPoolName(options);

return defer(async () => {
const pool = await createPool(
options.connectionUri,
options.clientConfigurationInput,
);
return await lastValueFrom(
defer(async () => {
const pool = await createPool(
options.connectionUri,
options.clientConfigurationInput,
);

// try to connect to database to catch errors if database is not reachable
await pool.connect(() => Promise.resolve());
// try to connect to database to catch errors if database is not reachable
await pool.connect(() => Promise.resolve());

return pool;
})
.pipe(
return pool;
}).pipe(
handleRetry(
options.retryAttempts,
options.retryDelay,
poolToken,
options.verboseRetryLog,
options.toRetry,
),
)
.toPromise();
),
);
}
}
Loading

0 comments on commit 06ca8cf

Please sign in to comment.