Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: dynamodb purge-partition #9

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
250 changes: 250 additions & 0 deletions packages/cli/src/commands/dynamodb/purge-partition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
import { GluegunCommand, GluegunToolbox } from 'gluegun'
import {
catchError,
count,
map,
mapTo,
mergeMap,
switchMap,
tap
} from 'rxjs/operators'
import {
combineLatest,
from,
iif,
Observable,
of,
OperatorFunction
} from 'rxjs'
import { DynamoDBClient, Table, KeySchema } from '@iwtethys/dynamodb-toolkit'
import { mergeObj } from '../../utils/operators'
import { Credentials, DynamoDB } from 'aws-sdk'
import { HelpOptions } from '../../extensions/help-extension'

// typings until tsconfig adds es6
// declare Object
type FromEntries<T = any> = (
entries: Iterable<readonly [PropertyKey, T]>
) => { [k: string]: T }

// polyfill until node v12 is mandatory
const fromEntries: FromEntries =
(Object as any).fromEntries ||
function fromEntries(iterable: [string, any]) {
return [...iterable].reduce(
(obj, [key, val]) => ({ ...obj, [key]: val }),
{}
)
}

const command: GluegunCommand = {
name: 'purge-partition',
alias: 'pp',
description: 'Deletes an partition key entry with all its sort key entries.',
run: async (toolbox: GluegunToolbox) => {
const { print, input, help } = toolbox
if (help.requested()) {
return help.print(buildHelp(toolbox))
}

await getParameters(toolbox)
.pipe(
confirm(
({ table, partitionKey, partitionKeyValue }) =>
input.confirm({
message: `Proceed deletion of items for partitionKey ${partitionKey}='${partitionKeyValue}' in table ${table.tableName} (${table.accessKeyId})?`,
initial: true
}),
({ table, partitionKeyValue, partitionKey, keySchema }) =>
exec(toolbox, table, partitionKey, partitionKeyValue, keySchema),
() => of(null).pipe(tap(() => print.info('Cancelled')))
)
)
.toPromise()
}
}

const exec = (
toolbox: GluegunToolbox,
table: Table,
partitionKey: string,
partitionKeyValue: string,
keySchema: KeySchema
): Observable<void> => {
const client = new DynamoDB(table)

const { print } = toolbox

const query: DynamoDB.QueryInput = {
TableName: table.tableName,
KeyConditions: {
[partitionKey]: {
ComparisonOperator: 'EQ',
AttributeValueList: [
{
[keySchema[partitionKey]]: partitionKeyValue
}
]
}
}
}

return from(client.query(query).promise()).pipe(
switchMap(({ Items }) => Items), // flatten
map((
// delete non KeySchema attributes
itemAttrMap
) =>
fromEntries(
Object.entries(itemAttrMap).filter(([key]) => key in keySchema)
)
),
mergeMap(item =>
from(
client.deleteItem({ TableName: table.tableName, Key: item }).promise()
)
),
count(),
tap(numDeletions =>
numDeletions > 0
? print.success(`Deleted ${numDeletions} entries`)
: print.warning('No entries found')
),
mapTo(void 0)
)
}

const getParameters = (toolbox: GluegunToolbox): Observable<Parameters> => {
const { aws, input, print, prompt } = toolbox

const getTable = (): Observable<Table> =>
aws.credentials().pipe(
map<Credentials, Table>((credentials: Credentials) => ({
accessKeyId: credentials.accessKeyId,
secretAccessKey: credentials.secretAccessKey,
tableName: null,
region: null
})),
mergeObj<Table, string>(
() =>
input.str({
argumentName: 'first',
message: 'Table name'
}),
(orig, tableName) => ({
...orig,
tableName
})
),
mergeObj<Table, string>(
() =>
aws.region({
defaultValue: 'eu-central-1'
}),
(orig, region) => ({
...orig,
region
})
)
)

const getKeySchema: (parameters: Parameters) => Observable<KeySchema> = (
parameters: Parameters
): Observable<KeySchema> => {
const client = new DynamoDBClient(parameters.table)

return client.getKeySchema().pipe(
catchError(() => {
print.warning('Could not determine key schema.')
return input
.str({
argumentName: 'keySchema',
message: 'Key schema'
})
.pipe(map((str: string) => JSON.parse(str) as KeySchema))
})
)
}

const getPartitionKeyName = ({ keySchema }: Parameters) =>
from(
prompt.ask({
type: 'select',
initial: 0,
choices: Object.keys(keySchema),
message: `Select Partition Key`,
name: 'partitionKeyName',
required: true
})
).pipe(map(response => response.partitionKeyName))

const getPartitionKeyValue = () =>
input.str({
argumentName: 'partitionKeyValue',
message: 'Partition Key Value'
})

return of({}).pipe(
mergeObj<Parameters, Table>(getTable, (orig, table) => ({
...orig,
table: {
...orig.table,
region: table.region,
tableName: table.tableName,
secretAccessKey: table.secretAccessKey,
accessKeyId: table.accessKeyId
}
})),
mergeObj(getKeySchema, (orig, keySchema) => ({
...orig,
keySchema
})),
mergeObj(getPartitionKeyName, (orig, partitionKey) => ({
...orig,
partitionKey
})),
mergeObj(getPartitionKeyValue, (orig, partitionKeyValue) => ({
...orig,
partitionKeyValue
}))
)
}

interface Parameters {
partitionKey: string
partitionKeyValue: string
table: Table
keySchema: KeySchema
}

const confirm = <T, K, R>(
condition: (obj: T) => Observable<boolean>,
trueCb: (obj: T) => Observable<K>,
falseCb: (obj: T) => Observable<R>
): OperatorFunction<T, K | R> => {
return source =>
source.pipe(
switchMap((obj: T) =>
combineLatest([
condition(obj).pipe(catchError(() => of(false))),
of(obj)
])
),
switchMap(([res, obj]) => iif(() => res, trueCb(obj), falseCb(obj)))
)
}

const buildHelp = (toolbox: GluegunToolbox): HelpOptions => ({
usage: `${toolbox.runtime.brand} ${toolbox.command.commandPath.join(
' '
)} <table>`,
arguments: {
'--profile': 'AWS Profile to use to authenticate',
'--access-key-id': 'AWS Access Key Id to use to authenticate',
'--secret-access-key': 'AWS Secret Access Key to use to authenticate',
'--region': 'Region of DynamoDB table to delete',
'--key-schema': 'JSON value representing the schema of the primary index'
}
})

module.exports = command
7 changes: 7 additions & 0 deletions packages/cli/src/extensions/aws-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import * as deepmerge from 'deepmerge'
import { map, switchMap } from 'rxjs/operators'
import { PromptOptions } from 'gluegun/build/types/toolbox/prompt-enquirer-types'

export interface AWSToolboxExtension {
credentials: (options?: CredentialOptions) => Observable<Credentials>
credentialsChain: (...options: CredentialOptions[]) => Observable<Credentials>
region: (options?: RegionOptions) => Observable<string>
regionChain: (...options: RegionOptions[]) => Observable<string>
}

module.exports = (toolbox: GluegunToolbox) => {
const { parameters, print, prompt } = toolbox

Expand Down
11 changes: 11 additions & 0 deletions packages/cli/src/extensions/extensions.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { AWSToolboxExtension } from './aws-extension'
import { HelpToolboxExtension } from './help-extension'
import { InputToolboxExtension } from './input-extension'

declare module 'gluegun/build/types/domain/toolbox' {
interface GluegunToolbox {
input?: InputToolboxExtension
aws?: AWSToolboxExtension
help?: HelpToolboxExtension
}
}
7 changes: 7 additions & 0 deletions packages/cli/src/extensions/help-extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { GluegunToolbox } from 'gluegun'

export interface HelpToolboxExtension {
requested: () => boolean
print: (options: HelpOptions) => void
getSubCommands: (levelAdjustment?: number) => { [key: string]: string }
printNamespaceHelp: (levelAdjustment?: number) => void
}

module.exports = (toolbox: GluegunToolbox) => {
const { parameters, print, runtime, command } = toolbox

Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/extensions/input-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { GluegunToolbox } from 'gluegun'
import { from, Observable, of } from 'rxjs'
import { map } from 'rxjs/operators'

export interface InputToolboxExtension {
str: (options: StrOptions) => Observable<string>
confirm: (options: ConfirmOptions) => Observable<boolean>
}

interface Options<T> {
message: string
initial?: T
Expand Down