Skip to content

Commit 187bffb

Browse files
Merge pull request #173 from shiftcode/#92-code-snippets
#92 code snippets
2 parents 5de726c + 80ec536 commit 187bffb

34 files changed

+369
-1
lines changed

.huskyrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
hooks:
22
commit-msg: "commitlint -E HUSKY_GIT_PARAMS"
33
pre-commit: "lint-staged"
4-
pre-push: "npm run test && npm run build"
4+
pre-push: "npm run check-snippets && npm run test && npm run build"

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"prebuild": "rimraf dist",
3030
"build": "tsc --outDir ./dist/_esm2015/ && tsc --outDir ./dist/_esm5/ --target es5 && tsc --outDir ./dist/ --module commonjs",
3131
"build:docs": "typedoc --out dist/docs --target es6 --theme minimal",
32+
"check-snippets": "npx tsc --outDir ./dist-snippets --project ./tsconfig.snippets.json && rimraf dist-snippets",
3233
"commity": "commit",
3334
"deploy-docs": "ts-node tools/gh-pages-publish",
3435
"lint": "tslint --project tsconfig.json -t codeFrame --fix",

snippets/app.snippet.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { DynamoStore } from '@shiftcoders/dynamo-easy'
2+
import * as AWS from 'aws-sdk/global'
3+
import { Person } from './models'
4+
5+
// update the aws config with your credentials to enable successful connection
6+
AWS.config.update({ region: 'yourAwsRegion' })
7+
8+
const personStore = new DynamoStore(Person)
9+
10+
// add a new item
11+
personStore.put({ id: 'wernerv', name: 'Werner Hans Peter Vogels', yearOfBirth: 1958 })
12+
.exec()
13+
.then(() => {
14+
console.log('person stored')
15+
})
16+
17+
// search for a single person by known id
18+
personStore.query()
19+
.wherePartitionKey('wernerv')
20+
.execSingle()
21+
.then(person => {
22+
console.log('got person', person)
23+
})
24+
25+
// returns all persons where the name starts with w
26+
personStore.scan()
27+
.whereAttribute('name').beginsWith('w')
28+
.exec()
29+
.then((persons: Person[]) => {
30+
console.log('all persons', persons)
31+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { updateDynamoEasyConfig } from '@shiftcoders/dynamo-easy'
2+
import { dateToNumberMapper } from '../models'
3+
4+
updateDynamoEasyConfig({
5+
dateMapper: dateToNumberMapper
6+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { LogInfo, updateDynamoEasyConfig } from '@shiftcoders/dynamo-easy'
2+
3+
updateDynamoEasyConfig({
4+
logReceiver: (logInfo: LogInfo) => {
5+
const msg = `[${logInfo.level}] ${logInfo.timestamp} ${logInfo.className} (${
6+
logInfo.modelConstructor
7+
}): ${logInfo.message}`
8+
console.debug(msg, logInfo.data)
9+
}
10+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { updateDynamoEasyConfig } from '@shiftcoders/dynamo-easy'
2+
3+
updateDynamoEasyConfig({
4+
sessionValidityEnsurer: (): Promise<void> => {
5+
// do whatever you need to do to make sure the session is valid
6+
// and return an Promise<void> when done
7+
return Promise.resolve()
8+
},
9+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { TableNameResolver, updateDynamoEasyConfig } from '@shiftcoders/dynamo-easy'
2+
3+
const myTableNameResolver: TableNameResolver = (tableName: string) => {
4+
return `myPrefix-${tableName}`
5+
}
6+
7+
updateDynamoEasyConfig({
8+
tableNameResolver: myTableNameResolver
9+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { DynamoStore } from '@shiftcoders/dynamo-easy'
2+
import { Person } from './models'
3+
4+
const personStore = new DynamoStore(Person)
5+
6+
personStore
7+
.scan()
8+
.whereAttribute('yearOfBirth').equals(1958)
9+
.exec()
10+
.then(res => console.log('ALL items with yearOfBirth == 1958', res))

snippets/models/another.model.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { CollectionProperty, DateProperty, Model, PartitionKey, SortKey } from '@shiftcoders/dynamo-easy'
2+
3+
@Model()
4+
export class AnotherModel {
5+
@PartitionKey()
6+
propA: string
7+
8+
@SortKey()
9+
propB: string
10+
11+
12+
propC?: string
13+
14+
@CollectionProperty()
15+
myNestedList?: any[]
16+
17+
@DateProperty()
18+
updated: Date
19+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { MapperForType, NumberAttribute } from '@shiftcoders/dynamo-easy'
2+
3+
export const dateToNumberMapper: MapperForType<Date, NumberAttribute> = {
4+
fromDb: attributeValue => new Date(parseInt(attributeValue.N, 10)),
5+
toDb: propertyValue => ({ N: `${propertyValue.getTime()}` }),
6+
}

0 commit comments

Comments
 (0)