textury / ardb

Interact with Arweave like if it was any other database. With typed definitions and without having to write a GQL story to retrieve data!
MIT License
54 stars 7 forks source link

ArDB

The best way to interact with the weave, without having to write any GQL and trying to remember param names!

ArDB is well typed and with the basic functions anyone will need to run and execute any request.

Plus, coming soon, we are planing to release an easy way to add or update data to Arwaeve right from ArDB!

Let's get coding!

First you'll need to add ardb to your project:

yarn add ardb

Import it into your project file:

import ArDB from 'ardb';
const ardb = new ArDB(arweaveInstance, logLevel? = LOG.ARWEAVE); // logLevel is optional and respects Arweave's logging by default.

Initialisation

// arweave is Arweave Client instance
const ardb = new ArDB(arweave);

And now we are ready to play with it!

Examples:

// Get a single transaction
const tx = await ardb.search('transaction').id('A235HBk5p4nEWfjBEGsAo56kYsmq7mCCyc5UZq5sgjY').findOne();

// Get an array of transactions, in this case it will be of length 1 since
// we asked to find only 1.
const txs = await ardb.search('transactions').appName('SmartWeaveAction').findOne();

// This is the same as doing:
const txs = await ardb.search('transactions').tag('App-Name', 'SmartWeaveAction').limit(1).find();

// Search for multiple transactions
const txs = await ardb.search('transactions').from('BPr7vrFduuQqqVMu_tftxsScTKUq9ke0rx4q5C9ieQU').find();

// You can then keep going and searching for more by doing:
const newTxs = await ardb.next();

// Or you could get everything at once by doing:
const txs = await ardb.search('blocks').id('BkJ_h-GGIwfek-cJd-RaJrOXezAc0PmklItzzCLIF_aSk36FEjpOBuBDS27D2K_T').findAll();

API

search(type: 'transaction' | 'transactions' | 'block' | 'blocks' = 'transactions')
id(id: string)
ids(ids: string[])
appName(name: string)
type(type: string)
tags(tags: {name: string, values: string|string[]})
tag(name: string, values: string|string[])
from(owners: string|string[])
to(recipients: string|string[])
min(min: number)
max(max: number)
only(fields: string|string[])
exclude(fields: string|string[])
limit(limit: number)
sort(sort: 'HEIGHT_DESC' | 'HEIGHT_ASC')
cursor(cursor: string)

Finding

After we complete our request with the previous methods, we can go ahead and find the items.

findOne(options: GlobalOptions = {})

export interface IBlockFilter { min?: number; max?: number; }

export interface ITransactionOptions { id?: string; }

export interface ITransactionsOptions { ids?: string[]; owners?: string[]; recipients?: string[]; tags?: { name: string; values: string[] }[]; block?: IBlockFilter; first?: number; after?: string; sort?: 'HEIGHT_DESC' | 'HEIGHT_ASC'; }

export interface IBlockOptions { id?: string; }

export interface IBlocksOptions { id?: string; height?: IBlockFilter; after?: string; sort?: 'HEIGHT_DESC' | 'HEIGHT_ASC'; }

export interface IGlobalOptions extends ITransactionOptions, ITransactionsOptions, IBlockOptions, IBlocksOptions {}

export type AmountType = string | { winston?: string; ar?: string }; // string = ar export type OwnerType = string | { address?: string; key?: string }; // string = address export type DataType = string | { size: string; type: string }; // string = type export type RequestType = 'transaction' | 'block' | 'transactions' | 'blocks';