Skip to content
This repository has been archived by the owner on Apr 3, 2022. It is now read-only.

Nest.js-Astra Module for Eddiehub-Project

License

Notifications You must be signed in to change notification settings

Cahllagerfeld/nestjs-astra-module

Repository files navigation

nestjs-astra-module

Description

Nest.js-Astra Module for EddieHub API Project. It uses @astrajs/collections to connect to Datastax AstraDB (free cloud hosting), or a local Stargate container.

Rules

Commits follow the standard Conventional Commits

Setup

The module is only available via Github. Thats why you have to create a .npmrc-File in your project and configure it.

$ npm install

Usage

The module is imported twice:

  1. The Core-Module sets up the connection to DataStax Astra / LocalStargate using forRoot or forRootAsync- Function
  2. The forFeature-Function sets collection and namespace in the specific module where the database should be used

Initialization Core

The Core-Module can be initialized synchronous and asynchronous using forRoot or forRootAsync. This part of the module sets up the actual connection to the service. That's why it only needs to be configured once - Preferable in the app.module. Depending on the use-case (connection to local stargate or astra), different configurations need to be passed in.

Some example implementations can be found in the docs folder

Feature-Modules

By developing different module within your nest-Application you need to store different data in different collections. The forFeature-function enables you to do this. It can be imported to specific feature-modules, but relies on the Core-Module.

Usage in Feature-Module

The following snippet shows how the forFeature-function is implemented. In this case the initialization between local stargate and astra dont differ.

/*users.module.ts*/

import { Module } from '@nestjs/common';
import { AstraModule } from '@cahllagerfeld/nestjs-astra';
import { usersController } from './users.controller';
import { usersService } from './users.service';

@Module({
  imports: [
    AstraModule.forFeature({ collection: 'users', namespace: 'Eddiehub' }),
  ],
  controllers: [usersController],
  providers: [usersService],
})
export class usersModule {}

Astra Service

The Astra-Service is a service, which can be ejected like every other service. The Service provides the actual functions for performing actions with local stargate or Astra The Services is observable-based.

//user.service.ts

import { AstraService, documentId } from '@cahllagerfeld/nestjs-astra';
import { Injectable } from '@nestjs/common';
import { Observable } from 'rxjs';
import { user } from './user.interface';

@Injectable()
export class UserService {
  constructor(private readonly astra: AstraService) {}
  addUser(data: user): Observable<documentId> {
    return this.astra.create<user>(data);
  }
}

License

MIT licensed.