テーブルの状態操作
ステート・プロバイダーには、ステートを保存するためのテーブル操作がある。
テーブル値の設定
テーブル名を使用すると、後で検索できるようにキーと値のペアを保存できます。
メソッド署名
mapSet(table: string, keyValuePairs: any)
テーブル値の設定
const session = vcr.createSession();
const state = new State(session);
const customer = req.body.customer;
await state.mapSet("customers", { [customer.number]: JSON.stringify(customer) });
session = vcr.createSession()
state = State(session)
customer = body['customer']
number = customer['number']
await state.mapSet('customers', { number: customer })
インクリメント・テーブルの値
テーブル名とキーを使って、その値を増やすことができる。
メソッド署名
mapIncrement(table: string, key: string, increment: number)
テーブル値のインクリメント
const number = req.body.number;
await state.mapIncrement("customers", number, 1);
number = body['number']
await state.mapIncrement('customers', number, 1)
テーブル値の取得
テーブル名とキーを使って、値を取り出すことができる。
メソッド署名
mapGetValue<T>(table: string, key: string)
テーブル値の取得
const number = req.body.number;
const customer = await state.mapGetValue("customers", number);
number = body['number']
customer = await state.mapGetValue('customers', number)
複数のテーブル値の取得
テーブル名とキーの配列を使って、複数の値を取得することができます。
メソッド署名
mapGetMultiple<T>(table: string, keys: string[])
複数のテーブル値の取得
const number = req.body.number;
const otherNumber = "447000000001";
const customers = await state.mapGetMultiple("customers", [number, otherNumber]);
number = body['number']
otherNumber = '447000000001'
customer = await state.mapGetMultiple('customers', [number, otherNumber])
スキャンテーブルの値
パターンを指定することで、指定したカーソルから始まるテーブルをスキャンすることができる。
メソッド署名
mapScan(table: string, cursor: string, pattern: string, count: number)
テーブル値のスキャン
const name = req.body.name;
await state.mapScan("customers", "0", name, 0);
name = body['name']
await state.mapScan('customers', '0', name, 0)
すべてのテーブル値の取得
テーブル名を使えば、保存されているすべての値を取り出すことができる。
メソッド署名
mapGetValues<T>(table: string)
すべてのテーブル値の取得
const customers = await state.mapGetValues("customers");
customers = await state.mapGetValues('customers')
テーブルを確保する
テーブル名を使用すると、テーブル全体を取得できます。
メソッド署名
mapGetAll<T>(table: string)
テーブルの確保
const customerNumberPairs = await state.mapGetAll("customers");
customerNumberPairs = await state.mapGetAll('customers')
テーブルの長さを取得する
テーブル名を使って、テーブルの長さを取得することができる。
メソッド署名
mapLength<T>(table: string)
テーブルの長さの取得
const tableLength = await state.mapLength("customers");
tableLength = await state.mapLength('customers')
テーブルキーが存在するかチェックする
テーブル名とキーを使って、テーブルにキーが存在するかどうかを調べることができます。
メソッド署名
mapExists(table: string, key: string)
テーブルキーが存在することを確認する
const number = req.body.number;
const isExistingCustomer = await state.mapExists("customers", number);
number = body['number']
isExistingCustomer = await state.mapExists('customers', number)
テーブル値の削除
テーブル名とキーのリストを使用して、値を削除することができます。
メソッド署名
mapDelete(table: string, keys: string[])
テーブル値の削除
const number = req.body.number;
await state.mapDelete("customers", [number]);
number = body['number']
await state.mapDelete('customers', [number])
テーブルの削除
テーブル名を使って、テーブル全体を削除することができます。
メソッド署名
delete(table: string)
テーブルの削除
await state.delete("customers");
await state.delete('customers')