Data Finder
Data Finder is a model-driven query library for Python. You define your domain model and map it to a relational schema; the library generates type-safe Python Finder classes that let you query your data without writing SQL. Temporal versioning (milestoning) is a first-class concept, not an afterthought.
Overview
The workflow has three steps:
- Define a model — describe your domain as classes and associations in Markdown.
- Define a mapping — map each class property to a database table column in Markdown.
- Generate finders — run the generator once to produce Python query classes.
Generated finders expose every mapped property as a typed method, and queries are assembled from those methods rather than from raw strings.
from finance.trade.trade_finder import TradeFinder
results = TradeFinder.find_all(
processing_time=datetime.datetime.now(),
display_columns=[TradeFinder.symbol(), TradeFinder.price()],
filter_op=TradeFinder.symbol().eq("AAPL"),
)
df = results.to_pandas()
Concepts
Class
A Class represents a logical entity — the equivalent of a UML class or a business object. It has a name, an optional description, and a set of properties.
### Class: Account
| Name | Description |
|---------|-------------------------------------------------|
| Account | Trading account used to buy and sell securities |
| Property | Id | Type | Multiplicity | Key | Description |
|----------|------|---------|--------------|-----|-------------|
| Id | id | Integer | 1 | Y | |
| Name | name | String | 1 | | |
Each property has:
| Column |
Meaning |
| Property |
Human-readable label used in generated docstrings |
| Id |
Machine identifier used in code and mappings |
| Type |
A primitive type or another class name (for navigations defined via Associations) |
| Multiplicity |
Required. * or a bare number (1, 2) stand alone; anything else is lower..upper, a number then a number or * (0..1, 1..*, 2..5). There is no default: a blank or invalid cell is an error |
| Key |
Y marks the business key |
| Description |
Optional documentation |
Supported primitive types: String, Integer, Double, Float, Decimal, Date, DateTime, Boolean, Vector (an embedding: see
Vector search).
Classes belong to a Sub-Domain (package), which controls the Python package structure of the generated finders.
## Sub-Domain: finance.reference_data
### Class: Account
...
A sub-domain with a dotted name (finance.reference_data) generates a nested package hierarchy.
Inheritance
A class can extend one or more parent classes:
### Class: Employee extends Person, Contactable
All inherited properties are available via all_properties() and appear in the generated finder.
Association
An Association declares a named relationship between two classes. Each end is the property added to that end's own class, and its multiplicity: the source property is added to the source class and returns the target (Trade.account), and the target property is added to the target class and returns the source (Account.trades). The property names are the navigation methods in the generated finders.
### Association: TradeAccount
| Name | Source | Source Property Name | Source Property ID | Source Multiplicity | Target | Target Property Name | Target Property ID | Target Multiplicity | Description |
|--------------|--------|----------------------|--------------------|---------------------|---------|----------------------|--------------------|---------------------|------------------------------|
| TradeAccount | Trade | Account | account | 1 | Account | Trades | trades | * | Links a trade to its account |
| Column |
Meaning |
| Source |
Name of the source class |
| Source Property Name |
Display name of the navigation property added to the source class (Trade.account). Required |
| Source Property ID |
camelCase id of that property (account). Required |
| Source Multiplicity |
Same forms as a property's Multiplicity: how many targets the source property returns |
| Target |
Name of the target class |
| Target Property Name |
Display name of the navigation property added to the target class (Account.trades). Optional |
| Target Property ID |
camelCase id of that property (trades). Optional, and given together with the name |
| Target Multiplicity |
Same forms as a property's Multiplicity: how many sources the target property returns, or would return if there were one. Required |
The source property and both multiplicities are mandatory. Leave the target property name and id empty for a one-way relationship, where the target class gets no accessor back:
| Name | Source | Source Property Name | Source Property ID | Source Multiplicity | Target | Target Property Name | Target Property ID | Target Multiplicity | Description |
|-----------------|--------|----------------------|--------------------|---------------------|------------|----------------------|--------------------|---------------------|-----------------------------|
| TradeInstrument | Trade | Instrument | instrument | 0..1 | Instrument | | | * | The instrument a trade is in |
The association is the single source of truth for cross-class navigation — do not also declare the same property explicitly on the class. The mapping defines the join in an #### Association: section, whether or not the relationship is one-way.
When an association is loaded, its navigation properties are automatically available via all_properties() on the relevant classes and generate methods in the finder.
Mapping
A Mapping file connects the logical model to the physical relational schema. It references one or more model files, declares the data store, and maps each class to a table.
Structure
# Finance Mapping
## Model: finance.md
## Model: finance_trade.md
## DataStore: finance_db (Database)
| Scheme | processing_start | processing_end | business_date |
|--------------------------|------------------|----------------|---------------|
| processing_only | in_z | out_z | |
| business_date_processing | in_z | out_z | DATE |
### Schema: ref_data
#### Table: account_master → Account
| Column | Type | Key | Nullable | Property ID |
|-----------|---------|-----|----------|-------------|
| ID | INT | PK | N | id |
| ACCT_NAME | VARCHAR | | Y | name |
### Schema: trading
#### Table: trades → Trade (milestoning: processing_only)
| Column | Type | Key | Nullable | Property ID |
|------------|-----------|-----|----------|-------------|
| sym | VARCHAR | | Y | symbol |
| price | DOUBLE | | Y | price |
| account_id | INT | FK | Y | account |
| in_z | TIMESTAMP | | Y | validFrom |
| out_z | TIMESTAMP | | Y | validTo |
#### Association: TradeAccount
| Source Column | Target Table | Target Column |
|---------------|----------------|---------------|
| account_id | account_master | ID |
Table mapping
#### Table: <table_name> → <ClassName> maps a database table to a class. The Property ID column references the property Id defined in the model.
Every column row needs a Nullable value: Y if the column can hold NULL, N if it is NOT NULL. A PK column is never nullable, so it should be N.
For foreign-key columns that represent association navigations, set Key to FK and use the association's Source Property ID as the property name. Follow the table block immediately with an #### Association: block that provides the join target.
Association mapping
#### Association: <AssociationName> specifies how the foreign key joins to the target table.
| Column |
Meaning |
| Source Column |
The FK column in the source table |
| Target Table |
The table the FK references |
| Target Column |
The primary key column in the target table |
Joining through other tables
An Association table with more than one row is a path through several tables. Each row after the first starts from a column of the table the row before it reached, and the last row ends at the table of the target class. The tables on the way need no class: declare them with #### Table: <name> and no arrow. Give such a table (milestoning: <scheme>) if its rows are milestoned, and the dates of the query apply to it like any other table on the path.
#### Association: EmployeeCitizenship
| Source Column | Target Table | Target Column |
|---------------|--------------|---------------|
| citizen_ref | country_refs | ref |
| code | countries | code |
Join conditions
A join can have a condition besides its key columns, such as firm.ID = person.FIRMID and firm.FLAG is null and person.FLAG is not null. Add a Condition column to the Association table and write the condition for each row over the two
tables that row joins:
#### Association: FirmPerson
| Source Column | Target Table | Target Column | Condition |
|---------------|--------------|---------------|---------------------------------------------------|
| firm_id | firms | id | firms.flag IS NULL AND persons.flag IS NOT NULL |
A column is written table.column (or schema.table.column) and says which side of the join it is on by its table, so the condition
reads the same from either end: person.firm().legal_name() and firm.employees().first_name() use it alike. It is ANDed into
the ON of the left outer join, so a row it rejects leaves the joined columns NULL and drops no row. In a path of several rows, each
row has its own condition, on the hop it belongs to. A blank cell is a hop with only its keys.
A condition can use =, <>, !=, <, <=, >, >=, IS [NOT] NULL, AND, OR, NOT, parentheses and string and number
literals. A function (now()), anything else, and a column with no table are refused when the mapping is loaded.
When a table is joined to itself its name is both sides, so the side the join reaches is written {target}, as in
personTable.MANAGERID = {target}.ID: the table's own name is the row the join starts from.
#### Association: EmployeeManager
| Source Column | Target Table | Target Column | Condition |
|---------------|--------------|---------------|------------------------------------------|
| manager_id | staff | id | staff.active = 1 AND {target}.active = 1 |
Followed from the other end, as employee.reports() is from employee.manager(), the sides trade places: {target} is then the
employee you started from. {target} can also be written in a join to another table, where it is that table.
SQL queries
A class can be mapped to a query as it is mapped to a table. Write the query as a Query section: its name and the class in the heading, its
SQL in a sql block right after it, and then the columns the query gives and what they map to, as a Table section has them:
#### Query: FIRM_QUERY → Firm
```sql
SELECT DISTINCT firm_id, firm_name
FROM people
```
| Column | Type | Key | Nullable | Property ID |
|-----------|---------|-----|----------|-------------|
| firm_id | INT | PK | N | id |
| firm_name | VARCHAR | | Y | legalName |
This is not a database view: nothing is created in the database. The query is a statement that is run, written into the SQL of a query over the class as
a derived table, FROM (SELECT DISTINCT ...) AS t0. It is read as PostgreSQL, the standard dialect here, and checked when the mapping is loaded: it is
read into the same query objects a query is made of (a TableAlias of the query, which has the columns the section lists), the tables and columns it reads
must exist, and the columns it gives must be the ones the section lists. A query can use DISTINCT, any join (INNER, LEFT, RIGHT, FULL, with an
ON), WHERE, GROUP BY with SUM, COUNT, MIN, MAX and AVG, HAVING, and named expressions ('Mr ' || first_name AS text,
CASE, UPPER, LOWER, TRIM, LENGTH, ABS). It can read another query that comes before it. A query is one SELECT: a UNION, INTERSECT or EXCEPT is
refused, and a union is written as sets (below).
Sets: a class that is the union of several queries
A class that is the union of several tables is mapped to a query for each, and each query is a set with a name of
its own: #### Query: RETAIL → Customer (set: retail). The mapping puts the sets together as one UNION ALL, gives each row the name of its set in the column _set, and checks what
a hand-written union would leave to chance: