Skip to content

Commit

Permalink
Merge pull request #15 from Giftbit/Scoped
Browse files Browse the repository at this point in the history
Scoped Dynameh
  • Loading branch information
pushplay authored Apr 9, 2019
2 parents 9b4881a + 5683c47 commit c616df0
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 219 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dynameh",
"version": "4.0.0-beta.1",
"version": "4.0.0",
"description": "DynamoDB on Node more easier",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
60 changes: 0 additions & 60 deletions src/FluentDynameh.test.ts

This file was deleted.

150 changes: 0 additions & 150 deletions src/FluentDynameh.ts

This file was deleted.

43 changes: 43 additions & 0 deletions src/ScopedDynameh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as aws from "aws-sdk";
import * as batchHelper from "./batchHelper";
import * as concurrentHelper from "./concurrentHelper";
import * as responseUnwrapper from "./responseUnwrapper";
import {DynamoKey, DynamoKeyPair, DynamoQueryConditionOperator} from "./validation";
import {TableSchema} from "./TableSchema";
import {UpdateExpressionAction} from "./UpdateExpressionAction";
import {Condition} from "./Condition";

/**
* An instance of Dynameh scoped to a particular TableSchema. All calls that require
* a TableSchema have that parameter already filled in.
*/
export interface ScopedDynameh {

batchHelper: typeof batchHelper;

concurrentHelper: typeof concurrentHelper;

requestBuilder: {
buildRequestPutItem: (item: object) => aws.DynamoDB.AttributeValue;
buildGetInput: (partitionKeyValue: DynamoKey, sortKeyValue?: DynamoKey) => aws.DynamoDB.GetItemInput;
buildPutInput: (item: object) => aws.DynamoDB.PutItemInput;
buildUpdateInputFromActions: (itemToUpdate: object, ...updateActions: UpdateExpressionAction[]) => aws.DynamoDB.UpdateItemInput;
buildDeleteInput: (itemToDelete: object) => aws.DynamoDB.DeleteItemInput;
buildQueryInput: (partitionKeyValue: DynamoKey, sortKeyOp?: DynamoQueryConditionOperator, ...sortKeyValues: DynamoKey[]) => aws.DynamoDB.QueryInput;
buildScanInput: (...filters: Condition[]) => aws.DynamoDB.ScanInput;
buildBatchPutInput: (items: object[]) => aws.DynamoDB.BatchWriteItemInput;
buildBatchDeleteInput: (keyValues: DynamoKey[] | DynamoKeyPair[]) => aws.DynamoDB.BatchWriteItemInput;
buildBatchGetInput: (keyValues: DynamoKey[] | DynamoKeyPair[]) => aws.DynamoDB.BatchGetItemInput;
buildTransactWriteItemsInput: (...input: (aws.DynamoDB.PutItemInput | aws.DynamoDB.DeleteItemInput | aws.DynamoDB.UpdateItemInput)[]) => aws.DynamoDB.TransactWriteItemsInput;
addTransactWriteItems: (request: aws.DynamoDB.TransactWriteItemsInput, ...input: (aws.DynamoDB.PutItemInput | aws.DynamoDB.DeleteItemInput | aws.DynamoDB.UpdateItemInput)[]) => void;
buildCreateTableInput: (additionalTableSchemas?: TableSchema[], readCapacity?: number, writeCapacity?: number) => aws.DynamoDB.CreateTableInput;
buildDeleteTableInput: () => aws.DynamoDB.DeleteTableInput;
buildUpdateTimeToLiveInput: () => aws.DynamoDB.UpdateTimeToLiveInput;
addProjection: <T extends { ProjectionExpression?: aws.DynamoDB.ProjectionExpression, ExpressionAttributeNames?: aws.DynamoDB.ExpressionAttributeNameMap }>(projectableRequest: T, attributes: string[]) => void;
addCondition: <T extends { ConditionExpression?: aws.DynamoDB.ConditionExpression, ExpressionAttributeNames?: aws.DynamoDB.ExpressionAttributeNameMap, ExpressionAttributeValues?: aws.DynamoDB.ExpressionAttributeValueMap }>(conditionableRequest: T, ...conditions: Condition[]) => void;
addFilter: <T extends { FilterExpression?: aws.DynamoDB.ConditionExpression, ExpressionAttributeNames?: aws.DynamoDB.ExpressionAttributeNameMap, ExpressionAttributeValues?: aws.DynamoDB.ExpressionAttributeValueMap }>(filterableRequest: T, ...filters: Condition[]) => void;

};

responseUnwrapper: typeof responseUnwrapper;
}
40 changes: 38 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,46 @@ import * as concurrentHelper from "./concurrentHelper";
import * as requestBuilder from "./requestBuilder";
import * as requestUnwrapper from "./requestUnwrapper";
import * as responseUnwrapper from "./responseUnwrapper";
import * as validation from "./validation";
import {TableSchema} from "./TableSchema";
import {ScopedDynameh} from "./ScopedDynameh";

export * from "./Condition";
export * from "./FluentDynameh";
export * from "./ScopedDynameh";
export * from "./TableSchema";
export * from "./UpdateExpressionAction";

export {batchHelper, concurrentHelper, requestBuilder, requestUnwrapper, responseUnwrapper};
export {batchHelper, concurrentHelper, requestBuilder, requestUnwrapper, responseUnwrapper, validation};

/**
* Create an instance of Dynameh scoped to the given TableSchema. All calls that require
* a TableSchema have that parameter already filled in.
* @param tableSchema
*/
export function scope(tableSchema: TableSchema): ScopedDynameh {
return {
requestBuilder: {
buildRequestPutItem: (item) => requestBuilder.buildRequestPutItem(tableSchema, item),
buildGetInput: (partitionKeyValue, sortKeyValue?) => requestBuilder.buildGetInput(tableSchema, partitionKeyValue, sortKeyValue),
buildPutInput: (item) => requestBuilder.buildPutInput(tableSchema, item),
buildUpdateInputFromActions: (itemToUpdate, ...updateActions) => requestBuilder.buildUpdateInputFromActions(tableSchema, itemToUpdate, ...updateActions),
buildDeleteInput: (itemToDelete) => requestBuilder.buildDeleteInput(tableSchema, itemToDelete),
buildDeleteTableInput: () => requestBuilder.buildDeleteTableInput(tableSchema),
buildQueryInput: (partitionKeyValue: validation.DynamoKey, sortKeyOp?, ...sortKeyValues: validation.DynamoKey[]) => requestBuilder.buildQueryInput(tableSchema, partitionKeyValue, sortKeyOp, ...sortKeyValues),
buildScanInput: (...filters) => requestBuilder.buildScanInput(tableSchema, ...filters),
buildBatchPutInput: (items) => requestBuilder.buildBatchPutInput(tableSchema, items),
buildBatchDeleteInput: (keyValues) => requestBuilder.buildBatchDeleteInput(tableSchema, keyValues),
buildBatchGetInput: (keyValues) => requestBuilder.buildBatchGetInput(tableSchema, keyValues),
buildTransactWriteItemsInput: (...input) => requestBuilder.buildTransactWriteItemsInput(...input),
addTransactWriteItems: (request, ...input) => requestBuilder.addTransactWriteItems(request, ...input),
buildCreateTableInput: (additionalTableSchemas: TableSchema[] = [], readCapacity: number = 1, writeCapacity: number = 1) => requestBuilder.buildCreateTableInput([tableSchema, ...additionalTableSchemas], readCapacity, writeCapacity),
buildUpdateTimeToLiveInput: () => requestBuilder.buildUpdateTimeToLiveInput(tableSchema),
addProjection: (projectableRequest, attributes) => requestBuilder.addProjection(tableSchema, projectableRequest, attributes),
addCondition: (conditionableRequest, ...conditions) => requestBuilder.addCondition(tableSchema, conditionableRequest, ...conditions),
addFilter: (filterableRequest, ...filters) => requestBuilder.addFilter(tableSchema, filterableRequest, ...filters)
},
responseUnwrapper: responseUnwrapper,
batchHelper: batchHelper,
concurrentHelper: concurrentHelper
};
}
25 changes: 22 additions & 3 deletions src/requestBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ describe("requestBuilder", () => {
},
{
action: "put",
attribute: "other",
attribute: "zing",
value: 123
}
);
Expand Down Expand Up @@ -894,7 +894,7 @@ describe("requestBuilder", () => {
Key: {
primary: {S: "qux"}
},
UpdateExpression: "SET other = :a",
UpdateExpression: "SET zing = :a",
ExpressionAttributeValues: {
":a": {N: "123"}
}
Expand Down Expand Up @@ -1180,6 +1180,24 @@ describe("requestBuilder", () => {
};

it("adds a condition to put input", () => {
const input = buildPutInput(defaultTableSchema, {primary: "foo"});
addCondition(defaultTableSchema, input, {
attribute: "attributetoset",
operator: "attribute_not_exists"
});

chai.assert.deepEqual(input, {
TableName: "table",
Item: {
primary: {
S: "foo"
}
},
ConditionExpression: "attribute_not_exists(attributetoset)"
});
});

it("adds a condition with a reserved word key to put input", () => {
const input = buildPutInput(defaultTableSchema, {primary: "foo"});
addCondition(defaultTableSchema, input, {
attribute: "primary",
Expand All @@ -1193,7 +1211,8 @@ describe("requestBuilder", () => {
S: "foo"
}
},
ConditionExpression: "attribute_not_exists(primary)"
ExpressionAttributeNames: {"#A": "primary"},
ConditionExpression: "attribute_not_exists(#A)"
});
});

Expand Down
Loading

0 comments on commit c616df0

Please sign in to comment.