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

sparkplug

v0.3.2

Published

A very thin wrapper over DynamoDB DocumentClient

Downloads

76

Readme


Build Status

Sparkplug is a very thin wrapper over DynamoDB DocumentClient with a nicer, Promise-based interface that feels more idiomatic to javascript. That means less nested indecipherable json and PascalCase'd properties.

Sparkplug isn't intended to be an ODM or a heavy abstraction over Amazon's client. It also doesn't deal with setting up table schemas programatically, as that is best left to Terraform, CloudFormation or configuration through tooling such as serverless.

const Sparkplug = require('sparkplug')
const plug = new Sparkplug()

plug
  .table('accounts')
  .get({ email: '[email protected]' })
  .then(({ data }) => {
    console.log(data.name)
  }).catch((err) => {
    // handle errors
  })

Installation

Sparkplug is available through the npm registry

Download and install using npm install.

npm install sparkplug

Usage


Configuration

Instances of Sparkplug can be passed configuration options. Sparkplug accepts the same config options that Amazon's DynamoDB client does, including endpoint and region.

If you're running in context such as a Lambda function, you might not need to pass in any values at all, as they are automatically configured on AWS.

const Sparkplug = require('sparkplug')

// Use default environment variables.
const plug = new Sparkplug()

If running locally via DynamoDB Local or Dynalite, you can use the localhost region along with the local endpoint.

// Use a locally running DynamoDB instance.
const localPlug = new Sparkplug({
  region: 'localhost',
  endpoint: 'http://localhost:4567'
})

Selecting Tables

Select which DynamoDB table to query with the .table() method. Database operations can be chained off of the return value of this method.

const plug = new Sparkplug()
const accounts = plug.table('accounts')

Reading Data

Use .get() method of Table to perform read operations on the database. .get() accepts an object with a primary key and value to look up, and returns a native Promise object.

In this example, query the accounts table for a record where email is '[email protected]'.

plug
  .table('accounts')
  .get({ email: '[email protected]' })
  .then(({ data }) => {
    console.log(data.name)
  }).catch((err) => {
    // handle errors
  })

Writing and Deleting

Use the .put() and .delete() methods to create/update or delete entries respectively. The object passed to put must include a primary key of the table (in our example it is email).

plug
  .table('accounts')
  .put({ 
    email: '[email protected]',
    name: 'Admiral Ackbar',
    planet: 'Mon Calamari'
   })
  .then(({ data }) => {
    console.log(data.name)
  }).catch((err) => {
    // handle errors
  })

.delete() accepts a primary key, similarly to .get().

plug
  .table('accounts')
  .delete({ email: '[email protected]' })
  .then(() => {
    // perform actions after deletion
  }).catch((err) => {
    // handle errors
  })

Queries and Scans

Queries and Scans are both supported by Sparkplug.

Scan

Scans are a simple way to search on a non-primary key. Use the exec() method to execute the scan and return a Promise.

plug
  .table('accounts')
  .scan({ planet: 'Mon Calamari' })
  .exec()
  .then(({ data }) => {
    // `data` contains results of scan
  }).catch((err) => {
    // handle errors
  })

Queries

Queries can be used as more performant lookups on primary keys or secondary indexes.

To query a primary key, use .query().

const promise = plug
  .table('accounts')
  .query({ email: '[email protected]' })
  .exec()

To query a secondary index, chain the .on() method to the query. The below example assumes you've set up a secondary index on name.

const promise = plug
  .table('accounts')
  .query({ name: 'Admiral Ackbar' })
  .on('name')
  .exec()

Start and Limit

Scans and queries can paginate through results with the .start() and .limit() methods of the Scan or Query objects.

This query starts at the object with the given primary key and limits the response to 2 results after the given key.

const promise = plug
  .table('accounts')
  .scan()
  .start({ email: '[email protected]' })
  .limit(2)
  .exec()

Strong Consistency

Scans perform eventually consistent reads by default. To use strong consistency, use the .strongRead() method of Scan

const promise = plug
  .table('accounts')
  .scan({ planet: 'Mon Calamari' })
  .strongRead()
  .exec()

Batch Operations

You can use Sparkplug to make batch get and put and delete requests by using the .batch() method. Batch operations accept a sparkplug Table as their first parameter and either an object or array of objects as their second.

const accounts = sparkplug.table(ACCOUNT_TABLE)
const orgs = sparkplug.table(ORG_TABLE)
const promise = plug
  .batch()
  .put(accounts, [{
    email: '[email protected]',
    name: 'Admiral Ackbar',
    planet: 'Mon Calamari'
  }, {
    email: '[email protected]',
    name: 'Darth Vader',
    planet: 'Tatooine'
  }])
  .put(orgs, {
    name: 'Github',
    id: 45678
  })
  .exec()

Contributing

Sparkplug is open for contributions via GitHub Pull Requests!

To run tests and a coverage report against the codebase:

  • clone the repository,
  • run npm i to install dependencies
  • run npm test