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

fastify-dynamodm

v1.0.1

Published

Fastify plugin to share DynamoDM handles to Dynamo DB table(s) across routes.

Downloads

10

Readme

fastify-dynamodm: Fastify plugin for DynamoDM

CI Coverage Status NPM version

Fastify plugin for the dynamodm dynamo DB document mapper

Install

npm -i fastify-dynamodm

Usage

For a single-table-design application, specify tableName when registering the plugin, and then fastify.table() will make available the dynamoDM Table handle:

const fastify = require('fastify')({ logger: true })

await fastify.register(require('.'), { tableName: 'my-dynamodb-table' })

// typically you would define your schemas in separate files:
const { Schema } = require('dynamodm')()
const MyUserSchema = Schema('user', {
  properties: {
    emailAddress: { type: 'string' },
    marketingComms: { type: 'boolean', default: false }
  }
})

// models should be registered in tables before .listen():
const UserModel = fastify.table().model(MyUserSchema)

fastify.get('/user/:id', async (req, reply) => {
  const user = await UserModel.getById(req.params.id)
  if (!user) {
    reply.code(404).send()
  } else {
    reply.type('application/json').code(200)
    return await user.toObject()
  }
})

fastify.listen({ port: 3000 }, err => {
  if (err) throw err
  console.log(`server listening on ${fastify.server.address().port}`)
})

For an application with multiple tables, specify the table name when calling fastify.table(tableName) to get the handle for the named table:

const fastify = require('fastify')()

await fastify.register(require('.'), { })

// typically you would define your schemas in separate files:
const dynamodm = require('dynamodm')()
const MyUserSchema = dynamodm.Schema('user', {
  properties: {
    emailAddress: { type: 'string' },
    marketingComms: { type: 'boolean', default: false }
  }
})

const MyCommentSchema = dynamodm.Schema('comment', {
  properties: {
    text: { type: 'string' },
    user: dynamodm.DocId,
    createdAt: dynamodm.CreatedAtField
  }
}, {
  // The schema also defines the indexes (GSI) that this model needs:
  index: {
    findByUser: {
      hashKey: 'user',
      sortKey: 'createdAt'
    }
  }
})

// models should be registered in tables before .listen():
const UserModel = fastify.table('users').model(MyUserSchema)
const CommentModel = fastify.table('comments').model(MyCommentSchema)

fastify.get('/user/:id', async (req, reply) => {
  const user = await UserModel.getById(req.params.id)
  if (!user) {
    reply.code(404).send()
  } else {
    reply.type('application/json').code(200)
    return await user.toObject()
  }
})
fastify.get('/user/:id/comments', async (req, reply) => {
  const comments = CommentModel.queryMany({ user: req.params.id })
  reply.type('application/json').code(200)
  return await Promise.all(comments.map(c => c.toObject()))
})

fastify.listen({ port: 3000 }, err => {
  if (err) throw err
  console.log(`server listening on ${fastify.server.address().port}`)
})

Examples

For a small example application, see ./example