NestJS Dynamoose Integration

0.6.0 · active · verified Sun Apr 19

nestjs-dynamoose is a module designed to seamlessly integrate the Dynamoose ODM (Object Document Mapper) for DynamoDB into NestJS applications. It provides NestJS-specific modules and decorators that leverage NestJS's robust dependency injection system, allowing developers to define DynamoDB schemas and models in a structured, modular way. The current stable version is 0.6.0, with releases occurring frequently to maintain compatibility with new versions of NestJS and Dynamoose. Key differentiators include its adherence to NestJS architectural patterns (e.g., `forRoot`, `forFeature`), simplifying the setup and management of DynamoDB connections and models compared to integrating Dynamoose directly without the NestJS wrapper, and enabling easy configuration of AWS credentials and local DynamoDB instances.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure Dynamoose at the root of a NestJS application, define a Dynamoose schema, register it with a feature module, and inject the Dynamoose model into a service for data operations.

import { Module } from '@nestjs/common';
import { DynamooseModule } from 'nestjs-dynamoose';
import { Schema } from 'dynamoose';

// user/user.schema.ts
export const UserSchema = new Schema({
  id: {
    type: String,
    hashKey: true
  },
  name: {
    type: String
  },
  email: {
    type: String
  }
});

// user/user.service.ts (simplified)
import { Injectable } from '@nestjs/common';
import { Model } from 'dynamoose';
import { InjectModel } from 'nestjs-dynamoose';

interface UserKey { id: string; }
interface User extends UserKey { name: string; email?: string; }

@Injectable()
export class UserService {
  constructor(@InjectModel('User') private userModel: Model<User, UserKey>) {}

  async create(user: User): Promise<User> {
    return this.userModel.create(user);
  }
}

// user/user.module.ts
@Module({
  imports: [
    DynamooseModule.forFeature([{
      name: 'User',
      schema: UserSchema,
      options: {
        tableName: 'user-table-name' // Explicit table name
      }
    }])
  ],
  providers: [UserService],
  exports: [UserService]
})
export class UserModule {}

// app.module.ts
@Module({
  imports: [
    DynamooseModule.forRoot({
      aws: {
        region: process.env.AWS_REGION ?? 'us-east-1',
        accessKeyId: process.env.AWS_ACCESS_KEY_ID ?? '',
        secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY ?? ''
      },
      local: process.env.DYNAMODB_ENDPOINT ?? false // Set to true or 'http://localhost:8000' for local DynamoDB
    }),
    UserModule
  ]
})
export class AppModule {}

view raw JSON →