Rayfin

@microsoft/rayfin-core

Complete decorator reference for @microsoft/rayfin-core — entities, field types, relationships, and permissions that generate Data API Builder configuration.

@microsoft/rayfin-core provides the decorators you apply to TypeScript classes in rayfin/data/ to define your data model. Decorators store metadata at runtime via Symbol.metadata; the Rayfin CLI reads that metadata to generate Data API Builder (DAB) configuration when you run rayfin up or rayfin up db apply.

Installation

npm install @microsoft/rayfin-core

npm create @microsoft/rayfin@latest and rayfin init configure this for you automatically. If you are adding Rayfin to an existing project, configure both manually.

@microsoft/rayfin-core 2.0 declares a TypeScript peer dependency of ^5.7.2 || ^6.0.2 — the same range Rayfin schema compilation enforces. TypeScript 6 is now supported; TypeScript 5.0 through 5.7.1, 6.0.0, 6.0.1, prereleases, and 7.x are not. Before you update core, update TypeScript in the package that owns your schema, then commit the manifest and lockfile:

npm install --save-dev "typescript@^5.7.2"

Use "typescript@^6.0.2" instead to move to TypeScript 6. If npm reports ERESOLVE on core's TypeScript peer, run npm ls typescript @microsoft/rayfin-core and fix the installed version; do not bypass the check with --force or --legacy-peer-deps. See Supported TypeScript versions. Changed in 2.0

TC39 Stage 3 decorators

Rayfin's decorators use the TC39 Stage 3 decorators proposal, not the older experimental decorators used by frameworks like NestJS or Angular.

  • Never enable experimentalDecorators or emitDecoratorMetadata in tsconfig.json.
  • Include ESNext.Decorators in the lib array.
  • No reflect-metadata import is required. Metadata is stored on the class itself via the native Symbol.metadata object, which the CLI and client SDKs read directly.
tsconfig.json
{
  "compilerOptions": {
    "target": "ES2022",
    "lib": ["ES2022", "ESNext.Decorators"],
    "module": "NodeNext",
    "moduleResolution": "NodeNext"
  }
}

Warning

Bundlers that compile decorators to an older target silently break Rayfin's metadata. If you see Expression expected errors on @entity() or other decorators, your build target is too old or your compiler is not parsing TC39 decorators. For Vite, set target: 'es2022'. @vitejs/plugin-react (esbuild) needs nothing more; @vitejs/plugin-react-swc works once you enable its Stage 3 decorator support — see Configure Vite for entity imports. Changed in 2.0

Class decorators

@entity()

Marks a class as a DAB entity — required on every class you want exposed through the data API. Entity settings are inferred from the class name and convention: the entity name is the kebab-case class name, and the source table is its pluralized snake_case form.

export declare function entity(name?: string): <T extends constructor<IEntity>>(
  _target: T,
  _context: ClassDecoratorContext<T>,
) => void;
rayfin/data/Todo.ts
import { entity, uuid, text } from '@microsoft/rayfin-core';

@entity()
export class Todo {
  @uuid() id!: string;
  @text({ max: 200 }) title!: string;
}

Every entity has an id field that serves as its UUID primary key. If you don't declare id explicitly, Rayfin adds it to the schema automatically; the field is always optional on create (the server generates one when omitted). Composite or non-id primary keys are not supported.

Reserved entity names

New in 1.36

Entity names become GraphQL type names, and the generated schema already defines scalar and root operation types. The CLI rejects an entity or connector entity whose name collides exactly, or whose name starts with GraphQL's introspection prefix __.

Matching is case-sensitive: Date is reserved, but TaskDate is not.

Reserved names: Any, Base64String, Boolean, Byte, ByteArray, Date, DateTime, Decimal, Duration, Float, ID, Int, LocalDate, LocalDateTime, LocalTime, Long, Mutation, Query, Short, SignedByte, Single, String, Subscription, Time, TimeSpan, UnsignedByte, UnsignedInt, UnsignedLong, UnsignedShort, URI, URL, UUID, plus any name beginning with __.

To resolve a collision, rename the class or pass an explicit safe name to @entity():

rayfin/data/DateRecord.ts
import { entity, text } from '@microsoft/rayfin-core';

@entity('DateRecord')
export class DateRecord {
  @text({ max: 200 }) label!: string;
}

@blob()

Changed in 2.0

Marks a class as a Rayfin Storage folder configuration. Storage is generally available in 2.0, and blob, StorageObject, ContentTypes, the bytes / kb / mb / gb helpers, and the BlobFolderOptions, ByteSize, and MimeGlob types are exported from the package root. The 1.x @microsoft/rayfin-core/experimental import path no longer resolves. The storage implementations are unchanged, so in this package only the import path moves.

rayfin/storage/ProfileImage.ts
import {
  blob,
  role,
  ContentTypes,
  mb,
  StorageObject,
} from '@microsoft/rayfin-core';

@blob({
  name: 'uploads',
  onConflict: 'overwrite',
  maxSize: mb(5),
  allowedContentTypes: [ContentTypes.AnyImage],
})
@role('authenticated', '*', {
  policy: (claims, item) => claims.sub.eq(item.owner_id),
})
export class ProfileImage extends StorageObject {}
function blob(folderNameOrOptions?: string | BlobFolderOptions): <T extends constructor<unknown>>(
  _target: T,
  context: ClassDecoratorContext<T>,
) => void;

interface BlobFolderOptions {
  name?: string;
  onConflict?: 'error' | 'overwrite';
  maxSize?: number | ByteSize;
  allowedContentTypes?: MimeGlob[];
}

Use StorageObject when a policy references server-managed intrinsic object fields such as owner_id. The user_metadata intrinsic was removed from StorageObject in 1.36; application metadata is represented by decorated fields on the folder class and sent through upload({ fields }). Removed in 1.36

Field decorators

Every field on an @entity() class needs exactly one type decorator. All of them accept a shared set of base options, plus type-specific options.

Shared base options

OptionTypeDefaultDescription
optionalbooleanfalseAllows NULL. Fields are required by default — combine with a TypeScript ? on the property.
uniquebooleanfalseAdds a unique constraint.
defaultmatches the field's typenoneDefault value for the column.
columnstringproperty nameSQL column name to create or map for this field while keeping the TypeScript property as the GraphQL field name. New in 1.36

Property names

Changed in 2.0

A property name becomes a GraphQL field name, so it must match the GraphQL name grammar /^[_A-Za-z][_0-9A-Za-z]*$/ and must not start with __. The CLI rejects other property names, such as $price or 'my-col', during schema analysis — before the schema is applied — with an error that suggests a valid name. The rule applies to relationship properties and to connector entities as well. When the underlying column has such a name, use a valid property name and map it with the column option:

@decimal({ column: '$price' }) price!: number;

@text(options?) / @email(options?)

Variable-length text. @email() accepts the identical option type (TextFieldOptions) and adds an email-format hint used by downstream validation and UI — it does not have its own narrower option set.

OptionTypeDefaultDescription
maxnumberdialect maximumMaximum character length. Omitting it (or passing -1) falls back to the database's maximum, which on MSSQL is NVARCHAR(MAX) — this breaks GraphQL schema generation at deploy time. Always set max explicitly; see Known limitations.
minnumbernoneMinimum character length.
regexRegExpnonePattern the value must match.
import { entity, uuid, text, email } from '@microsoft/rayfin-core';

@entity()
export class Author {
  @uuid() id!: string;
  @text({ max: 200 }) name!: string;
  @text({ optional: true, max: 2000 }) bio?: string;
  @email({ max: 254, unique: true }) emailAddress!: string;
}

@uuid(options?)

UUID identifier — the conventional type for primary keys and foreign key columns. Accepts only the shared base options (no type-specific ones). A foreign key column that references another entity's @uuid() primary key must itself be @uuid() — a @text() field cannot stand in for it (an auth-derived field like user_id sourced from claims.sub is a @text() field, not a foreign key, so this rule doesn't apply to it).

@uuid() id!: string;
@uuid() category_id!: string; // foreign key — must match the referenced PK type

@int(options?)

Whole numbers.

OptionTypeDefaultDescription
max / minnumbernoneValue bounds.

@decimal(options?)

Fixed-point decimal, for money and other precise numeric values.

OptionTypeDefaultDescription
max / minnumbernoneValue bounds.
precisionnumber18Total digits. Maximum 28 (the Data API Builder runtime limit). Must be specified together with scale.
scalenumber2Digits after the decimal point. Must be between 0 and precision.

With neither option set, @decimal() defaults to DECIMAL(18,2).

@decimal() price!: number;                       // DECIMAL(18,2)
@decimal({ precision: 10, scale: 4 }) weight!: number; // DECIMAL(10,4)

@boolean(options?)

True/false values. Base options only.

@date(options?)

ISO-8601 date/time, serialized from Date objects or ISO strings. Base options only.

@set(...)

String enum with a database check constraint. @set has three call forms, all producing the same field behavior — pick whichever reads best at the call site:

type FieldDecoratorFn<T> = (
  target: unknown,
  context: ClassFieldDecoratorContext<unknown, T | undefined>,
) => void;

// 1. Values only
function set<T extends [string, ...string[]]>(...values: T): FieldDecoratorFn<T[number]>;

// 2. A single options object, with values in `enum`
function set<T extends [string, ...string[]]>(options: SetFieldOptions<T>): FieldDecoratorFn<T[number]>;

// 3. Base options object, followed by values
function set<T extends [string, ...string[]]>(
  options: Omit<SetFieldOptions<T>, 'enum'>,
  ...values: T
): FieldDecoratorFn<T[number]>;

SetFieldOptions<T> adds one required property, enum: T, on top of the shared base options.

// Form 1 — values only
@set('todo', 'in-progress', 'done') status!: 'todo' | 'in-progress' | 'done';

// Form 2 — options object with `enum`
@set({ enum: ['todo', 'in-progress', 'done'], optional: true })
status?: 'todo' | 'in-progress' | 'done';

// Form 3 — base options, then values
@set({ optional: true }, 'todo', 'in-progress', 'done')
status?: 'todo' | 'in-progress' | 'done';

Field type to database mapping

DecoratorLogical typeDatabase mapping
@uuid()UUIDUNIQUEIDENTIFIER
@text()stringNVARCHAR(n), sized from max
@int()integerINT
@decimal()decimalDECIMAL
@boolean()booleanBIT
@date()datetimeDATETIME2
@email()stringSame as @text(), plus an email-format hint
@set()enumString column with a check constraint

Relationship decorators

@one() and @many() take a lazy () => Target function (not the class directly) so entities in different files can reference each other without import-order issues. Both accept only RelationshipFieldOptions — the base options minus default:

OptionTypeDefaultDescription
optionalbooleanfalseNullable relationship (optional foreign key).
uniquebooleanfalseMakes a @one() relationship one-to-one instead of many-to-one.

Custom foreign key or target key names are not supported — see Known limitations.

@one(target, options?)

Many-to-one (or one-to-one with unique: true). Rayfin auto-generates a foreign key column named {property}_id — you don't need to declare it unless your application code reads or writes the raw ID.

import { entity, uuid, text, one } from '@microsoft/rayfin-core';
import { Category } from './Category.js'; // `import`, not `import type` — the decorator needs the runtime class

@entity()
export class Todo {
  @uuid() id!: string;
  @text({ max: 200 }) title!: string;
  @one(() => Category) category!: Category;   // generates category_id
  @one(() => Category, { optional: true }) parentCategory?: Category;
}

@many(target, options?)

The inverse, one-to-many side of a @one() relationship.

import { entity, uuid, text, many } from '@microsoft/rayfin-core';
import { Todo } from './Todo.js';

@entity()
export class Category {
  @uuid() id!: string;
  @text({ max: 100 }) name!: string;
  @many(() => Todo) todos?: Todo[];
}

Many-to-many relationships are not supported directly — model them with an explicit join entity that has two @one() fields. See Known limitations.

Note

@microsoft/rayfin-core ships a built-in, read-only User system entity (id, email) that is always a valid @one() / @many() target — you don't need to declare it in your own schema to reference it.

Connector-only custom relationship keys

CustomKeyRelationshipFieldOptions is exported from the stable package root in 1.36 for connector entities with explicit primary keys. Data-path entities still use the conventional id primary key; sourceFields and targetFields are not available for relationships between normal rayfin/data/ entities. New in 1.36

Permission decorators

Every entity needs an explicit permission decorator. Omitting one is not the same as denying access — it silently applies authenticated: * (full CRUD for any signed-in user), which is rarely what you want in production.

@authenticated(actions?, options?)

Shorthand for the built-in authenticated role — requires a valid user session.

type RoleDecoratorFn<TEntity> = (
  target: constructor<TEntity>,
  context: ClassDecoratorContext<constructor<TEntity>>,
) => void;

function authenticated<TEntity extends object = object>(
  actions?: SimpleAction | SimpleAction[], // default: '*'
  options?: RoleDeclarationOptions<TEntity>,
): RoleDecoratorFn<TEntity>;
ParameterTypeDefaultDescription
actions'create' | 'read' | 'update' | 'delete' | 'execute' | '*', or an array of them'*'The DAB action(s) this role may perform. 'execute' applies to stored procedures.
options.policy(claims, item) => PolicyExpressionnoneRow-level filter — see Policy DSL below.
options.include(keyof TEntity)[]noneRestrict the role to only these fields.
options.exclude(keyof TEntity)[]noneHide these fields from the role.
rayfin/data/Todo.ts
import { entity, authenticated, uuid, text } 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;
  @text({ max: 128 }) user_id!: string;
}

@role(roleName, actions, options?)

The general-purpose form that @authenticated() and @anonymous() are shorthands for.

function role<TEntity extends object = object>(
  roleName: 'authenticated' | 'anonymous',
  actions: SimpleAction | SimpleAction[],
  options?: RoleDeclarationOptions<TEntity>,
): RoleDecoratorFn<TEntity>;

@anonymous(actions?, options?)

Public, unauthenticated access. Exported from the package root:

function anonymous<TEntity extends object = object>(
  actions?: SimpleAction | SimpleAction[], // default: '*'
  options?: RoleDeclarationOptions<TEntity>,
): RoleDecoratorFn<TEntity>;
import { entity, uuid, text, anonymous, authenticated } from '@microsoft/rayfin-core';

@entity()
@anonymous('read')
@authenticated('*')
export class PublicPost {
  @uuid() id!: string;
  @text({ max: 200 }) title!: string;
}

Note

On a deployed Fabric app, anonymous permissions require a Fabric tenant administrator to turn on the tenant setting Enable anonymous data access for Fabric Apps (Preview) for the app owner. The decorators always record their metadata, but without the setting, applying a schema that grants the anonymous role any permission fails with HTTP 403 (ANONYMOUS_DATA_ACCESS_BLOCKED_BY_TENANT_ADMIN). The setting covers the data API only. See Fabric tenant setting for anonymous access.

Policy DSL and field visibility

The policy callback receives two typed arguments and returns a composable expression that compiles to a DAB policy string:

  • claims — a ClaimsDsl exposing the base claims claims.sub, claims.email, and claims.role, plus any app-defined claims merged into RayfinCustomClaims.
  • item — a proxy with one property per entity field, typed from the class itself so a typo fails at compile time.

App-defined claims come from a dynamic auth claims provider (udf.authClaims): code generation writes rayfin/auth/claims.generated.d.ts, which merges the provider's return type into RayfinCustomClaims, so claims.accountId type-checks in a policy. Without a provider, only the base claims exist. See Dynamic auth claims. New in 2.0

Both claims.<name> and item.<field> support .eq(value) and .neq(value), each returning a PolicyExpression. Combine expressions with .and() / .or(), which parenthesize both sides automatically:

policy: (claims, item) =>
  claims.role.eq('admin').or(claims.sub.eq(item.owner_id).and(item.isActive.eq(true)))

Use include / exclude on any role decorator to control field-level visibility per action. Both arrays are typed against the entity's actual property names:

import { entity, authenticated, uuid, text } from '@microsoft/rayfin-core';

@entity()
@authenticated('read', { exclude: ['secret'] })
@authenticated(['create', 'update'], {
  policy: (claims, item) => claims.sub.eq(item.owner_id),
})
export class Document {
  @uuid() id!: string;
  @text({ max: 128 }) owner_id!: string;
  @text({ max: 200 }) title!: string;
  @text({ optional: true, max: 5000 }) secret?: string;
}

Multiple role decorators on the same class are aggregated; conflicting declarations for the same role and action produce a warning at generation time.

Package entry points

Changed in 2.0

Only the entry points declared in the package's exports map resolve, and deep imports into dist/ fail. 2.0 changes the map:

Import2.0 status
@microsoft/rayfin-coreEvery public API: decorators, field options, the policy DSL, form validation, storage declarations (blob, StorageObject, ContentTypes, bytes / kb / mb / gb, BlobFolderOptions, ByteSize, MimeGlob), and the claims types RayfinBaseClaims, RayfinCustomClaims, AllClaims, and ClientClaims.
@microsoft/rayfin-core/experimentalRemoved. Import storage symbols and claims types from the package root. Removed in 2.0
@microsoft/rayfin-core/experimental/indexCompatibility alias that re-exports only the claims types and ClaimRef / ClaimsDsl — not the storage symbols. Prefer the package root.
@microsoft/rayfin-core/decoratorsRemoved. Import decorators from the package root. Removed in 2.0
@microsoft/rayfin-core/analysis, @microsoft/rayfin-core/schemaRemoved. Their internal replacements under _internal/ are for Rayfin tooling, not applications. Removed in 2.0

Because @microsoft/rayfin-core/experimental/index re-exports the same claims declarations as the root, augmenting RayfinCustomClaims through either specifier merges into one interface.

Connector-oriented exports that were previously under the core experimental subpath — Source, connector-widened entity / field decorators, AutoGenerated, and the experimental connector schema types — were removed in 1.36. Use the connector packages documented under Connectors for connector models. Removed in 1.36

Form validation

@microsoft/rayfin-core can build a Standard Schema validator directly from an @entity() class, so you can validate form input without a separate library.

function toStandardSchema<T, K extends keyof T = never>(
  entity: EntityClass<T>,
  options?: { omit?: readonly K[] },
): RayfinStandardSchema<Omit<T, K | 'id'>>;

function getFieldConstraints<T, K extends keyof T>(
  entity: EntityClass<T>,
  field: K & string,
): FieldConstraints | undefined;

toStandardSchema automatically omits the id primary key and any @one() / @many() relationship properties — pass additional field names (like timestamps) via omit.

import { toStandardSchema, getFieldConstraints } from '@microsoft/rayfin-core';
import { Todo } from '../rayfin/data/Todo.js';

const todoInputSchema = toStandardSchema(Todo, {
  omit: ['createdAt', 'updatedAt'] as const,
});

const result = todoInputSchema.validate({ title: 'Write docs' });
if (result.issues) {
  // result.issues: readonly StandardSchemaV1.Issue[]
} else {
  // result.value is typed as Omit<Todo, 'id' | 'createdAt' | 'updatedAt'>
}

const titleConstraints = getFieldConstraints(Todo, 'title');
// { type: 'string', min: 1, max: 200, optional: false }

The returned object implements the StandardSchemaV1 contract (~standard), so it works directly with TanStack Form, Conform, tRPC v11, and any other Standard Schema–compatible library, in addition to its own convenience .validate() method.

Something wrong on this page?Report an issueEdit this page

On this page