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-qldb

v3.1.0

Published

NestJS ODM for Quantum Ledger Database QLDB

Downloads

387

Readme

Features

  • Extends the amazon-qldb-driver-nodejs work to add ODM functionality for QLDB common in NestJS.
  • A simple dependency injection model with NestQldbModule.forRoot(), NestQldbModule.forRootAsync().
  • Simple auto-generated CRUD repositories for models, injectable by @InjectRepository(model).
  • Provides a QldbQueryService provider for a simple Dapper-inspired query layer.

How To Use

Install

npm install --save nest-qldb

Importing

NestQldbModule.forRoot()

NestQldbModule.forRoot() is the simplest way to import the QLDB module and autowire @QldbTable decorators. We re-export the QldbDriver from amazon-qldb-driver-nodejs. Note: It is necessary that the ledger be created before application initialization.

// app.module.ts

import { NestQldbModule, QldbDriver } from 'nest-qldb';
import { User } from './user.model.ts';

@Module({
  imports: [
    NestQldbModule.forRoot({
      qldbDriver: new QldbDriver('fake-ledger')
      createTablesAndIndexes: true,
      tables: [User],
    }),
  ],
})
class AppRootModule {}

driver

qldbDriver is the driver to manage connections to Amazon's Quantum Ledger Database.

NestQldbModule.forRootAsync()

NestQldbModule.forRootAsync() allows for a FactoryProvider or ValueProvider dependency declaration to import our module. Note that ExistingProvider and ClassProvider are not yet supported.

// app.module.ts
import { NestQldbModule, QldbDriver } from 'nest-qldb';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { User } from './user.model.ts';

@Module({
  imports: [
    NestQldbModule.forRootAsync({
      qldbDriver: {
        useFactory: async (configService: ConfigService) => {
          return new QldbDriver(configService.qldbLedgerName, {
            credentials: fromIni({
              profile: config.qldbProfileName,
            }),
          });
        },
        inject: [ConfigService],
      },
      createTablesAndIndexes: true,
      tables: [User],
    }),
    ConfigModule.forRoot(),
  ],
})
class AppRootModule {}

Registering Models Binds them to QLDB tables

Models

Model classes are the shape of the data in your QLDB tables. The @QldbTable() decorator is used to register your model as a table in Quantum Ledger Database. You can also define the tableName and indexes associated with that model. NOTE: You can only create index when tables are empty so in the module config. There is config on the module to perform this action at startup createTablesAndIndexes, it gracefully handles tables/indexes already created, however, it'll cost you a few seconds on a cold start so it is advised to turn it off in a production serverless scenario. NOTE: Any id properties will be peeled off models before they are saved! id is reserved for the qldb documentid

With decorator

// user.model.ts
import { QldbTable } from 'nest-qldb';

@QldbTable({
  tableName: 'users',
  tableIndexes: ['name', 'phoneNumber'],
})
class User {
  name: string;
  dateOfBirth: Date;
  email: string;
  phoneNumber: string;
}
tableName

This is the name of the collection stored in QLDB.

tableIndexes

These indexes will be created upon table creation. You cannot create indexes after records are inserted, so be aware.

useMetadataKey

Default: true

This declares whether the key field should be a value in the QLDB metadata object (true), or a part of the data object (false).

keyField

Default: id

This declares the field that will be used as the key.

Repository injection

Repositories can be injected using the @InjectRepository() decorator. The repositories are created by nest-qldb and are a simple CRUD interface handy for quickly creating REST controllers.

  • query(query QldbQuery) - Performs Partiql query against table.
  • create(data: T) - Adds an object to the QLDB table
  • retrieve(id: string) - Fetches a single document by it's QLDB assigned id.
  • replace(id: string, data: T) - Replaces an entire document based on the id.
  • destroy(id: string) - Deletes an object from the table but not its change history from the ledger.
  • history(id: string) - Fetches all versions of a document across history
  • createMany(data: T[]) - Will save many records in transactional batches of 40.
@Controller()
class UserController {
  constructor(
    @InjectRepository(User) readonly usersRepository: Repository<User>,
  ) {}

  @Get(':id')
  async getById(id: string) {
    return await this.usersRepository.retrieve(id);
  }
}

QldbQueryService

The QldbQueryService provider exposes simple methods that query and map the object to plain JSON objects.

  • query - Performs a query returning a list of rows mapped to a plain JSON object.
  • querySingle - Performs a query returning a single row mapped to a plain JSON object.
  • queryForSubdocument - Performs a query returning a list of nested documents in rows mapped to a plain JSON object.
  • querySingleForSubdocument - Performs a query returning a nested documents in single row mapped to a plain JSON object.
  • execute - Performs a query that returns no results. Ideal for inserts, updates, and deletes.
import { Injectable } from '@nestjs/common';
import { QldbQueryService } from 'nest-qldb';
import { SearchResult } from './search.interfaces';

@Injectable()
class UserService {
  constructor(private readonly queryService: QldbQueryService) {}

  async searchUsers(searchQuery: string) {
    return await this.queryService.query<SearchResult>(
      `
      SELECT
        id as userId,
        u.name as userName,
        u.email as userEmail
      FROM users AS u BY id
      WHERE LOWER(u.name) LIKE ?
        OR LOWER(u.email) LIKE ?
      `,
      searchQuery?.toLocaleLowerCase(),
    );
  }
}

Stay In Touch

License

nest-qldb is MIT licensed.

Several core NestDependencies moved to peer dependencies to shrink package size.