This repository has been archived by the owner on Jun 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifecycle.py
58 lines (45 loc) · 1.58 KB
/
lifecycle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import logging
import json
from kwil import Kwil
def run():
# assume that account has enough fund
# here we use test_db.kf as our dataset schema, we'll use test_db.json(compiled schema)
# create client
client = Kwil(Kwil.GRPCProvider(os.getenv("KWIL_PROVIDER", "grpc.kwil.com:80")),
Kwil.load_wallet(os.getenv("KWIL_CLI_PRIVATE_KEY", "EMPTY_KEY")))
db_name = "testdb" # The name of the database, from the schema file
# create dataset
with open("./testdb.json", "r") as f:
schema_json = f.read()
schema = json.loads(schema_json)
db_name = schema["name"]
try:
client.deploy_database(schema_json.encode("utf-8"))
except Exception as e:
logging.exception(e)
# list dataset
dbs = client.list_databases()
print("datasets after create: ", dbs)
# execute an action
db_id = Kwil.generate_dbi(client.wallet.address, db_name)
action = "create_user"
tx_receipt = client.execute_action(
db_id,
action,
# `create_user` is defined in test_db.kf
[{"$id": 1, "$username": "aha", "$age": 18}],
)
print("create user result: ", tx_receipt)
# call a view action
action = "view_user_info"
tx_receipt = client.call_action(db_id, action, {})
print("user info:", tx_receipt)
# drop dataset
tx_receipt = client.drop_database(db_name)
print("drop dataset result: ", tx_receipt)
# list dataset
dbs = client.list_databases()
print("datasets after delete: ", dbs)
if __name__ == "__main__":
run()