Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

3.4 KB
Raw Blame
import { PluginController, PluginMethod, PluginOpenMethod } from '../../../../nest-rpc/rpc-kit/decorators';
import type { PluginRpcContext } from '../../../../nest-rpc/rpc-kit/types';
import { asArgs, asTxOps, str } from '../../../../nest-rpc/rpc-params';
import { PluginUserSettingsService } from '../../plugin-user-settings.service';

/**
 * A plugin's OWN database (a SQLite file, or a schema of its own when TREK runs on
 * Postgres), plus the three methods the router registers unconditionally.
 *
 * Every method reads `ctx.data` fresh. It must never be captured in a field or a
 * closure: the handle is per plugin, and disable()/re-enable builds a new host while
 * the old one's dispose() may still be closing the previous handle. A captured one
 * would be both a stale-handle bug and, in a singleton like this, a cross-plugin data
 * leak, since one instance serves every plugin.
 */
@PluginController()
export class DbRpc {
  constructor(private readonly settings: PluginUserSettingsService) {}

  @PluginMethod('db.query', { permission: 'db:own' })
  query(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
    return ctx.data.query(str(params.sql, 'sql'), asArgs(params.args));
  }

  @PluginMethod('db.exec', { permission: 'db:own' })
  exec(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
    return ctx.data.exec(str(params.sql, 'sql'), asArgs(params.args));
  }

  @PluginMethod('db.migrate', { permission: 'db:own' })
  migrate(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
    return ctx.data.migrate(str(params.id, 'id'), str(params.sql, 'sql'));
  }

  @PluginMethod('db.tx', { permission: 'db:own' })
  tx(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
    return ctx.data.tx(asTxOps(params.ops));
  }

  /** The engine the plugin's SQL runs on, so a plugin can write the dialect it takes. */
  @PluginMethod('db.engine', { permission: 'db:own' })
  engine(_params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
    return { engine: ctx.data.engine };
  }

  /**
   * Inter-plugin call. No permission of its own: the router authorises it against
   * the declared dependency edge and the target's `provides` allowlist. The acting
   * user is forwarded so the target's export runs as the caller's user.
   */
  @PluginOpenMethod('plugins.call')
  callPlugin(params: Record<string, unknown>, ctx: PluginRpcContext): Promise<unknown> {
    return ctx.plugins.call(str(params.targetId, 'targetId'), str(params.fn, 'fn'), params.args, ctx.actingUserId);
  }

  /** Same deal: the router checks the emitter's `emits` allowlist. */
  @PluginOpenMethod('events.emit')
  async emitEvent(params: Record<string, unknown>, ctx: PluginRpcContext): Promise<unknown> {
    await ctx.plugins.emit(str(params.event, 'event'), params.payload);
    return { ok: true };
  }

  /**
   * The plugin's OWN per-user settings, decrypted host-side. Unconditional and not
   * cross-tenant: it only ever returns THIS plugin's config for the acting user. A
   * userless context (a job or onLoad) has no user, so it returns undefined and the
   * plugin falls back to ctx.config.
   */
  @PluginOpenMethod('settings.get')
  async getSetting(params: Record<string, unknown>, ctx: PluginRpcContext): Promise<unknown> {
    if (ctx.actingUserId === undefined) return { value: undefined };
    return { value: await this.settings.readOne(ctx.pluginId, ctx.actingUserId, str(params.key, 'key')) };
  }
}