Files
common/docs/examples/trie-db/05_empty_close_drop_trie/index.js
T
Luke Schoen 1fb1f9d743 docs: Fixes #157. Add Gitbook documentation for Polkadot-JS Common (#231)
* fix: Remove prior docs attempt files

* docs: Add Gitbook docs for common repo with examples. See detailed comments

* Remove old attempt at docs in docs/ folders

* Add Typedoc config typedoc.js

* Add .nvmrc for easily switching Node.js version with NVM

* Add ESLint config .eslintrc.json and .eslintignore

* Fix in tsconfig.json so is keyring not util-keyring

* Update package.json with dependencies and script same as in polkadot-js/api

* Update root README fixing CI icons, adding guide same as in polkadot-js/api, and fixing link to keyring, not util-keyring

* Update root .gitignore with Gitbook build files same as in polkadot-js/api

* Add Gitbook docs structure same as in polkadot-js/api

* Add Gitbook examples for keyring, trie-db, util-crypto

* Add Gitbook outer summary for the menu, Readme, logo and styles same as in polkadot-js/api

* Add Gitbook config book.json

* docs: Remove db and util-rlp from Gitbook summary menu since RLP not in PoC-3

* docs: Add Typedocs with examples for db MemoryDb and DiskDb

* docs: Update db/ Readme with CI icons fixed, and indicating that no methods exposed in Common Docs

* docs: Update keyring Readme with CI icons fixed and link to Common Docs

* fix: Fix keyring package.json info to refer to common, not client

* fix: Updates license in 4x files in keyring/address folder with keyring, not util-keyring

* docs: Add Typedocs to Keyring class, pair function, and testKeyring function

* docs: Update keyring Readme with CI icons fixed and link to Common Docs

* fix: Update package.json moving @polkadot/db from devDependencies to dependencies

* fix: Update test to iterate array not object with forEach

* fix: Use semicolon instead of comma in tests to fix linting errors. Fix typo

* docs: Add Typedoc to Trie class

* docs: Add Typedoc to is functions

* docs: Update keyring Readme with CI icons fixed and link to Common Docs

* fix: Add return type to encodeAux function

* docs: Add Typedoc to encode function

* fix: Fix formatting of Typedoc example for root

* docs: Add Typedoc for genRoot

* docs: Add Typedoc for pairsUniq

* docs: Add Typedoc for getSharedLength

* docs: Add Typedoc for sharedPrefixLength

* fix: Fix formatting of Typedoc example for rootOrdered, asNibbles, fromNibbles

* fix: Various fixes to util-crypto. See commit detail comments

* Update root Readme with class and function info
* Fix typos and Typedoc examples
* Expose mnemonic by exporting it in an index.ts
* Fix naming from naclKeypair to naclKeypairFromRandom
* Fix typos
* Fix randomAsU8s to be randomAsU8a
* Fix spelling of bitLength

* fix: Various fixes to util-rlp. See detailed commit comments

* Update keyring Readme with CI icons fixed and link to Common Docs mentioning since RLP not in PoC-3 nothing exposed in Common Docs
* Add Typedocs
* Move Typedocs to different file so they appear in Common Docs

* fix: Various fixes to util. See detailed commit comments

* Update root Readme to use Yarn and list and explain exposed Common Docs methods
* Fix typos and Typedoc examples

* fix: Restore tests back using variable they were iterating over before

* docs: Move nested part of Summary to individual package Readme. Add Usage to Readmes

* fix: Add missing semicolon to existing test

* fix: Remove docs/gitbook folder and files since generated by CI

* feat: Update Gitignore to ignore CI-generated files like .html and docs/gitbook/ folder

* docs: Remove CI-icons from all packages within polkadot-js/common, except its root Readme

* fix: Update to incorporate changes from PR #233 and #229

* fix: Ignore build directories in Jest tests using modulePathIgnorePatterns

* fix: Remove useless docs/html from .gitignore since there is no html subdirectory

* fix: Update util-crypto README with link to mnemonicToSeed instead of toSecret

* docs: Update keyring create account example to show result to user

* fix: Update dependencies in examples so the examples work again

* fix: Update keyring example 02 load accounts not to use addFromAddress

* fix: Update .gitignore so it is suitable for both polkadot-js common and api

* fix: Adjust .gitignore after testing changes that work with polkadot-js api too

* fix: Resolve .gitignore to overcome FileNotFoundError: No README file (or is ignored)

* fix: Remove from Git tracking docs/.nojekyll since generated

* fix: Add lerna to gitignore to match api repo

* Revert to original .gitignore. Does not work without Gitbooks files Git tracked

* docs: Hide trie-db Examples since have limited use
2018-10-19 17:33:58 +02:00

56 lines
1.8 KiB
JavaScript
Executable File

const toU8a = require('@polkadot/util/u8a/toU8a').default;
const Trie = require('@polkadot/trie-db').default;
const {
isEmptyNode,
isKvNode
} = require('@polkadot/trie-db/util/is');
async function main () {
const trie = new Trie();
// Open the database for each of the trie instances
trie.open();
// Initial root value is valid
console.log(`Initial root Uint8Array value: ${trie.getRoot()}`);
// Create a branch under the trie root with a key of 'branch1' and 'branch2'
// and a value of 'one' and 'two'
trie.put(toU8a('branch1'), toU8a('one'));
trie.put(toU8a('branch2'), toU8a('two'));
// Retrieve the value of a given key named 'branch1' and 'branch2' in the trie
console.log(`Stored value of key 'branch1': ${trie.get(toU8a('branch1'))}`);
console.log(`Stored value of key 'branch2': ${trie.get(toU8a('branch2'))}`);
// Store the root hash of the trie root node in a variable
const trieRootNodeHash = trie.getRoot();
// Store the root node object in a variable
const trieRootNode = trie.getNode(trieRootNodeHash);
// Show information about the trie node
console.log(`Is root node object empty? : ${isEmptyNode(trieRootNode)}`);
console.log(`Is root node object a KV node? : ${isKvNode(trieRootNode)}`);
// Deletes the value of a given key named 'branch1' in the trie branch
trie.del(toU8a('branch1'));
// Retrieve the value of a given key named 'test' in the trie
console.log(`Stored value of key 'branch1': ${trie.get(toU8a('branch1'))}`);
// Empty, close, and drop the database for each of the trie instances
trie.empty();
// Retrieve the value of a given key named 'test' in the trie
console.log(`Stored value of key 'branch2': ${trie.get(toU8a('branch2'))}`);
// Close the trie
trie.close();
// Drop the trie
trie.drop();
}
main().catch(console.error).finally(_ => process.exit());