戻る

オブジェクトスキーマ(テーブル)の一覧を取得
オブジェクトスキーマ(テーブル)の一覧

指定したデータベースに格納されているオブジェクトスキーマ(テーブル)の一覧を取得します。

[サンプル]
copy
async function tableList(dbName) 
{
	return new Promise((resolve, reject) => 
	{
		const request = indexedDB.open(dbName);
	
		request.onsuccess = function(event) 
		{
			const db = event.target.result;
			const objectStoreNames = Array.from(db.objectStoreNames);
			db.close();
			resolve(objectStoreNames);
		};
	
		request.onerror = function(event) 
		{
			reject(event.target.error);
		};
	});
}
function tableListArray(dbName)
{
	tableList(dbName)
	.then((tables) => 
	{
		printArray(tables)
	})
	.catch((error) => 
	{
		console.error("error:", error);
		return null;
	});
}
function printArray(ary)
{
	let count = ary.length;
	if(count === null)
	{
		console.log("ary is null");
		return;
	}
	for(i=0; i < count; i++)
	{
		console.log("i:" + i + " ary:" + ary[i]);
	}
}

const request = indexedDB.open(dbName);
指定したデータベースをopenします。

request.onsuccess = function(event)
openが成功したイベント

const db = event.target.result;
dbオブジェクトを取得します。

const objectStoreNames = Array.from(db.objectStoreNames);
データベースに格納されているオブジェクトスキーマ(テーブル)を配列として返します。

db.close();
データベースを閉じます。

resolve(objectStoreNames);
配列を返します。

request.onerror = function(event)
openに失敗した場合のイベント

reject(event.target.error);
エラーを戻り値として返します。

function tableListArray(dbName)
オブジェクトスキーマ(テーブル)一覧を実行する関数をコールする関数です。

tableList(dbName)
オブジェクトスキーマ(テーブル)一覧を実行する関数を実行します。

.then((tables) =>
処理が成功した場合、オブジェクトスキーマ(テーブル)一覧を配列として受け取ります。

printArray(tables)
この関数の内部で、オブジェクトスキーマ(テーブル)一覧の結果をconsole.logで出力しています。

.catch((error) =>
処理が失敗した場合のエラー情報を引数のerrorオブジェクトとして受け取ります。




戻る


著作権情報
ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。
ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、
または公開したりすることはできません。
当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、
その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について
一切の責任を負わないものとします。