0
0

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 3 years have passed since last update.

Node.js: Lambda で MariaDB のデータを読む (Read)

Posted at

フォルダー構造

$ tree -a
.
├── .env
├── function_update.sh
├── index.js
└── test_local.js
maria_read/index.js
// ---------------------------------------------------------------
//	maria_read/index.js
//
//					Jun/08/2020
//
// ---------------------------------------------------------------
'use strict'

// ---------------------------------------------------------------
async function read01 (mysql,host,user,password,data_base)
{
	try {
		console.error ("*** read01 *** start ***")
	var conn = await mysql.createConnection({
		host: host,
		user: user,
		password: password,
		database: data_base
		})
	const sql_str = 'select * from cities'
		console.error (sql_str)
	const [rows, fields] = await conn.execute(sql_str)

	var dict_aa = new Object ()

	rows.forEach(function(row)
		{
		dict_aa[row.id] = {"name": row.name,
			"population": row.population,
			"date_mod": row.date_mod}
		})

	return dict_aa
	} catch (ee) {
		console.log(ee)
		console.error ("*** error *** read01 ***")
		console.error(ee)
		return ''
	} finally {
		if (conn && conn.connection) {
		conn.end()
	}
}

}

// ---------------------------------------------------------------
async function main()
{
	var mysql = require('mysql2/promise')

	const host = `${process.env.host}`
	const user = `${process.env.user}`
	const password = `${process.env.password}`
	const data_base = `${process.env.data_base}`

	console.error("host = " + host)
	console.error("user = " + user)
	console.error("password = " + password)
	console.error("data_base = " + data_base)

	const dict_aa = await read01 (mysql,host,user,password,data_base)

	console.error ("*** main *** end ***")

	return dict_aa
}

// ---------------------------------------------------------------
console.error ("*** 開始 ***")
exports.handler = async (event,context,callback) => {
	console.error("***** start maria_read PM 20:20 ***")
	var rvalue = {}
	rvalue['message'] = 'Hello from maria_read'

	const dict_aa = await main()

	rvalue['data'] = dict_aa

	const response = {
		statusCode: 200,
		body: JSON.stringify(rvalue),
	}

	console.error("***** end maria_read ***")

	callback(null, response) 

//	return response
}

// ---------------------------------------------------------------
test_local.js
// ---------------------------------------------------------------
//	test_local.js
//
//						Jun/08/2020
//
// ---------------------------------------------------------------
'use strict'
// ---------------------------------------------------------------
var maria_read = require('./')
// ---------------------------------------------------------------
function func01(aaa,rvalue)
{
    console.error ("*** func01 *** start ***")

//    console.log(aaa)
    console.log(rvalue)

    console.error ("*** func01 *** end ***")
}

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

const event = {}
const context = ""

maria_read.handler(event,context,func01)

console.error ("*** 終了 *** test_local.js ***")
// ---------------------------------------------------------------

実行スクリプト

export NODE_PATH=/usr/lib/node_modules
node -r dotenv/config ./test_local.js

AWS のプログラムを更新するスクリプト

function_update.sh
FUNCTION='maria_read'
ZIP_FILE=$FUNCTION".zip"
#
rm -f *.zip
#
zip -r $ZIP_FILE index.js
#
#
aws lambda update-function-code \
	--function-name $FUNCTION \
	--zip-file fileb://$ZIP_FILE
#

AWS のコンソールで実行した結果
maria_read_jun08.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?