Skip to content

Commit

Permalink
feat(aws-sdk-v3): resolve ts issues in snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Wittwer committed Feb 21, 2023
1 parent e561484 commit e2bcb11
Show file tree
Hide file tree
Showing 20 changed files with 42 additions and 25 deletions.
6 changes: 2 additions & 4 deletions snippets/app.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import {DynamoDB} from '@aws-sdk/client-dynamodb'
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import * as AWS from 'aws-sdk/global'
import { Person } from './models'

// update the aws config with your credentials to enable successful connection
AWS.config.update({ region: 'yourAwsRegion' })

const personStore = new DynamoStore(Person)
const personStore = new DynamoStore(Person, new DynamoDB({ region: 'yourAwsRegion' }))

// add a new item
personStore.put({ id: 'wernerv', name: 'Werner Hans Peter Vogels', yearOfBirth: 1958 })
Expand Down
3 changes: 2 additions & 1 deletion snippets/expressions/attribute2-condition.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {DynamoDB} from '@aws-sdk/client-dynamodb'
import { attribute2, DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'

const personStore = new DynamoStore(Person)
const personStore = new DynamoStore(Person, new DynamoDB({}))

personStore.delete('volgelsw')
.onlyIf(
Expand Down
3 changes: 2 additions & 1 deletion snippets/expressions/or-not-and-condition.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { and, attribute, DynamoStore, not, or } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'
import { DynamoDB } from '@aws-sdk/client-dynamodb'

const personStore = new DynamoStore(Person)
const personStore = new DynamoStore(Person, new DynamoDB({}))
personStore.delete('vogelsw')
.onlyIf(
or(
Expand Down
3 changes: 2 additions & 1 deletion snippets/expressions/update-expression.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { DynamoStore, update } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'
import { DynamoDB } from '@aws-sdk/client-dynamodb'

const personStore = new DynamoStore(Person)
const personStore = new DynamoStore(Person, new DynamoDB({}))
personStore.update('vogelsw')
.operations(
update('name').set('Werner Vogels'),
Expand Down
3 changes: 2 additions & 1 deletion snippets/looks-easy-right.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from './models'
import { DynamoDB } from '@aws-sdk/client-dynamodb'

const personStore = new DynamoStore(Person)
const personStore = new DynamoStore(Person, new DynamoDB({}))

personStore
.scan()
Expand Down
3 changes: 2 additions & 1 deletion snippets/multi-model-requests/batch-get.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { BatchGetRequest, BatchGetResponse } from '@shiftcoders/dynamo-easy'
import { AnotherModel, Person } from '../models'
import { DynamoDB } from '@aws-sdk/client-dynamodb'

const keysToFetch: Array<Partial<Person>> = [{ id: 'vogelsw' }]
const otherKeysToFetch: Array<Partial<AnotherModel>> = [{ propA: 'Foo', propB: 'Bar' }]

new BatchGetRequest()
new BatchGetRequest(new DynamoDB({}))
.forModel(Person, keysToFetch)
.forModel(AnotherModel, otherKeysToFetch)
.exec()
Expand Down
5 changes: 3 additions & 2 deletions snippets/multi-model-requests/batch-write.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BatchWriteRequest } from '@shiftcoders/dynamo-easy'
import { AnotherModel, Person } from '../models'
import { DynamoDB, ReturnConsumedCapacity } from '@aws-sdk/client-dynamodb'

const keysToDelete: Array<Partial<Person>> = [{ id: 'vogelsw' }]
const otherKeysToDelete: Array<Partial<AnotherModel>> = [{ propA: 'Foo', propB: 'Bar' }]
Expand All @@ -8,8 +9,8 @@ const objectsToPut: AnotherModel[] = [
{ propA: 'foo2', propB: 'bar2', updated: new Date() },
]

new BatchWriteRequest()
.returnConsumedCapacity('TOTAL')
new BatchWriteRequest(new DynamoDB({}))
.returnConsumedCapacity(ReturnConsumedCapacity.TOTAL)
.delete(Person, keysToDelete)
.delete(AnotherModel, otherKeysToDelete)
.put(AnotherModel, objectsToPut)
Expand Down
5 changes: 3 additions & 2 deletions snippets/multi-model-requests/transact-get.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { TransactGetRequest } from '@shiftcoders/dynamo-easy'
import { AnotherModel, Person } from '../models'
import { DynamoDB, ReturnConsumedCapacity } from '@aws-sdk/client-dynamodb'

new TransactGetRequest()
.returnConsumedCapacity('TOTAL')
new TransactGetRequest(new DynamoDB({}))
.returnConsumedCapacity(ReturnConsumedCapacity.TOTAL)
.forModel(Person, { id: 'vogelsw' })
.forModel(AnotherModel, { propA: 'Foo', propB: 'Bar' })
.exec()
Expand Down
3 changes: 2 additions & 1 deletion snippets/multi-model-requests/transact-write.snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import {
TransactWriteRequest,
} from '@shiftcoders/dynamo-easy'
import { AnotherModel, Person } from '../models'
import { DynamoDB } from '@aws-sdk/client-dynamodb'

const objectToPut: AnotherModel = { propA: 'Foo', propB: 'Bar', updated: new Date() }

new TransactWriteRequest()
new TransactWriteRequest(new DynamoDB({}))
.transact(
new TransactConditionCheck(Person, 'vogelsw').onlyIf(attribute('yearOfBirth').gte(1958)),
new TransactDelete(AnotherModel, 'Foo', 'Bar'),
Expand Down
3 changes: 2 additions & 1 deletion snippets/store-requests/batch-get.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'
import { DynamoDB } from '@aws-sdk/client-dynamodb'

new DynamoStore(Person)
new DynamoStore(Person, new DynamoDB({}))
.batchGet([{ id: 'a' }, { id: 'b' }])
.exec()
.then(res => console.log('fetched items:', res))
3 changes: 2 additions & 1 deletion snippets/store-requests/batch-write.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'
import { DynamoDB } from '@aws-sdk/client-dynamodb'

new DynamoStore(Person)
new DynamoStore(Person, new DynamoDB({}))
.batchWrite()
.delete([{ id: 'a' }, { id: 'b' }])
.put([{ id: 'vogelsw', name: 'Werner Hans Peter Vogels', yearOfBirth: 1958 }])
Expand Down
3 changes: 2 additions & 1 deletion snippets/store-requests/delete.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'
import { DynamoDB } from '@aws-sdk/client-dynamodb'

new DynamoStore(Person)
new DynamoStore(Person, new DynamoDB({}))
.delete('vogelsw')
.onlyIfAttribute('yearOfBirth').lte(1958)
.exec()
Expand Down
3 changes: 2 additions & 1 deletion snippets/store-requests/get.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'
import { DynamoDB } from '@aws-sdk/client-dynamodb'

new DynamoStore(Person)
new DynamoStore(Person, new DynamoDB({}))
.get('wernerv') // returns an instance of GetRequest
.consistentRead(true) // sets params.ConsistentRead = true
.exec() // returns a Promise<Person|null>
Expand Down
3 changes: 2 additions & 1 deletion snippets/store-requests/put.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'
import { DynamoDB } from '@aws-sdk/client-dynamodb'

const objectToPut: Person = {
id: 'vogelsw',
name: 'Werner Hans Peter Vogels',
yearOfBirth: 1958,
} // object literal or new Person(...)

new DynamoStore(Person)
new DynamoStore(Person, new DynamoDB({}))
.put(objectToPut)
.ifNotExists()
.exec()
Expand Down
3 changes: 2 additions & 1 deletion snippets/store-requests/query.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { AnotherModel } from '../models'
import { DynamoDB } from '@aws-sdk/client-dynamodb'

new DynamoStore(AnotherModel)
new DynamoStore(AnotherModel, new DynamoDB({}))
.query()
.wherePartitionKey('2018-01')
.whereSortKey().beginsWith('a')
Expand Down
3 changes: 2 additions & 1 deletion snippets/store-requests/scan.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'
import { DynamoDB } from '@aws-sdk/client-dynamodb'

new DynamoStore(Person)
new DynamoStore(Person, new DynamoDB({}))
.scan()
.whereAttribute('yearOfBirth').equals(1958)
.execFetchAll()
Expand Down
3 changes: 2 additions & 1 deletion snippets/store-requests/transact-get.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'
import { DynamoDB } from '@aws-sdk/client-dynamodb'

new DynamoStore(Person)
new DynamoStore(Person, new DynamoDB({}))
.transactGet([{ id: 'a' }, { id: 'b' }])
.exec()
.then(() => console.log('transactionally read a and b'))
3 changes: 2 additions & 1 deletion snippets/store-requests/update-complex.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { attribute, DynamoStore, or, update } from '@shiftcoders/dynamo-easy'
import { AnotherModel } from '../models'
import { DynamoDB } from '@aws-sdk/client-dynamodb'

const index = 3
const oneHourAgo = new Date(Date.now() - 1000 * 60 * 60)

new DynamoStore(AnotherModel)
new DynamoStore(AnotherModel, new DynamoDB({}))
.update('myPartitionKey', 'mySortKey')
.operations(
update(`myNestedList[${index}].propertyX`).set('value'),
Expand Down
3 changes: 2 additions & 1 deletion snippets/store-requests/update-simple.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { AnotherModel } from '../models'
import { DynamoDB } from '@aws-sdk/client-dynamodb'

const oneHourAgo = new Date(Date.now() - 1000 * 60 * 60)

new DynamoStore(AnotherModel)
new DynamoStore(AnotherModel, new DynamoDB({}))
.update('myPartitionKey', 'mySortKey')
.updateAttribute('propC').set('newValue')
.updateAttribute('updated').set(new Date())
Expand Down
3 changes: 2 additions & 1 deletion snippets/store.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from './models'
import { DynamoDB } from '@aws-sdk/client-dynamodb'

const personStore = new DynamoStore(Person)
const personStore = new DynamoStore(Person, new DynamoDB({}))

0 comments on commit e2bcb11

Please sign in to comment.