@egomobile/queue-mongo
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

npm last build PRs Welcome

@egomobile/queue-mongo

Queue storage for @egomobile/queue, using MongoDB as backend.

Install

Execute the following command from your project folder, where your package.json file is stored:

npm install --save @egomobile/queue-mongo

Usage

import { Queue } from "@egomobile/queue";
import {
  IMongoQueueStorageOptions,
  MongoQueueStorage,
} from "@egomobile/queue-mongo";
import { MongoClient } from "mongodb";

let queue!: Queue;

async function main() {
  const url = process.env.MONGO_URL?.trim() || "mongodb://localhost:27017";
  const mongoClient = new MongoClient(url);
  const db = mongoClient.db("my_database");

  const storageOptions: IMongoQueueStorageOptions = {
    getDb: () => {
      return db;
    },
  };

  queue = new Queue({
    storageClass: MongoQueueStorage,
    storageClassArgs: [storageOptions],
  });

  // register 2 tasks at once
  queue.register({
    // 1st
    myTask1: async (context) => {
      console.log("This is task 1 with following data:", context.data);
    },

    // 2nd
    myTask2: async (context) => {
      console.log("This is task 2 with following data:", context.data);
    },
  });

  // anything is prepared, lets start the queue
  await queue.start();

  // first enqueue 2nd task
  await queue.enqueue("myTask2", {
    data: {
      buzz: 5979,
    },
  });
  // then 1st one
  await queue.enqueue("myTask1", {
    data: {
      foo: "bar",
    },
  });
}

main().catch(console.error);

Documentation

The API documentation can be found here.

Package Sidebar

Install

npm i @egomobile/queue-mongo

Weekly Downloads

2

Version

0.2.0

License

LGPL-3.0

Unpacked Size

43.8 kB

Total Files

20

Last publish

Collaborators

  • ekmobile
  • egodux
  • mkloubert
  • ekegodigital