Skip to main content
Version: Next

Routine <TConfig, TArgs, TReturn>

Stored procedure or function declaration. Register instances via the routines config option passed to MikroORM.init, then call them through em.callRoutine(routine, args).

TArgs and TReturn are inferred from the literal config, so em.callRoutine is fully typed without threading generics through the call site. Reach for Routine.create when the inferred type is too loose — typically an object return that should be a concrete shape rather than Dictionary.

@example
const HashUser = new Routine({
name: 'hash_user',
type: 'function',
params: {
name: { type: 'varchar(255)' },
salt: { type: 'varchar(255)' },
},
returns: { runtimeType: 'string', columnType: 'char(40)' },
body: 'SELECT SHA1(CONCAT(name, salt))',
});

await MikroORM.init({ entities: [User], routines: [HashUser] });

// args typed as `{ name: string; salt: string }`, result typed as `string`:
const hash = await em.callRoutine(HashUser, { name: 'jon', salt: 'pepper' });

Index

Constructors

constructor

  • new Routine<TConfig, TArgs, TReturn>(config): Routine<TConfig, TArgs, TReturn>
  • Parameters

    • config: TConfig

    Returns Routine<TConfig, TArgs, TReturn>

Properties

optionalreadonlybody

body?: string | RawQueryFragment<string> | (params, em) => string | RawQueryFragment<string>

optionalreadonlybodyJs

bodyJs?: (params) => unknown

Type declaration

    • (params): unknown
    • Parameters

      • params: any

      Returns unknown

optionalreadonlycomment

comment?: string

optionalreadonlydataAccess

dataAccess?: RoutineDataAccess

optionalreadonlydefiner

definer?: string

optionalreadonlydeterministic

deterministic?: boolean

optionalreadonlyexpression

expression?: string

optionalreadonlyignoreSchemaChanges

ignoreSchemaChanges?: RoutineIgnoreField[]

optionalreadonlylanguage

language?: string

readonlyname

name: string

readonlyparams

params: RoutineProperty<any>[]

optionalreadonlyreturnCustomType

returnCustomType?: Type<unknown, unknown>

optionalreadonlyreturns

returns?: RoutineReturns

optionalreadonlyschema

schema?: string

optionalreadonlysecurity

security?: RoutineSecurity

readonlytype

type: RoutineKind

Methods

staticcreate

  • create<TArgs, TReturn, TConfig>(config): Routine<TConfig, [TArgs] extends [never] ? RoutineArgsOf<TConfig> : TArgs, [TReturn] extends [never] ? RoutineReturnOf<TConfig> : TReturn>
  • Overrides the inferred TArgs/TReturn. Omit a generic to keep inference; pass never in the args slot to refine only the return type.

    @example
    interface UserStats { totalOrders: number; lastOrderAt: Date }
    const GetStats = Routine.create<never, UserStats>({
    name: 'get_user_stats',
    type: 'function',
    params: { user_id: { type: 'int', runtimeType: 'number' } },
    returns: { runtimeType: 'object', columnType: 'json' },
    body: '...',
    });

    Parameters

    • config: TConfig

    Returns Routine<TConfig, [TArgs] extends [never] ? RoutineArgsOf<TConfig> : TArgs, [TReturn] extends [never] ? RoutineReturnOf<TConfig> : TReturn>

staticis