Skip to content
This repository has been archived by the owner on Apr 4, 2021. It is now read-only.

Commit

Permalink
Add migrate for dynamodb table and sample data
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangsetup committed Feb 24, 2020
1 parent ca3668d commit e779f7b
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .env-example
@@ -0,0 +1,2 @@
DYNAMO_ENDPOINT=http://localhost:8000
REGION=ap-northeast-1
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -109,3 +109,6 @@ dist

.dist/

src/migrate/data/*
!src/migrate/data/keep.me

38 changes: 38 additions & 0 deletions README.md
@@ -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
```





114 changes: 114 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Expand Up @@ -16,8 +16,14 @@
"author": "hoangdv <hoang.dv@outlook.com> (https://codetheworld.io/)",
"license": "ISC",
"devDependencies": {
"@types/dotenv": "^8.2.0",
"@types/node": "^13.7.4",
"ts-node-dev": "^1.0.0-pre.44",
"tslint": "^6.0.0",
"typescript": "^3.8.2"
},
"dependencies": {
"aws-sdk": "^2.624.0",
"dotenv": "^8.2.0"
}
}
12 changes: 12 additions & 0 deletions src/config/index.ts
@@ -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',
};
38 changes: 38 additions & 0 deletions src/migrate/create-tables.ts
@@ -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 added src/migrate/data/keep.me
Empty file.
58 changes: 58 additions & 0 deletions src/migrate/load-simple-movies.ts
@@ -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!');
})();
9 changes: 8 additions & 1 deletion tslint.json
Expand Up @@ -6,7 +6,14 @@
"jsRules": {},
"rules": {
"quotemark": [true, "single"],
"import-spacing": true
"import-spacing": true,
"ordered-imports": true,
"trailing-comma": [
true,
{
"multiline": "always"
}
]
},
"rulesDirectory": []
}

0 comments on commit e779f7b

Please sign in to comment.