This repository has been archived by the owner on Apr 4, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migrate for dynamodb table and sample data
- Loading branch information
1 parent
ca3668d
commit e779f7b
Showing
10 changed files
with
279 additions
and
1 deletion.
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,2 @@ | ||
DYNAMO_ENDPOINT=http://localhost:8000 | ||
REGION=ap-northeast-1 |
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 |
---|---|---|
|
@@ -109,3 +109,6 @@ dist | |
|
||
.dist/ | ||
|
||
src/migrate/data/* | ||
!src/migrate/data/keep.me | ||
|
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,38 @@ | ||
# Dynamodb Generic Repository | ||
|
||
## Install | ||
|
||
### Deploying DynamoDB Locally on Your Computer | ||
|
||
- Docker | ||
|
||
```shell script | ||
docker run -p 8000:8000 -it --rm instructure/dynamo-local-admin | ||
``` | ||
|
||
|
||
- Amazon DynamoDB is provided as an executable .jar file | ||
|
||
[Documentation](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html) | ||
|
||
|
||
## Migrate database | ||
|
||
### Create tables | ||
|
||
```shell script | ||
ts-node ./src/migrate/create-tables.ts | ||
``` | ||
|
||
### Load Sample Data | ||
|
||
[moviedata.zip](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/samples/moviedata.zip) | ||
|
||
```shell script | ||
ts-node ./src/migrate/load-simple-movies.ts | ||
``` | ||
|
||
|
||
|
||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 @@ | ||
/** | ||
* Created by SUN-ASTERISK\dinh.van.hoang on 2/24/20 | ||
*/ | ||
|
||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
export const ENVIRONMENTS = { | ||
DYNAMO_ENDPOINT: process.env.DYNAMO_ENDPOINT || 'http://localhost:8000', | ||
REGION: process.env.REGION || 'local', | ||
}; |
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,38 @@ | ||
/* tslint:disable:no-console */ | ||
/** | ||
* Created by SUN-ASTERISK\dinh.van.hoang on 2/24/20 | ||
*/ | ||
|
||
import { DynamoDB } from 'aws-sdk'; | ||
import { CreateTableInput } from 'aws-sdk/clients/dynamodb'; | ||
import { ENVIRONMENTS } from '../config'; | ||
|
||
(async () => { | ||
const dynamodb = new DynamoDB({ | ||
endpoint: ENVIRONMENTS.DYNAMO_ENDPOINT, | ||
region: ENVIRONMENTS.REGION, | ||
}); | ||
|
||
const params: CreateTableInput = { | ||
TableName: 'Movies', | ||
KeySchema: [ | ||
{ AttributeName: 'year', KeyType: 'HASH' }, // Partition key | ||
{ AttributeName: 'title', KeyType: 'RANGE' }, // Sort key | ||
], | ||
AttributeDefinitions: [ | ||
{ AttributeName: 'year', AttributeType: 'N' }, | ||
{ AttributeName: 'title', AttributeType: 'S' }, | ||
], | ||
ProvisionedThroughput: { | ||
ReadCapacityUnits: 10, | ||
WriteCapacityUnits: 10, | ||
}, | ||
}; | ||
|
||
try { | ||
const result = dynamodb.createTable(params).promise(); | ||
console.log('Created table. Table description JSON:', JSON.stringify(result, null, 2)); | ||
} catch (error) { | ||
console.error('Unable to create table. Error JSON:', JSON.stringify(error, null, 2)); | ||
} | ||
})(); |
Empty file.
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,58 @@ | ||
/* tslint:disable:no-console */ | ||
|
||
/** | ||
* Created by SUN-ASTERISK\dinh.van.hoang on 2/24/20 | ||
*/ | ||
import { DynamoDB } from 'aws-sdk'; | ||
import { DocumentClient } from 'aws-sdk/clients/dynamodb'; | ||
import fs from 'fs'; | ||
import { ENVIRONMENTS } from '../config'; | ||
|
||
(async () => { | ||
const docClient = new DynamoDB.DocumentClient({ | ||
endpoint: ENVIRONMENTS.DYNAMO_ENDPOINT, | ||
region: ENVIRONMENTS.REGION, | ||
}); | ||
|
||
console.log('Importing movies into DynamoDB. Please wait.'); | ||
|
||
const allMovies: { | ||
year: number; | ||
title: string; | ||
info: string; | ||
}[] = JSON.parse(fs.readFileSync(`${__dirname}/data/moviedata.json`, 'utf8')); | ||
|
||
let promisesBatch: Promise<any>[] = []; | ||
for (const movie of allMovies) { | ||
const params: DocumentClient.PutItemInput = { | ||
TableName: 'Movies', | ||
Item: { | ||
'year': movie.year, | ||
'title': movie.title, | ||
'info': movie.info, | ||
}, | ||
}; | ||
|
||
const promise = docClient.put(params).promise() | ||
.then(_ => { | ||
console.error('Done: ', movie.title); | ||
}) | ||
.catch(reason => { | ||
// Skip error and continue | ||
console.error('Error JSON:', JSON.stringify(reason, null, 2)); | ||
}); | ||
|
||
promisesBatch.push(promise); | ||
|
||
if (promisesBatch.length === 100) { | ||
await Promise.all(promisesBatch); | ||
// Clear batch | ||
promisesBatch = []; | ||
} | ||
} | ||
|
||
// Final block | ||
await Promise.all(promisesBatch); | ||
|
||
console.error('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