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

dynarest

v0.9.0

Published

Lightweight Restful API for DynamoDB

Downloads

12

Readme

dynarest

Lightweight Restful API for DynamoDB

install

npm install -S dynarest

Routes

The following routes are made available when calling register.
See an example below. | method | route | example | description | | ------ | ----- | ---- | ----- | | GET | /prefix/table | /api/cars | gets an array of all the rows | | GET | /prefix/table/key | /api/cars/1234 | get a row by primary key | | PUT | /prefix/table | /api/cars | add a row and return it, or add an array of rows and return them | | DELETE | /prefix/table | /api/cars | delete all rows | | DELETE | /prefix/table/key | /api/cars/key | delete row by primary key |

Routing

const express = require("express");
const { register } = require("./dynarest.js");

const app = express();

// add json support to express app
app.use(express.json());

// register dynarest routes with the express app
await register(app, {
  autoCreate: false, // automatically create the table if it doesn't exist
  addMethod: "POST", // HTTP method for adding data. default is "PUT"
  debug: false, // set to true for informational logging
  endpoint: 'http://localhost:9000', // useful if running DynamoDB locally
  ignoreProps: ["_private"], // remove these properties from items before processing
  key: "uuid", // Primary Hash Key for the DynamoDB Table
  local: false, // Optional, running DynamoDB locally
  prefix: 'api', // prefix to add before each route
  region: "us-east-1",
  schema, // Ajv Schema for items in DynamoDB table, see https://ajv.js.org/
  table: 'cars', // the name of the database table (will be created if missing)
  timestamp: true, // add a timestamp attribute to each item when created
  ttl: 240, // how many seconds new items should live for
  ttlAttribute: "expireAt" // name of attribute to add if ttl is set
  uuid: true // add a uuid attribute to each item when created
});

Dynarest Client

import { Dynarest } from "dynarest";

// automatically tries to create the table if it doesn't exist
const cars = await Dynarest.init({

  debug: true,

  endpoint: "http://localhost:9000", // optional

  // Primary Key
  key: 'uuid',

  ignoreProps: ["_private"], // optional

  region: 'us-east-1',

  // Ajv Schema, see https://ajv.js.org/
  schema: {
    type: "object",
    properties: {
      uuid: {
        type: "string",
        pattern: "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
      },
      make: { "type": "string" },
      model: { "type": "string" },
      year: { "type": "number" }
    },
    required: [
      "uuid",
      "make",
      "model"
    ]
  },

  // table name
  table: "cars"
});

// add item to table
cars.put({
  uuid: "9d22081f-47f2-433c-87af-2ebe936b4f87",
  make: "Nissan",
  model: "Versa",
  year: 2023
});

// add multiple items to the table
cars.put([...])

// get all items
cars.get();
[
  {
    uuid: "9d22081f-47f2-433c-87af-2ebe936b4f87",
    make: "Nissan",
    model: "Versa",
    year: 2023
  },
  {
    uuid: 'b0827664-128a-436a-b1b8-2722bcc444c5',
    make: "Tiguan",
    model: "Volkswagen",
    year: 2023
  },
  // ...
]

// get specific item by primary key
cars.get("9d22081f-47f2-433c-87af-2ebe936b4f87")
[
  {
    uuid: "9d22081f-47f2-433c-87af-2ebe936b4f87",
    make: "Nissan",
    model: "Versa",
    year: 2023
  }
]

// delete all the items from the table
cars.delete();

// delete item by primary key
cars.delete("9d22081f-47f2-433c-87af-2ebe936b4f87");

advanced features

fields

You can return only specific fields (aka attributes)

// only return the uuid and year of the cars
fetch("/api/cars?fields=uuid,year");

filtering

You can also filter by certain params

// only return cards made by Toyota in 2023
fetch("/api/cars?year=2023&make=Toyota");

limiting

You can also limit your results with the limit param. This is applied after filtering and sorting.

// only return 10 cars
fetch("/api/cars?limit=10");

sorting

You can pass a sort parameter to your GET request to sort your results.

fetch("/api/cars?sort=year");

// reverse the sort by adding a "-" in front of the value
fetch("/api/cars?sort=-year");