Skip to content

Commit 9e8a50b

Browse files
committed
chore: publish 0.2.3
1 parent c313b06 commit 9e8a50b

File tree

7 files changed

+48
-18
lines changed

7 files changed

+48
-18
lines changed

lib/express-casandra-core.module.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { DynamicModule, Module, Global, Provider } from '@nestjs/common';
1+
import {
2+
DynamicModule,
3+
Module,
4+
Global,
5+
Provider,
6+
OnModuleDestroy,
7+
Inject,
8+
Logger,
9+
} from '@nestjs/common';
10+
import { ModuleRef } from '@nestjs/core';
211
import {
312
ExpressCassandraModuleOptions,
413
ExpressCassandraModuleAsyncOptions,
@@ -7,10 +16,18 @@ import {
716
import { EXPRESS_CASSANDRA_MODULE_OPTIONS } from './express-cassandra.constant';
817
import { getConnectionToken, handleRetry } from './utils/cassandra-orm.utils';
918
import { createClient } from 'express-cassandra';
19+
import { defer } from 'rxjs';
20+
import { map } from 'rxjs/operators';
1021

1122
@Global()
1223
@Module({})
13-
export class ExpressCassandraCoreModule {
24+
export class ExpressCassandraCoreModule implements OnModuleDestroy {
25+
constructor(
26+
@Inject(EXPRESS_CASSANDRA_MODULE_OPTIONS)
27+
private readonly options: ExpressCassandraModuleOptions,
28+
private readonly moduleRef: ModuleRef,
29+
) {}
30+
1431
static forRoot(options: ExpressCassandraModuleOptions = {}): DynamicModule {
1532
const expressModuleOptions = {
1633
provide: EXPRESS_CASSANDRA_MODULE_OPTIONS,
@@ -45,6 +62,15 @@ export class ExpressCassandraCoreModule {
4562
};
4663
}
4764

65+
async onModuleDestroy() {
66+
if (this.options.keepConnectionAlive) {
67+
return;
68+
}
69+
Logger.log('Closing connection', 'ExpressCassandraModule');
70+
const connection = this.moduleRef.get(getConnectionToken(this.options));
71+
connection && (await connection.closeAsync());
72+
}
73+
4874
private static createAsyncProviders(
4975
options: ExpressCassandraModuleAsyncOptions,
5076
): Provider[] {
@@ -81,7 +107,13 @@ export class ExpressCassandraCoreModule {
81107
private static async createConnectionFactory(
82108
options: ExpressCassandraModuleOptions,
83109
): Promise<any> {
84-
const { name, retryAttempts, retryDelay, ...cassandraOptions } = options;
85-
return await createClient(cassandraOptions);
110+
const { retryAttempts, retryDelay, ...cassandraOptions } = options;
111+
const client = await createClient(cassandraOptions);
112+
return await defer(() => client.initAsync())
113+
.pipe(
114+
handleRetry(retryAttempts, retryDelay),
115+
map(() => client),
116+
)
117+
.toPromise();
86118
}
87119
}

lib/express-cassandra.providers.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
getModelToken,
33
getConnectionToken,
4-
handleRetry,
54
getRepositoryToken,
65
} from './utils/cassandra-orm.utils';
76
import { EXPRESS_CASSANDRA_MODULE_OPTIONS } from './express-cassandra.constant';
@@ -16,9 +15,7 @@ export function createExpressCassandraProviders(
1615
const providerModel = entity => ({
1716
provide: getModelToken(entity),
1817
useFactory: async (client: any, options) => {
19-
return await defer(() => loadModel(client, entity))
20-
.pipe(handleRetry(options.retryAttempts, options.retryDelay))
21-
.toPromise();
18+
return await defer(() => loadModel(client, entity)).toPromise();
2219
},
2320
inject: [getConnectionToken(connection), EXPRESS_CASSANDRA_MODULE_OPTIONS],
2421
});

lib/interfaces/express-cassandra-module-options.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export type ExpressCassandraModuleOptions = {
66
name?: string;
77
retryAttempts?: number;
88
retryDelay?: number;
9+
keepConnectionAlive?: boolean;
910
} & Partial<ClientOptionsStatic>;
1011

1112
export interface ExpressCassandraOptionsFactory {

lib/orm/interfaces/column-options.interface.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ import { ColumnType, DataType } from './data.type';
33
export interface ColumnOptions {
44
type?: ColumnType | DataType;
55
typeDef?: string;
6-
default?: string | (() => any) | { $db_function: dbFunctionType };
6+
default?: string | (() => any) | { $db_function: string };
77
virtual?: { get?: any; set?: any };
88
rule?: ColumnRuleOptions | ((value: any) => boolean);
99
}
1010

11-
export type dbFunctionType = 'uuid()' | 'timeuuid()';
12-
1311
export interface ColumnRuleOptions {
1412
validator?: (value: any) => boolean;
1513
validators?: any[];

lib/orm/utils/model.utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { getAttributes, getOptions } from './decorator.utils';
2+
import { Logger } from '@nestjs/common';
23

34
export function loadModel(client, entity): Promise<any> {
45
const schema = getSchema(entity);
56
const modelName = entity.name || entity.table_name;
67
const model = client.loadSchema(modelName, schema);
7-
return new Promise((resolve, reject) => {
8+
return new Promise(resolve => {
89
model.syncDB(err => {
910
if (err) {
10-
return reject(err);
11+
Logger.error(err.message, err.stack, 'ExpressCassandraModule');
12+
return resolve(model);
1113
}
1214
return resolve(model);
1315
});

lib/utils/cassandra-orm.utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function handleRetry(
1414
Logger.error(
1515
`Unable to connect to the database. Retrying (${errorCount +
1616
1})...`,
17-
error.message,
17+
error.stack,
1818
'ExpressCassandraModule',
1919
);
2020
if (errorCount + 1 >= retryAttempts) {
@@ -39,8 +39,8 @@ export function getConnectionToken(
3939
return 'string' === typeof connection
4040
? `${connection}Connection`
4141
: connection.name
42-
? `${connection.name}Connection`
43-
: 'defaultConnection';
42+
? `${connection.name}Connection`
43+
: 'defaultConnection';
4444
}
4545

4646
/**

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@iaminfinity/express-cassandra",
3-
"version": "0.2.2",
3+
"version": "0.2.3",
44
"description": "Nest - modern, fast, powerful node.js web framework (@express-cassandra)",
55
"author": {
66
"name": "Fahim Rahman",
@@ -62,4 +62,4 @@
6262
"engines": {
6363
"node": ">=4.2.4"
6464
}
65-
}
65+
}

0 commit comments

Comments
 (0)