BaseTen
Search…
✍️ Creating database models within BaseTen
You can create your own database models and have BaseTen manage both a) the database backing it, and b) the necessary DB migrations as you create and update your data model. You can then use your database models in your worklets and view as demonstrated below. To get started, visit the "Database schema" tab:
Example database model
The database models that you create here are available in your Code block and Decision nodes as SQLAlchemy ORM classes that you can use to query existing objects and create new ones. Use the context parameter to get a) the SQLAlchemy ORM class representing your database model, and b) the SQLAlchemy session object.
Let's look at two examples that show how to use these constructs:

Querying objects

Let's say that we want to query all FraudCases in a Code block node:
1
def do(node_input, env, context):
2
# All database model ORM classes are available under context.classes
3
FraudCase = context.classes.FraudCase
4
# And the SQLAlchemy session object is here:
5
session = context.session
6
7
for case in session.query(FraudCase).order_by(FraudCase.id):
8
print(case.id, case.transaction_id, case.fraud_probability)
Copied!

Creating objects

Let's say that we want to create a new FraudCase in a Code block node:
1
def do(node_input, env, context):
2
FraudCase = context.classes.FraudCase
3
session = context.session
4
5
case = FraudCase(
6
transaction_id=node_input['transaction_id'],
7
fraud_probability=node_input['fraud_probability']
8
)
9
session.add(case)
Copied!
Last modified 6mo ago