Storage

near-runtime-ts > "near" > Storage

Class: Storage

Represents contract storage.

Hierarchy

Storage

Index

Methods

  • get

  • getBytes

  • getItem

  • getString

  • getU64

  • hasKey

  • keys

  • remove

  • removeItem

  • set

  • setBytes

  • setItem

  • setString

  • setU64

Methods

get

get<T>(key: string, defaultValue?: T): T

Defined in near.ts:143

Gets given generic value stored under the key. Key is encoded as UTF-8 strings. Supported types: bools, integers, string and data objects defined in model.ts. For common/dynamic arrays use {@link #getArray}

Type parameters:

T

Parameters:

Name

Type

Default value

Description

key

string

-

A key to read from storage.

Default value defaultValue

T

null

The default value if the key is not available

Returns: T A value of type T stored under the given key.

getBytes

getBytes(key: string): Uint8Array

Defined in near.ts:80

Get byte array stored under given key. Key is encoded as UTF-8 strings. Byte array stored as is.

It's convenient to use this together with DomainObject.decode().

Parameters:

Name

Type

key

string

Returns: Uint8Array

getItem

getItem(key: string): string

Defined in near.ts:46

deprecated: Use getString or get

Parameters:

Name

Type

key

string

Returns: string

getString

getString(key: string): string

Defined in near.ts:60

Get string value stored under given key. Both key and value are encoded as UTF-8 strings.

Parameters:

Name

Type

key

string

Returns: string

getU64

getU64(key: string): u64

Defined in near.ts:113

Get 64-bit unsigned int stored under given key. Key is encoded as UTF-8 strings. Number is encoded as decimal string.

Parameters:

Name

Type

key

string

Returns: u64 int value or 0 if value is not found

hasKey

hasKey(key: string): bool

Defined in near.ts:84

Parameters:

Name

Type

key

string

Returns: bool

keys

keys(prefix: string): string[]

Defined in near.ts:24

Returns list of keys starting with given prefix.

NOTE: Must be very careful to avoid exploding amount of compute with this method. Make sure there is a hard limit on number of keys returned even if contract state size grows.

Parameters:

Name

Type

prefix

string

Returns: string[]

remove

remove(key: string): void

Defined in near.ts:88

Parameters:

Name

Type

key

string

Returns: void

removeItem

removeItem(key: string): void

Defined in near.ts:95

deprecated: Use #remove

Parameters:

Name

Type

key

string

Returns: void

set

set<T>(key: string, value: T): void

Defined in near.ts:124

Stores given generic value under the key. Key is encoded as UTF-8 strings. Supported types: bools, integers, string and data objects defined in model.ts.

Type parameters:

T

Parameters:

Name

Type

Description

key

string

A key to use for storage.

value

T

A value to store.

Returns: void

setBytes

setBytes(key: string, value: Uint8Array): void

Defined in near.ts:70

Store byte array under given key. Key is encoded as UTF-8 strings. Byte array stored as is.

It's convenient to use this together with domainObject.encode().

Parameters:

Name

Type

key

string

value

Uint8Array

Returns: void

setItem

setItem(key: string, value: string): void

Defined in near.ts:39

deprecated: Use setString or set

Parameters:

Name

Type

key

string

value

string

Returns: void

setString

setString(key: string, value: string): void

Defined in near.ts:53

Store string value under given key. Both key and value are encoded as UTF-8 strings.

Parameters:

Name

Type

key

string

value

string

Returns: void

setU64

setU64(key: string, value: u64): void

Defined in near.ts:103

Store 64-bit unsigned int under given key. Key is encoded as UTF-8 strings. Number is encoded as decimal string.

Parameters:

Name

Type

key

string

value

u64

Returns: void