Exercise - Define entity connectors and build an inventory view
In this exercise, you expose the Adventure Works Cycles inventory table through a typed connector and build a view of the lowest stock levels. You use the generated metadata, limit access to read operations, wire the client, and verify the view in the deployed app.
What connector add generates
For tabular sources, connector add writes the connectors: entry in rayfin.yml, scaffolds rayfin/connectors/<name>/schema.ts, and runs schema discovery that writes metadata.json. It doesn't install packages, and it doesn't write entity .ts files. You write one entity file for each table that the app needs, using metadata.json as the only source of truth. A coding agent can draft these files, but you review them against the metadata.
The connector folder follows its name in rayfin/rayfin.yml, not the source's table name. The earlier exercise names the warehouse connector adventureworksconnectors and the semantic model connector adventureworkssales.
Open
rayfin/connectors/adventureworksconnectors/and findschema.tsandmetadata.json.In
metadata.json, findProductInventoryunder thedboschema. Confirm the columns and their types. For the sample table,primaryKeyColumnscontainsProductID,productNamehasmaxLength: 100, andwarehouseLocationhasmaxLength: 50.Open
rayfin/rayfin.yml. Confirm thatadventureworksconnectorshasauth.type: applicationand only thereadoperation. If you added it through interactive search, removecreate,update, anddeletefrom itsoperationslist.
These rules guide the entity definition:
metadata.jsondecides primary keys and relationships. Don't infer a primary key from a column name. If a table has no key in the metadata, the entity is keyless and doesn't support operations that address a row by key. Lakehouse SQL endpoints commonly have no key metadata.Don't edit
metadata.json. Re-add the connector to refresh it, preserving the intended name, operations, and identity flags. Generated frontend wiring has an explicit ownership mechanism, which you use later in this exercise.Run the version-pinned
npm installthatconnector addprints, verbatim. A different version of a connector package can pull a second copy of@microsoft/rayfin-data.
Define an entity from metadata
An entity class extends Source({...}), which names the schema, table, and primary key. Each property uses a field decorator that matches the SQL type, and column maps a property to a column whose name differs.
Create
rayfin/connectors/adventureworksconnectors/ProductInventory.tswith the following code. Keep relative imports inrayfin/ending in.js.import { authenticated, entity, int, text } from '@microsoft/rayfin-core'; import { Source } from '@microsoft/rayfin-connectors'; @authenticated(['read']) @entity() export class ProductInventory extends Source({ schema: 'dbo', table: 'ProductInventory', primaryKey: ['productId'], }) { @int({ column: 'ProductID' }) productId!: number; @text({ max: 100 }) productName!: string; @text({ max: 50 }) warehouseLocation!: string; @int() quantityOnHand!: number; }The metadata names the SQL key
ProductID. The entity maps that column to the propertyproductId, soprimaryKeyusesproductId. Themaxvalues come from each column'smaxLength. If your table has no key metadata, omitprimaryKeyrather than guessing one.@authenticated(['read'])grants read access to signed-in users, within the source permissions of the configured identity. The entity role and the connector's operations both allow onlyread.
Register the entity in schema.ts
The generated schema.ts is a placeholder for tabular sources. An aggregate exports the entity, the connector schema type, and the connectorConfig value. The entities map gives each entity a default list of scalar property names.
Replace the contents of
rayfin/connectors/adventureworksconnectors/schema.tswith the following code:import type { GraphQLBackedConnector } from '@microsoft/rayfin-connector-fabric-graphql'; import type { ConnectorConfig } from '@microsoft/rayfin-connectors'; import type { ProductInventory } from './ProductInventory.js'; export type { ProductInventory } from './ProductInventory.js'; export const connectorConfig = { connector: 'fabric-warehouse', operations: ['read'], entities: { ProductInventory: ['productId', 'productName', 'warehouseLocation', 'quantityOnHand'], }, } as const satisfies ConnectorConfig; export type InventorySchema = GraphQLBackedConnector< { ProductInventory: typeof ProductInventory }, typeof connectorConfig >;
Use entity property names in entities, not SQL column names. Prefer string arrays unless the app selects relationship paths. The class form supports relationship selections, but it puts TC39 decorators in the browser bundle, and the bundler must compile them. The 2.0 templates already enable that.
Wire the connector into the client
Each connector is available as client.connectors.<name>. Its key matches the name in rayfin/rayfin.yml, the schema type, and the configuration map. The semantic model also needs a client-side runtime; the warehouse doesn't.
Open
packages/frontend/src/lib/connectors.ts. It already wiresadventureworkssales. The warehouse wasn't wired when you added it because its schema didn't yet exportconnectorConfig.Take ownership of this file as described in its header: remove the
@generated bybanner line. Keep any copyright header, and replace the imports and exports with the following code. The CLI no longer overwrites this file after you remove the banner, so you maintain these entries yourself.import type { ConnectorConfigs, ConnectorsRuntime, } from '@microsoft/rayfin-connectors'; import { fabricSemanticModel } from '@microsoft/rayfin-connector-fabric-semanticmodel'; import { connectorConfig as inventoryConfig, type InventorySchema, } from '../../../../rayfin/connectors/adventureworksconnectors/schema'; import { connectorConfig as salesConfig, type AdventureworkssalesSchema, } from '../../../../rayfin/connectors/adventureworkssales/schema'; export type AppConnectorsSchema = { adventureworksconnectors: InventorySchema; adventureworkssales: AdventureworkssalesSchema; }; export const connectorConfigs = { adventureworksconnectors: inventoryConfig, adventureworkssales: salesConfig, } satisfies ConnectorConfigs<AppConnectorsSchema>; export const connectorRuntimes: ConnectorsRuntime = { adventureworkssales: fabricSemanticModel(), };Open
packages/frontend/src/lib/rayfin-client.ts. Confirm thatRayfinClient.createregisters the connectors service using these maps. Leave the existing authentication and configuration resolution unchanged.connectors: connectorsService<AppConnectorsSchema>({ configs: connectorConfigs, runtime: connectorRuntimes, }),
Build the inventory view
Queries use the same chain as the data client, with one difference. A chain that uses where, orderBy, or first needs an explicit select([...]), or it throws SELECTION_REQUIRED.
Create
packages/frontend/src/InventoryView.tsxwith the following code. It uses the template'sgetRayfinClient()function and reads the fields needed for the view.import { useEffect, useState } from 'react'; import { getRayfinClient } from './lib/rayfin-client'; type Row = { productId: number; productName: string; quantityOnHand: number; }; export function InventoryView() { const [rows, setRows] = useState<Row[]>([]); const [error, setError] = useState(''); useEffect(() => { async function loadInventory() { try { const client = await getRayfinClient(); const result = await client.connectors.adventureworksconnectors.ProductInventory .select(['productId', 'productName', 'quantityOnHand']) .orderBy({ quantityOnHand: 'asc' }) .first(20) .execute(); setRows(result as Row[]); } catch (error) { setError(error instanceof Error ? error.message : 'Query failed.'); } } loadInventory(); }, []); return ( <section className="p-400 text-foreground"> <h2 className="mb-400 text-600 font-semibold">Lowest stock</h2> {error && <p role="alert" className="mb-400 text-destructive">{error}</p>} <div className="overflow-x-auto"> <table className="w-full max-w-xl text-300"> <thead className="bg-muted"> <tr> <th scope="col" className="px-400 py-300 text-left font-semibold"> Product </th> <th scope="col" className="px-400 py-300 text-right font-semibold"> Quantity on hand </th> </tr> </thead> <tbody> {rows.map((row) => ( <tr key={row.productId} className="border-b border-border hover:bg-accent"> <td className="px-400 py-300">{row.productName}</td> <td className="px-400 py-300 text-right tabular-nums"> {row.quantityOnHand} </td> </tr> ))} </tbody> </table> </div> </section> ); }In
packages/frontend/src/App.tsx, replace the starting view's import and theAppcomponent with the following code. Keep any file header and the default export. Leave the template's authentication setup unchanged.import { InventoryView } from './InventoryView'; function App() { return <InventoryView />; } export default App;
Limit what a connector can do
TypeScript types describe the shape of data and don't decide who can see it, so they aren't an authorization boundary. Apply these controls together:
Limit operations. Use
--operations readwhen you add the connector, or edit theoperationslist. You can narrow the catalog defaults but not widen them.Add roles to connector entities. Use
@authenticated,@anonymous, or@roleon each entity, with actions that are a subset of the connector'soperations. If an entity role includes an action that the connector doesn't list,rayfin up connector applyfails.Add row policies or field rules when users need only part of an entity. With delegated access,
claimsrepresents the real caller, so a policy can compareclaims.suborclaims.emailto a column. With application access,claimsdoesn't distinguish users in the source query.Choose the identity deliberately. With application access, enforce per-user access through app and entity permissions. With delegated access, also verify each user's source access.
Deploy and verify the inventory view
rayfin dev reads the connectors: block but doesn't wire it, so connector calls need a deployed app. The npx rayfin up connector apply command applies only the generated connector configuration. It's in Preview and needs a prior rayfin up.
- From the project root, preview the deployment and then deploy. Resolve any build or type errors before testing the app. A missing
adventureworksconnectorsproperty usually means the connector name differs between the configuration and client maps.
npx rayfin up --dry-run
npx rayfin up
npx rayfin up status
- Open the deployed app URL and sign in. Confirm that the five sample products appear, ordered by quantity from lowest to highest: Brake lever, Mountain bike, Road bike, Touring bike, and Helmet.
Next step
In the next exercise, you learn how the semantic model connector returns DAX query results and use them to build a sales view alongside the inventory view.