Skip to content

Commit 78932df

Browse files
authored
Merge pull request #83 from shelfio/feature/migrate-js-to-ts
Migrate js to ts
2 parents 42cd92d + 69eedee commit 78932df

File tree

12 files changed

+195
-111
lines changed

12 files changed

+195
-111
lines changed

.babelrc.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
presets: [
3+
'@babel/preset-typescript',
4+
[
5+
'@babel/preset-env',
6+
{
7+
targets: {
8+
node: '12'
9+
}
10+
}
11+
]
12+
]
13+
};

.circleci/config.yml

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
version: 2
1+
version: 2.1
22

3-
jobs:
4-
build:
5-
docker:
6-
- image: circleci/node:10-browsers
3+
orbs:
4+
node: circleci/node@1.1.6
75

8-
working_directory: ~/repo
6+
commands:
7+
install_deps:
8+
steps:
9+
- node/with-cache:
10+
cache-version: v1-all
11+
cache-key: yarn.lock
12+
dir: ~/repo/node_modules
13+
use-strict-cache: true
14+
steps:
15+
- run: yarn install --pure-lockfile --no-progress
916

17+
jobs:
18+
build:
19+
executor:
20+
name: node/default
21+
tag: '12-browsers'
1022
steps:
1123
- checkout
12-
- restore_cache:
13-
keys:
14-
- v1-dependencies-{{ checksum "package.json" }}
15-
- v1-dependencies-
16-
17-
- run: yarn
18-
19-
- save_cache:
20-
paths:
21-
- /tmp/dynamodb-local
22-
- node_modules
23-
- yarn.lock
24-
key: v1-dependencies-{{ checksum "package.json" }}
25-
24+
- install_deps
2625
- run: yarn test
26+
- run: yarn type-check
27+
- run: yarn lint:ci

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
lib/
2+
renovate.json
3+
tsconfig.json

.eslintrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
22
"root": true,
3-
"extends": ["@shelf/eslint-config/backend"]
3+
"extends": [
4+
"@shelf/eslint-config/typescript"
5+
]
46
}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
.idea/
22
coverage/
33
node_modules/
4+
dist/
5+
lib/
46
temp
57
yarn.lock
68
*.log
79
.DS_Store
8-
dist/

package.json

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,84 @@
22
"name": "@shelf/dynamodb-parallel-scan",
33
"version": "1.0.2",
44
"description": "Scan large DynamoDB tables faster with parallelism",
5-
"license": "MIT",
5+
"keywords": [
6+
"parallel-scan",
7+
"dynamodb",
8+
"aws"
9+
],
610
"repository": "shelfio/dynamodb-parallel-scan",
11+
"license": "MIT",
712
"author": {
813
"name": "Vlad Holubiev",
914
"email": "vlad@shelf.io",
1015
"url": "shelf.io"
1116
},
12-
"engines": {
13-
"node": ">=8"
14-
},
17+
"files": [
18+
"lib"
19+
],
20+
"main": "lib/index.js",
21+
"types": "lib/index.d.ts",
1522
"scripts": {
23+
"build": "rm -rf lib/ && yarn build:types && babel src --out-dir lib --ignore '**/*.test.ts' --extensions '.ts'",
24+
"build:types": "tsc --emitDeclarationOnly --declaration --isolatedModules false --declarationDir lib",
1625
"coverage": "jest --coverage",
17-
"lint": "eslint . --fix",
18-
"test": "export ENVIRONMENT=local && jest src --runInBand"
26+
"lint": "eslint . --ext .js,.ts,.json --fix --quiet",
27+
"lint:ci": "eslint . --ext .js,.ts,.json --quiet",
28+
"test": "export ENVIRONMENT=local && jest src --runInBand",
29+
"type-check": "tsc --noEmit",
30+
"type-check:watch": "npm run type-check -- --watch"
31+
},
32+
"husky": {
33+
"hooks": {
34+
"pre-commit": "lint-staged",
35+
"post-commit": "git update-index --again"
36+
}
37+
},
38+
"lint-staged": {
39+
"*.{html,json,md,yml}": [
40+
"prettier --write --ignore-path=./.eslintignore",
41+
"git add"
42+
],
43+
"*.{ts,js}": [
44+
"eslint --fix",
45+
"git add"
46+
]
47+
},
48+
"jest": {
49+
"testEnvironment": "node"
1950
},
20-
"files": [
21-
"src"
22-
],
23-
"main": "src/index.js",
24-
"keywords": [],
2551
"dependencies": {
2652
"debug": "4.1.1",
2753
"lodash.clonedeep": "4.5.0",
2854
"lodash.times": "4.3.2",
2955
"p-map": "3.0.0"
3056
},
3157
"devDependencies": {
58+
"@babel/cli": "7.8.3",
59+
"@babel/core": "7.8.3",
60+
"@babel/preset-env": "7.8.3",
61+
"@babel/preset-typescript": "7.8.3",
3262
"@shelf/eslint-config": "0.13.0",
3363
"@shelf/jest-dynamodb": "1.6.0",
64+
"@types/debug": "4.1.5",
65+
"@types/jest": "24.9.0",
66+
"@types/lodash.clonedeep": "4.5.6",
67+
"@types/lodash.times": "4.3.6",
68+
"@types/node": "12",
69+
"@types/p-map": "2.0.0",
3470
"aws-sdk": "2.536.0",
3571
"eslint": "6.8.0",
3672
"husky": "4.0.9",
3773
"jest": "24.9.0",
3874
"lint-staged": "9.5.0",
39-
"prettier": "1.19.1"
75+
"prettier": "1.19.1",
76+
"typescript": "3.7.4"
4077
},
4178
"peerDependencies": {
4279
"aws-sdk": "2.x.x"
4380
},
44-
"jest": {
45-
"testEnvironment": "node"
46-
},
47-
"husky": {
48-
"hooks": {
49-
"pre-commit": "lint-staged"
50-
}
51-
},
52-
"lint-staged": {
53-
"*.js": [
54-
"eslint --fix",
55-
"git add"
56-
],
57-
"*.{html,json,md,yml}": [
58-
"prettier --write",
59-
"git add"
60-
]
81+
"engines": {
82+
"node": ">=12"
6183
},
6284
"publishConfig": {
6385
"access": "public"

src/ddb.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/ddb.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import DynamoDB from 'aws-sdk/clients/dynamodb';
2+
import {DocumentClient} from 'aws-sdk/lib/dynamodb/document_client';
3+
4+
const isTest = process.env.JEST_WORKER_ID;
5+
const config = {
6+
convertEmptyValues: true,
7+
...(isTest && {endpoint: 'localhost:8000', sslEnabled: false, region: 'local-env'})
8+
};
9+
10+
const documentClient: DocumentClient = new DynamoDB.DocumentClient(config);
11+
12+
export async function scan(params: DocumentClient.ScanInput): Promise<DocumentClient.ScanOutput> {
13+
return documentClient.scan(params).promise();
14+
}
15+
16+
export function insertMany({items, tableName}): Promise<DocumentClient.BatchWriteItemOutput> {
17+
const params: DocumentClient.BatchWriteItemInput = {
18+
RequestItems: {
19+
[tableName]: items.map(item => ({
20+
PutRequest: {
21+
Item: item
22+
}
23+
}))
24+
}
25+
};
26+
27+
return batchWrite(params);
28+
}
29+
30+
async function batchWrite(items): Promise<DocumentClient.BatchWriteItemOutput> {
31+
return documentClient.batchWrite(items).promise();
32+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const {insertMany} = require('./ddb');
2-
const {parallelScan} = require('./');
1+
import {insertMany} from './ddb';
2+
import {parallelScan} from './';
33

44
describe('parallelScan', () => {
55
const files = [
@@ -33,7 +33,7 @@ describe('parallelScan', () => {
3333

3434
expect(items).toHaveLength(10);
3535

36-
for (let item of items) {
36+
for (const item of items) {
3737
expect(files).toContainEqual(item);
3838
}
3939
});
@@ -52,7 +52,7 @@ describe('parallelScan', () => {
5252

5353
expect(items).toHaveLength(10);
5454

55-
for (let item of items) {
55+
for (const item of items) {
5656
expect(files).toContainEqual(item);
5757
}
5858
});
@@ -71,7 +71,7 @@ describe('parallelScan', () => {
7171

7272
expect(items).toHaveLength(3);
7373

74-
for (let item of items) {
74+
for (const item of items) {
7575
expect(files).toContainEqual(item);
7676
}
7777
});

src/index.js renamed to src/index.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
1-
const pMap = require('p-map');
2-
const cloneDeep = require('lodash.clonedeep');
3-
const times = require('lodash.times');
4-
const {scan} = require('./ddb');
5-
6-
const debug = require('debug')('ddb-parallel-scan');
7-
8-
async function parallelScan(scanParams, {concurrency}) {
9-
const segments = times(concurrency);
10-
const docs = [];
1+
import pMap from 'p-map';
2+
import cloneDeep from 'lodash.clonedeep';
3+
import times from 'lodash.times';
4+
import Debug from 'debug';
5+
import {DocumentClient} from 'aws-sdk/lib/dynamodb/document_client';
6+
import {scan} from './ddb';
7+
8+
const debug = Debug('ddb-parallel-scan');
9+
10+
export async function parallelScan(
11+
scanParams: DocumentClient.ScanInput,
12+
{concurrency}: {concurrency: number}
13+
): Promise<DocumentClient.ItemList> {
14+
const segments: number[] = times(concurrency);
15+
const docs: DocumentClient.ItemList = [];
1116

1217
debug(`Started parallel scan with ${concurrency} threads`);
1318

1419
await pMap(segments, async (_, segmentIndex) => {
15-
let ExclusiveStartKey = '';
20+
let ExclusiveStartKey: DocumentClient.Key;
1621

17-
const params = cloneDeep(scanParams);
18-
params.Segment = segmentIndex;
19-
params.TotalSegments = concurrency;
22+
const params: DocumentClient.ScanInput = {
23+
...cloneDeep(scanParams),
24+
Segment: segmentIndex,
25+
TotalSegments: concurrency
26+
};
2027

21-
const now = Date.now();
28+
const now: number = Date.now();
2229
debug(`[${segmentIndex}/${concurrency}][start]`, {ExclusiveStartKey});
2330

2431
do {
@@ -43,7 +50,3 @@ async function parallelScan(scanParams, {concurrency}) {
4350

4451
return docs;
4552
}
46-
47-
module.exports = {
48-
parallelScan
49-
};

0 commit comments

Comments
 (0)