2.3.1 ā€¢ Published 16 days ago

@castore/command-json-schema v2.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
16 days ago

JSON Schema Command

DRY Castore Command definition using JSON Schemas and json-schema-to-ts.

šŸ“„ Installation

# npm
npm install @castore/command-json-schema

# yarn
yarn add @castore/command-json-schema

This package has @castore/core and json-schema-to-ts (above v2) as peer dependencies, so you will have to install them as well:

# npm
npm install @castore/core json-schema-to-ts

# yarn
yarn add @castore/core json-schema-to-ts

šŸ‘©ā€šŸ’» Usage

import { JSONSchemaCommand } from '@castore/command-json-schema';
import { tuple } from '@castore/core';

const pokemonAppearedInputSchema = {
  type: 'object',
  properties: {
    name: { type: 'string' },
    level: { type: 'integer' },
  },
  required: ['name', 'level'],
  additionalProperties: false,
} as const; // šŸ‘ˆ Don't forget the "as const" statement
// (Cf json-schema-to-ts documentation)

const pokemonAppearedOutputSchema = {
  type: 'object',
  properties: {
    pokemonId: { type: 'string', format: 'uuid' },
  },
  required: ['pokemonId'],
  additionalProperties: false,
} as const;

// šŸ‘‡ generics are correctly inferred
const pokemonAppearCommand = new JSONSchemaCommand({
  commandId: 'POKEMON_APPEAR',
  requiredEventStores: tuple(pokemonsEventStore),
  inputSchema: pokemonAppearedInputSchema,
  outputSchema: pokemonAppearedOutputSchema,
  // šŸ‘‡ handler input/output types are correctly inferred
  handler: async (
    commandInput,
    [pokemonsEventStore],
    { generateUuid }: { generateUuid: () => string },
  ) => {
    const { name, level } = commandInput;
    const pokemonId = generateUuid();

    await pokemonsEventStore.pushEvent({
      aggregateId: pokemonId,
      version: 1,
      type: 'POKEMON_APPEARED',
      payload: { name, level },
    });

    return { pokemonId };
  },
});

šŸ‘‡ Equivalent to:

import { Command } from '@castore/core';

type RequiredEventStores = [typeof pokemonsEventStore];
type CommandInput = { name: string; level: number };
type CommandOutput = { pokemonId: string };

const pokemonAppearCommand = new Command<
  RequiredEventStores,
  RequiredEventStores,
  CommandInput,
  CommandOutput
>({
  commandId: 'POKEMON_APPEAR',
  requiredEventStores: [pokemonsEventStore],
  handler: async (
    commandInput,
    [pokemonsEventStore],
    { generateUuid }: { generateUuid: () => string },
  ) => {
    // ...same code
  },
});

āš™ļø Properties & Methods

JSONSchemaCommand implements the Command class and adds the following properties to it:

  • inputSchema (?object): The command input JSON schema
const inputSchema = pokemonAppearCommand.inputSchema;
// => pokemonAppearedInputSchema
  • outputSchema (?object): The command output JSON schema
const outputSchema = pokemonAppearCommand.outputSchema;
// => pokemonAppearedOutputSchema
2.3.1

16 days ago

2.3.0

2 months ago

2.2.0

4 months ago

2.1.0

6 months ago

2.0.0

7 months ago