Node.js: 更新 Redis 数据 (Update)

编程

#! /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