Skip to content

Instantly share code, notes, and snippets.

@hattorix
Last active December 10, 2015 23:28
Show Gist options
  • Save hattorix/4509323 to your computer and use it in GitHub Desktop.
Save hattorix/4509323 to your computer and use it in GitHub Desktop.
node.js で AzureTable のクエリ実行
azure = require 'azure'
process.env['AZURE_STORAGE_ACCOUNT'] = 'xxxx'
process.env['AZURE_STORAGE_ACCESS_KEY'] = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=='
EPOCH = 621355968000000000
# Unix epoch to Ticks
exports.getTicks = (date) ->
(date.getTime() * 10000) + EPOCH
# Ticks to Unix epoch
exports.ticksToDate = (ticks) ->
offset = (ticks - EPOCH) / 10000
new Date(offset)
# query にマッチするすべての行を取得
exports.queryAll = (query, process, continuous = false) ->
tableService = azure.createTableService()
lastEntity = null
QueryCallback = (error, entities, options) ->
return if error
if continuous and lastEntity? and entities?
# 最新行の継続取得してる場合の被っている行を削除
top = entities.shift()
if lastEntity.id != top.id
entities.unshift top
for entity in entities
# 行ごとの処理
process entity
if options.nextPartitionKey?
# 継続行があるなら繰り返す
lastEntity = null
query.whereNextKeys options.nextPartitionKey, options.nextRowKey
tableService.queryEntities query, QueryCallback
else if continuous
# 最新行を継続取得する
if entities.length > 0
lastEntity = entities.pop()
query.whereNextKeys lastEntity.PartitionKey, lastEntity.RowKey
timeout = ->
tableService.queryEntities query, QueryCallback
setTimeout timeout, 30000
tableService.queryEntities query, QueryCallback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment