Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Apollo DynamoDB Data Source

**This package is forked from https://github.com/cmwhited/apollo-datasource-dynamodb and has gone through major update**

This package exports a ([`DynamoDBDataSource`](https://github.com/cmwhited/apollo-datasource-dynamodb/blob/master/src/DynamoDBDataSource.ts)) class which is used for fetching data from a DynamoDB Table and exposing it via GraphQL within Apollo Server.

## Documentation
Expand Down Expand Up @@ -274,22 +276,32 @@ const items: TestHashOnlyItem[] = await this.scan(scanInput, ttl);

### put

`this.put(item, 180)`
`this.put(putItemInput, 180)`

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

[DynamoDB.DocumentClient.put](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property)

#### put Example

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

const created: TestHashOnlyItem = await this.put(item, ttl);
const putItemInput: DocumentClient.PutItemInput = {
TableName: testHashOnly.tableName,
Item: item,
ConditionExpression: 'id <> :value',
ExpressionAttributeValues: {
':value': 'testing'
},
ReturnValues: 'NONE',
};

const created: TestHashOnlyItem = await testHashOnly.put(putItemInput, ttl);
```

### update
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-datasource-dynamodb",
"version": "1.1.0",
"name": "@newsperform/apollo-datasource-dynamodb",
"version": "2.1.0",
"description": "Apollo DataSource framework for AWS DynamoDB",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand All @@ -9,7 +9,7 @@
"lint:eslint": "eslint --ext .ts . --ignore-path .gitignore",
"format:eslint": "eslint --ext .ts . --fix --ignore-path .gitignore",
"lint:prettier": "prettier \"**/*.ts\" --list-different --ignore-path .gitignore || (echo '↑↑ these files are not prettier formatted ↑↑' && exit 1)",
"format:prettier": "prettier \"**/*.ts\" --write --ignore-path .gitignore",
"format": "prettier \"**/*.ts\" --write --ignore-path .gitignore",
"lint": "npm run lint:eslint && npm run lint:prettier",
"clean-report": "rimraf ./coverage",
"test": "npm run clean-report && jest -c jest.config.js --runInBand --detectOpenHandles",
Expand All @@ -23,15 +23,15 @@
"version": "npm run format && git add -A src",
"postversion": "git push && git push --tags"
},
"homepage": "https://github.com/cmwhited/apollo-datasource-dynamodb",
"homepage": "https://bitbucket.org/punterswebteam/apollo-datasource-dynamodb/src/updated-api/",
"repository": {
"type": "git",
"url": "git+https://github.com/cmwhited/apollo-datasource-dynamodb.git"
"url": "git+https://bitbucket.org/punterswebteam/apollo-datasource-dynamodb/src/updated-api/"
},
"bugs": {
"url": "https://github.com/cmwhited/apollo-datasource-dynamodb"
"url": "https://bitbucket.org/punterswebteam/apollo-datasource-dynamodb/src/updated-api/"
},
"author": "Chris Whited <[email protected]>",
"author": "Dina Basumatary",
"license": "MIT",
"private": false,
"keywords": [
Expand Down
43 changes: 12 additions & 31 deletions src/DynamoDBDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,11 @@ export abstract class DynamoDBDataSource<ITEM = unknown, TContext = unknown> ext

/**
* Store the item in the table and add the item to the cache
* @param item the item to store in the table
* @param putItemInput parameters to pass in to the dynamodb put operation
* @param ttl the time-to-live value of how long to persist the item in the cache
*/
async put(item: ITEM, ttl?: number): Promise<ITEM> {
const putItemInput: DynamoDB.DocumentClient.PutItemInput = {
TableName: this.tableName,
Item: item,
};
async put(putItemInput: DynamoDB.DocumentClient.PutItemInput, ttl?: number): Promise<ITEM> {
const item: ITEM = putItemInput.Item as ITEM;
await this.dynamoDbDocClient.put(putItemInput).promise();

if (ttl) {
Expand All @@ -129,29 +126,15 @@ export abstract class DynamoDBDataSource<ITEM = unknown, TContext = unknown> ext

/**
* Update the item in the table and reset the item in the cache
* @param key the key of the item in the table to update
* @param updateItemInput the input to be passed to dynamodb update
* @param ttl the time-to-live value of how long to persist the item in the cache
*/
async update(
key: DynamoDB.DocumentClient.Key,
updateExpression: DynamoDB.DocumentClient.UpdateExpression,
expressionAttributeNames: DynamoDB.DocumentClient.ExpressionAttributeNameMap,
expressionAttributeValues: DynamoDB.DocumentClient.ExpressionAttributeValueMap,
ttl?: number
): Promise<ITEM> {
const updateItemInput: DynamoDB.DocumentClient.UpdateItemInput = {
TableName: this.tableName,
Key: key,
ReturnValues: 'ALL_NEW',
UpdateExpression: updateExpression,
ExpressionAttributeNames: expressionAttributeNames,
ExpressionAttributeValues: expressionAttributeValues,
};
async update(updateItemInput: DynamoDB.DocumentClient.UpdateItemInput, ttl?: number): Promise<ITEM> {
const output = await this.dynamoDbDocClient.update(updateItemInput).promise();
const updated: ITEM = output.Attributes as ITEM;

if (updated && ttl) {
const cacheKey: string = buildCacheKey(CACHE_PREFIX_KEY, this.tableName, key);
const cacheKey: string = buildCacheKey(CACHE_PREFIX_KEY, this.tableName, updateItemInput.Key);
await this.dynamodbCache.setInCache(cacheKey, updated, ttl);
}

Expand All @@ -160,16 +143,14 @@ export abstract class DynamoDBDataSource<ITEM = unknown, TContext = unknown> ext

/**
* Delete the given item from the table
* @param key the key of the item to delete from the table
* @param deleteItemInput the input to be passed to dynamodb delete
*/
async delete(key: DynamoDB.DocumentClient.Key): Promise<void> {
const deleteItemInput: DynamoDB.DocumentClient.DeleteItemInput = {
TableName: this.tableName,
Key: key,
};
async delete(deleteItemInput: DynamoDB.DocumentClient.DeleteItemInput): Promise<ITEM> {
const output = await this.dynamoDbDocClient.delete(deleteItemInput).promise();
const deleted: ITEM = output.Attributes as ITEM;

await this.dynamoDbDocClient.delete(deleteItemInput).promise();
await this.dynamodbCache.removeItemFromCache(this.tableName, deleteItemInput.Key);

await this.dynamodbCache.removeItemFromCache(this.tableName, key);
return deleted;
}
}
50 changes: 36 additions & 14 deletions src/__tests__/DynamoDBDataSource.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApolloError } from 'apollo-server-errors';
import { DataSourceConfig } from 'apollo-datasource';
import { DynamoDB } from 'aws-sdk';
import { ClientConfiguration } from 'aws-sdk/clients/dynamodb';
import { ClientConfiguration, DocumentClient } from 'aws-sdk/clients/dynamodb';

import { DynamoDBDataSource } from '../DynamoDBDataSource';
import { CACHE_PREFIX_KEY } from '../DynamoDBCache';
Expand Down Expand Up @@ -267,7 +267,12 @@ describe('DynamoDBDataSource', () => {

dynamodbCacheSetInCacheMock.mockResolvedValueOnce();

const actual: TestHashOnlyItem = await testHashOnly.put(item2, ttl);
const input2: DocumentClient.PutItemInput = {
TableName: testHashOnly.tableName,
Item: item2,
};

const actual: TestHashOnlyItem = await testHashOnly.put(input2, ttl);
const { Item } = await testHashOnly.dynamoDbDocClient
.get({
TableName: testHashOnly.tableName,
Expand Down Expand Up @@ -297,7 +302,12 @@ describe('DynamoDBDataSource', () => {
test: 'testing3',
};

const actual: TestHashOnlyItem = await testHashOnly.put(item3);
const input3: DocumentClient.PutItemInput = {
TableName: testHashOnly.tableName,
Item: item3,
};

const actual: TestHashOnlyItem = await testHashOnly.put(input3);
const { Item } = await testHashOnly.dynamoDbDocClient
.get({
TableName: testHashOnly.tableName,
Expand Down Expand Up @@ -349,10 +359,14 @@ describe('DynamoDBDataSource', () => {
dynamodbCacheSetInCacheMock.mockResolvedValueOnce();

const actual = await testHashOnly.update(
givenKey,
givenUpdateExpression,
givenExpressionAttributeNames,
givenExpressionAttributeValues,
{
TableName: testHashOnly.tableName,
Key: givenKey,
ReturnValues: 'ALL_NEW',
UpdateExpression: givenUpdateExpression,
ExpressionAttributeNames: givenExpressionAttributeNames,
ExpressionAttributeValues: givenExpressionAttributeValues,
},
ttl
);
const { Item } = await testHashOnly.dynamoDbDocClient
Expand Down Expand Up @@ -401,12 +415,14 @@ describe('DynamoDBDataSource', () => {
':test': 'testing_updated',
};

const actual = await testHashOnly.update(
givenKey,
givenUpdateExpression,
givenExpressionAttributeNames,
givenExpressionAttributeValues
);
const actual = await testHashOnly.update({
TableName: testHashOnly.tableName,
Key: givenKey,
ReturnValues: 'ALL_NEW',
UpdateExpression: givenUpdateExpression,
ExpressionAttributeNames: givenExpressionAttributeNames,
ExpressionAttributeValues: givenExpressionAttributeValues,
});
const { Item } = await testHashOnly.dynamoDbDocClient
.get({
TableName: testHashOnly.tableName,
Expand Down Expand Up @@ -448,7 +464,13 @@ describe('DynamoDBDataSource', () => {

dynamodbCacheRemoveItemFromCacheMock.mockResolvedValueOnce();

await testHashOnly.delete(givenKey);
const deleted = await testHashOnly.delete({
TableName: testHashOnly.tableName,
Key: givenKey,
ReturnValues: 'ALL_OLD',
});

expect(deleted).toEqual(itemToDelete);

const { Item } = await testHashOnly.dynamoDbDocClient
.get({
Expand Down