Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial reaction sdk (nodejs) #106

Merged
merged 6 commits into from
Nov 8, 2024

Conversation

danielgerlag
Copy link
Contributor

Description

  • Added TypeSpec definitions for contract between query and reaction.
  • TypeSpec generates the models for each specific language SDK
  • Added externalImage flag to services for sources/reaction to indicate that an image should/not be pulled from the Drasi registry. This enables users to publish their reaction/source images to their own registry.
  • Created initial NodeJS SDK for Reactions, along with samples.

Reaction SDK for Drasi

This library provides the building blocks and infrastructure to implement a Drasi Reaction in Node.js

Getting started

Install the package

npm install --save @drasi/reaction-sdk

Basic example

The following example simply breaks down and logs the various parts of the incoming change event from a Continuous Query.

import { DrasiReaction, ChangeEvent } from '@drasi/reaction-sdk';

let myReaction = new DrasiReaction(async (event: ChangeEvent) => {

    console.log(`Received change sequence: ${event.sequence} for query ${event.queryId}`);

    for (let added of event.addedResults) {
        console.log(`Added result: ${JSON.stringify(added)}`);
    }

    for (let deleted of event.deletedResults) {
        console.log(`Removed result: ${JSON.stringify(deleted)}`);
    }

    for (let updated of event.updatedResults) {
        console.log(`Updated result - before: ${JSON.stringify(updated.before)}, after: ${JSON.stringify(updated.after)}`);
    }
});

myReaction.start();

A more advanced example

The following example illustrates

  • Retrieving a configuration value from the Reaction manifest
  • Defining a custom per query configuration object
  • Parsing the per query configuration object from Yaml
  • Process change events form the query
  • Process control events (start, stop, etc.) from the query
import { DrasiReaction, ChangeEvent,parseYaml, ControlEvent, getConfigValue } from '@drasi/reaction-sdk';

// Retrieve the connection string from the Reaction configuration
const myConnectionString = getConfigValue("MyConnectionString");

// Define a custom per query configuration object
class MyQueryConfig {
    greeting: string = "Default greeting";
}

// Define the function that will be called when a change event is received
async function onChangeEvent(event: ChangeEvent, queryConfig?: MyQueryConfig): Promise<void> {
    // We can access the per query config here
    console.log(queryConfig.greeting);  

    // do something with the result set diff
}

// Define the function that will be called when a control event is received
async function onControlEvent(event: ControlEvent, queryConfig?: MyQueryConfig): Promise<void> {    
    console.log(`Received control signal: ${JSON.stringify(event.controlSignal)} for query ${event.queryId}`);    
}

console.log(`Starting Drasi reaction with connection string: ${myConnectionString}`);

// Configure the Reaction with the onChangeEvent and onControlEvent functions
let myReaction = new DrasiReaction<MyQueryConfig>(onChangeEvent, {
    parseQueryConfig: parseYaml,  // Parse the per query configuration from Yaml
    onControlEvent: onControlEvent
});

// Start the Reaction
myReaction.start();

@ruokun-niu
Copy link
Contributor

Seems like we are able to retrieve the queyrId from the event

console.log(`Received change sequence: ${event.sequence} for query ${event.queryId}`);

Does this mean that we do not need to specify the queryIds in the reaction yaml file anymore?

@@ -28,6 +28,8 @@ pub struct ProviderSpecDto {
#[derive(Serialize, Deserialize, Debug)]
pub struct ProviderServiceDto {
pub image: String,
#[serde(rename = "externalImage")]
pub external_image: Option<bool>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does external_image do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Added externalImage flag to services for sources/reaction to indicate that an image should/not be pulled from the Drasi registry. This enables users to publish their reaction/source images to their own registry.

@danielgerlag
Copy link
Contributor Author

Seems like we are able to retrieve the queyrId from the event

console.log(`Received change sequence: ${event.sequence} for query ${event.queryId}`);

Does this mean that we do not need to specify the queryIds in the reaction yaml file anymore?

We still do, because you might not want a reaction to listen to all queries. You will only get events for the queries configured in the yaml, but this tells you which one of those it is.

@ruokun-niu
Copy link
Contributor

ruokun-niu commented Nov 1, 2024

Seems like we are able to retrieve the queyrId from the event

console.log(`Received change sequence: ${event.sequence} for query ${event.queryId}`);

Does this mean that we do not need to specify the queryIds in the reaction yaml file anymore?

We still do, because you might not want a reaction to listen to all queries. You will only get events for the queries configured in the yaml, but this tells you which one of those it is.

Ah I see, is this how the Reaction will know which queries it should listen for from the yaml(copied from the advanced example)?

let myReaction = new DrasiReaction(onChangeEvent, {
    parseQueryConfig: parseYaml, // Parse the per query configuration from Yaml
    onControlEvent: onControlEvent
});

@danielgerlag
Copy link
Contributor Author

Seems like we are able to retrieve the queyrId from the event

console.log(`Received change sequence: ${event.sequence} for query ${event.queryId}`);

Does this mean that we do not need to specify the queryIds in the reaction yaml file anymore?

We still do, because you might not want a reaction to listen to all queries. You will only get events for the queries configured in the yaml, but this tells you which one of those it is.

Ah I see, is this how the Reaction will know which queries it should listen for from the yaml(copied from the advanced example)?

let myReaction = new DrasiReaction(onChangeEvent, {
    parseQueryConfig: parseYaml, // Parse the per query configuration from Yaml
    onControlEvent: onControlEvent
});

No, that is how it extracts per query config. The list of queries to listen to is dealt with internally by the library, the user does not need to think about it.

@ruokun-niu
Copy link
Contributor

Seems like we are able to retrieve the queyrId from the event

console.log(Received change sequence: ${event.sequence} for query ${event.queryId});

Does this mean that we do not need to specify the queryIds in the reaction yaml file anymore?

We still do, because you might not want a reaction to listen to all queries. You will only get events for the queries configured in the yaml, but this tells you which one of those it is.

Ah I see, is this how the Reaction will know which queries it should listen for from the yaml(copied from the advanced example)?

let myReaction = new DrasiReaction(onChangeEvent, {

parseQueryConfig: parseYaml, // Parse the per query configuration from Yaml
onControlEvent: onControlEvent

});

No, that is how it extracts per query config. The list of queries to listen to is dealt with internally by the library, the user does not need to think about it.

Oh that's cool. I'm so not used to the fact that we don't need to explicitly pubsub the queries

@danielgerlag danielgerlag merged commit c40e6f8 into drasi-project:main Nov 8, 2024
29 of 30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants