-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(snippets): adding snippets for doc
- Loading branch information
1 parent
4cbba5c
commit ab4272f
Showing
32 changed files
with
367 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}` }), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,7 @@ | |
], | ||
"exclude": [ | ||
"test/**/*", | ||
"snippets/**/*", | ||
"**/*.spec.ts" | ||
] | ||
} |
Oops, something went wrong.