Rayfin

Data

Model entities once as decorated TypeScript classes and get a database schema, GraphQL API, type-safe client, permissions, and validation from the same source.

A Rayfin entity is a TypeScript class. Decorate it once and Rayfin generates the database table, a GraphQL API, a type-safe client, row-level security, and form validation from that single definition — there is no separate schema file, migration file, or API contract to keep in sync by hand.

rayfin/data/Todo.ts
import { entity, authenticated, uuid, text, boolean, date } from '@microsoft/rayfin-core';

@entity()
@authenticated('*', {
  policy: (claims, item) => claims.sub.eq(item.user_id),
})
export class Todo {
  @uuid() id!: string;
  @text({ max: 200 }) title!: string;
  @boolean({ default: false }) isCompleted!: boolean;
  @date() createdAt!: Date;
  @text({ max: 128 }) user_id!: string;
}

Register it in schema.ts, then apply it:

npx rayfin up

From there, client.data.Todo is a fully typed read/write API, scoped by the row-level policy declared above.

Set up the client

Changed in 2.0

Create one client with RayfinClient.create and reuse it across your app. Register the data service with dataService<AppSchema>() so every entity access is fully typed. The 2.0 client depends only on @microsoft/rayfin-lib, so install the auth and data packages yourself:

npm install @microsoft/rayfin-client @microsoft/rayfin-auth @microsoft/rayfin-data
src/services/rayfinClient.ts
import { RayfinClient } from '@microsoft/rayfin-client';
import { browserSession } from '@microsoft/rayfin-auth';
import { dataService } from '@microsoft/rayfin-data';
import type { AppSchema } from '../../rayfin/data/schema';

export const rayfinClient = RayfinClient.create({
  baseUrl: import.meta.env.VITE_RAYFIN_API_URL,
  publishableKey: import.meta.env.VITE_RAYFIN_PUBLISHABLE_KEY,
  auth: browserSession({ storage: true }),
  services: {
    data: dataService<AppSchema>(),
  },
});

export type AppClient = typeof rayfinClient;

VITE_RAYFIN_API_URL and VITE_RAYFIN_PUBLISHABLE_KEY are generated into .env.local by rayfin dev during development, and by deployment/environment commands for deployed flows — see Schema changes for the apply workflow that produces them.

The client exposes only what you register: auth: browserSession() adds client.auth, and services.data adds client.data. The 1.x form new RayfinClient<AppSchema>(...) was removed in 2.0, and the EntitySchema and TypedDataClients types now come from @microsoft/rayfin-data instead of the client package. See @microsoft/rayfin-client for every option and the 1.x migration. Removed in 2.0

Register other capabilities on the same client. functionsService from @microsoft/rayfin-functions adds client.functions, and connectorsService from @microsoft/rayfin-connectors adds client.connectors for existing Fabric data sources — see Wiring connectors into your app.

Model your schema

  • Modeling entities — @entity(), file layout, primary keys, property names, the schema.ts registration step, and the TypeScript versions and module settings schema compilation needs.
  • Field types — every field decorator and its options, including the MSSQL text-length rule.
  • Relationships — @one() / @many(), foreign key columns, reading related records, and the many-to-many workaround.
  • Permissions — @role(), @anonymous(), @authenticated(), the row-level policy DSL, and stamping owner fields from functions.

Read and write

  • Querying — the select / where / orderBy / execute chain, filtering, pagination, and result types that contain only the fields you select.
  • Aggregations — sums, averages, and counts computed on the server with groupBy() and aggregate().
  • Creating, updating, deleting — writes, what each mutation returns, and setting relationships correctly.
  • Form validation — generate form validation from the same entity, with no separate schema library.

Operate

  • Schema changes — how edits to rayfin/data/ reach the database, and how to verify they actually did.
  • Seeding data — populate a database from a Node.js script.
PromptUpgrade Rayfin data code to 2.0
Upgrade the Rayfin data code in my project to Rayfin 2.0. Install @microsoft/rayfin-client, @microsoft/rayfin-auth, @microsoft/rayfin-data, and @microsoft/rayfin-core at 2.0.0, and make sure TypeScript is ^5.7.2 or ^6.0.2. Replace `new RayfinClient<AppSchema>(...)` with `RayfinClient.create({ baseUrl, publishableKey, auth: browserSession({ storage: true }), services: { data: dataService<AppSchema>() } })`, importing browserSession from @microsoft/rayfin-auth and dataService from @microsoft/rayfin-data. Import EntitySchema and TypedDataClients from @microsoft/rayfin-data, and anything imported from @microsoft/rayfin-core/experimental from @microsoft/rayfin-core. Make every relative import between files under rayfin/ end in .js. Then fix queries and mutations for the 2.0 result types. Results contain only the fields passed to .select(), and findById, findFirst(filter), and findMany(filter) return only the primary key, so replace reads of other fields with .select([...]).where({ id: { eq: id } }).findFirst(). Always use the builder that .select() returns. Add the [] marker to every one-to-many selection path ('notes.title' becomes 'notes[].title'). Convert selected date fields with new Date(value) and boolean fields with value === true || value === 'true'. create and update now return only the primary key plus the fields you passed, and delete returns only the primary key. Run the type-checker and fix every remaining error.
PromptModel your first entity
In my Rayfin project, create a new entity in rayfin/data/ that models [describe your data]. Give it a uuid id, appropriate field decorators from @microsoft/rayfin-core with explicit max lengths on every text field, and an @authenticated('*') permission decorator with a policy scoping rows to the signed-in user via claims.sub. Register it in rayfin/data/schema.ts, then apply the schema with `rayfin up`.
Something wrong on this page?Report an issueEdit this page

On this page