Fields

Fields in EffortlessDB are powerful tools for querying and manipulating data. They allow you to specify conditions, sort data, and perform various operations on your database entries.

Properties

field

The field name or names to query. Can be a string for a single field, or a tuple/list for embedded fields.

db.add({"embedded": {"field": "data"}}) db.filter(Field(("embedded", "field")).equals("data")) [{"embedded": {"field": "data"}}]

condition

A function that defines the condition for the entry. It is given the entry's data for the given key and returns if the entry passes. To set this, use a method defined below.

Methods

__init__

Creates a new Field object with the given key. This automatically creates the condition that the field must exist.

db.add({"test": "data"}) has_age = Field("age") db.filter(has_age) []

equals

Creates a condition that the entry's field is equal to the given value.

named_alice = Field("name").equals("Alice")

greater_than

Creates a condition that the entry's field is greater than the given value.

senior_discount = Field("age").greater_than(65)

less_than

Creates a condition that the entry's field is less than the given value.

free_entry = Field("age").less_than(5)

contains

Creates a condition that checks if the field contains the given value. For strings, it checks if the value is a substring. For lists, it checks if the value is in the list. Case-sensitive by default.

python_skill = Field("skills").contains("Python")

startswith

Creates a condition that checks if the field starts with the given value. Case-sensitive by default.

starts_with_A_or_a = Field("name").startswith("A", case_sensitive=False)

endswith

Creates a condition that checks if the field ends with the given value.

lives_at_lane = Field(("address", "street")).endswith("ln")

matches_regex

Creates a condition that checks if the field matches the given regex pattern and regex flags (if given).

valid_email = Field("email").matches_regex(r"[^@]+@[^@]+\.[^@]+")

between_dates

Creates a condition that checks if the field's date is between the given dates. Valid dates are either datetime objects, strings in the yyyy-mm-dd format, or Unix timestamps.

summer_2023 = Field("date").between_dates("2023-06-21", "2023-09-22")

fuzzy_match

Creates a condition that checks if the field fuzzy matches the given value. Don't use this for exact filtering!

name_like_john = Field("name").fuzzy_match("John", threshold=0.7)

passes

Creates a condition that checks if the field passes the given lambda or function.

def is_even(number): ‎ return number % 2 == 0 even_number = Field("number").passes(is_even) # Or, even_number = Field("number").passes(lambda x: x % 2 == 0)

is_type

Creates a condition that checks if the field is of the expected type.

is_string = Field("name").is_type(str)

Note: Fields in EffortlessDB are Queries with field attributes under the hood. If you want to create a passes condition that receives the entire data of an entry, import and create a Query and pass in a Callable that expects an entire entry dict. You'll need to do input validation yourself.

multifield = Query(lambda entry: entry["field1"] + entry["field2"] == 7)
EffortlessConfig Docs Home