Posted in JavaScript

[AWS] How to do Scan / Query in AWS DynamoDb for JS

aws_logo

For query items from DymanoDb, AWS supports some ways: Scan, Query, BatchGetItem

Below are some examples:

If I have a table ‘GtfsStopTime’ with 2 primaries (tripId & index)

  • Scan items from a list of id:
async getGtfsStopTimeListByTripIds (tripIds) {
    try {
      var list = []
      const attributeValueList = []
      for (let tripId of tripIds) {
        attributeValueList.push({
          S: tripId
        })
      }

      const param = {
        TableName: this._getTableName(),
        ScanFilter: {
          'tripId': {
            'AttributeValueList': attributeValueList,
            'ComparisonOperator': 'IN'
          }
        }
      }

      const data = await this.dynamoDB.scan(param).promise()
      list = data.Items
      return list
    } catch (err) {
      console.error('Error happen')
    }
  }

Note: We cannot apply BatchGetItem in this case because the BatchGetItem require all primary keys of table (in this case, we only query by one primary key – tripId )

  • Query items in DynamoDb using BatchGetItem

Below is example code to fetch items in ‘GtfsStop’ table by list of stopId as input which has stopId is primary key

async getGtfsStopListByIds (stopIdList) {
    const keys = stopIdList.map(function (stopId) {
      return {
        'stopId': {
          S: stopId
        }
      }
    })

    try {
      const params = {
        RequestItems: {
          'GtfsStop': { Keys: keys }
        }
      }
      const fetchedResponse = await this.dynamoDB.batchGetItem(params).promise()
      const gtfsStopItems = fetchedResponse.Responses.GtfsStop
      return gtfsStopItems
    } catch (err) {
      throw new Error(err)
    }
  }
  • Others

 

Hope this topic is useful for you 🙂

Advertisements
Posted in Cloud, JavaScript

[Cloud] Kii Cloud – helpfull script Server Code

kii

Kii Cloud is one of powerful cloud for developer. It provides a helpful data storage solution. So, the developer donot need to care the way store data.

The Kii Cloud also provide one strong feature that is Server Code. When you create a new project on Kii cloud, a Server Code for that project is created. You can make your own Server Code then deploy it to your project. I worked with Server Code for some projects, I used it to create triggers, scheduler, etc.

For example: you can create a trigger for a certain record which is inserted into one bucket (table), then Server Code will call your trigger function to do something automatically.

For Server Code implementation, please refer this guide

I created some useful script (for Window system) to help working on Server Code more convenient. I hope it is helpful for person who work on Server Code of Kii Cloud.

The script for Window system has 3 main part:

Part 1: define configuration for Kii project:

set kiiDeveloperLogFodder=%cd%\Kii_Logger
set KiiLogFile=”D:\KiiLogFile.txt”
set ServerCodeFilePath=”D:\your_server_code_path\servercode.js”
set HookConfigFilePath=”D:\your_hookConfig_path\hookConfig\hookConfig.json”

echo %kiiDeveloperLogFodder%

del %KiiLogFile%

set APP_ID=your_app_id
set APP_KEY=your_app_key
set CLIENT_ID=your_client_id
set CLIENT_SECRET=your_client_secret

set KiiSite=jp
set dateFrom=2015-11-18
set dateTo=2018-11-18
set codeVersion=your_code_version
set userId=your_user_id

set expectedLogLinesNumber=1500

set appKeys=–site %KiiSite% –app-id %APP_ID% –app-key %APP_KEY% –client-id %CLIENT_ID% –client-secret %CLIENT_SECRET%
set fromDateToDate=–date-from %dateFrom% –date-to %dateTo%
set userIdCommand=–user-id %userId%
set expectedLogLinesCommand=–num %expectedLogLinesNumber%
set infoLevelCommand=–level INFO
set debugLevelCommand=–level DEBUG
set errorLevelCommand=–level ERROR

Part 2: define Option for each script

set option_1=”Get Kii log”
set option_2=”Deploy Server Code”
set option_3=”List the Available Versions”
set option_4=”Get your Server Code”
set option_5=”Get Server Hook Config File”
set option_6=”List Server Code Execution Results”
set option_7=”Get the Detailed Execution Result”
set option_8=”NA”
set option_9=”NA”

ECHO 1) %option_1%
ECHO 2) %option_2%
ECHO 3) %option_3%
ECHO 4) %option_4%
ECHO 5) %option_5%
ECHO 6) %option_6%
ECHO 7) %option_7%
ECHO 8) %option_8%
ECHO 9) %option_9%

ECHO.

CHOICE /C 123456789 /M “Enter your choice:”

:: Note – list ERRORLEVELS in decreasing order
IF ERRORLEVEL 9 GOTO option_9
IF ERRORLEVEL 8 GOTO option_8
IF ERRORLEVEL 7 GOTO option_7
IF ERRORLEVEL 6 GOTO option_6
IF ERRORLEVEL 5 GOTO option_5
IF ERRORLEVEL 4 GOTO option_4
IF ERRORLEVEL 3 GOTO option_3
IF ERRORLEVEL 2 GOTO option_2
IF ERRORLEVEL 1 GOTO option_1

Part 3: define script contents to run for options which are defined at Part 2

:option_1
ECHO %option_1%
rem Get Kii log
node bin/kii-logs.js -t %appKeys% > %KiiLogFile%
GOTO End

:option_2
ECHO %option_2%
rem Deploy Server Code ==> Make sure that you update 2 files: KiiServerCode/servercode.js & KiiHookConfig/hookConfig.json
node bin/kii-servercode.js deploy-file –file ../KiiServerCode/servercode.js %appKeys% –hook-config ../KiiHookConfig/hookConfig.json
GOTO End

Result:

kii_servercode_script_result

PS: You also find full of scripts from my Github at KiiCloudServerCodeScript (Remember read README file before running scripts)

Share this topic if it is helpful 🙂

 

Posted in JavaScript

[Javascript] Regular Express for URL and Email detection in a sentence.

javascript

There are many ways to detect URL(s) in a sentence:

  • Use validUrl lib: Refer detail at here

This lib has a disadvantage that is it can analyze a URL is valid or not. It means if you put that URL in a sentence such as “Hello http://www.saothienhat.wordpress.com site !”, it cannot detect the URL

  • Use below Regular Express:
function linkify(inputText) {
    var replacedText, replacePattern1, replacePattern2, replacePattern3;

    //URLs starting with http://, https://, or ftp://
    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
    replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

    //Change email addresses to mailto:: links.
    replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
    replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

    return replacedText;
}

Using above Regular Express, you can detect URL(s) as well as Email(s) in a sentence. You can modify this Regular Express for your jobs :).

You can refer detail of Regular Express guide at here

Like and Share if it is helpful 🙂