Skip to content

Commit cc958b2

Browse files
committed
feat: change to options only
1 parent 0d3a87f commit cc958b2

File tree

14 files changed

+154
-123
lines changed

14 files changed

+154
-123
lines changed

src/delete.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,40 @@ export type DeleteItemFunction<TPK extends PK, TSK extends SK> = (
1818
},
1919
) => Promise<boolean>;
2020

21+
type CreateDeleteItemOptions<Attributes extends DynamoDBTypes> = {
22+
tablename: string;
23+
/**
24+
* Name of the Partitionkey Attribute
25+
*/
26+
pkName: keyof Attributes;
27+
/**
28+
* Name of the Sortkey Attribute
29+
*/
30+
skName?: keyof Attributes;
31+
};
32+
2133
/**
2234
* Setup delete Function that removes item from ddb table
23-
* @param tablename Tablename
24-
* @param partitionKeyName Name of the Partitionkey
35+
* @param options Options for the delete item function
2536
* @returns Boolean indicating success
2637
*/
2738
export const createDeleteItem = <
2839
Attributes extends DynamoDBTypes,
2940
TPK extends PK,
3041
TSK extends SK = undefined,
3142
>(
32-
tablename: string,
33-
partitionKeyName: keyof Attributes,
34-
sortKeyName?: keyof Attributes,
43+
options: CreateDeleteItemOptions<Attributes>,
3544
): DeleteItemFunction<TPK, TSK> => {
36-
return (key, options = {}) =>
45+
const { tablename, pkName, skName } = options;
46+
47+
return (key, { sortKey, dynamodbOptions = {} } = {}) =>
3748
deleteItem(
3849
tablename,
3950
{
40-
[partitionKeyName]: convertToAttr(key),
41-
...maybeMerge(sortKeyName, maybeConvertToAttr(options.sortKey)),
51+
[pkName]: convertToAttr(key),
52+
...maybeMerge(skName, maybeConvertToAttr(sortKey)),
4253
},
43-
options.dynamodbOptions ?? {},
54+
dynamodbOptions,
4455
);
4556
};
4657

@@ -50,7 +61,7 @@ export const createDeleteItem = <
5061
* @param key Identifier of the DDB item
5162
* @returns Boolean indicating success
5263
*/
53-
export const deleteItem = async <T>(
64+
export const deleteItem = async <_T>(
5465
tablename: string,
5566
key: DeleteItemCommandInput["Key"],
5667
options: DeleteItemOptions,

src/get.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,40 @@ export type GetItemFunction<
2424
options?: GetItemOptions<TSK>,
2525
) => Promise<Attributes | undefined>;
2626

27+
type CreateGetItemOptions<Attributes extends DynamoDBTypes> = {
28+
tablename: string;
29+
/**
30+
* Name of the Partitionkey Attribute
31+
*/
32+
pkName: keyof Attributes;
33+
/**
34+
* Name of the Sortkey Attribute
35+
*/
36+
skName?: keyof Attributes;
37+
};
38+
2739
/**
2840
* Create Function that gets item from ddb table
29-
* @param tablename Tablename
30-
* @param partitionKeyName Name of the Partitionkey
41+
* @param options Options for the get item function
3142
* @returns Function that gets item
3243
*/
3344
export const createGetItem = <
3445
Attributes extends DynamoDBTypes,
3546
TPK extends PK,
3647
TSK extends SK = undefined,
3748
>(
38-
tablename: string,
39-
partitionKeyName: keyof Attributes,
40-
sortKeyName?: keyof Attributes,
49+
options: CreateGetItemOptions<Attributes>,
4150
): GetItemFunction<Attributes, TPK, TSK> => {
42-
return async (key, options = {}) => {
51+
const { tablename, pkName, skName } = options;
52+
53+
return async (key, { sortKey, dynamodbOptions = {} } = {}) => {
4354
return getItem(
4455
tablename,
4556
{
46-
[partitionKeyName]: convertToAttr(key),
47-
...maybeMerge(sortKeyName, maybeConvertToAttr(options.sortKey)),
57+
[pkName]: convertToAttr(key),
58+
...maybeMerge(skName, maybeConvertToAttr(sortKey)),
4859
},
49-
options.dynamodbOptions ?? {},
60+
dynamodbOptions,
5061
);
5162
};
5263
};

src/put.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@ export type PutItemFunction<Attributes extends DynamoDBTypes> = (
1414
options?: PutItemOptions,
1515
) => Promise<Attributes>;
1616

17+
export type CreatePutItemOptions<_Attributes extends DynamoDBTypes> = {
18+
tablename: string;
19+
};
20+
1721
/**
1822
* Create Function to put item into ddb table
1923
* @param tablename Tablename
2024
* @returns Function that puts item
2125
*/
2226
export const createPutItem = <Attributes extends DynamoDBTypes>(
23-
tablename: string,
27+
options: CreatePutItemOptions<Attributes>,
2428
): PutItemFunction<Attributes> => {
29+
const { tablename } = options;
30+
2531
return (item, options = {}) => putItem(tablename, marshall(item), options);
2632
};
2733

src/query.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@ import { createConditionExpression, type FilterOptions } from "./expression";
66
import { maybeMerge } from "./object";
77
import type { DynamoDBTypes, PK, SK } from "./types";
88

9-
type CreateQueryOptions<Attributes extends DynamoDBTypes> = {
9+
type CreateQueryItemsOptions<Attributes extends DynamoDBTypes> = {
10+
tablename: string;
1011
indexName?: string;
11-
sortKeyName?: keyof Attributes;
12+
/**
13+
* Name of the Partitionkey Attribute
14+
*/
15+
pkName: keyof Attributes;
16+
/**
17+
* Name of the Sortkey Attribute
18+
*/
19+
skName?: keyof Attributes;
1220
};
1321

1422
type QueryDynamoDBOptions = Omit<QueryCommandInput, "TableName">;
@@ -30,36 +38,33 @@ export type QueryItemsFunction<
3038

3139
/**
3240
* Creates A function to query the Table
33-
* @param tablename Name of DynamoDB Table
34-
* @param options Key and GSI options
41+
* @param options Options for the query items function
3542
* @returns Function to query table
3643
*/
3744
export const createQueryItems = <
3845
Attributes extends DynamoDBTypes,
3946
TPK extends PK,
4047
TSK extends SK = undefined,
4148
>(
42-
tablename: string,
43-
partitionKeyName: keyof Attributes,
44-
options: CreateQueryOptions<Attributes>,
49+
options: CreateQueryItemsOptions<Attributes>,
4550
): QueryItemsFunction<Attributes, TPK, TSK> => {
46-
const { indexName, sortKeyName } = options;
51+
const { indexName, skName, pkName, tablename } = options;
4752

48-
return (key, options = {}) => {
53+
return (key, { sortKey, dynamodbOptions = {}, filterOptions } = {}) => {
4954
const keyOptions = {
50-
[partitionKeyName]: key,
55+
[pkName]: key,
5156

52-
...maybeMerge(sortKeyName, options.sortKey),
57+
...maybeMerge(skName, sortKey),
5358
} as Partial<Attributes>;
5459

5560
const queryOptions = createQueryOptions(
5661
keyOptions,
5762
indexName,
58-
options.filterOptions,
63+
filterOptions,
5964
);
6065

6166
return queryItems(tablename, {
62-
...options.dynamodbOptions,
67+
...dynamodbOptions,
6368
...queryOptions,
6469
});
6570
};

src/scan.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,26 @@ export type ScanItemsFunction<Attributes extends DynamoDBTypes> = (
1616
options?: ScanOptions<Attributes>,
1717
) => Promise<Attributes[]>;
1818

19+
type CreateScanItemsOptions<_Attributes extends DynamoDBTypes> = {
20+
tablename: string;
21+
};
22+
1923
/**
2024
* Create Function that scan items from ddb table
2125
* @param tablename Tablename
2226
* @returns Function that scans table
2327
*/
2428
export const createScanItems = <Attributes extends DynamoDBTypes>(
25-
tablename: string,
29+
options: CreateScanItemsOptions<Attributes>,
2630
): ScanItemsFunction<Attributes> => {
27-
return (options = {}) => {
28-
const scanOptions = createScanOptions(options.filterOptions);
31+
const { tablename } = options;
32+
33+
return ({ filterOptions, dynamodbOptions } = {}) => {
34+
const scanOptions = createScanOptions(filterOptions);
2935

3036
return scanItems(tablename, {
3137
...scanOptions,
32-
...options.dynamodbOptions,
38+
...dynamodbOptions,
3339
});
3440
};
3541
};

src/table.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@ export const createTableFunctions = <
2020
partitionKeyName: string,
2121
sortKeyName?: string,
2222
) => {
23-
const putItem = createPutItem<Attributes>(tablename);
23+
const putItem = createPutItem<Attributes>({ tablename });
2424

25-
const getItem = createGetItem<Attributes, TPK, TSK>(
25+
const getItem = createGetItem<Attributes, TPK, TSK>({
2626
tablename,
27-
partitionKeyName,
28-
sortKeyName,
29-
);
27+
pkName: partitionKeyName,
28+
skName: sortKeyName,
29+
});
3030

31-
const updateItem = createUpdateItem<Attributes>(
31+
const updateItem = createUpdateItem<Attributes>({
3232
tablename,
33-
partitionKeyName,
34-
sortKeyName,
35-
);
33+
pkName: partitionKeyName,
34+
skName: sortKeyName,
35+
});
3636

37-
const scanItems = createScanItems<Attributes>(tablename);
37+
const scanItems = createScanItems<Attributes>({ tablename });
3838

39-
const deleteItem = createDeleteItem<Attributes, TPK>(
39+
const deleteItem = createDeleteItem<Attributes, TPK>({
4040
tablename,
41-
partitionKeyName,
42-
);
41+
pkName: partitionKeyName,
42+
});
4343

4444
return {
4545
scanItems,

src/update.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,38 @@ export type UpdateItemFunction<Attributes extends DynamoDBTypes> = (
3131
options: UpdateItemOptions<Attributes>,
3232
) => Promise<Attributes | undefined>;
3333

34+
type CreateUpdateItemOptions<Attributes extends DynamoDBTypes> = {
35+
tablename: string;
36+
/**
37+
* Name of the Partitionkey Attribute
38+
*/
39+
pkName: keyof Attributes;
40+
/**
41+
* Name of the Sortkey Attribute
42+
*/
43+
skName?: keyof Attributes;
44+
};
45+
3446
/**
3547
* Create util function that updates item in DB
36-
* @param tablename Tablename
48+
* @param options Options for the update item function
3749
* @returns Function to update item
3850
*/
3951
export const createUpdateItem = <Attributes extends DynamoDBTypes>(
40-
tablename: string,
41-
partitionKeyName: keyof Attributes,
42-
sortKeyName?: keyof Attributes,
52+
options: CreateUpdateItemOptions<Attributes>,
4353
): UpdateItemFunction<Attributes> => {
44-
return async (item, options) => {
45-
const updateOptions = createUpdateOptions(
46-
partitionKeyName,
47-
sortKeyName,
48-
item,
49-
options,
50-
);
54+
const { tablename, pkName, skName } = options;
55+
56+
return async (item, options = {}) => {
57+
const { dynamodbOptions = {}, removeKeys = [] } = options;
58+
59+
const updateOptions = createUpdateOptions(pkName, skName, item, options);
5160
if (!updateOptions) {
5261
return item;
5362
}
5463

5564
const updatedItemSuccess = await updateItem(tablename, {
56-
...options.dynamodbOptions,
65+
...dynamodbOptions,
5766
...updateOptions,
5867
});
5968

@@ -62,7 +71,7 @@ export const createUpdateItem = <Attributes extends DynamoDBTypes>(
6271
}
6372

6473
// remove the values for the updated item
65-
const removeKeys = options.removeKeys ?? [];
74+
6675
for (const key of removeKeys) {
6776
delete item[key];
6877
}

test/delete.smoke.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import test from "ava";
2-
32
import { GetItemCommand, PutItemCommand } from "@aws-sdk/client-dynamodb";
43
import { convertToAttr, marshall } from "@aws-sdk/util-dynamodb";
5-
64
import { createDeleteItem, DDBClient } from "../src";
75
import { createTable } from "./helper/db";
86

@@ -13,7 +11,10 @@ test("Delete removes Item", async (t) => {
1311
BillingMode: "PAY_PER_REQUEST",
1412
});
1513

16-
const deleteItem = createDeleteItem<{ pk: string }, string>(tablename, "pk");
14+
const deleteItem = createDeleteItem<{ pk: string }, string>({
15+
tablename,
16+
pkName: "pk",
17+
});
1718

1819
await DDBClient.instance.send(
1920
new PutItemCommand({ TableName: tablename, Item: marshall({ pk: "1" }) }),
@@ -52,7 +53,7 @@ test("Delete item with SK", async (t) => {
5253
{ pk: string; sk: string },
5354
string,
5455
string
55-
>(tablename, "pk", "sk");
56+
>({ tablename, pkName: "pk", skName: "sk" });
5657

5758
await DDBClient.instance.send(
5859
new PutItemCommand({

test/expression.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import test from "ava";
2-
32
import {
43
expressionAttributeNameKey,
54
expressionAttributeValueKey,

test/get.smoke.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { PutItemCommand } from "@aws-sdk/client-dynamodb";
22
import { marshall } from "@aws-sdk/util-dynamodb";
33
import test from "ava";
4-
54
import { createGetItem, DDBClient } from "../src";
65
import { createTable } from "./helper/db";
76

@@ -12,7 +11,10 @@ test("Get fetches Item", async (t) => {
1211
BillingMode: "PAY_PER_REQUEST",
1312
});
1413

15-
const get = createGetItem<{ pk: string }, string>(tablename, "pk");
14+
const get = createGetItem<{ pk: string }, string>({
15+
tablename,
16+
pkName: "pk",
17+
});
1618

1719
await DDBClient.instance.send(
1820
new PutItemCommand({ TableName: tablename, Item: marshall({ pk: "1" }) }),
@@ -39,11 +41,11 @@ test("Get fetches Item with SK", async (t) => {
3941
BillingMode: "PAY_PER_REQUEST",
4042
});
4143

42-
const get = createGetItem<{ pk: string; sk: string }, string, string>(
44+
const get = createGetItem<{ pk: string; sk: string }, string, string>({
4345
tablename,
44-
"pk",
45-
"sk",
46-
);
46+
pkName: "pk",
47+
skName: "sk",
48+
});
4749

4850
await DDBClient.instance.send(
4951
new PutItemCommand({

0 commit comments

Comments
 (0)