All Projects → hardyscc → nestjs-dynamoose

hardyscc / nestjs-dynamoose

Licence: MIT license
Dynamoose module for Nest

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to nestjs-dynamoose

aws-nestjs-starter
Serverless, AWS, NestJS, GraphQL and DynamoDB starter
Stars: ✭ 200 (+138.1%)
Mutual labels:  dynamodb, nest, dynamoose, nestjs
bundled-nest
💥 Nest 🔰 Webpack 🔰 Docker 💥 --- 🏯 Now archived for historical reference ⛩
Stars: ✭ 59 (-29.76%)
Mutual labels:  nest, nestjs
nestjs-asyncapi
NestJS AsyncAPI module - generate the documentation of your event-based services using decorators
Stars: ✭ 88 (+4.76%)
Mutual labels:  nest, nestjs
unnue-nuxt
开媛笔记,基于nuxt ssr首屏服务器端渲染 ⚡。用于分享、记录、交流和学习,希望可以帮助到小伙伴们。同时网站在永久更新,备好鸡血,一起来战 Ooh aah!
Stars: ✭ 98 (+16.67%)
Mutual labels:  nest, nestjs
azure-func-http
Azure Functions HTTP adapter for Nest framework (node.js) 🌥
Stars: ✭ 121 (+44.05%)
Mutual labels:  nest, nestjs
prime-nestjs
A production-ready NestJS boilerplate using Typescript, Postgres, TypeORM, and Docker.
Stars: ✭ 140 (+66.67%)
Mutual labels:  nest, nestjs
nestjs-ioredis
IORedis module for Nest
Stars: ✭ 21 (-75%)
Mutual labels:  nest, nestjs
twitch-project
A weekly stream in which I build a web application with Neo4j and Typescript
Stars: ✭ 78 (-7.14%)
Mutual labels:  nest, nestjs
typegraphql-nestjs
TypeGraphQL integration with NestJS
Stars: ✭ 117 (+39.29%)
Mutual labels:  nest, nestjs
nest-xray
Distributed tracing for Nestjs with AWS X-Ray as the backend. Instrument incoming and outgoing HTTP requests
Stars: ✭ 50 (-40.48%)
Mutual labels:  nest, nestjs
nestjs-session
Idiomatic Session Module for NestJS. Built on top of `express-session` 😎
Stars: ✭ 150 (+78.57%)
Mutual labels:  nest, nestjs
nestjs-telegraf
🤖 Powerful Nest module for easy and fast creation Telegram bots
Stars: ✭ 300 (+257.14%)
Mutual labels:  nest, nestjs
nestjs-objection
Objection module for NestJS
Stars: ✭ 24 (-71.43%)
Mutual labels:  nest, nestjs
azure-serverless-deprecated
[Deprecated] Azure Serverless module for Nest framework (node.js) 🌩
Stars: ✭ 44 (-47.62%)
Mutual labels:  nest, nestjs
nest-queue
Queue manager for NestJS Framework for Redis (via bull package)
Stars: ✭ 69 (-17.86%)
Mutual labels:  nest, nestjs
nestjs-starter
🚀 Nest framework starter
Stars: ✭ 30 (-64.29%)
Mutual labels:  nest, nestjs
angular-chat
Angular v.9, Node.js, Nest.js v.6, Mongoose, Socket.io, Passport, Angular Universal SSR (in progress...)
Stars: ✭ 35 (-58.33%)
Mutual labels:  nest, nestjs
MyAPI
A template to create awesome APIs easily ⚡️
Stars: ✭ 117 (+39.29%)
Mutual labels:  nest, nestjs
nestjs-ratelimiter
Distributed consistent flexible NestJS rate limiter based on Redis
Stars: ✭ 49 (-41.67%)
Mutual labels:  nest, nestjs
ogma
A monorepo for the ogma logger and related packages
Stars: ✭ 201 (+139.29%)
Mutual labels:  nest, nestjs

Nest Logo

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

NPM Version Package License NPM Downloads CI

Description

Dynamoose module for Nest.

Installation

$ npm install --save nestjs-dynamoose dynamoose

Example Project

A AWS NestJS Starter project has been created to demo the usage of this library.

Quick Start

1. Add import into your app module

src/app.module.ts

import { DynamooseModule } from 'nestjs-dynamoose';
import { UserModule } from './user/user.module';

@Module({
 imports: [
   DynamooseModule.forRoot(),
   UserModule,
 ],
})
export class AppModule {

forRoot() optionally accepts the following options defined by DynamooseModuleOptions:

interface DynamooseModuleOptions {
  aws?: {
    accessKeyId?: string;
    secretAccessKey?: string;
    region?: string;
  };
  local?: boolean | string;
  ddb?: DynamoDB;
  table?: TableOptionsOptional;
  logger?: boolean | LoggerService;
}

There is also forRootAsync(options: DynamooseModuleAsyncOptions) if you want to use a factory with dependency injection.

2. Create a schema

src/user/user.schema.ts

import { Schema } from 'dynamoose';

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

src/user/user.interface.ts

export interface UserKey {
  id: string;
}

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

UserKey holds the hashKey/partition key and (optionally) the rangeKey/sort key. User holds all attributes of the document/item. When creating this two interfaces and using when injecting your model you will have typechecking when using operations like Model.update().

3. Add the models you want to inject to your modules

This can be a feature module (as shown below) or within the root AppModule next to DynamooseModule.forRoot().

src/user/user.module.ts

import { DynamooseModule } from 'nestjs-dynamoose';
import { UserSchema } from './user.schema';
import { UserService } from './user.service';

@Module({
  imports: [
    DynamooseModule.forFeature([{ name: 'User', schema: UserSchema }]),
  ],
  providers: [
    UserService,
    ...
  ],
})
export class UserModule {}

There is also forFeatureAsync(factories?: AsyncModelFactory[]) if you want to use a factory with dependency injection.

4. Inject and use your model

src/user/user.service.ts

import { Injectable } from '@nestjs/common';
import { InjectModel, Model } from 'nestjs-dynamoose';
import { User, UserKey } from './user.interface';

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

  create(user: User) {
    return this.userModel.create(user);
  }

  update(key: UserKey, user: Partial<User>) {
    return this.userModel.update(key, user);
  }

  findOne(key: UserKey) {
    return this.userModel.get(key);
  }

  findAll() {
    return this.userModel.scan().exec();
  }
}

Additional Example

1. Transaction Support

Both User and Account model objects will commit in same transaction.

import { Injectable } from '@nestjs/common';
import { InjectModel, Model, TransactionSupport } from 'nestjs-dynamoose';
import { User, UserKey } from './user.interface';
import { Account, AccountKey } from './account.interface';

@Injectable()
export class UserService extends TransactionSupport {
  constructor(
    @InjectModel('User')
    private userModel: Model<User, UserKey>,
    @InjectModel('Account')
    private accountModel: Model<Account, AccountKey>,
  ) {
    super();
  }

  async create(user: User, account: Account) {
    await this.transaction([
      this.userModel.transaction.create(user),
      this.accountModel.transaction.create(account),
    ]);
  }
}

2. Serializers Support

Define the additional serializers under DynamooseModule.forFeature().

@Module({
  imports: [
    DynamooseModule.forFeature([
      {
        name: 'User',
        schema: UserSchema,
        serializers: {
          frontend: { exclude: ['status'] },
        },
      },
    ]),
  ],
  ...
})
export class UserModule {}

Call the serialize function to exclude the status field.

@Injectable()
export class UserService {
  ...
  async create(user: User) {
    const createdUser = await this.userModel.create(user);
    return createdUser.serialize('frontend');
  }
  ...
}

License

Dynamoose module for Nest is MIT licensed.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].