Git Product home page Git Product logo

nestjs-dynamo-onetable's Introduction

nestjs-dynamo-onetable

Start by installing the required dependencies:

  $ npm i nestjs-dynamo-onetable

Once the installation process is complete, we can import the OnetableModule into the root AppModule.

(app.module.ts)

import { Module } from '@nestjs/common';
import { OnetableModule } from 'nestjs-dynamo-onetable';
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';

@Module({
  imports: [
      OnetableModule.forRoot({
        useFactory(config: ConfigService, client: DynamoDBClient) {
            return {
            client,
            global: false, // defaults to true
            name: config.getOrThrow('TABLE_NAME'),
            partial: true,
            schema: {
                format: 'onetable:1.1.0',
                version: '0.0.1',
                indexes: {
                primary: { hash: 'pk', sort: 'sk' },
                },
                models: {},
            },
            };
        },
        inject: [ConfigService, DynamoDBClient],
    })
  ],
})
export class AppModule {}

The forRoot() method accepts the same configuration object as dynamodb-onetable

Model injection

Define your schema object table:

(UserSchema.ts)

export const UserSchema = {
    pk: {type: String, value: 'account:${id}'},
    sk: {type: String, value: 'account:'},
    id: {type: String, generate: 'ulid', validate: /^[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}$/i},
    username: {type: String, required: true},
    email: {type: String, required: true},
}

Import schema into your module

(UserModule.ts)

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

Inject model into service or repository

(UserService.ts)
import { Model } from 'nestjs-dynamo-onetable';

@Injectable()
export class UserService {
  constructor(@InjectModel('User') private readonly userModel: Model<typeof UserSchema>) {}

  getUser() {
    this.userModel.find({ pk: 'pk01', sk: 'sk01' });
  }
}

It able to use with repository then inject your repository into service.

nestjs-dynamo-onetable's People

Stargazers

 avatar  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.