NestJS Application

Enterprise-grade TypeScript application with NestJS and NexaDB.

Project Setup

Terminal
# Create NestJS app
npx @nestjs/cli new nexadb-app
cd nexadb-app

# Install NexaDB client
npm install nexaclient

Database Module

database.module.ts

src/database/database.module.ts
import { Module, Global } from '@nestjs/common';
import { DatabaseService } from './database.service';

@Global()
@Module({
  providers: [DatabaseService],
  exports: [DatabaseService],
})
export class DatabaseModule {}

database.service.ts

src/database/database.service.ts
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
import NexaClient from 'nexaclient';

@Injectable()
export class DatabaseService implements OnModuleInit, OnModuleDestroy {
  private client: NexaClient;

  async onModuleInit() {
    this.client = new NexaClient({
      host: process.env.NEXA_HOST || 'localhost',
      port: parseInt(process.env.NEXA_PORT) || 6970,
      username: 'root',
      password: 'nexadb123'
    });
    await this.client.connect();
    console.log('✓ Connected to NexaDB');
  }

  async onModuleDestroy() {
    await this.client.disconnect();
  }

  getClient(): NexaClient {
    return this.client;
  }
}

Users Service

users.service.ts

src/users/users.service.ts
import { Injectable } from '@nestjs/common';
import { DatabaseService } from '../database/database.service';

@Injectable()
export class UsersService {
  constructor(private readonly dbService: DatabaseService) {}

  async create(userData: any) {
    const db = this.dbService.getClient();
    return db.create('users', userData);
  }

  async findOne(id: string) {
    const db = this.dbService.getClient();
    return db.get('users', id);
  }

  async findAll(limit = 10) {
    const db = this.dbService.getClient();
    return db.query('users', {}, { limit });
  }

  async update(id: string, userData: any) {
    const db = this.dbService.getClient();
    return db.update('users', id, userData);
  }

  async remove(id: string) {
    const db = this.dbService.getClient();
    return db.delete('users', id);
  }
}

Enterprise Ready!

NestJS provides dependency injection, decorators, and modular architecture perfect for large-scale applications.