Introducing a new way to review your PRs.

Read the announcement

frappe / insights

Open Source Business Intelligence Tool

View on GitHub

25 documents · Updated Nov 23, 2026

docs / data-source-management

Data source management

Data source management gives Insights a named connection to an analytical source and turns that source into metadata that queries can use. The client configures and lists sources. The backend checks access, tests connections, discovers tables, exposes columns, and keeps table relationships available to query building. Map support uses query results and chart settings to match user location names to the regions supported by a map.

Resource model

The resource model separates a connection from the tables and links discovered through it. This lets the same source support source selection, table search, schema inspection, previews, and query execution without placing connection details in each query.

Data source records

An Insights Data Source v3 record names a source and stores the information needed to reach it. A source has a type of Database or REST API. Database sources use a database kind such as MariaDB, PostgreSQL, SQLite, DuckDB, BigQuery, ClickHouse, or MSSQL. A REST API source uses a base URL and one of the supported authentication modes.

The client uses a small creation model for the connection forms. Remote database forms collect a title, host, port, database name, credentials, and optional SSL. PostgreSQL also accepts a schema. DuckDB can use a local file or a remote HTTP-based source with optional headers. The list model adds the server name, owner, status, source flags, and creation dates so the source list can show health and ownership without exposing connection details.

References: frontend/…/data_source.types.ts• L2-59 insights/…/insights_data_source_v3.json• L43-245 insights/…/insights_data_source_v3.py• L157-205 insights/…/duckdb.py• L169-219

An Insights Table v3 record is the stable metadata entry for one table in one data source. It keeps the source identity, remote table name, display label, and sync information. An Insights Table Link v3 record describes a joinable relationship between two tables through a pair of columns.

The source owns discovery of these records. A normal refresh adds tables that are present in the remote source and repairs stored names when the same remote table receives a new spelling. A forced refresh rebuilds the source table list and can rebuild its links. Removing a non-site source also removes its table and link records, so downstream queries must not treat those records as independent connections.

References: insights/…/insights_table_v3.py• L20-42 insights/…/insights_table_v3.py• L105-133 insights/…/insights_table_link_v3.py• L9-66 insights/…/insights_data_source_v3.py• L468-519

Configuration lifecycle

The client keeps one shared source list and uses backend calls for all source actions. A source must pass a connection test before the client allows creation. After creation or a successful refresh, the source becomes available as a query and table option.

The main source lifecycle is shown below.

flowchart LR
    A[Source form] --> B[Test connection]
    B -->|valid| C[Create source]
    B -->|invalid| D[Show connection error]
    C --> E[Source list]
    E --> F[Discover tables]
    F --> G[Queryable table metadata]
    G --> H[Query source and table options]

Source creation

The source list presents database connection choices and file upload as entry points. The client sends the typed connection object to test_connection first. Only an administrator can run the test or create the saved source through the exposed API. The backend validates the fields for the selected source type, assigns a name from the title, tests access, and records an active or inactive status.

CSV, Excel, and JSON uploads follow a separate import path. The client first previews the uploaded file and then asks the backend to import it as a table. This creates usable table data rather than a remote connection form.

References: frontend/…/DataSourceList.vue• L31-84 frontend/…/data_source.ts• L26-51 insights/…/data_sources.py• L139-165 insights/…/insights_data_source_v3.py• L42-52 insights/…/insights_data_source_v3.py• L157-205 frontend/…/UploadCSVFileDialog.vue• L22-79

Source refresh

Refreshing a source reads its current remote table names and updates the table metadata used by Insights. The source model removes internal table names from the discovered set. PostgreSQL can read more than one schema, and table names carry a schema only when that is needed to distinguish tables. The refresh compares PostgreSQL tables by schema and table identity, so a change in display spelling or schema qualification does not create a second table record for the same remote table.

For Frappe sources, a separate refresh can rebuild known table links. Those links help the query builder suggest or support joins. The refresh actions are available from the source table list rather than from the query itself.

References: frontend/…/tables.ts• L76-128 frontend/…/DataSourceTableList.vue• L79-104 insights/…/insights_data_source_v3.py• L359-425 insights/…/insights_data_source_v3.py• L468-519

Queryable metadata

Queryable metadata is the bridge between a configured source and the query experience. The backend exposes source-owned tables and derives column metadata from the table that the current user is allowed to read. The client stores table lists by source and turns them into table choices.

Table discovery

get_data_source_tables returns table records for one source or for all sources. It supports a search term over the table label or table name and applies a result limit. The source table page uses this endpoint for its list and calls update_data_source_tables when the list must reflect the remote source.

References: insights/…/data_sources.py• L37-66 frontend/…/tables.ts• L8-30 frontend/…/DataSourceTableList.vue• L15-23

Schema and column metadata

get_schema builds a source-wide schema view from the discovered table records. Each table includes its source, name, label, and columns. Column types are translated into Insights data types so query controls can choose valid dimensions and measures without knowing each database's native type system. The type map covers common scalar values as well as JSON and array values. The same type map is used when queries run against a live source or the local data store.

The narrower get_data_source_table_columns endpoint returns the columns for one table. The client maps each result to the common query column shape. This shared shape is also used by table previews and query result displays.

References: insights/…/data_sources.py• L102-113 insights/…/data_sources.py• L190-219 frontend/…/tables.ts• L57-67 insights/…/ibis_utils.py• L1319-1349

Expression metadata and safety

Expression metadata makes typed columns usable in custom query expressions. The client can show the available functions and parameter help, while the backend checks syntax, names, and data types before it accepts an expression for query building.

JSON helpers turn a JSON column into values that filters, summaries, and other expressions can use. A dotted path reaches nested keys or list positions, including positions from the end. The helpers accept normal JSON text and common serialized object values. json_value lets the expression state the target type, while json_extract infers types from values that are present. Missing keys, JSON null, malformed values, and failed typed casts become empty values. null_if provides the same empty-value contract for source placeholders such as an empty string.

Expressions can transform query values but cannot open files, read URLs, create backend connections, or write result files. The same boundary applies during validation and execution, so expression features do not turn query authoring into a data import or export path.

References: insights/…/utils.py• L30-138 insights/…/utils.py• L452-473 insights/…/functions.py• L572-709 insights/…/functions.py• L1066-1080 insights/…/ibis_utils.py• L1207-1225 insights/…/ibis_utils.py• L1330-1349 insights/…/test_expression_io.py• L24-98 insights/…/test_json_functions.py• L15-114 insights/…/test_validate_expression.py• L14-119

Table previews and counts

A table preview reads the live source through the same permission-aware table path used for source exploration. It returns the table schema and at most the first 100 rows. A separate count endpoint returns the current row count. These calls help an engineer or query author inspect data without turning the preview into a full data load.

References: insights/…/data_sources.py• L69-99 frontend/…/tables.ts• L33-74 frontend/…/DataSourceTable.vue• L36-49

Table relationships

Table links provide relationship metadata for a pair of tables. The API checks access to the left table before returning links and returns links in either direction so callers can use the same result shape regardless of join order. Link refresh is restricted to the source types that can derive Frappe table relationships.

References: insights/…/data_sources.py• L124-136 frontend/…/tables.ts• L99-128 insights/…/insights_table_link_v3.py• L41-89

Stored table state

A discovered table can also have a local stored copy in the data store. A query can request that copy through a background import. Full imports replace the stored table. Incremental imports read after a bookmark or an initial sync date, then append new rows or update existing rows when the table has the required sync fields. If the source gains columns, an append or update import adds those columns before it writes the new rows. The table record keeps the stored state and updates the bookmark from the warehouse only after the import has committed.

Local DuckDB reads use shared read connections. A write stages import batches and takes a file lock only for the commit, then releases cached readers and waits for active readers within the time allowed by the operation. This keeps imports and maintenance from racing with query reads while preserving the source-to-data-store handoff.

If a stored table is missing when a query needs it, the system can queue a rebuild and return an empty table with the right shape while the import runs. REST API sources do not use this table import path. Cleanup can remove unused non-incremental stored state and safely re-importable warehouse tables. It keeps unexplained non-empty tables, or tables whose row count cannot be checked, because their rows may be the only copy. This state belongs to the data store as well as to source metadata, so changes to import behavior should be reviewed with Data store administration.

References: insights/…/data_warehouse.py• L144-305 insights/…/data_warehouse.py• L318-427 insights/…/data_warehouse.py• L430-751 insights/…/data_warehouse.py• L787-818 insights/…/data_warehouse.py• L856-1176 insights/…/insights_data_source_v3.py• L304-330

Map region metadata

Map region metadata connects a discrete location column from a query result to the fixed region names in a map definition. The supported map types are world and india. A chart stores manual mappings in its chart configuration, so the mapping belongs to the chart rather than to the source or table.

Region matching

The map configuration sends the values from the selected location column to find_unresolved_regions. The backend resolves a value by checking a saved manual mapping first, then a case-insensitive exact match, and finally similarity suggestions. The response separates resolved values from unresolved values and gives up to five suggestions for each unresolved value.

The matching flow is shown below.

flowchart LR
    A[Query location values] --> B[Region matching API]
    C[Map region definitions] --> B
    D[Saved chart mappings] --> B
    B --> E[Resolved values]
    B --> F[Unresolved values and suggestions]
    F --> G[User selects target region]
    G --> H[Chart mapping saved]

References: frontend/…/MapChartConfigForm.vue• L49-141 insights/…/maps.py• L10-47 insights/…/maps.py• L79-141

Mapping persistence

get_available_regions returns the valid region names for the selected map. save_region_mappings accepts additions, changes, and empty values that remove old mappings. It rejects a target that is not part of the selected map and requires chart write access before saving the chart configuration.

The map editor reloads matching data after a save. A changed map type or location column causes the editor to check unresolved values again, which keeps the warning tied to the current query result.

References: insights/…/maps.py• L50-77 insights/…/maps.py• L144-172 frontend/…/RegionMappingDialog.vue• L34-106 frontend/…/MapChartConfigForm.vue• L129-141

Access boundaries

Source management separates administrative connection changes from user-level data inspection. The backend applies source and table permission checks before it returns source metadata or live table data. For a site database, the same path can also apply row and column permissions so a preview does not reveal more than a query can return.

Connection failures follow the same boundary. A user who can change a source can receive the driver detail needed to fix it. Other users receive only a generic connection error, while the detail remains in the server error log.

Queries can use a live source or a local stored copy. Live execution uses the source connection and its source dialect. Data-store execution uses the local DuckDB warehouse, which lets background imports prepare data without holding a remote connection open for the full import. The query layer uses the source's type and SQL dialect metadata to keep these paths aligned.

Expression evaluation stays inside the query boundary. It can build values from the current query and approved functions, but it cannot use expression syntax to reach a file, URL, backend connection, or output file.

Site database handling is special. Only one site database can exist, it cannot be deleted, and it remains active as the source for Insights' own database. Deleting another source removes its discovered table and link records.

References: insights/…/data_sources.py• L69-77 insights/…/data_sources.py• L116-136 insights/…/insights_data_source_v3.py• L151-174 insights/…/insights_data_source_v3.py• L252-272 insights/…/insights_data_source_v3.py• L274-357 insights/…/data_warehouse.py• L345-421 insights/…/data_warehouse.py• L1235-1316 insights/…/ibis_utils.py• L1352-1391

Change checklist

When these files or sections are changed, remember to consider these:

  • A new source kind must stay aligned across the client creation model, its connection form, backend field validation, connection dispatch, list metadata, and SQL dialect support.
  • A change to table naming or schema rules must stay aligned across remote discovery, table records, table lookup, schema responses, and query references. PostgreSQL names are especially sensitive when more than one schema is configured.
  • A change to column metadata must preserve the common Insights type shape used by schema responses, table previews, query controls, and result caches that carry column names.
  • A change to source or table permission checks must cover table lists, schema data, previews, row counts, table links, and query execution as one access contract.
  • A change to expression functions, JSON value types, or expression safety must stay aligned across editor help, validation, runtime evaluation, and live or data-store query execution.
  • A change to map region definitions must account for available-region responses, match suggestions, and chart mappings that already point to old region names.
  • A change to source deletion or forced refresh must account for table links and queries that refer to the affected table records.
  • A change to local DuckDB reads or writes must preserve the handoff between cached readers, file locks, queued table imports, warehouse cleanup, and the stored table state.
  • A change to full or incremental import rules must keep sync settings, import logs, stored-table flags, bookmarks, and cleanup protection consistent with the data that was actually committed.
  • A change to append or update imports must preserve the contract that incoming columns are matched by name and that new source columns are added before rows are written.
  • A change to warehouse table naming or source schemas must stay aligned across stored-table records, import jobs, orphan detection, legacy table cleanup, and warehouse compaction.

Important failure modes

TriggerConsequenceGuard
Required connection fields are missing or the source cannot be reachedThe connection test fails. A source may remain inactive and table discovery cannot refresh.Validate the source-specific fields and require a successful test before creation.
A remote table gains columns while a stored append or update import runsNew rows can fail to land if the local table schema does not match the source.Add missing incoming columns before the append or update write.
Remote tables change without a metadata refreshThe source list and query controls show stale or missing tables.Run the source table refresh before using newly added or removed tables.
A PostgreSQL schema name or table name is interpreted with the wrong qualificationA table can be duplicated, missed, or fail to resolve in a query.Keep schema parsing, table identity, and table record naming consistent for single and multiple schemas.
A user lacks access to a source or tableMetadata, preview, count, or link requests are rejected or return only permitted data.Apply source and table checks before live inspection and keep preview permissions aligned with queries.
A user without source write access receives a raw connection errorThe error can expose connection details that the user must not see.Return generic connection errors to non-configurators and keep driver detail in the server log.
An expression uses an unknown name or an operation that does not support the column typeExpression validation returns an error and the query cannot use that expression.Validate syntax, names, and types against the current column metadata before query execution.
An expression tries to read or write through an I/O attribute or object methodValidation or execution rejects the expression without giving it a data path or backend connection.Keep the expression context limited to approved functions and reject I/O attributes in the expression source.
A JSON path is missing, contains JSON null, has malformed input, or has a value that cannot use the requested typeThe extracted value is empty. Filters and summaries do not receive a fake text value such as null.Use typed JSON value extraction and handle empty values with the query's normal null rules.
A map value has no exact or saved matchThe value remains unresolved and cannot map to a chart region without user action.Use the supplied suggestions or save a valid region mapping for the chart.
A saved map target is not in the selected map definitionThe mapping request is rejected.Validate every target against the selected map's available regions.
A local table import overlaps with another warehouse write or a reader holds the file openThe write waits for access or fails after its time limit.Use the shared warehouse write path and treat import and maintenance timeouts as operational errors.
Incremental sync has no valid bookmark or start dateThe import cannot define its first range and does not start.Set a sync-from date for the first import and keep the committed bookmark with the stored table.
A stored table is pruned or missing while a query needs itThe query can show no rows until a background re-import completes.Check import status and logs, then refresh the query after the import succeeds.
Cleanup cannot match a warehouse table to stored metadata or an active importA non-empty table is kept and reported instead of being removed, or an unsafe cleanup can leave stale storage.Keep the stored-table, import-job, and warehouse naming contracts aligned.

Key files

FileWhy to read it
frontend/…/data_source.types.tsDefines the client source list and connection shapes.
frontend/…/data_source.tsShows the shared client source store and its backend calls.
frontend/…/DataSourceList.vueShows source selection, creation entry points, and list behavior.
insights/…/insights_data_source_v3.jsonDefines the server resource fields and source-level roles.
insights/…/insights_data_source_v3.pyExplains source validation, connection status, discovery, refresh, deletion, and connection error boundaries.
insights/…/duckdb.pyExplains local and remote DuckDB source access and the read/write boundary for local files.
insights/…/data_sources.pyDefines the source, table metadata, preview, schema, count, and link APIs.
frontend/…/tables.tsShows how the client loads and refreshes table metadata.
insights/…/insights_table_v3.pyDefines the table resource and the permission-aware table access path.
insights/…/insights_table_link_v3.pyDefines stored table relationships and their symmetric lookup.
insights/…/data_warehouse.pyExplains stored table imports, schema changes, sync state, cleanup, and the local data-store boundary.
insights/…/utils.pyDefines the approved expression context and expression checks.
insights/…/functions.pyDefines query functions, typed JSON extraction, and placeholder-to-null conversion.
insights/…/ibis_utils.pyExplains shared type conversion and the live-versus-data-store query path.
insights/…/test_expression_io.pyGuarantees that expression evaluation cannot reach file, URL, or backend I/O.
insights/…/test_json_functions.pyGuarantees typed JSON extraction, path handling, and consistent empty-value behavior.
insights/…/test_validate_expression.pyGuarantees safe expression validation and useful editor metadata.
insights/…/maps.pyDefines map region discovery, matching, and chart mapping persistence.
frontend/…/MapChartConfigForm.vueShows how query location values drive map checks.
frontend/…/RegionMappingDialog.vueShows the user flow for reviewing and saving region mappings.

The Data sources and data store page places these resources in the wider storage model. See Data store administration for managed local data and Query editor and execution workflow for how source metadata enters query construction.