Skip to content

Commit

Permalink
Merge pull request #697 from jeremydaly/create-primary-key-parser-ope…
Browse files Browse the repository at this point in the history
…ration

create primary key parser operation
  • Loading branch information
ThomasAribart committed Mar 22, 2024
2 parents dd4b488 + 0e51107 commit d133829
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { DeleteCommandInput } from '@aws-sdk/lib-dynamodb'

import type { EntityV2 } from 'v1/entity/class'
import type { KeyInput } from 'v1/operations/types'
import { parsePrimaryKey } from 'v1/operations/utils/parsePrimaryKey'
import { PrimaryKeyParser } from 'v1/operations/primaryKeyParser'
import { Parser } from 'v1/schema/actions/parse'

import type { DeleteItemOptions } from '../options'
Expand All @@ -25,7 +25,7 @@ export const deleteItemParams = <
const transformedInput = parser.next().value

const keyInput = entity.computeKey ? entity.computeKey(validKeyInput) : transformedInput
const primaryKey = parsePrimaryKey(entity, keyInput)
const primaryKey = entity.build(PrimaryKeyParser).parse(keyInput)

const options = parseDeleteItemOptions(entity, deleteItemOptions)

Expand Down
4 changes: 2 additions & 2 deletions src/v1/operations/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { ErrorBlueprint } from 'v1/errors/blueprint'

import type { ScanCommandErrorBlueprints } from './scan/errors'
import type { QueryCommandErrorBlueprints } from './query/errors'
import type { OperationUtilsErrorBlueprints } from './utils/errors'
import type { ExpressionParsersErrorBlueprints } from './expression/errors'
import type { PrimaryKeyParserErrorBlueprints } from './primaryKeyParser/errors'

type IncompleteOperationErrorBlueprint = ErrorBlueprint<{
code: 'operations.incompleteOperation'
Expand Down Expand Up @@ -74,7 +74,7 @@ type UnknownOptionErrorBlueprint = ErrorBlueprint<{
export type OperationsErrorBlueprints =
| ScanCommandErrorBlueprints
| QueryCommandErrorBlueprints
| OperationUtilsErrorBlueprints
| PrimaryKeyParserErrorBlueprints
| IncompleteOperationErrorBlueprint
| InvalidCapacityOptionErrorBlueprint
| InvalidClientRequestTokenOptionErrorBlueprint
Expand Down
4 changes: 2 additions & 2 deletions src/v1/operations/getItem/getItemParams/getItemParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { GetCommandInput } from '@aws-sdk/lib-dynamodb'

import type { EntityV2 } from 'v1/entity/class'
import type { KeyInput } from 'v1/operations/types'
import { parsePrimaryKey } from 'v1/operations/utils/parsePrimaryKey'
import { PrimaryKeyParser } from 'v1/operations/primaryKeyParser'
import { Parser } from 'v1/schema/actions/parse'

import type { GetItemOptions } from '../options'
Expand All @@ -22,7 +22,7 @@ export const getItemParams = <ENTITY extends EntityV2, OPTIONS extends GetItemOp
const transformedInput = parser.next().value

const keyInput = entity.computeKey ? entity.computeKey(validKeyInput) : transformedInput
const primaryKey = parsePrimaryKey(entity, keyInput)
const primaryKey = entity.build(PrimaryKeyParser).parse(keyInput)

const options = parseGetItemOptions(entity, getItemOptions)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ type InvalidKeyPartErrorBlueprint = ErrorBlueprint<{
}
}>

export type ParsePrimaryKeyErrorBlueprints = InvalidKeyPartErrorBlueprint
export type PrimaryKeyParserErrorBlueprints = InvalidKeyPartErrorBlueprint
1 change: 1 addition & 0 deletions src/v1/operations/primaryKeyParser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { PrimaryKeyParser } from './primaryKeyParser'
70 changes: 70 additions & 0 deletions src/v1/operations/primaryKeyParser/primaryKeyParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { EntityV2 } from 'v1/entity/class'
import type { Schema } from 'v1/schema/schema'
import type { ParsedValue } from 'v1/schema/actions/parse'
import type { PrimaryKey } from 'v1/table/generics'
import { validatorsByPrimitiveType } from 'v1/utils/validation'
import { DynamoDBToolboxError } from 'v1/errors/dynamoDBToolboxError'

import { $entity, EntityOperation } from '../class'

// TODO: Move to table operations
export class PrimaryKeyParser<ENTITY extends EntityV2 = EntityV2> extends EntityOperation<ENTITY> {
static operationName = 'parsePrimaryKey' as const

constructor(entity: ENTITY) {
super(entity)
}

parse(keyInput: ParsedValue<Schema, { operation: 'key' }>): PrimaryKey<ENTITY['table']> {
const { table } = this[$entity]
const { partitionKey, sortKey } = table

const primaryKey: ParsedValue<Schema, { operation: 'key' }> = {}

const partitionKeyValidator = validatorsByPrimitiveType[partitionKey.type]
const partitionKeyValue = keyInput[partitionKey.name]

if (!partitionKeyValidator(partitionKeyValue)) {
throw new DynamoDBToolboxError('operations.parsePrimaryKey.invalidKeyPart', {
message: `Invalid partition key: ${partitionKey.name}`,
path: partitionKey.name,
payload: {
expected: partitionKey.type,
received: partitionKeyValue,
keyPart: 'partitionKey'
}
})
}

/**
* @debt type "TODO: Make validator act as primitive typeguard"
*/
primaryKey[partitionKey.name] = partitionKeyValue as number | string | Buffer

if (sortKey === undefined) {
return primaryKey as PrimaryKey<ENTITY['table']>
}

const sortKeyValidator = validatorsByPrimitiveType[sortKey.type]
const sortKeyValue = keyInput[sortKey.name]

if (!sortKeyValidator(sortKeyValue)) {
throw new DynamoDBToolboxError('operations.parsePrimaryKey.invalidKeyPart', {
message: `Invalid sort key: ${sortKey.name}`,
path: sortKey.name,
payload: {
expected: sortKey.type,
received: sortKeyValue,
keyPart: 'sortKey'
}
})
}

/**
* @debt type "TODO: Make validator act as primitive typeguard"
*/
primaryKey[sortKey.name] = sortKeyValue as number | string | Buffer

return primaryKey as PrimaryKey<ENTITY['table']>
}
}
4 changes: 2 additions & 2 deletions src/v1/operations/putItem/putItemParams/putItemParams.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PutCommandInput } from '@aws-sdk/lib-dynamodb'

import type { EntityV2 } from 'v1/entity/class'
import { parsePrimaryKey } from 'v1/operations/utils/parsePrimaryKey'
import { PrimaryKeyParser } from 'v1/operations/primaryKeyParser'
import { Parser } from 'v1/schema/actions/parse'

import type { PutItemInput } from '../types'
Expand All @@ -22,7 +22,7 @@ export const putItemParams = <ENTITY extends EntityV2, OPTIONS extends PutItemOp
const transformedInput = parser.next().value

const keyInput = entity.computeKey ? entity.computeKey(validInput) : transformedInput
const primaryKey = parsePrimaryKey(entity, keyInput)
const primaryKey = entity.build(PrimaryKeyParser).parse(keyInput)

const options = parsePutItemOptions(entity, putItemOptions)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { TransactWriteCommandInput } from '@aws-sdk/lib-dynamodb'

import type { EntityV2 } from 'v1/entity/class'
import type { KeyInput } from 'v1/operations/types'
import { parsePrimaryKey } from 'v1/operations/utils/parsePrimaryKey'
import { PrimaryKeyParser } from 'v1/operations/primaryKeyParser'
import { Parser } from 'v1/schema/actions/parse'

import type { DeleteItemTransactionOptions } from '../options'
Expand All @@ -29,7 +29,7 @@ export const transactDeleteItemParams = <
const transformedInput = parser.next().value

const keyInput = entity.computeKey ? entity.computeKey(validKeyInput) : transformedInput
const primaryKey = parsePrimaryKey(entity, keyInput)
const primaryKey = entity.build(PrimaryKeyParser).parse(keyInput)

const options = parseDeleteItemTransactionOptions(entity, deleteItemTransactionOptions)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { TransactWriteCommandInput } from '@aws-sdk/lib-dynamodb'

import type { EntityV2 } from 'v1/entity/class'
import type { PutItemInput } from 'v1/operations/putItem'
import { parsePrimaryKey } from 'v1/operations/utils/parsePrimaryKey'
import { PrimaryKeyParser } from 'v1/operations/primaryKeyParser'
import { Parser } from 'v1/schema/actions/parse'

import type { PutItemTransactionOptions } from '../options'
Expand All @@ -28,7 +28,7 @@ export const transactPutItemParams = <
const transformedInput = parser.next().value

const keyInput = entity.computeKey ? entity.computeKey(validInput) : transformedInput
const primaryKey = parsePrimaryKey(entity, keyInput)
const primaryKey = entity.build(PrimaryKeyParser).parse(keyInput)

const options = parsePutItemTransactionOptions(entity, putItemTransactionOptions)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { EntityV2 } from 'v1/entity/class'
import type { UpdateItemInput } from 'v1/operations/updateItem'
import { parseUpdate } from 'v1/operations/updateItem/updateExpression/parse'
import { parseUpdateExtension } from 'v1/operations/updateItem/updateItemParams/extension/parseExtension'
import { parsePrimaryKey } from 'v1/operations/utils/parsePrimaryKey'
import { PrimaryKeyParser } from 'v1/operations/primaryKeyParser'
import { Parser } from 'v1/schema/actions/parse'

import type { UpdateItemTransactionOptions } from '../options'
Expand Down Expand Up @@ -36,7 +36,7 @@ export const transactUpdateItemParams = <
const transformedInput = parser.next().value

const keyInput = entity.computeKey ? entity.computeKey(validInput) : transformedInput
const primaryKey = parsePrimaryKey(entity, keyInput)
const primaryKey = entity.build(PrimaryKeyParser).parse(keyInput)

const {
ExpressionAttributeNames: updateExpressionAttributeNames,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import isEmpty from 'lodash.isempty'
import omit from 'lodash.omit'

import type { EntityV2 } from 'v1/entity/class'
import { parsePrimaryKey } from 'v1/operations/utils/parsePrimaryKey'
import { PrimaryKeyParser } from 'v1/operations/primaryKeyParser'
import { Parser } from 'v1/schema/actions/parse'

import type { UpdateItemInput } from '../types'
Expand Down Expand Up @@ -31,7 +31,7 @@ export const updateItemParams = <
const transformedInput = parser.next().value

const keyInput = entity.computeKey ? entity.computeKey(validInput) : transformedInput
const primaryKey = parsePrimaryKey(entity, keyInput)
const primaryKey = entity.build(PrimaryKeyParser).parse(keyInput)

const {
ExpressionAttributeNames: updateExpressionAttributeNames,
Expand Down
3 changes: 0 additions & 3 deletions src/v1/operations/utils/errors.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/v1/operations/utils/parsePrimaryKey/index.ts

This file was deleted.

61 changes: 0 additions & 61 deletions src/v1/operations/utils/parsePrimaryKey/parsePrimaryKey.ts

This file was deleted.

0 comments on commit d133829

Please sign in to comment.