Skip to content

Commit

Permalink
Merge pull request #659 from guillaumeduboc/feat/optionnal-doc-client
Browse files Browse the repository at this point in the history
feat : make document client optional in tables
  • Loading branch information
ThomasAribart committed Mar 22, 2024
2 parents d133829 + 77420a3 commit 6cd0366
Show file tree
Hide file tree
Showing 24 changed files with 63 additions and 151 deletions.
2 changes: 1 addition & 1 deletion src/v1/operations/batch/batchWrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const batchWrite = async (
} = {}
): Promise<BatchWriteCommandOutput> => {
const documentClient =
options?.dynamoDBDocumentClient ?? requests[0][$entity].table.documentClient
options?.dynamoDBDocumentClient ?? requests[0][$entity].table.getDocumentClient()

const commandInput = getBatchWriteCommandInput(requests, options?.batchWriteOptions)

Expand Down
13 changes: 2 additions & 11 deletions src/v1/operations/batch/batchWrite.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'

import { DynamoDBToolboxError, EntityV2, TableV2, schema, string } from 'v1'
import { BatchPutItemRequest } from './putItem/operation'
import { BatchDeleteItemRequest } from './deleteItem/operation'
import { getBatchWriteCommandInput } from './batchWrite'

const dynamoDbClient = new DynamoDBClient({})

const documentClient = DynamoDBDocumentClient.from(dynamoDbClient)

const TestTable = new TableV2({
name: 'test-table',
partitionKey: {
Expand All @@ -19,8 +12,7 @@ const TestTable = new TableV2({
sortKey: {
type: 'string',
name: 'sk'
},
documentClient
}
})
const TestTable2 = new TableV2({
name: 'test-table2',
Expand All @@ -38,8 +30,7 @@ const TestTable2 = new TableV2({
sortKey: { name: 'GSIsk1', type: 'string' },
type: 'global'
}
},
documentClient
}
})

const TestEntity = new EntityV2({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'

import { DynamoDBToolboxError, EntityV2, TableV2, schema, string } from 'v1'
import { BatchDeleteItemRequest } from './operation'

const dynamoDbClient = new DynamoDBClient({})

const documentClient = DynamoDBDocumentClient.from(dynamoDbClient)

const TestTable = new TableV2({
name: 'test-table',
partitionKey: {
Expand All @@ -17,8 +10,7 @@ const TestTable = new TableV2({
sortKey: {
type: 'string',
name: 'sk'
},
documentClient
}
})

const TestEntity = new EntityV2({
Expand Down
10 changes: 1 addition & 9 deletions src/v1/operations/batch/putItem/BatchPutItemParams.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'

import { DynamoDBToolboxError, EntityV2, TableV2, schema, string } from 'v1'
import { BatchPutItemRequest } from './operation'

const dynamoDbClient = new DynamoDBClient({})

const documentClient = DynamoDBDocumentClient.from(dynamoDbClient)

const TestTable = new TableV2({
name: 'test-table',
partitionKey: {
Expand All @@ -17,8 +10,7 @@ const TestTable = new TableV2({
sortKey: {
type: 'string',
name: 'sk'
},
documentClient
}
})

const TestEntity = new EntityV2({
Expand Down
6 changes: 3 additions & 3 deletions src/v1/operations/deleteItem/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ export class DeleteItemCommand<
async send(): Promise<DeleteItemResponse<ENTITY, OPTIONS>> {
const deleteItemParams = this.params()

const commandOutput = await this[$entity].table.documentClient.send(
new DeleteCommand(deleteItemParams)
)
const commandOutput = await this[$entity].table
.getDocumentClient()
.send(new DeleteCommand(deleteItemParams))

const { Attributes: attributes, ...restCommandOutput } = commandOutput

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'

import {
TableV2,
EntityV2,
Expand All @@ -11,10 +8,6 @@ import {
prefix
} from 'v1'

const dynamoDbClient = new DynamoDBClient({})

const documentClient = DynamoDBDocumentClient.from(dynamoDbClient)

const TestTable = new TableV2({
name: 'test-table',
partitionKey: {
Expand All @@ -24,8 +17,7 @@ const TestTable = new TableV2({
sortKey: {
type: 'string',
name: 'sk'
},
documentClient
}
})

const TestEntity = new EntityV2({
Expand Down
7 changes: 7 additions & 0 deletions src/v1/operations/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ type UnknownOptionErrorBlueprint = ErrorBlueprint<{
payload: { option: unknown }
}>

type MissingDocumentClientErrorBlueprint = ErrorBlueprint<{
code: 'operations.missingDocumentClient'
hasPath: false
payload: undefined
}>

export type OperationsErrorBlueprints =
| ScanCommandErrorBlueprints
| QueryCommandErrorBlueprints
Expand All @@ -87,3 +93,4 @@ export type OperationsErrorBlueprints =
| InvalidSelectOptionErrorBlueprint
| UnknownOptionErrorBlueprint
| ExpressionParsersErrorBlueprints
| MissingDocumentClientErrorBlueprint
6 changes: 3 additions & 3 deletions src/v1/operations/getItem/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ export class GetItemCommand<
async send(): Promise<GetItemResponse<ENTITY, OPTIONS>> {
const getItemParams = this.params()

const commandOutput = await this[$entity].table.documentClient.send(
new GetCommand(getItemParams)
)
const commandOutput = await this[$entity].table
.getDocumentClient()
.send(new GetCommand(getItemParams))

const { Item: item, ...restCommandOutput } = commandOutput

Expand Down
13 changes: 2 additions & 11 deletions src/v1/operations/getItem/getItemParams/getItemParams.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'

import { TableV2, EntityV2, schema, string, DynamoDBToolboxError, GetItemCommand, prefix } from 'v1'

const dynamoDbClient = new DynamoDBClient({})

const documentClient = DynamoDBDocumentClient.from(dynamoDbClient)

const TestTable = new TableV2({
name: 'test-table',
partitionKey: {
Expand All @@ -16,8 +9,7 @@ const TestTable = new TableV2({
sortKey: {
type: 'string',
name: 'sk'
},
documentClient
}
})

const TestEntity = new EntityV2({
Expand All @@ -33,8 +25,7 @@ const TestEntity = new EntityV2({
const TestTable2 = new TableV2({
name: 'test-table',
partitionKey: { type: 'string', name: 'pk' },
sortKey: { type: 'string', name: 'sk' },
documentClient
sortKey: { type: 'string', name: 'sk' }
})

const TestEntity2 = new EntityV2({
Expand Down
6 changes: 3 additions & 3 deletions src/v1/operations/putItem/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ export class PutItemCommand<
async send(): Promise<PutItemResponse<ENTITY, OPTIONS>> {
const putItemParams = this.params()

const commandOutput = await this[$entity].table.documentClient.send(
new PutCommand(putItemParams)
)
const commandOutput = await this[$entity].table
.getDocumentClient()
.send(new PutCommand(putItemParams))

const { Attributes: attributes, ...restCommandOutput } = commandOutput

Expand Down
16 changes: 3 additions & 13 deletions src/v1/operations/putItem/putItemParams/putItemParams.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'

import {
TableV2,
EntityV2,
Expand All @@ -19,10 +16,6 @@ import {
prefix
} from 'v1'

const dynamoDbClient = new DynamoDBClient({})

const documentClient = DynamoDBDocumentClient.from(dynamoDbClient)

const TestTable = new TableV2({
name: 'test-table',
partitionKey: {
Expand All @@ -32,8 +25,7 @@ const TestTable = new TableV2({
sortKey: {
type: 'string',
name: 'sk'
},
documentClient
}
})

const TestEntity = new EntityV2({
Expand Down Expand Up @@ -62,8 +54,7 @@ const TestEntity = new EntityV2({

const TestTable2 = new TableV2({
name: 'test-table',
partitionKey: { type: 'string', name: 'pk' },
documentClient
partitionKey: { type: 'string', name: 'pk' }
})

const TestEntity2 = new EntityV2({
Expand Down Expand Up @@ -97,8 +88,7 @@ const TestEntity3 = new EntityV2({
const TestTable3 = new TableV2({
name: 'TestTable3',
partitionKey: { type: 'string', name: 'pk' },
sortKey: { type: 'string', name: 'sk' },
documentClient
sortKey: { type: 'string', name: 'sk' }
})

const TestEntity4 = new EntityV2({
Expand Down
2 changes: 1 addition & 1 deletion src/v1/operations/query/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class QueryCommand<
ScannedCount: pageScannedCount,
ConsumedCapacity: pageConsumedCapacity,
$metadata: pageMetadata
} = await this[$table].documentClient.send(new _QueryCommand(pageQueryParams))
} = await this[$table].getDocumentClient().send(new _QueryCommand(pageQueryParams))

for (const item of items) {
const itemEntityName = item[this[$table].entityAttributeSavedAs]
Expand Down
9 changes: 1 addition & 8 deletions src/v1/operations/query/queryParams/queryParams.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'
import type { A } from 'ts-toolbelt'

import {
Expand All @@ -14,10 +12,6 @@ import {
prefix
} from 'v1'

const dynamoDbClient = new DynamoDBClient({})

const documentClient = DynamoDBDocumentClient.from(dynamoDbClient)

const TestTable = new TableV2({
name: 'test-table',
partitionKey: {
Expand Down Expand Up @@ -47,8 +41,7 @@ const TestTable = new TableV2({
type: 'string'
}
}
},
documentClient
}
})

const Entity1 = new EntityV2({
Expand Down
2 changes: 1 addition & 1 deletion src/v1/operations/scan/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class ScanCommand<
ScannedCount: pageScannedCount,
ConsumedCapacity: pageConsumedCapacity,
$metadata: pageMetadata
} = await this[$table].documentClient.send(new _ScanCommand(pageScanParams))
} = await this[$table].getDocumentClient().send(new _ScanCommand(pageScanParams))

for (const item of items) {
const itemEntityName = item[this[$table].entityAttributeSavedAs]
Expand Down
9 changes: 1 addition & 8 deletions src/v1/operations/scan/scanParams/scanParams.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'
import type { A } from 'ts-toolbelt'

import {
Expand All @@ -14,10 +12,6 @@ import {
prefix
} from 'v1'

const dynamoDbClient = new DynamoDBClient({})

const documentClient = DynamoDBDocumentClient.from(dynamoDbClient)

const TestTable = new TableV2({
name: 'test-table',
partitionKey: {
Expand All @@ -40,8 +34,7 @@ const TestTable = new TableV2({
type: 'string'
}
}
},
documentClient
}
})

const Entity1 = new EntityV2({
Expand Down
2 changes: 1 addition & 1 deletion src/v1/operations/transactions/deleteItem/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class DeleteItemTransaction<
params: TransactDeleteItemParams
} {
return {
documentClient: this[$entity].table.documentClient,
documentClient: this[$entity].table.getDocumentClient(),
type: 'Delete',
params: this.params()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'

import { TableV2, EntityV2, schema, string, DynamoDBToolboxError } from 'v1'
import { DeleteItemTransaction } from '../operation'

const dynamoDbClient = new DynamoDBClient({})

const documentClient = DynamoDBDocumentClient.from(dynamoDbClient)

const TestTable = new TableV2({
name: 'test-table',
partitionKey: {
Expand All @@ -17,8 +10,7 @@ const TestTable = new TableV2({
sortKey: {
type: 'string',
name: 'sk'
},
documentClient
}
})

const TestEntity = new EntityV2({
Expand Down
2 changes: 1 addition & 1 deletion src/v1/operations/transactions/putItem/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class PutItemTransaction<
params: TransactPutItemParams
} {
return {
documentClient: this[$entity].table.documentClient,
documentClient: this[$entity].table.getDocumentClient(),
type: 'Put',
params: this.params()
}
Expand Down

0 comments on commit 6cd0366

Please sign in to comment.