0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Node.js: Redis のデータを作成 (Create)

Last updated at Posted at 2020-12-30

プログラム

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

const redis = require('redis')

// ---------------------------------------------------------------
function dict_append_proc (dict_aa,id_in,name_in,population_in,date_mod_in)
{
	var unit_aa = {}
	unit_aa['name'] = name_in
	unit_aa['population'] = population_in
	unit_aa['date_mod'] = date_mod_in

	dict_aa[id_in] = unit_aa

	return	dict_aa
}

// ---------------------------------------------------------------
function data_prepare_proc ()
{
	var dict_aa = new Object ()

	dict_aa = dict_append_proc (dict_aa,'t1851','福井',92714,'1950-9-12')
	dict_aa = dict_append_proc (dict_aa,'t1852','敦賀',28158,'1950-3-15')
	dict_aa = dict_append_proc (dict_aa,'t1853','小浜',67241,'1950-10-2')
	dict_aa = dict_append_proc (dict_aa,'t1854','大野',32164,'1950-6-22')
	dict_aa = dict_append_proc (dict_aa,'t1855','勝山',41358,'1950-8-14')
	dict_aa = dict_append_proc (dict_aa,'t1856','鯖江',64792,'1950-9-12')
	dict_aa = dict_append_proc (dict_aa,'t1857','あわら',38251,'1950-3-21')
	dict_aa = dict_append_proc (dict_aa,'t1858','越前',52486,'1950-7-26')
	dict_aa = dict_append_proc (dict_aa,'t1859','坂井',25397,'1950-11-9')
	
	return	dict_aa
}

// ---------------------------------------------------------------
async function create_proc(options)
{
	var dict_aa = data_prepare_proc ()

	const client = redis.createClient()
	await client.connect()

	const keys = Object.keys(dict_aa)

	for (var it in keys)
		{
		const key = keys[it]
		const str_json = JSON.stringify (dict_aa[key])
		console.log(key,str_json)
		try
			{
			await client.set(key, str_json)
			}
		catch (error)
			{
			console.error ("*** error *** from client.set ***")
			console.error (error)
			console.error (key)
			}

		}

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

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

create_proc({ argv: process.argv })

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

実行コマンド

./redis_create.js

確認したバージョン

$ node --version
v20.2.0
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?