Git Product home page Git Product logo

cassandra-driver-adapter's Introduction

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

Description

Cassandra Driver Adapter utilities module for NestJS based on the cassandra-driver package.

Installation

$ npm i --save @vlxdisluv/cassandra-driver-adapter
$ yarn add @vlxdisluv/cassandra-driver-adapter

Usage

Import CassandraDriverAdapterModule:

@Module({
  imports: [
    CassandraDriverAdapterModule.forRootAsync({...})
  ],
  providers: [...]
})
export class AppModule {}

Async options

Quite often you might want to asynchronously pass your module options instead of passing them beforehand. In such case, use registerAsync() method, that provides a couple of various ways to deal with async data.

1. Use factory

CassandraDriverAdapterModule.forRootAsync({
  useFactory: () => ({...}),
})

Obviously, our factory behaves like every other one (might be async and is able to inject dependencies through inject).

CassandraDriverAdapterModule.forRootAsync({
  imports: [ConfigModule],
  useFactory: (configService: ConfigService) => configService.getDbConfig(),
  inject: [ConfigService],
});

2. Use class

CassandraDriverAdapterModule.forRootAsync({
  useClass: ConfigService,
});

Above construction will instantiate ConfigService inside CassandraDriverAdapterModule and will leverage it to create options object.

class ConfigService implements CassandraDriverAdapterOptionsFactory {
  createCassandraDriverAdapterOptions(): CassandraDriverAdapterModuleOptions {
    return {...};
  }
}

3. Use existing

CassandraDriverAdapterModule.forRootAsync({
  imports: [ConfigModule],
  useExisting: ConfigService,
});

It works the same as useClass with one critical difference - CassandraDriverAdapterModule will lookup imported modules to reuse already created ConfigService, instead of instantiating it on its own.

ORM Options

import { Entity } from '@vlxdisluv/cassandra-driver-adapter/lib/orm';

@Entity({
  tableName: 'messages',
})
export class Message {
  id: any;
  text: string;
}

Let's have a look at the MessagesModule

import { Module } from '@nestjs/common';
import { CassandraDriverAdapterModule } from '@vlxdisluv/cassandra-driver-adapter';
import { MessagesService } from './messages.service';
import { MessagesController } from './messages.controller';
import { Message } from './message.entity';

@Module({
  imports: [CassandraDriverAdapterModule.forFeature([Message])],
  providers: [MessagesService],
  controllers: [MessagesController],
})
export class MessagesModule {}

Using Repository

import { Module } from '@nestjs/common';
import { CassandraDriverAdapterModule } from '@vlxdisluv/cassandra-driver-adapter';
import { MessagesService } from './messages.service';
import { MessagesController } from './messages.controller';
import { Message } from './message.entity';

@Module({
  imports: [CassandraDriverAdapterModule.forFeature([Message])],
  providers: [MessagesService],
  controllers: [MessagesController],
})
export class MessagesModule {}
import { Injectable } from '@nestjs/common';
import {
  InjectRepository,
  Repository,
} from '@vlxdisluv/cassandra-driver-adapter';
import { Message } from './message.entity';
import { Observable } from 'rxjs';

@Injectable()
export class PersonService {
  constructor(
    @InjectRepository(Message)
    private readonly messagesRepository: Repository<Message>,
  ) {}

  getById(id: id): Promise<PhotoEntity> {
    return this.messagesRepository.findOne({ id });
  }
}

Using Custom Repository

Let's create a repository:

import {
  Repository,
  EntityRepository,
} from '@vlxdisluv/cassandra-driver-adapter';
import { Message } from './message.entity';
import { Observable } from 'rxjs';

@EntityRepository(Message)
export class MessagesRepository extends Repository<Message> {
  findById(id: any): Message {
    return this.findOne({ id: id });
  }
}

Stay in touch

cassandra-driver-adapter's People

Contributors

vlxdisluv avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.