npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nest-onetable

v0.0.5

Published

Welcome to the **Nest-Onetable** library! This library provides a convenient way to integrate the power of [DynamoDB OneTable](https://github.com/sensedeep/dynamodb-onetable) with [NestJS](https://nestjs.com/) applications. It simplifies the process of cr

Downloads

452

Readme

@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! 🚀