customEvents

A custom event is a recurring function that can generate events

The customEvents directory contains your scheduled events functions. Marjory reads all the .js files inside this directory and automatically creates the functions for you.

A custom event is an exported function that exists inside of the customEvents folder. You do not have to write only one custom event per file, but we recommend doing so as it will keep your code clean.

If you do not export your function, it will not be defined as a custom events

// This is an action, because it's exported
export const customEvent1 = (credentials) => "working";

// This is not an action, because it's not exported
const customEvent2 = (credentials) => "not working";

Properties

credentials

The property credentials contains all the credentials information defined when the module was installed by someone. The list of given credentials are defined in the marjory.yml file.

Here is an example of a marjory.yml credentials section and the object you can work with in an action function:

...
credentials:
  apiKey:
    type: string
    isCrypt: true
    description: The api key to work with the Pancake API
...

SDK

callEventRepository

callEventRepository is a function that takes the name of the event and the payload of your events. You can use it multiple times inside of the same function.

import Marjory from "marjory";

export const customEvent = (credentials) => {
    // Your custom logic
    Marjory.callEventRepository('event_name', payload);
}

Do not use const { callEventRepository } = Marjory from outside your function

Storage

You might need to keep storage between multiple action execution for the same operator. You can do so by using the DB object of the SDK.

Here is all the functions you can use on the DB object:

import Marjory from "marjory";

export const customEvent = (credentials) => {
    constDB= Marjory;
    
    // Search
    DB.collection('pancakes').find();
    DB.collection('pancakes').findOne();
    
    // Insertion
    DB.collection('pancakes').insertOne();
    DB.collection('pancakes').insertMany();
    
    // Update
    DB.collection('pancakes').updateOne();
    DB.collection('pancakes').updateMany();
    DB.collection('pancakes').replaceOne();
    DB.collection('pancakes').findOneAndUpdate();
    DB.collection('pancakes').findOneAndReplace();
    
    // Deletion
    DB.collection('pancakes').deleteOne();
    DB.collection('pancakes').deleteMany();
    DB.collection('pancakes').findOneAndDelete();
    
    // Stats
    DB.collection('pancakes').countDocuments();
    DB.collection('pancakes').estimatedDocumentCount();
}

Do not use const { DB } = Marjory from outside your function

Behind the DB object is a MongoDB instance. You can learn more on how to use these functions directly from mongo documentation.

Last updated