1.3.5 • Published 3 years ago

dynamodb-utility v1.3.5

Weekly downloads
11
License
ISC
Repository
github
Last release
3 years ago

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

VariableTypeRequiredDefault
regionstringfalseenv: AWS_REGION
timestampbooleanfalseNO
accessKeyIdsstringfalseenv: AWS_ACCESS_KEY
secretAccessKeystringfalseenv: AWS_SECRET_KEY

ScanOptions

VariableTypeRequiredDescription
filterKeyConditiontrueเงื่อนไขการกรอง ดู KeyCondition เพิ่มเติม
limitstringfalseจำกัดจำนวนผลลัพธ์
lastKeybooleanfalseKey สุดท้ายเพื่อทำ Pagination
timesstringfalseจำนวนรอบของการแสกน
indexNamestringfalseชื่อ index กรณีใช้ GSI หรือ LSI
attributesArrayfalseAttribute ที่ต้องการเลือกออกมาแสดง

QueryOptions (extended from ScanOptions)

VariableTypeRequiredDescription
keyConditionKeyConditionfalseเงื่อนไขการ Query ดู KeyCondition เพิ่มเติม
filterKeyConditionfalseเงื่อนไขการกรอง ดู 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!

  • Website at developer.fbw.center
  • Facebook at ceremonial.statistic.center

License

License

1.3.5

3 years ago

1.3.4

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago