Skip to content

Latest commit

 

History

History

README.md

Tableland Client

Tableland Client is a convenient wrapper around validator's HTTP APIs. Here is the list of public APIs.

APIs

Usage

Following are few example usage of these APIs.

First, we instantiate the client:

import (

// ...
// ...

  clientV1 "github.com/textileio/go-tableland/pkg/client/v1"
  "github.com/textileio/go-tableland/pkg/wallet"
)

// create the wallet from the given private key
wallet, _ := wallet.NewWallet("PRIVATE_KEY")
ctx := context.Background()

// create the new client
client, _ := clientV1.NewClient(
    ctx, wallet, clientV1.[]NewClientOption{}...)

Checkout the available client options here.

Create

Create a new table with the schema and options. Schema should be a valid SQL DDL as a string.

tableID, fullTableName := client.Create(
    ctx, "(id int, name text)",
    clientV1.WithPrefix("foo"))
Write

Write will execute an mutation query, returning the txn hash. Additional options can be passed in.

  // Create a new table
  tableID, fullTableName := client.Create(
    ctx, "(id int, name text)",
    clientV1.WithPrefix("foo"))

  // Update the table
  query := fmt.Sprintf(
    "update %s set name = alice where id = 1", fullTableName)
  hash := client.Write(ctx, query)

  // Alter table to rename a column
  query := fmt.Sprintf(
    "alter table %s rename name to username", fullTableName)
  hash := client.Write(ctx, query)
Receipt

Receipt will get the transaction receipt given the transaction hash. Additional configuration is possible with options.

    txHash := "0xabcd"
    receipt, ok, err := client.Receipt(
      ctx, txnHash, clientV1.WaitFor(cp.receiptTimeout))
Read

Read runs a read SQL query with the provided options.

    // Create a new table: `myTable`
    client.Create(ctx, "(counter int)", clientV1.WithPrefix("myTable"))

    // Insert a row in `myTable`
    client.write(ctx, "insert into myTable (counter) values(42)")

    // Read it into the `target` struct
    result := map[string]interface{}{}
    client.Read(
        ctx, "select counter from myTable",
        result, clientV1.ReadExtract())
GetTable

The GetTable API will return the Table struct given the table id.

    // create a new table 
    tableID, fullTableName := client.Create(
        ctx, "(bar text DEFAULT 'foo',zar int, CHECK (zar>0))",
	    WithPrefix("foo"), WithReceiptTimeout(time.Second*10))
	
    // get the table using tableID
    table, err := client.GetTable(ctx, tableID)