dynamodb-utility
TypeScript icon, indicating that this package has built-in type declarations

1.3.5 • Public • Published

AWS_DynamoDB Build Status License


Table of Contents


Features

Basic DynamoDB operation in table

  • get
  • put
  • update
  • patch
  • delete
  • scan
  • query
  • raw

support dynamodb Comparison Operator and Function

support nested attribute filter expression

Installation

$ npm install --save dynamodb-utility

Setup

  • ตั้งค่า AWS IAM

    • ตั้งค่า User และให้ policy ให้เรียบร้อย และนำใน AWS Access Key ID และ AWS Secret Access Key มาใช้
  • ตั้งค่า AWS Environment

    • แนะนำให้ติดตั้ง AWS CLI และ Configuration Environment พื้นฐานให้เรียบร้อย อ่านเพิ่มเติมเกี่ยวกับ AWS Configure
  • ตรวจสอบ AWS Environment

$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: json

Example Code

สามารถดูตัวอย่างการเขียนได้ที่

example code


Usage

โดยพื้นฐานแล้ว Library จะใช้ AWS Environment ที่อยู่บนเครื่องในการตั้งค่า AWS DynamoDB ใน Module

import DynamodbUtil from 'dynamodb-utility'
const userModel = new DynmodbUtil('User-Table', {
    region: 'ap-southeast-1',
    // ...DynamoDBUtilOptions
});

Put

userModel.put({
    pkey: 'User1',
    skey: 'Profile',
    firstname: 'Araiva',
    lastname: 'Viruskizz',
    gender: 'Male',
    age: 20,
    address: {
        province: 'London',
        country: 'England'
    }
}).then(res => console.log(res));

Get

userModel.get({ pkey: 'User1', skey: 'Profile'})
    .then(res => console.log(res));

Update

userModel.update({ id: 'User1', skey: 'Profile'}, {
    age: 20
}).then(res => console.log(res));

Patch

userModel.patch({ id: 'User1', skey: 'Profile'}, {
    age: 20,
    'address.province': 'Man Chester'
}).then(res => console.log(res));

Delete

userModel.delete({ pkey: 'User1', skey: 'Profile' })
    .then(res => console.log(res));

Scan

userModel.scan().then(res => console.log(res));

// or by pass argument scan option
// DynamodbUtil.query(ScanOptions);
userModel.scan({
    filter: {
        gender: 'Male',
        age: {
            greaterThan: 10
        },
        'address.country': 'England'
    },
    attributes: ['firstname', 'lastname', 'age']
}).then(res => console.log(res));

Query

// DynamodbUtil.query(QueryOptions);
userModel.query({
    keyCondition: {
        pkey: 'User1',
        skey: {
            beginsWith: 'Profile'
        }
    }
}).then(res => console.log(res));

Raw (Use Raw DynamodbClient)

// DynamodbUtil.raw.scan(ScanParam);
userModel.raw.scan({
    Table: 'User-Table'
}).promise().then(res => console.log(res.Items));

Attribute Reference

DynamoDBUtilOptions

Variable Type Required Default
region string false env: AWS_REGION
timestamp boolean false NO
accessKeyIds string false env: AWS_ACCESS_KEY
secretAccessKey string false env: AWS_SECRET_KEY

ScanOptions

Variable Type Required Description
filter KeyCondition true เงื่อนไขการกรอง ดู KeyCondition เพิ่มเติม
limit string false จำกัดจำนวนผลลัพธ์
lastKey boolean false Key สุดท้ายเพื่อทำ Pagination
times string false จำนวนรอบของการแสกน
indexName string false ชื่อ index กรณีใช้ GSI หรือ LSI
attributes Array false Attribute ที่ต้องการเลือกออกมาแสดง

QueryOptions (extended from ScanOptions)

Variable Type Required Description
keyCondition KeyCondition false เงื่อนไขการ Query ดู KeyCondition เพิ่มเติม
filter KeyCondition false เงื่อนไขการกรอง ดู KeyCondition เพิ่มเติม
sort 'DESC' or 'ASC' false เรียงลำดับตาม sort key ,default: ASC

KeyCondition

Attribute: 'Value'
  
Or 
  
Attribute: {
    ConditionFunction: 'Value'
}

ConditionFunction ที่สามารถเรียกใช้งานได้

  • equal
  • greaterThan
  • lessThan
  • attributeExists
  • attributeNotExists
  • beginsWith
  • contains

ดูรายละเอียดเพิ่มเติ่ม Comparison Operator and Function Reference

Contributors

Or Contributors/People

Araiva Viruskizz
viruskizz

Support

Reach out to me at one of the following places!


License

License

Package Sidebar

Install

npm i dynamodb-utility

Weekly Downloads

0

Version

1.3.5

License

ISC

Unpacked Size

73.2 kB

Total Files

16

Last publish

Collaborators

  • araiva