db3.js is the db3 network javascript API and you can use it to write and query JSON documents against the db3 network. and you can build fully decentralized applications combining web3.js and db3.js
yarn add db3.js
Connect to db3 network with db3 browser wallet
// the key seed
const mnemonic ='...'
// create a wallet
const wallet = DB3BrowserWallet.createNew(mnemonic, 'DB3_SECP256K1')
// build db3 client
const client = new DB3Client('http://127.0.0.1:26659', wallet)
Connect to db3 network with metamask
const wallet = new MetamaskWallet(window)
const client = new DB3Client('http://127.0.0.1:46659', wallet)
const [dbId, txId] = await client.createDatabase()
const {db} = initializeDB3('http://127.0.0.1:26659', dbId, wallet)
// add a index to collection
const indexList: Index[] = [
{
name: 'idx1',
id: 1,
fields: [
{
fieldPath: 'name',
valueMode: {
oneofKind: 'order',
order: Index_IndexField_Order.ASCENDING,
},
},
],
},
]
// create a collecion
const collectionRef = await collection(db, 'cities', indexList)
// add a doc to collection
const result = await addDoc(collectionRef, {
name: 'beijing',
address: 'north',
})
// get all docs from collection
const docs = await getDocs(collectionRef)
Please ⭐️ this repository if this project helped you!
git clone https://github.com/dbpunk-labs/db3.js.git
git submodule update --recursive
cd tools && bash start_localnet.sh
git submodule update
# install the dependency
yarn
# generate the protobuf
make
# run test
yarn test
# format the code
yarn prettier --write src
# run benchmark
yarn benny-sdk