# TableStore
A table store provides storage for multiple rows of data in block chain and methods for adding, modifying, and deleting data. This is a higher level wrapper for MultiIndex instance.
Any value stored in TableStore must be an instance inherited from Table class.
There are two options to get and search data in the storage. The first is to get data by primary key (u64 value).
The second is to pass a value inherited from Table class. The method getPrimaryValue will be used to get a primary key then.
# Constructors
constructor( code: Name, scope: Name, table: Name, indexes: Array<IDXDB> )code- TheNameobject with for the contractExample: `Name.fromString('mycontract')`scope- TheNameobject with for the scope of the table. Recommended same as contract nameExample: `Name.fromString('mycontract')`table- TheNameobject with for the table nameExample: `Name.fromString('mytable')`indexes- An optional array ofIDXDBobjects to get quick access to the dataExample: `[ new IDX64(code.N, scope.N, 0, 0) ]`Example:
import { TableStore } from 'proton-tsc' const contract = Name.fromString('mycontract'); const scope = Name.fromString('mycontract'); const table = Name.fromString('mytable'); const tablestore = new TableStore(contract, scope, table);
# Instance Methods
store(value: T, payer: Name): voidCreates new record in the storage for the provided value and account passed as
payerargument.Throws if:
- If the record with provided primary value already exists
 
Example:
tablestore.store(value, contract)set(value: T, payer: Name): voidCreates new record or updates the existing one in the storage for the provided value and account passed as
payerargument.Example:
tablestore.set(value, contract)update(value: T, payer: Name): voidUpdates the existing record in the storage for the provided value and account passed as
payerargument.Throws if:
- If the record with provided primary value does not exist
 
Example:
tablestore.update(value, contract)remove(value: T): voidRemoves the existing record in the storage for the provided value
Throws if:
- If the record with provided primary value does not exist
 
Example:
tablestore.remove(value)get(key: u64): T | nullReturns the value for a provided key.
nullis returned if there is not value found.Example:
tablestore.get(key)requireGet(key: u64, errorMsg: string): TA wrapper for
getmethod to throw error if no value for the provided key found. Displays theerrorMsgwhen throws.Throws if:
- If the record with provided primary value does not exist.
 
Example:
tablestore.requireGet(id, `no recored with ID ${id} found.`)exists(pk: u64): boolChecks if the record for provided primary key exist or not.
existsValue(value: T): boolChecks if the record for provided value exist or not.
next(value: T): T | nullReturns the record that is the next after the provided value Throws if:
- If the record for the provided value is the last in the storage
 
previous(value: T): T | nullReturns the record that is the previous to the provided value
Throws if:
- If no record for the provided value is the first in the storage
 
first(): T | nullReturns the first record in the storage. Or
nullif no records yet.last(): T | nullReturns the last record in the storage. Or
nullif no records yet.isEmpty(): boolChecks if there is any record in the storage
lowerBound(id: u64): T | nullReturns the first element with primary key greater than or equal to the provided primary key.
Example:
// there are records with ids: 3, 5, 7 const record = tablestore.lowerBound(4) // will return record with id 5. const record = tablestore.lowerBound(3) // will return record with id 3.upperBound(id: u64): T | nullReturns the first element with primary key less than or equal to the provided primary key.
Example:
// there are records with ids: 3, 5, 7 const record = tablestore.upperBound(4) // will return record with id 3. const record = tablestore.upperBound(5) // will return record with id 5.get availablePrimaryKey(): u64Returns the available primary key that can be used to save data.
getBySecondaryIDX64(secondaryValue: u64, index: u8): T | nullUtility method. Find the first table element that matches secondary value.
secondaryValueis the secondary value to search for.indexis the index to search in.getBySecondaryIDX128(secondaryValue: U128, index: u8): T | nullThe same as
getBySecondaryIDX64but with extended size of the secondary valuegetBySecondaryIDX256(secondaryValue: U256, index: u8): T | nullThe same as
getBySecondaryIDX64but with extended size of the secondary valuegetBySecondaryIDXDouble(secondaryValue: f64, index: u8): T | nullThe same as
getBySecondaryIDX64but withf64type of the secondary value