Skip to content

SQL (Alchemy)

Type

Database Table = GraphQL Type

Table | Model | Type

import dbcontroller as dbc

sql = dbc.Controller(sql="sqlite:///example.db")

# Types
@sql.model
class User:
    name: str
    notes: dbc.text
    meta: dbc.json
    disabled: bool = False

Manager

table = User.objects

C.U.D — Examples

form = {
    "name": "joe doe",
}

# Create One
results = await table.create(form)

# Create Many
results = await table.create([{"name": "joe doe"}, {"name": "jane doll"}])
selector = "Encoded-ID" # ["Some-ID-1", "Some-ID-2", "More-IDS..."]
form = {
    "name": "jane doll",
}

# Update One or Many
results = await table.update(selector, form)
# Delete One
results = await table.delete("Encoded-ID")

# Delete Many
results = await table.delete(["Encoded-ID-1", "Encoded-ID-2", "More-IDS..."])

Reading | Querying (One-Record)

results = await table.detail("Encoded-ID")
results = await table.get_by(id=1)
query = table.where("name", "contains", "joe")
results = await table.find_one(query)

Reading | Querying (Multiple-Records)

results = await table.all()
query = (
    table.where("name", "contains", "jane")
    | table.where("name", "contains", "joe")
)
results = await table.find(query, page=1, limit=100, sort_by="-id")
query = {"name": "joe doe"}
results = await table.filter_by(search=query, page=1, limit=100, sort_by="-id")
search_value = "j"
columns = ["name", "notes"]
results = await table.search(columns=columns, value=search_value, page=1, limit=100, sort_by="-id")