Skip to content

Commit

Permalink
docs(snippets): adding snippets for doc
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmumenthaler committed Feb 6, 2019
1 parent 4cbba5c commit ab4272f
Show file tree
Hide file tree
Showing 32 changed files with 367 additions and 0 deletions.
31 changes: 31 additions & 0 deletions snippets/app.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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)

// add a new item
personStore.put({ id: 'wernerv', name: 'Werner Hans Peter Vogels', yearOfBirth: 1958 })
.exec()
.then(() => {
console.log('person stored')
})

// search for a single person by known id
personStore.query()
.wherePartitionKey('wernerv')
.execSingle()
.then(person => {
console.log('got person', person)
})

// returns all persons where the name starts with w
personStore.scan()
.whereAttribute('name').beginsWith('w')
.exec()
.then((persons: Person[]) => {
console.log('all persons', persons)
})
6 changes: 6 additions & 0 deletions snippets/config/date-mapper.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { updateDynamoEasyConfig } from '@shiftcoders/dynamo-easy'
import { dateToNumberMapper } from '../models'

updateDynamoEasyConfig({
dateMapper: dateToNumberMapper
})
10 changes: 10 additions & 0 deletions snippets/config/log-receiver.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { LogInfo, updateDynamoEasyConfig } from '@shiftcoders/dynamo-easy'

updateDynamoEasyConfig({
logReceiver: (logInfo: LogInfo) => {
const msg = `[${logInfo.level}] ${logInfo.timestamp} ${logInfo.className} (${
logInfo.modelConstructor
}): ${logInfo.message}`
console.debug(msg, logInfo.data)
}
})
9 changes: 9 additions & 0 deletions snippets/config/session-validity-ensurer.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { updateDynamoEasyConfig } from '@shiftcoders/dynamo-easy'

updateDynamoEasyConfig({
sessionValidityEnsurer: (): Promise<void> => {
// do whatever you need to do to make sure the session is valid
// and return an Promise<void> when done
return Promise.resolve()
},
})
9 changes: 9 additions & 0 deletions snippets/config/table-name-resolver.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { TableNameResolver, updateDynamoEasyConfig } from '@shiftcoders/dynamo-easy'

const myTableNameResolver: TableNameResolver = (tableName: string) => {
return `myPrefix-${tableName}`
}

updateDynamoEasyConfig({
tableNameResolver: myTableNameResolver
})
10 changes: 10 additions & 0 deletions snippets/looks-easy-right.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from './models'

const personStore = new DynamoStore(Person)

personStore
.scan()
.whereAttribute('yearOfBirth').equals(1958)
.exec()
.then(res => console.log('ALL items with yearOfBirth == 1958', res))
19 changes: 19 additions & 0 deletions snippets/models/another.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { CollectionProperty, DateProperty, Model, PartitionKey, SortKey } from '@shiftcoders/dynamo-easy'

@Model()
export class AnotherModel {
@PartitionKey()
propA: string

@SortKey()
propB: string


propC?: string

@CollectionProperty()
myNestedList?: any[]

@DateProperty()
updated: Date
}
6 changes: 6 additions & 0 deletions snippets/models/date-to-number.mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { MapperForType, NumberAttribute } from '@shiftcoders/dynamo-easy'

export const dateToNumberMapper: MapperForType<Date, NumberAttribute> = {
fromDb: attributeValue => new Date(parseInt(attributeValue.N, 10)),
toDb: propertyValue => ({ N: `${propertyValue.getTime()}` }),
}
3 changes: 3 additions & 0 deletions snippets/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './another.model'
export * from './person.model'
export * from './date-to-number.mapper'
10 changes: 10 additions & 0 deletions snippets/models/my-model-with-gsi.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Model, GSIPartitionKey, GSISortKey } from '@shiftcoders/dynamo-easy'

@Model()
class MyModel {
@GSIPartitionKey('NameOfGSI')
myGsiPartitionKey: string

@GSISortKey('NameOfGSI')
myGsiSortKey: number
}
13 changes: 13 additions & 0 deletions snippets/models/my-model-with-lsi.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Model, LSISortKey, PartitionKey, SortKey } from '@shiftcoders/dynamo-easy'

@Model()
class MyModel {
@PartitionKey()
myPartitionKey: string

@SortKey()
mySortKey: number

@LSISortKey('NameOfLSI')
myLsiSortKey: number
}
10 changes: 10 additions & 0 deletions snippets/models/my-model-with-sort-key.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Model, PartitionKey, SortKey } from '@shiftcoders/dynamo-easy'

@Model()
export class MyModel {
@PartitionKey()
myPartitionKey: string

@SortKey()
mySortKey: number
}
7 changes: 7 additions & 0 deletions snippets/models/my-model-with-transient.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Model, Transient } from '@shiftcoders/dynamo-easy'

@Model()
class MyModel {
@Transient()
myPropertyToIgnore: any
}
7 changes: 7 additions & 0 deletions snippets/models/my-model.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// tslint:disable:no-unnecessary-class
import { Model } from '@shiftcoders/dynamo-easy'

@Model({ tableName: 'my-model-table-name' })
export class MyModel {

}
9 changes: 9 additions & 0 deletions snippets/models/person.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Model, PartitionKey } from '@shiftcoders/dynamo-easy'

@Model()
export class Person {
@PartitionKey()
id: string
name: string
yearOfBirth: number
}
14 changes: 14 additions & 0 deletions snippets/multi-model-requests/batch-get.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { BatchGetRequest, BatchGetResponse } from '@shiftcoders/dynamo-easy'
import { AnotherModel, Person } from '../models'

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

new BatchGetRequest()
.forModel(Person, keysToFetch)
.forModel(AnotherModel, otherKeysToFetch)
.exec()
.then((result: BatchGetResponse) => {
console.log('items fetched from example table', result.tableNameOfExampleModel)
console.log('items fetched from another table', result.tableNameOfAnotherModel)
})
19 changes: 19 additions & 0 deletions snippets/multi-model-requests/batch-write.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { BatchWriteRequest } from '@shiftcoders/dynamo-easy'
import { AnotherModel, Person } from '../models'

const keysToDelete: Array<Partial<Person>> = [{ id: 'vogelsw' }]
const otherKeysToDelete: Array<Partial<AnotherModel>> = [{ propA: 'Foo', propB: 'Bar' }]
const objectsToPut: AnotherModel[] = [
{ propA: 'foo', propB: 'bar', updated: new Date() },
{ propA: 'foo2', propB: 'bar2', updated: new Date() },
]

new BatchWriteRequest()
.returnConsumedCapacity('TOTAL')
.delete(Person, keysToDelete)
.delete(AnotherModel, otherKeysToDelete)
.put(AnotherModel, objectsToPut)
.execFullResponse()
.then(resp => {
console.log(resp.ConsumedCapacity)
})
12 changes: 12 additions & 0 deletions snippets/multi-model-requests/transact-get.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TransactGetRequest } from '@shiftcoders/dynamo-easy'
import { AnotherModel, Person } from '../models'

new TransactGetRequest()
.returnConsumedCapacity('TOTAL')
.forModel(Person, { id: 'vogelsw' })
.forModel(AnotherModel, { propA: 'Foo', propB: 'Bar' })
.exec()
.then(result => {
console.log(result[0]) // Person item
console.log(result[1]) // AnotherModel item
})
21 changes: 21 additions & 0 deletions snippets/multi-model-requests/transact-write.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
attribute,
TransactConditionCheck,
TransactDelete,
TransactPut,
TransactUpdate,
TransactWriteRequest,
} from '@shiftcoders/dynamo-easy'
import { AnotherModel, Person } from '../models'

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

new TransactWriteRequest()
.transact(
new TransactConditionCheck(Person, 'vogelsw').onlyIf(attribute('yearOfBirth').gte(1958)),
new TransactDelete(AnotherModel, 'Foo', 'Bar'),
new TransactPut(AnotherModel, objectToPut),
new TransactUpdate(Person, 'vogelsw').updateAttribute('yearOfBirth').set(1984),
)
.exec()
.then(() => console.log('done'))
7 changes: 7 additions & 0 deletions snippets/store-requests/batch-get.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'

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

new DynamoStore(Person)
.batchWrite()
.delete([{ id: 'a' }, { id: 'b' }])
.put([{ id: 'vogelsw', name: 'Werner Hans Peter Vogels', yearOfBirth: 1958 }])
.exec()
.then(() => console.log('item a, b deleted; werner vogels added'))
8 changes: 8 additions & 0 deletions snippets/store-requests/delete.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'

new DynamoStore(Person)
.delete('vogelsw')
.onlyIfAttribute('yearOfBirth').lte(1958)
.exec()
.then(() => console.log('done'))
8 changes: 8 additions & 0 deletions snippets/store-requests/get.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'

new DynamoStore(Person)
.get('wernerv') // returns an instance of GetRequest
.consistentRead(true) // sets params.ConsistentRead = true
.exec() // returns a Promise<Person|null>
.then(obj => console.log(obj))
14 changes: 14 additions & 0 deletions snippets/store-requests/put.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'

const objectToPut: Person = {
id: 'vogelsw',
name: 'Werner Hans Peter Vogels',
yearOfBirth: 1958,
}

new DynamoStore(Person)
.put(objectToPut)
.ifNotExists()
.exec()
.then(() => console.log('done'))
9 changes: 9 additions & 0 deletions snippets/store-requests/query.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { AnotherModel } from '../models'

new DynamoStore(AnotherModel)
.query()
.wherePartitionKey('2018-01')
.whereSortKey().beginsWith('a')
.execSingle()
.then(r => console.log('first found item:', r))
8 changes: 8 additions & 0 deletions snippets/store-requests/scan.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'

new DynamoStore(Person)
.scan()
.whereAttribute('yearOfBirth').equals(1958)
.execFetchAll()
.then(res => console.log('ALL items with yearOfBirth == 1958', res))
7 changes: 7 additions & 0 deletions snippets/store-requests/transact-get.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'

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

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

new DynamoStore(AnotherModel)
.update('myPartitionKey', 'mySortKey')
.operations(
update(`myNestedList[${index}].propertyX`).set('value'),
update('updated').set(new Date()),
)
.onlyIf(
or(
attribute('id').attributeNotExists(), // item not existing
attribute('updated').lt(oneHourAgo), // or was not updated in the last hour
),
)
.returnValues('ALL_OLD')
.exec()
.then(oldVal => console.log('old value was:', oldVal))
12 changes: 12 additions & 0 deletions snippets/store-requests/update-simple.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { AnotherModel } from '../models'

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

new DynamoStore(AnotherModel)
.update('myPartitionKey', 'mySortKey')
.updateAttribute('propC').set('newValue')
.updateAttribute('updated').set(new Date())
.onlyIfAttribute('updated').lt(oneHourAgo)
.exec()
.then(() => console.log('done'))
4 changes: 4 additions & 0 deletions snippets/store.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from './models'

const personStore = new DynamoStore(Person)
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
],
"exclude": [
"test/**/*",
"snippets/**/*",
"**/*.spec.ts"
]
}
Loading

0 comments on commit ab4272f

Please sign in to comment.