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

@pingid/apollo-datasource-dynamodb

v1.0.2

Published

Apollo DataSource framework for AWS DynamoDB

Downloads

10

Readme

Apollo DynamoDB Data Source

This package exports a (DynamoDBDataSource) class which is used for fetching data from a DynamoDB Table and exposing it via GraphQL within Apollo Server.

Documentation

View the Apollo Server documentation for data sources for more details.

Usage

To get stated, install the apollo-datasource-dynamodb packageL

# with npm
npm install --save apollo-datasource-dynamodb
# with yarn
yarn add apollo-datasource-dynamodb

To define a data source, extend the DynamoDBDataSource class and pass in the name of the table, the Key schema, and the ClientConfiguration that allows the lib to connect to the DynamoDB.DocumentClient to interact with data in the table. Creating an instance of this class then allows you to utilize the API methods.

Example

Say you have a DynamoDB table called test_data which has a Schema:

{
  TableName: 'test_hash_only',
  KeySchema: [{ AttributeName: 'id', KeyType: 'HASH' }],
  AttributeDefinitions: [{ AttributeName: 'id', AttributeType: 'S' }],
  ProvisionedThroughput: {
    ReadCapacityUnits: 1,
    WriteCapacityUnits: 1,
  },
}

Here is an example interface of the items that will be returned from this table:

interface TestHashOnlyItem {
  id: string;
  test: string;
}

To use the DynamoDBDataSource we create a class that subclasses this Data Source and can then implement API:

// ./src/data-sources/test-hash-only.datasource.ts

import { DynamoDBDataSource } from 'apollo-datasource-dynamodb';
import { DocumentClient } from 'aws-sdk/clients/dynamodb';

class TestHashOnly extends DynamoDBDataSource<TestHashOnlyItem> {
  private readonly tableName = 'test_hash_only';
  private readonly tableKeySchema: DocumentClient.KeySchema = [
    {
      AttributeName: 'id',
      KeyType: 'HASH',
    },
  ];
  private readonly ttl = 30 * 60; // 30minutes

  constructor(config?: ClientConfiguration) {
    super(this.tableName, this.tableKeySchema, config);
  }

  async getTestHashOnlyItem(id: string): Promise<TestHashOnlyItem> {
    const getItemInput: DocumentClient.GetItemInput = {
      TableName: this.tableName,
      ConsistentRead: true,
      Key: { id },
    };
    return this.getItem(getItemInput, this.ttl);
  }

  async scanForTestHashOnlyItems(): Promise<TestHashOnlyItem[]> {
    const scanInput: DocumentClient.ScanInput = {
      TableName: this.tableName,
      ConsistentRead: true,
    };
    return this.scan(scanInput, this.ttl);
  }
}

And then to utilize this instance as a data source in the ApolloServer instance:

// ./src/server.ts

import { ApolloServer } from 'apollo-server-lambda';
import { TestHashOnly } from './data-sources/test-hash-only.datasource';

const server = new ApolloServer({
  typeDefs,
  resolvers,
  dataSources: () => ({
    testHashOnly: new TestHashOnly(),
  }),
});

The to use the use the TestHashOnly data source in the resolvers:

// ./src/schema.ts

import { gql, IResolvers } from 'apollo-server-lambda';
import { DocumentNode } from 'graphql';

export const typeDefs: DocumentNode = gql`
  type TestHashOnlyItem {
    id: String!
    test: String!
  }

  type Query {
    getTestHashOnlyItem(id: String!): TestHashOnlyItem
    scanHashOnlyItems: [TestHashOnlyItem]
  }
`;

export const resolvers: IResolvers = {
  Query: {
    getTestHashOnlyItem: async (_source, { id }, { dataSources }) => dataSources.testHashOnly.getTestHashOnlyItem(id),
    scanHashOnlyItems: async (_source, _params, { dataSources }) => dataSources.testHashOnly.scanForTestHashOnlyItems(),
  },
};

API

getItem

this.getItem(getItemInput, 180)

Returns a single instance of the item being retrieved from the table by the key value. It checks the cache for the record, if the value is found in the cache, it returns the item, otherwise it uses the DynamoDB.DocumentClient.get method to retrieve the item from the table; if a record is found in the table, it is then added to the cache with the passed in ttl.

DynamoDB.DocumentClient.get

getItem Example

const getItemInput: DocumentClient.GetItemInput = {
  TableName: 'test_hash_only',
  ConsistentRead: true,
  Key: {
    id: 'testId',
  },
};
const ttl = 30 * 60; // 30minutes

const item: TestHashOnlyItem = await this.getItem(getItemInput, ttl);

query

this.query(queryInput, 180)

Returns all records from the table found by the query. If the ttl is provided, it adds all of the items to the cache.

DynamoDB.DocumentClient.query

query Example

const queryInput: DynamoDB.DocumentClient.QueryInput = {
  TableName: 'test_hash_only',
  ConsistentRead: true,
  KeyConditionExpression: 'id = :id',
  ExpressionAttributeValues: {
    ':id': 'testId',
  },
};
const ttl = 30 * 60; // 30minutes

const items: TestHashOnlyItem[] = await this.query(queryInput, ttl);

scan

this.scan(scanInput, 180)

Returns all scanned records from the table by the scanInput. A scan is different from a query because in a query a portion of the key schema on the table must be provided. A scan allows you to retrieve all items from the table, it also lets you paginate.

DynamoDB.DocumentClient.scan

scan Example

const scanInput: DynamoDB.DocumentClient.ScanInput = {
  TableName: 'test_hash_only',
  ConsistentRead: true,
};
const ttl = 30 * 60; // 30minutes

const items: TestHashOnlyItem[] = await this.scan(scanInput, ttl);

put

this.put(item, 180)

Saves the given item to the table. If a ttl value is provided it will also add the item to the cache

DynamoDB.DocumentClient.put

put Example

const item: TestHashOnlyItem = {
  id: 'testId2',
  test: 'testing2',
};
const ttl = 30 * 60; // 30minutes

const created: TestHashOnlyItem = await this.put(item, ttl);

update

this.update(key, updateExpression, expressionAttributeNames, expressionAttributeValues, 180)

Updates the item in the table found by the given key and then uses the update expressions to update the record in the table. These input values are used to build a DocumentClient.UpdateItemInput instance to tells DynamoDB how to update the record.

DynamoDB.DocumentClient.update

update Example

const key: DocumentClient.Key = {
  id: 'testId',
};
const updateExpression: DocumentClient.UpdateExpression = 'SET #test = :test';
const expressionAttributeNames: DocumentClient.ExpressionAttributeNameMap = { '#test': 'test' };
const expressionAttributeValues: DynamoDB.DocumentClient.ExpressionAttributeValueMap = {
  ':test': 'testing_updated',
};
const ttl = 30 * 60; // 30minutes

const updated: TestHashOnlyItem = await this.update(
  key,
  updateExpression,
  expressionAttributeNames,
  expressionAttributeValues,
  ttl
);

delete

this.delete(key)

Deletes the item found by the key from the table. It also evicts the item from the cache.

DynamoDB.DocumentClient.delete

delete Example

const key: DocumentClient.Key = {
  id: 'testId',
};

await this.delete(key);