1.25.3 ā€¢ Published 8 months ago

@castore/json-schema-command v1.25.3

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

JSON Schema Command

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

šŸ“„ Installation

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

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

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 { tuple } from '@castore/core';
import { JSONSchemaCommand } from '@castore/json-schema-command';

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]) => {
    const { name, level } = commandInput;
    const pokemonId = generateUuid();

    await pokemonsEventStore.pushEvent({
      aggregateId: pokemonId,
      version: 1,
      type: 'POKEMON_APPEARED',
      timestamp: new Date().toISOString(),
      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]) => {
    // ...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
1.21.0

11 months ago

1.22.0

10 months ago

1.25.0

9 months ago

1.24.1

9 months ago

1.25.1

9 months ago

1.24.2

9 months ago

1.23.0

10 months ago

1.22.1

10 months ago

1.24.0

9 months ago

1.22.2

10 months ago

1.25.2

8 months ago

1.25.3

8 months ago

1.20.1

12 months ago

1.20.2

12 months ago

1.20.0

1 year ago

1.19.0

1 year ago

1.18.1

1 year ago

1.17.2

1 year ago

1.18.0

1 year ago

1.17.1

1 year ago

1.19.1

1 year ago

1.15.0

1 year ago

1.14.0

1 year ago

1.13.0

1 year ago

1.17.0

1 year ago

1.16.1

1 year ago

1.15.2

1 year ago

1.16.0

1 year ago

1.15.1

1 year ago

1.12.0

1 year ago

1.11.0

1 year ago

1.10.0

1 year ago

1.9.0

1 year ago

1.8.0

1 year ago

1.4.4

1 year ago

1.7.0

1 year ago

1.4.3

1 year ago

1.6.0

1 year ago

1.5.1

1 year ago

1.4.2

1 year ago

1.5.0

1 year ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.1

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago