Skip to content

Latest commit

 

History

History
61 lines (50 loc) · 1.52 KB

File metadata and controls

61 lines (50 loc) · 1.52 KB
title Runtime Service Examples
description Practical examples for flow nodes, hooks, and plugin event subscriptions.

Runtime Service Examples

1) Flow custom node: read related records

export async function run(ctx: any) {
  const { record: order } = await ctx.services.data.get('sales_order', ctx.input.orderId);
  const lines = await ctx.services.data.find('sales_order_line', {
    where: { sales_order_id: order.id },
    orderBy: [{ field: 'line_no', order: 'asc' }],
    limit: 200,
  });

  return {
    order,
    lines: lines.records ?? [],
  };
}

2) Hook: check sharing permission before mutation

export async function beforeUpdate(ctx: any) {
  const ok = await ctx.services?.sharing?.canEdit('contract', ctx.input.id, {
    userId: ctx.session?.userId,
    // Read the caller's org under `organizationId` (the `session.tenantId` alias
    // was removed in v11, #3290); it feeds the sharing context's `tenantId`.
    tenantId: ctx.session?.organizationId,
    positions: ctx.session?.positions,
  });

  if (!ok) {
    throw new Error('PERMISSION_DENIED');
  }
}

3) Plugin: subscribe to kernel lifecycle events

import type { Plugin } from '@objectstack/core';

export const ExamplePlugin: Plugin = {
  name: 'example-plugin',
  async init(ctx) {
    ctx.hook('kernel:ready', async () => {
      ctx.logger.info('Kernel ready, initializing subscriptions');
    });

    ctx.hook('kernel:shutdown', async () => {
      ctx.logger.info('Shutdown signal received, cleaning up');
    });
  },
};