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

@crster/dynamo-blade

v1.0.4

Published

Dynamodb single-table client with added magic

Downloads

320

Readme

Dynamo Blade

Simple dynamodb client with single table design

#Not yet for production use

To use

import DynamoBlade from "@crster/dynamo-blade";
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
import { testDb } from "./testDb";

const blade = new DynamoBlade(
  DynamoDBDocumentClient.from(
    new DynamoDBClient({
      region: "us-east-1",
      endpoint: "http://localhost:8000",
    })
  )
);

const db = blade.table(testDb); // Note: must add BladeTable to DynamoBlade to use it

To define table schema

export const testDb = new BladeTable("testDb", { //<---- the name of table
    keySchema: {
      pk: HashKey(String), //<---- Mark pk as the hashkey of the table
      sk: SortKey(String), //<---- Mark sk as the sortkey of the table
      tk: TypeKey(),       //<---- Mark tk as typekey. this will save the document attribute class
      createdOn: CreatedOn(),   //<--- Add createdOn timestamp
      modifiedOn: ModifiedOn(), //<--- Add modifiedOn timestamp
    },
    index: {
      byRelease, //<---- BladeIndex(local) instance
      byType,    //<---- BladeIndex(global) instance
    },
    attribute: {
      artist, //<------- BladeAttribute instance
    },
  })

To define BladeAttribute

export const album = new BladeAttribute({
  albumId: PrimaryKey(String), //<------ The primary key of the attribute
  title: RequiredField(String), //<----------- Mark title as a required field of type string
  releaseDate: Default(() => new Date(2000)), //<--- Use this default value if undefined upon creation only. will not set default value for modify event.
  song, //<--- Sub document of this schema
});

export const artist = new BladeAttribute({
  artistId: PrimaryKey(String), //<------ The primary key of the attribute
  name: String, //<------- Scalar type see below for other supported Scalar type
  age: Number,
  genres: SetField(String), //<---------- Mark genres field type as Set<string>
  album, //<----------------------------- This is a sub document defination. It is an instance of BladeAttribute
});

List if BladeAttribute field types

Scalar Types: String, Number, Boolean, Buffer, Date, OptionalField(), RequiredField()
Key Types: PrimaryKey(), HashKey(), SortKey(), TypeKey(),
Event Types: CreatedOn(), ModifiedOn(), Override(), Default()
Document Types: SetField(), ListField(), DocumentField()

SetField is for defining sets of the same type
ListField is for defining arrays of different item type
DocumentField is for defining map type

To initialize table

// Only use init if you want to create tables. You can use the library without calling this
const [result] = await blade.init();

To add

// The add method will return true when success
const result = await db.open("artist").is("john").add({
    name: "John Doe",
    age: 10,
  });

To get

// The get method will return artist document
const result = await db.open("artist").is("john").get();

console.log(result)

To add sub item

const result = await db.open("artist").is("john").open("album").is("j2").add({
    title: "J2",
    price: 150,
    rating: 3,
  });

To update

const result = await db
    .open("artist")
    .is("john")
    .open("album")
    .is("love1")
    .set({ price: 210 });

To query local index

// query byRelease index where pk = artist#john and releaseDate between 2007-01 and 2007-02
 const result = await db
    .query("byRelease")
    .where({ // Note: the db.query.where method should only contain key field of the index
      pk: db.open("artist").is("john"),
      releaseDate: BladeFilter("BETWEEN", [
        new Date(2007, 1),
        new Date(2007, 2),
      ]),
    })
    .get();

To query global index

const result = await db
    .query("byType")
    .where({
      tk: BladeFilter("=", "artist:album:song"), // Use BladeFilter to define the condition of the query
    })
    .get();

Result object sample

console.log(result)
// Will display
{
  "count": 3,
  "data": {
    "artist:album:song": [
      {
        "songId": "icw",
        "artistAlbum": "john#kontra",
        "title": "I Could Wait",
        "length": 3.46,
        "downloadable": true,
      },
      {
        "songId": "mama",
        "artistAlbum": "john#love",
        "title": "Mama Loves You",
        "length": 4.26,
        "downloadable": true,
      },
      {
        "songId": "smt",
        "artistAlbum": "john#kontra",
        "title": "Start More That",
        "length": 3.33,
        "downloadable": false,
      }
    ]
  }
}