Package Exports
- @mich4l/nestjs-cassandra
- @mich4l/nestjs-cassandra/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@mich4l/nestjs-cassandra) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Nest.js Cassandra module
Cassandra module based on npm library cassandra-driver.
Features:
- Simple codebase
- Graceful shutdown
- Multiple connections
- Simple lifecycle hooks (
onReady,beforeShutdown)
Installation
npm
npm install @mich4l/nestjs-cassandra cassandra-driverYarn
yarn add @mich4l/nestjs-cassandra cassandra-driverExamples
app.module.ts
import { Module } from '@nestjs/common';
import { CassandraModule } from 'mich4l/nestjs-cassandra';
@Module({
imports: [
CassandraModule.forRoot({
keyspace: 'my_keyspace',
contactPoints: ['127.0.0.1'],
localDataCenter: 'datacenter1',
}),
controllers: [],
providers: [],
})
export class AppModule {}example.service.ts
import { Inject, Injectable } from '@nestjs/common';
import { InjectCassandra } from '@mich4l/nestjs-cassandra';
import { Client } from 'cassandra-driver';
@Injectable()
export class ExampleService {
constructor(
@InjectCassandra()
private readonly dbClient: Client,
) {}
async getAllPosts() {
const query = 'SELECT * FROM posts';
const result = await this.dbClient.execute(query);
return result.rows[0];
}
}Async register example
@Module({
imports: [
ConfigModule.forRoot({
cache: true,
}),
CassandraModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (config: ConfigService) => {
return {
keyspace: config.get('CASSANDRA_KEYSPACE'),
localDataCenter: config.get('CASSANDRA_DATACENTER'),
contactPoints: ['127.0.0.1'],
}
}
})
],
controllers: [],
providers: [],
})
export class AppModule {}Graceful shutdown
Module closes connection using onApplicationShutdown hook. You may need:
main.ts
...
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
});
app.enableShutdownHooks();
await app.listen(3000);
}
bootstrap();