ZTeraDB Query Builder Guide
The ZTeraDBQuery class helps you build database queries without writing SQL.
π― What is ZTeraDBQuery?
It is a chainable query builder that lets you perform:
- SELECT
- INSERT
- UPDATE
- DELETE
- Filtering
- Sorting
- Pagination
- Related field lookups (joins)
Example:
query = ZTeraDBQuery("user").select()
π§ How Query Building Works
Start Query
β
Choose Type (select/insert/update/delete)
β
Add Fields (optional)
β
Apply Filters
β
Run the Query
π Creating a Query
query = ZTeraDBQuery("schemaName")
schemaName β table/schema in your ZTeraDB instance
π₯ Query Types
1οΈβ£ SELECTβ
query = ZTeraDBQuery("user").select()
2οΈβ£ INSERTβ
query = (
ZTeraDBQuery("user")
.insert()
.fields({
"email": "test@site.com",
"status": True
})
)
3οΈβ£ UPDATEβ
query = (
ZTeraDBQuery("user")
.update()
.fields({"status": False})
.filter({"id": 1})
)
4οΈβ£ DELETEβ
query = (
ZTeraDBQuery("user")
.delete()
.filter({"id": 5})
)
π· fields() β Selecting, Inserting, Updating
SELECT Specific Fieldsβ
query.select().fields({
"email": 1,
"status": 1
})
INSERT New Rowβ
query.insert().fields({
"name": "John",
"email": "john@test.com",
"status": True
})
UPDATE Existing Rowβ
query.update().fields({
"status": False
})
π― Basic Filters (filter)
query.filter({"status": True})
query.filter({"id": 10})
query.filter({"email": "abc@test.com"})
Equivalent SQL:
WHERE status = true
π Advanced Filters (filterCondition)
Advanced filters allow:
- Math operations
- AND / OR conditions
- String filters
- Comparisons
(>, <, >=, <=)
from zteradb.filter_condition import ZTEQUAL, ZTMUL
query.filterCondition(
ZTEQUAL(
ZTMUL(["price", "quantity"]),
200
)
)
Equivalent SQL:
WHERE price * quantity = 200
π relatedFields() β Joins Made Easy
Fetch data from related schemas.
userFilter = (
ZTeraDBQuery("user")
.select()
.fields({"email": 1})
.filter({"status": True})
)
query = (
ZTeraDBQuery("order")
.select()
.relatedFields({
"user": userFilter
})
)
π Sorting
query.sort({"price": 1}) # ascending
query.sort({"price": -1}) # descending
Equivalent:
- 1 β ASC
- -1 β DESC
π limit(start, end) β Pagination
query.limit(0, 10)
π’ count()
Returns number of matching records instead of row data.
query.count()
π§ͺ Full Example
query = (
ZTeraDBQuery("product")
.select()
.fields({"name": 1, "price": 1})
.filter({"status": "A"})
.sort({"price": 1})
.limit(0, 20)
)
π Common Mistakes
β Using filterCondition for simple equality
β Use .filter() unless advanced logic is needed
β Not calling .fields() for insert/update
β Always define fields for INSERT and UPDATE
β Wrong sort values
β Only use 1 or -1
β Missing schemaName
β Always pass a valid schema name to constructor
π Youβre Ready for Filtering Logic!
Continue to:
π filter-condition.md