Apps might need to communicate with the Corva Platform API and Corva Data API.

This SDK provides an CorvaDataSource class, which is node-api-client and based on got

Hierarchy

  • CorvaDataSource

Implements

  • CorvaApi

Properties

aggregate: (<T>(__namedParameters: AggregateOpts) => Promise<T[]>)

Type declaration

createData: (<T>(createDataOpts: CreateDataOpts<T>) => Promise<string[]>)

Type declaration

    • <T>(createDataOpts: CreateDataOpts<T>): Promise<string[]>
    • Create one or multiple records.

      This method will create new records.

      Type Parameters

      • T

      Parameters

      Returns Promise<string[]>

createOrUpdateData: (<T>(__namedParameters: CreateOrUpdateDataOpts<T>) => Promise<CreateOrUpdateDataResult>)

Type declaration

getAppSettings: (<T>() => Promise<AssetSettings & T>)

Type declaration

    • <T>(): Promise<AssetSettings & T>
    • Get settings for the app

      Type Parameters

      • T = Record<string, unknown>

      Returns Promise<AssetSettings & T>

getAsset: ((assetId: number) => Promise<Asset>)

Type declaration

    • (assetId: number): Promise<Asset>
    • Get asset info

      Parameters

      • assetId: number

      Returns Promise<Asset>

getDataset: (<T>(__namedParameters: GetDatasetOpts) => Promise<T[]>)

Type declaration

    • <T>(__namedParameters: GetDatasetOpts): Promise<T[]>
    • Search records in provided dataset

      This method is good for getting a small amount of records from dataset

      Type Parameters

      • T

      Parameters

      Returns Promise<T[]>

getIteratedDataset: (<T>(__namedParameters: GetDatasetOpts) => AsyncIterable<T[]>)

Type declaration

    • <T>(__namedParameters: GetDatasetOpts): AsyncIterable<T[]>
    • Search records in provided dataset

      This method is designed for obtaining a huge amount of records from dataset (10000+)

      Type Parameters

      • T

      Parameters

      Returns AsyncIterable<T[]>

produceMessages: ((data: ProduceMessagesPayload) => Promise<void>)

Type declaration

    • (data: ProduceMessagesPayload): Promise<void>
    • Post data to the /api/v1/message_producer/ endpoint using context.api.produceMessages method. The method will work for both stream and scheduled types of apps.

      Example

      /* eslint-disable @typescript-eslint/require-await */
      import { CollectionRecord, Corva, ScheduledDataTimeEvent, StreamTimeEvent } from '@corva/node-sdk';

      const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));

      export const producer = new Corva().stream(async (event: StreamTimeEvent, { api }) => {
      const data = [
      {
      timestamp: 1,
      company_id: 42,
      asset_id: event.asset_id,
      data: { answer: 10 },
      },
      {
      timestamp: 2,
      company_id: 42,
      asset_id: event.asset_id,
      data: { answer: 11 },
      },
      ];

      await api.provider('my-provider').dataset('quiz-answers').createEntries(data);

      // do some other computations
      for (let i = 0; i < 3; i++) await sleep(100);

      // call message producer with time index
      await api.produceMessages(data);
      });

      export const streamConsumer = new Corva().stream(async (event: StreamTimeEvent) => {
      // data from the producer - same that was published
      expect(event.records.map((record) => record.data)).toEqual([{ answer: 10 }, { answer: 11 }]);
      });

      export const scheduledConsumer = new Corva().scheduled(async (event: ScheduledDataTimeEvent, context) => {
      const searchParams = {
      query: { timestamp: { $gte: event.start_time, $lte: event.end_time } },
      limit: 10,
      sort: { timestamp: 1 },
      };
      const records: CollectionRecord<{ answer: number }>[] = [];

      for await (const record of context.api
      .provider('my-provider')
      .dataset<{ answer: number }>('quiz-answers')
      .search(searchParams)) {
      records.push(...record);
      }

      // data from the producer - same that was published
      expect(records.map((record) => record.data)).toEqual([{ answer: 10 }, { answer: 11 }]);
      });

      Parameters

      Returns Promise<void>

provider: ((name: string) => Provider)

Type declaration

    • (name: string): Provider
    • User-friendly interface for interaction with Corva API

      Parameters

      • name: string

      Returns Provider

raw: Got

Make a request to any URL.

For options' details see got documentation

Example

import { Corva } from '@corva/node-sdk';

type Movie = {
id: number;
};

export const handler = new Corva().scheduled(async (event, { api, secrets }) => {
// Call any URL
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
await api.raw<Movie>('https://api.themoviedb.org/3/movie/76341', {
searchParams: { api_key: (secrets as Record<string, string>).api_key },
});

return { status: 'OK' };
});
request: (<T>(path: string, options?: Options) => Promise<CancelableRequest<Response<T>>>)

Type declaration

    • <T>(path: string, options?: Options): Promise<CancelableRequest<Response<T>>>
    • Make a request to any Corva URL.

      For options' details see got documentation

      Example

      import { Corva } from '@corva/node-sdk';

      export const handler = new Corva().scheduled(async (event, { api }) => {
      // Corva API calls
      await api.request('/v2/pads', {
      searchParams: { param: 'val' },
      });
      await api.request('/v2/pads', {
      method: 'POST',
      json: { key: 'val' },
      });
      await api.request('/v2/pads', {
      method: 'PUT',
      json: { key: 'val' },
      });
      await api.request('/v2/pads', {
      method: 'DELETE',
      });

      // Corva Data API calls
      await api.request('api/v1/data/provider/dataset/', {
      searchParams: { param: 'val' },
      });
      await api.request('api/v1/data/provider/dataset/', {
      method: 'POST',
      json: { key: 'val' },
      });
      await api.request('api/v1/data/provider/dataset/', {
      method: 'PUT',
      json: { key: 'val' },
      });
      await api.request('api/v1/data/provider/dataset/', {
      method: 'DELETE',
      });

      return { status: 'OK' };
      });

      Type Parameters

      • T = any

      Parameters

      • path: string
      • options: Options = {}

      Returns Promise<CancelableRequest<Response<T>>>

saveData: (<T>(__namedParameters: SaveDataOpts<T>) => Promise<SaveDataResult>)

Type declaration

    • <T>(__namedParameters: SaveDataOpts<T>): Promise<SaveDataResult>
    • Create/update one or multiple records.

      This method will update existing records or create new ones.

      Deprecated

      use createOrUpdate instead

      Type Parameters

      • T extends Record<string, unknown>

      Parameters

      Returns Promise<SaveDataResult>