Giter Club home page Giter Club logo

nest-onetable's Introduction

@OneTable

Welcome to the Nest-Onetable library! This library provides a convenient way to integrate the power of DynamoDB OneTable with NestJS applications. It simplifies the process of creating and working with DynamoDB tables using the OneTable library within your NestJS modules.

Table of Contents

Installation

To start using the Nest-Onetable library in your NestJS project, follow these steps:

  1. Install the package using npm or yarn:
npm install nest-onetable

or

yarn add nest-onetable

Usage

Module Registration

To get started, you need to register the nest-onetable module in your NestJS application. Here's how you can do it:

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

@Module({
  imports: [
    OnetableModule.register({
      // here you can pass either the configuration object you would pass to the new Table() constructor directly or 
      // a factory provider that returns the configuration object but with the benefits of dependency injection
      useFactory(config: ConfigService, client: DynamoDBClient) {
        return {
          client,
          global: false, // optional, defaults to true
          name: config.get('ONETABLE_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 {}

Now you can inject the Table instance into your services (but you don't need to do that, keep reading!):

import { Injectable } from '@nestjs/common';
import { Table } from 'dynamodb-onetable';
import { OneTable } from 'nest-onetable';

@Injectable()
class UsersService {
  constructor(@OneTable() public readonly table: Table) {
  }
}

Creating a Model

The boring part of creating a model is to have the Table instance to pass to the Model constructor. But the nest-onetable OneModel class factory simplifies the process of creating OneTable Models. See below:

import { Injectable } from '@nestjs/common';
import { OneModel } from 'nest-onetable';

@Injectable()
export class UsersModel extends OneModel('Users', {
  pk: { type: String, value: 'Users#', hidden: true },
  sk: { type: String, value: 'Users#${id}', hidden: true },
  id: { type: String, generate: 'ulid', required: true },
  name: { type: String, required: true }
}) {}

@Injectable()
class UsersService {
  constructor(public readonly model: UsersModel) {
  }

  getUsers() {
    return this.model.query(); // thanks to OneTable type inferences from the schema you get a typed object here
  }
}

Under the Hood

The OneModel class factory is a wrapper around the Model class constructor. It creates a new class that extends the Model and already injects the Table instance into the constructor facilitated by Nest.js the dependency injection.

See the code

Contributing

Contributions are welcome! If you find a bug or have a feature request, please open an issue on the GitHub repository.

License

This project is licensed under the MIT License.


We hope you find the nest-onetable library helpful for your NestJS applications. If you have any questions or need assistance, feel free to reach out to us on the GitHub repository. Happy coding! ๐Ÿš€

nest-onetable's People

Contributors

onhate avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

nest-onetable's Issues

Typing error in generated ".d.ts" preventing usage

Without any issue guidelines or template I'll try to write this issue as well as I can :)

When importing library into the project, the .d.ts files show following errors:

Type 'K' does not satisfy the constraint 'keyof Required<T> | keyof OptionalOrUndefined<T>'.
Type 'keyof T' is not assignable to type 'keyof Required<T> | keyof OptionalOrUndefined<T>'.
Type 'string | number | symbol' is not assignable to type 'keyof Required<T> | keyof OptionalOrUndefined<T>'. Type 'string' is not assignable to type 'keyof Required<T> | keyof OptionalOrUndefined<T>'.

all of those come from decorators.d.ts

This could obviously be skipped with TypeScript's skipLibCheck but it remains a problem to be fixed.
To replicate, simply create a new nest project, add the nest-onetable library and disable skipLibCheck.

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.