ImmuDB with Anvil Uplink

For anyone like me, with a want and need for immutable database records, here’s a simple way to integrate ImmuDB with your anvil apps using uplink (so you can do this from any local or virtual machine, or within your docker network or K8s cluster).

Step 1:

Set up Immudb ‘locally’ using the following command lines:

docker volume create immudb-data
docker run -it -d -p 3322:3322 -p 9497:9497 -v immudb-data:/var/lib/immudb --name immudb -e IMMUDB_ADDRESS="0.0.0.0" codenotary/immudb
wget https://github.com/vchain-us/immudb/releases/download/v1.3.0/immuadmin-v1.3.0-linux-amd64
mv immuadmin-v1.3.0-linux-amd64 immuadmin
chmod +x immuadmin
./immuadmin login immudb #default pw is immudb, change on first use#
./immuadmin database create #dbname#

Now you are ready to crack on.

Step 2:

Create a .env file locally with a variable called IMMUDBPW (or whatever else you like):
IMMUDBPW=A5TR0ngP1755w0RD

Then create your python script in the same folder (i.e. immutable.py).

Step 3:

Pip install anvil-uplink, immudb-py, and python-dotenv and also get your uplink server key.

Add an UPLINK variable to your .env with your uplink key:

UPLINK=server_KEY

Step 4:

Now write your script:

from immudb.client import ImmudbClient
import anvil.server
import anvil.secrets
import json
import os
from dotenv import load_dotenv

dotenv_path = os.path.join(os.getcwd(), '.env')
load_dotenv(dotenv_path)

anvil.server.connect(os.getenv('UPLINK'))

@anvil.server.background_task
def save_immutable(transaction_id,dict):

    try:
        client = ImmudbClient("localhost:3322")
        pw = os.getenv('IMMUDBPW')
        client.login("immudb", pw)
        client.useDatabase(b"dbname")
        transaction_id = str(transaction_id).encode('utf-8')
        record_json = json.dumps(dict).encode('utf-8')
        client.verifiedSet(transaction_id, record_json)
        results = client.verifiedGet(transaction_id)

    except Exception as e:
        print(f"Error: {e}")

    client.shutdown()

Done:

Now you have an instance of an immutable database running, which you can connect to via uplink, and send across a transaction_id and dict-based content for tamperproof storage.

No Horizon on the horizon for Anvil developers!

1 Like