Schema

Retrieves a schema by event name

Let's implement our first command:

sf events schema NAME

We will use the same generator to create commands as well, in order to create this command let's run:

sf dev generate command --name events:schema
Salesforce CLI generating a new command

Let’s see what files were created (and updated) for the schema command.

File
Description

package.json

[Updated] npm file that describes package dependencies and versions.

src/commands/events/schema.ts

Main TypeScript file that contains the code for the schema command. The command imports and extends classes from @salesforce/sf-plugins-core.

messages/events.schema.md

Markdown file that contains the messages that make up the command help and errors.

test/commands/events/schema.nut.ts

Complex integration, smoke, and end-to-end tests. Also known as NUTS (non-unit-tests.)

test/commands/events/schema.test.ts

Unit tests.

Now, lets replace the auto-generated code with the the implementation of the src/commands/events/schema.ts command:

import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core';
import { AnyJson } from '@salesforce/ts-types';

// Import messages from Markdown file
Messages.importMessagesDirectory(__dirname);
const messages = Messages.load('events-plugin', 'events.schema', [
  'summary',
  'description',
  'examples',
  'args.name.summary',
  'flags.org.summary',
]);

// Define output type
export type EventsSchemaResult = {
  schema: AnyJson;
};

export default class EventsSchema extends SfCommand<EventsSchemaResult> {
  public static summary = messages.getMessage('summary');
  public static description = messages.getMessage('description');
  public static examples = messages.getMessages('examples');

  // Positional arguments
  public static args = [
    {
      name: 'name',
      required: true,
      description: messages.getMessage('args.name.summary'),
    },
  ];

  // Non-positional arguments
  public static flags = {
    'target-org': Flags.requiredOrg({
      summary: messages.getMessage('flags.org.summary'),
      char: 'o',
      required: true,
    }),
  };

  public async run(): Promise<EventsSchemaResult> {
    const { flags, args } = await this.parse(EventsSchema);

    const name = args.name as string;

    // Gets the org from the flag
    const org = flags['target-org'];

    // Gets the pre-authenticated connection
    const conn = org.getConnection();

    // Performns a REST API using the internal instance of JSForce
    const response = await conn.request<AnyJson>({
      method: 'GET',
      url: `/sobjects/${name}/eventSchema`,
      headers: {
        'content-type': 'application/json',
      },
    });

    // Prints the JSON response
    this.styledJSON(response);

    return {
      schema: response,
    };
  }
}

And the documentation file: messages/events.schema.md

# summary

Retrieves a schema by event name

# description

It retrieves a schema by event name using the REST API

# args.name.summary

Event name

# flags.org.summary

Login username or alias for the target org.

# examples

- <%= config.bin %> <%= command.id %> Test_Event__e

Now you can give it a try by running:

./bin/dev events schema EVENT_NAME__e
{
  "name": "Test_Event__e",
  "namespace": "com.sforce.eventbus",
  "type": "expanded-record",
  "fields": [
    {
      "name": "data",
      "type": {
        "type": "record",
        "name": "Data",
        "namespace": "",
        "fields": [
          {
            "name": "schema",
            "type": "string"
          },
          {
            "name": "payload",
            "type": {
              "type": "record",
              "name": "Payload",
              "doc": "",
              "fields": [
                {
                  "name": "CreatedDate",
                  "type": "string",
                  "doc": "CreatedDate:DateTime"
                },
                {
                  "name": "CreatedById",
                  "type": "string",
                  "doc": "CreatedBy:EntityId"
                },
                {
                  "name": "Name__c",
                  "type": [
                    "null",
                    "string"
                  ],
                  "doc": "Data:Text:00N8c00000hD6sn",
                  "default": null
                },
                {
                  "name": "Value__c",
                  "type": [
                    "null",
                    "double"
                  ],
                  "doc": "Data:Double:00N8c00000hD6ss",
                  "default": null
                }
              ]
            }
          },
          {
            "name": "event",
            "type": {
              "type": "record",
              "name": "Event",
              "fields": [
                {
                  "name": "replayId",
                  "type": "long"
                }
              ]
            }
          }
        ]
      }
    },
    {
      "name": "channel",
      "type": "string"
    }
  ]
}

Last updated