DynamoDB勉強する
DynamoDBの勉強記録をここに残していく
DynamoDBは永久無料枠があるので他のAWS DBより「とりあえず」で導入しやすいはず
ツール導入
❯ brew install dynamodb-local awscli
てきとうに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]:
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": []
}
aws cliからでは操作しづらいのでserverlessおよびdynamodb-adminの使用を考える
nodeプロジェクトにdependencies導入
❯ yarn add -D serverless serverless-dynamodb-local dynamodb-admin
serverlessの設定を追加
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分ちょいかかった
プロジェクトに.dynamodb
が追加される
sls
でdynamodb起動、localhost:8000/shell
でローカルシェル起動
❯ yarn sls dynamodb start
別窓でdynamodb-admin起動、http://localhost:8001/
にてadminページが開く
❯ yarn dynamodb-admin
inMemory: true
で起動しているため、動かすたびにデータが消える
このため、migrationを追加
partition keyとsort keyを設定したいものをAttributeDefinitions
に記述し、キーの種類をKeySchema
に記載する
partition keyはHASH
、sort keyはRANGE
とする
create-table — AWS CLI 1.19.92 Command Reference
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
migrateでテーブルは作れるが、データがないのでseedを使う
てきとうにseeds/users.json
を作成する
partition keyとsort keyの項目は必須、その他のカラムも追加可能
[
{
"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.yml
のcustom
に読み込み設定を追加
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 へアクセスするとデータが入った状態でテーブルができている
webのデータを取得してdynamoへ入れてみる
ここではCryptowatch OHLC Candlesticsを使う
dependenciesを追加する
❯ yarn add -D typescript ts-node ts-node-dev @types/node date-fns node-fetch
tsの実行scriptを追加する
"scripts":{
+ "ts-run": "node --loader ts-node/esm"
},
lint系も追加した
❯ yarn add -D eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser prettier
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: {}
{
"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/**/*"
]
}
先にServerlessの勉強をしよう