Closed15

DynamoDB勉強する

kawarimidollkawarimidoll

てきとうにconfigure

❯ aws configure 
AWS Access Key ID [None]: test
AWS Secret Access Key [None]: test
Default region name [None]: ap-northeast-1      
Default output format [None]: 
kawarimidollkawarimidoll

dbを起動

❯ dynamodb-local 
Initializing DynamoDB Local with the following configuration:
Port:	8000
InMemory:	false
DbPath:	null
SharedDb:	false
shouldDelayTransientStatuses:	false
CorsParams:	*

端末の別窓を開いて接続

❯ aws dynamodb list-tables --endpoint-url http://localhost:8000
{
    "TableNames": []
}
kawarimidollkawarimidoll

serverlessの設定を追加

serverless.yml
org: kawarimidoll
app: learn-dynamo
service: learn-dynamo

frameworkVersion: '2'

provider:
  name: aws
  runtime: nodejs12.x
  lambdaHashingVersion: 20201221

functions:
  hello:
    handler: handler.hello

plugins:
  - serverless-dynamodb-local

インストール実行、暫し待つ

❯ yarn sls dynamodb install
yarn run v1.22.10
$ /Users/kawarimidoll/ghq/github.com/kawarimidoll/learn-dynamo/node_modules/.bin/sls dynamodb install
Serverless: Deprecation warning: CLI options definitions were upgraded with "type" property (which could be one of "string", "boolean", "multiple"). Below listed plugins do not predefine type for introduced options:
             - ServerlessDynamodbLocal for "online", "port", "cors", "inMemory", "dbPath", "sharedDb", "delayTransientStatuses", "optimizeDbBeforeStartup", "migrate", "seed", "migration", "heapInitial", "heapMax", "convertEmptyValues", "localPath"
            Please report this issue in plugin issue tracker.
            Starting with next major release, this will be communicated with a thrown error.
            More Info: https://www.serverless.com/framework/docs/deprecations/#CLI_OPTIONS_SCHEMA
Started downloading dynamodb-local from http://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.tar.gz into /Users/kawarimidoll/ghq/github.com/kawarimidoll/learn-dynamo/.dynamodb. Process may take few minutes.

 Installation complete!
✨  Done in 213.19s.

3分ちょいかかった

kawarimidollkawarimidoll

slsでdynamodb起動、localhost:8000/shellでローカルシェル起動

❯ yarn sls dynamodb start

別窓でdynamodb-admin起動、http://localhost:8001/にてadminページが開く

❯ yarn dynamodb-admin 

kawarimidollkawarimidoll

inMemory: trueで起動しているため、動かすたびにデータが消える
このため、migrationを追加

partition keyとsort keyを設定したいものをAttributeDefinitionsに記述し、キーの種類をKeySchemaに記載する
partition keyはHASH、sort keyはRANGEとする

create-table — AWS CLI 1.19.92 Command Reference

serverless.yml
custom:
  dynamodb:
    stages:
      - dev
    start:
      inMemory: true
+      migrate: true
+ 
+ resources:
+   Resources:
+     usersTable:
+       Type: AWS::DynamoDB::Table
+       Properties:
+         TableName: users
+         AttributeDefinitions:
+           - AttributeName: email
+             AttributeType: S
+           - AttributeName: age
+             AttributeType: N
+         KeySchema:
+           - AttributeName: email
+             KeyType: HASH
+           - AttributeName: age
+             KeyType: RANGE
+         ProvisionedThroughput:
+           ReadCapacityUnits: 1
+           WriteCapacityUnits: 1

それ以外のカラムはここでは記述不要

起動するとテーブル作成の旨が表示される

❯ yarn sls dynamodb start
yarn run v1.22.10
(略)
Dynamodb Local Started, Visit: http://localhost:8000/shell
Serverless: DynamoDB - created table users
kawarimidollkawarimidoll

migrateでテーブルは作れるが、データがないのでseedを使う

てきとうにseeds/users.jsonを作成する
partition keyとsort keyの項目は必須、その他のカラムも追加可能

seeds/users.json
[
  {
    "email": "alice@example.com",
    "age": 10,
    "name": "Alice"
  },
  {
    "email": "bob@example.com",
    "age": 11,
    "name": "Bob"
  },
  {
    "email": "carol@example.com",
    "age": 12,
    "name": "Carol"
  },
  {
    "email": "dave@example.com",
    "age": 13,
    "name": "Dave"
  },
  {
    "email": "ellen@example.com",
    "age": 14,
    "name": "Ellen"
  },
  {
    "email": "frank@example.com",
    "age": 15,
    "name": "Frank"
  }
]

serverless.ymlcustomに読み込み設定を追加

serverless.yml
custom:
  dynamodb:
    stages:
      - dev
    start:
      inMemory: true
      migrate: true
+      seed: true
+    seed:
+      development:
+        sources:
+          - table: users
+            sources: [ ./seeds/users.json ]

これで起動するとSeed読み込みの旨が表示される

❯ yarn sls dynamodb start
yarn run v1.22.10
(略)
Dynamodb Local Started, Visit: http://localhost:8000/shell
Serverless: DynamoDB - created table users
Seed running complete for table: users

http://localhost:8001/tables/users へアクセスするとデータが入った状態でテーブルができている

kawarimidollkawarimidoll

webのデータを取得してdynamoへ入れてみる
ここではCryptowatch OHLC Candlesticsを使う

https://docs.cryptowat.ch/rest-api/markets/ohlc

dependenciesを追加する

❯  yarn add -D typescript ts-node ts-node-dev @types/node date-fns node-fetch

tsの実行scriptを追加する

package.json
  "scripts":{
+    "ts-run": "node --loader ts-node/esm"
  },
kawarimidollkawarimidoll

lint系も追加した

❯ yarn add -D eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser prettier
eslintrc.yml
root: true
env:
  browser: true
  es2021: true
  node: true
plugins:
  - "@typescript-eslint"
extends:
  - "eslint:recommended"
  - "plugin:@typescript-eslint/eslint-recommended"
  - "plugin:@typescript-eslint/recommended"
  - "plugin:@typescript-eslint/recommended-requiring-type-checking"
  - "prettier"
parser: "@typescript-eslint/parser"
parserOptions:
  ecmaVersion: 2015
  sourceType: module
  project: ./tsconfig.json
rules: {}
tsconfig.json
{
  "compilerOptions": {
    "alwaysStrict": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "noFallthroughCasesInSwitch": true,
    "noImplicitAny": true,
    "noImplicitOverride": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noPropertyAccessFromIndexSignature": true,
    "noUncheckedIndexedAccess": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "outDir": "./dist",
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "strictBindCallApply": true,
    "strictFunctionTypes": true,
    "strictNullChecks": true,
    "strictPropertyInitialization": true,
    "target": "ES2020"
  },
  "include": [
    "src/**/*"
  ]
}
このスクラップは2023/05/24にクローズされました