LoginSignup
0
0

Node.js: Redis のデータを更新 (Update)

Last updated at Posted at 2020-12-30

プログラム

redis_update.js
#! /usr/local/bin/node
// ---------------------------------------------------------------
//	redis_update.js
//
//					May/25/2023
//
// ---------------------------------------------------------------
'use strict'

const redis = require('redis')

// ---------------------------------------------------------------
function get_current_date_proc ()
{
	const today = new Date ()
	var ddx = today.getFullYear () + "-" + (today.getMonth () +1)
	ddx += "-" + today.getDate ()

	return ddx
}

// ---------------------------------------------------------------
async function update_proc(options)
{
	var argv = options.argv
	const key_in=argv[2]
	const population_in=parseInt(argv[3],10)

	console.log (key_in + "\t" + population_in)

	const client = redis.createClient()

	await client.connect()

	try
		{
		const value = await client.get(key_in)
		const unit_aa = JSON.parse(value)
		var out_str = key_in + "\t"
		out_str  += unit_aa.name + "\t"
		out_str += unit_aa.population + "\t"
		out_str += unit_aa.date_mod
		console.log (out_str)
		unit_aa.population = population_in
		unit_aa.date_mod = get_current_date_proc ()
		const json_out = JSON.stringify (unit_aa)

		console.log (json_out)

		await client.set(key_in, json_out)
		}
	catch (error)
		{
		console.error ("*** error *** from JSON.parse ***")
		console.error (error)
		console.error (key_in)
		}

	await client.disconnect()
	console.error ("*** 終了 ***")
}

// ---------------------------------------------------------------
console.error ("*** 開始 ***")

update_proc({ argv: process.argv })


// ---------------------------------------------------------------

実行コマンド

./redis_update.js t1857 82597400

確認したバージョン

$ node --version
v20.2.0
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0