Skip to content
Permalink
Browse files

Add migrate for dynamodb table and sample data

  • Loading branch information
hoangsetup committed Feb 24, 2020
1 parent ca3668d commit e779f7b142fc8c0626bf3f6cd1ed0071c45f0489
Showing with 279 additions and 1 deletion.
  1. +2 −0 .env-example
  2. +3 −0 .gitignore
  3. +38 −0 README.md
  4. +114 −0 package-lock.json
  5. +6 −0 package.json
  6. +12 −0 src/config/index.ts
  7. +38 −0 src/migrate/create-tables.ts
  8. 0 src/migrate/data/keep.me
  9. +58 −0 src/migrate/load-simple-movies.ts
  10. +8 −1 tslint.json
@@ -0,0 +1,2 @@
DYNAMO_ENDPOINT=http://localhost:8000
REGION=ap-northeast-1
@@ -109,3 +109,6 @@ dist

.dist/

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

@@ -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.

@@ -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"
}
}
@@ -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',
};
@@ -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));
}
})();
No changes.
@@ -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!');
})();
@@ -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.
You can’t perform that action at this time.