Skip to content

SQL (Alchemy)

Type

Database Collection = GraphQL Type

Collection | Model | Type

import dbcontroller as dbc

mongo = dbc.Controller(mongo="mongodb://localhost:27017/example")

# Types
@mongo.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 = {"name": {"$regex": "joe"}}
results = await table.find_one(query)

Reading | Querying (Multiple-Records)

results = await table.all()
query = {"$or": [
    {"name": {"$regex": "joe"}},
    {"name": {"$regex": "jane"}}
]}
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")