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

@ezee/dyngoose

v4.11.2

Published

Elegant DynamoDB object modeling for Typescript

Downloads

1,169

Readme

Build Status npm version Semantic Release enabled

Dyngoose

Elegant DynamoDB object modeling for Typescript.

Let's face it, all good databases need good model casting. DynamoDB is powerful but libraries for it were not. That's where Dyngoose comes in.

Getting Started

Take a look the docs! to find information about how to get started.

Features

  1. Cast your tables, attributes, and indexes using TypeScript interfaces.
  2. Generate your CloudFormation template resources, CDK constructs based on your code, or perform your table operations on demand; see Deployment.
  3. Intelligent and powerful querying syntax, see Querying and MagicSearch.
  4. Selectively update item attributes, prevents wasteful uploading of unchanged values.
  5. Data serialization, cast any JavaScript value into a DynamoDB attribute value.
  6. Amazon X-Ray support, see Connections.
  7. Incredibly easy local development, with support for seeding a local database.
  8. Supports conditional writes, see Saving.
  9. Use AsyncGenerators to page through results efficiently, see MagicSearch.

NOTE: DynamoDB Accelerator (DAX) support has been dropped as DAX is not yet supported by aws-sdk v3, see aws-sdk-js-v3#4263.

Example Usage

import { Dyngoose } from 'dyngoose'

@Dyngoose.$Table({ name: 'Card' })
class Card extends Dyngoose.Table {
  @Dyngoose.Attribute.Number()
  public id: number

  // Dyngoose supports inferring the attribute types based on the object types
  // of your values, however, you can also specify strict attribute types,
  // which offers more utilities
  @Dyngoose.Attribute()
  public title: string

  @Dyngoose.Attribute()
  public number: number

  @Dyngoose.Attribute.String({ trim: true })
  public description: string

  @Dyngoose.Attribute.StringSet()
  public owners: Set<string>

  @Dyngoose.Attribute.Date({ timeToLive: true })
  public expiresAt: Date

  @Dyngoose.$PrimaryKey('id', 'title')
  static readonly primaryKey: Dyngoose.Query.PrimaryKey<Card, number, string>

  @Dyngoose.$DocumentClient()
  static readonly documentClient: Dyngoose.DocumentClient<Card>
}

// Perform table operations
await Card.createTable()
await Card.deleteTable()

// Creating records
const card = new Card()
card.id = 100
card.title = 'Title'

// note: Card.new is correct, this is a custom method that allows for a strongly-typed object
const card2 = Card.new({
  id: 100,
  title: 'Title'
})

// Save a record
await card.save()

// Batch Write
const batchWrite = new Dyngoose.BatchWrite()
batchWrite.put(
  Card.new(…),
  Card.new(…)
)
await batchWrite.commit()

// Get record by the primary key
await Card.primaryKey.get({ id: 100, title: 'Title' })

// Batch Get
const batchGet = new Dyngoose.BatchGet()

batchGet.get(
  Card.primaryKey.fromKey({ id: 100, title: 'Title' }),
  Card.primaryKey.fromKey({ id: 100, title: 'Title' })
)

await batchGet.retrieve()

// Searching and Advanced Querying
// Your values will be strictly typed based on the attribute being filtered
await Card.search()
  .filter('id').eq(100)
  .and()
  .filter('title').gte('Title')
  .exec()

// Easily delete record
await card.delete()

// Query
// Queries are always strongly typed. (['>=', T] | ['=', T] ...)
const cards = await Card.primaryKey.query({
  id: 100,
  title: ['>=', 'Title']
})

// You can loop through outputs, which is a native JavaScript array
for (const card of cards) {
  console.log(card.id, card.title)
}

// The output contains additional properties
console.log(`Your query returned ${cards.count} and scanned ${cards.scannedCount} documents`)

// Atomic counters, advanced update expressions
// Increment or decrement automatically, based on the current value in DynamoDB
card.set('number', 2, { operator: 'increment' }) // if the current value had been 5, it would now be 7
card.set('number', 2, { operator: 'decrement' }) // if the current value had been 5, it would now be 3

// Use the add or remove operator on Sets to only partially change an attribute
card.set('owners', ['some value'], { operator: 'add' })

// Use an abort controller all on read requests
const abortController = new AbortController()
await Card.primaryKey.get({ id: 10, title: 'abc' }, { abortSignal: abortController.signal })
abortController.abort()

TS Compiler Setting

Dyngoose utilizes TypeScript decorators, to use them you must enable them within your tsconfig.json file:

{
    "compilerOptions": {
        // other options…
        //
        "experimentalDecorators": true, // required
        "emitDecoratorMetadata": true // required
    }
}

Honorable mentions

I originally based a lot of of this work on Dynamoose, reworking it for TypeScript and adding adding better querying logic. About two years later, I pulled in some work from dynamo-types and reworked it further to make what has become Dyngoose. I want to thank the creators and all the people who worked on both of those projects.