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

@bhoos/dynamodb

v0.1.16

Published

Node DyanmoDB Library

Downloads

37

Readme

@bhoos/dynamodb

DynamoDB wrapper

Installation

$ yarn add @bhoos/dynamodb

Usage

Environment Variables:

AWS_REGION

DYNAMODB_ENDPOINT [optional]

Example (Single partition key)

import { createCollection } from '@bhoos/dynamodb'

// Create collections as required
// A Simple collection with single key
const User = createCollection('User', ({ key }) => [
  key('username'),
], ({ self }) => {
  get: async (username) => {
    const u = await self.findOne(username);
    const { password, ...res } = u;
    return u;
  },

  create: async (username, password, name) => self.insert({ 
    username, 
    name,
    password: hash(password),
  }),

  validate: async (username, password) => {
    const u = await self.findOne(user);
    if (!u || hash(password) !== u.password) {
      throw new Error('Invalid username/password');
    }

    // It's not a good idea to send back the password, even if its hashed
    const { password, ...res } = u;
    return res;
  },

  changePassword: async (username, password) => {
    // Will throw if the username doesn't exist
    await self.update({ password: hash(password) }, username);
  },

  deleteUser: async (username) => {
    await self.delete(username);
  },
});

// User can now be used with the extended operations
const email = await User.create('[email protected]', 'SuperSecret', 'John Doe');
const user = await User.get('[email protected]'); // Retrieve record
await User.validate('[email protected]', 'Wrong'); // throws error
await User.validate('[email protected]', 'SuperSecret'); // Returns user instance
await User.deleteUser('[email protected]'); // Delete from database

// Also available are generic methods- insert, findOne, update, & delete
await User.insert({ username: '[email protected]', password: 'not-hashed', name: 'Jane Doe' }); // Direct
await User.findOne('[email protected]');
await User.update({ password: 'not-hashed-new' }, '[email protected]');
await User.delete('[email protected]');

Example (Multiple keys)

const Movie = createCollection('Movie', ({ key }) => ([
  key('year'),
  key('title'),
}), ({ self, doc }) => ({
  get: (year, title) => self.findOne(year, title),
  findAnnual: (year) => doc.query(.......), // Write your dynamodb query to find something
});

Example (Local Secondary Index)

const Thread = createCollection('Thread', ({ key, field }) => ([
  key('forum'),
  key('subject').sort(),
  field('timestamp').local('LastPostIndex'),
]), ({ doc }) => ({
  // Get all recent posts, (with paging)
  getRecentPosts: (forum) => {
    const params = {
      TableName: Thread.name,
      IndexName: 'LastPostIndex',
      KeyConditionExpression: '#forum=:forum',
      ExpressionAttributeNames: { '#forum': 'forum' },
      ExpressionAttributeValues: { ':forum': forum },
      ScanIndexForward: false,
    };

    return doc.query(params).promise().then(data => data.Items);
  },
}));

Example (Global Secondary Index)

const GameScore = createCollection('GameScore', ({ key, field }) => ([
  key('UserId'),
  key('GameTitle').sort().global('GameTitleIndex'), // Sort key for main, partition key for global
  field('TopScore').global('GameTitleIndex', true) // Sort key for global index
]), ({ doc }) => ({
  getTopScorer: (title) => {
    const params = {
      TableName: GameScore.name,
      IndexName: 'GameTitleIndex',
      KeyConditionExpression: 'GameTitle = :title',
      ExpressionAttributeValues: { ':title': title },
      ScanIndexForward: false,
    };

    return doc.query(params).promise().then(data => data.Items);
  },
}));

Example (With TTL)

const Logs = createCollection('Log', ({ key, field }) => ([
  key('id').auto(),
  field('name'),
  field('expiry').ttl(),
]));

// Run the update ttl after creating the table to enable TTL on expiry
await Logs.updateTTL(true);

// The expiry field can now be set with a future EPOCH time for automatic removal
await Logs.insert({ name: 'Tomorrow', expiry: parseInt(Date.now() / 1000) + 86400 });
await Logs.insert({ name: 'next week', expiry: parseInt(Date.now() / 1000) + 7 * 86400 });