Nodejs Node-queue这个npm貌似不错哦

Nodejs Node-queue这个npm貌似不错哦

Node-queue is a node.js module for multiple databases. It can be very useful if you work with (d)ddd, cqrs, eventsourcing, commands and events, etc.

https://github.com/adrai/node-queue

Currently these databases are supported:

  • inmemory
  • mongodb (node-mongodb-native)
  • couchdb (cradle)
  • tingodb (tingodb)
  • redis (redis)

看起来挺不错的样子


3 回复

Nodejs Node-queue 这个 npm 看起来不错哦

Node-queue 是一个用于 Node.js 的模块,它支持多种数据库。如果你正在处理领域驱动设计(DDD)、命令查询责任分离(CQRS)、事件溯源、命令和事件等复杂架构模式时,Node-queue 可能会非常有用。

你可以在这里找到它的 GitHub 仓库:

目前,Node-queue 支持以下数据库:

  • inmemory
  • mongodb(使用 node-mongodb-native
  • couchdb(使用 cradle
  • tingodb(使用 tingodb
  • redis(使用 redis

示例代码

让我们通过几个简单的例子来了解一下如何使用 Node-queue

首先,你需要安装 Node-queue 和相应的数据库客户端。例如,如果你想使用 MongoDB,你需要安装 mongodb 客户端。

npm install node-queue mongodb

接下来,我们创建一个简单的示例来演示如何将任务添加到队列中,并从队列中获取任务。

const Queue = require('node-queue');
const MongoClient = require('mongodb').MongoClient;

// 创建 MongoDB 链接
MongoClient.connect('mongodb://localhost:27017/test', { useNewUrlParser: true, useUnifiedTopology: true })
    .then(client => {
        const db = client.db();
        const collection = db.collection('tasks');

        // 初始化队列
        const queue = new Queue({
            storage: 'mongodb',
            collection: collection,
            options: {}
        });

        // 添加任务
        queue.add({ task: 'task1' }).then(result => {
            console.log(`Task added: ${result.insertedId}`);
        });

        // 获取任务
        queue.get().then(task => {
            console.log(`Task retrieved: ${JSON.stringify(task)}`);
        });
    })
    .catch(err => {
        console.error('Error connecting to MongoDB:', err);
    });

在这个示例中,我们首先连接到 MongoDB 数据库,然后初始化一个 Queue 实例,指定使用 MongoDB 作为存储引擎。接着,我们向队列中添加了一个任务,并从队列中获取了该任务。

通过这种方式,你可以在不同的数据库之间灵活地管理任务队列,而无需担心底层数据存储的具体实现细节。这使得 Node-queue 成为处理复杂业务逻辑的一个强大工具。


still need time to prove it is a good one. for database, we need to be conservative. Try not to use these new npms

Node-queue 是一个用于 Node.js 的模块,支持多种数据库。它非常适合处理领域驱动设计(DDD)、命令查询责任分离(CQRS)、事件溯源(Event Sourcing)等场景。

以下是一些常用的数据库支持:

  • In-Memory 存储
  • MongoDB(使用 node-mongodb-native
  • CouchDB(使用 cradle
  • TingoDB
  • Redis(使用 redis

示例代码:

假设我们想使用 Node-queue 模块将任务存储到 Redis 中,并执行一些操作。

首先,安装必要的依赖包:

npm install node-queue redis

然后,你可以这样使用 Node-queue 模块:

const Queue = require('node-queue');
const Redis = require('redis');

// 创建 Redis 客户端
const redisClient = Redis.createClient();

// 使用 Redis 创建队列实例
const queue = new Queue({
  storage: 'redis',
  client: redisClient,
});

// 添加任务
queue.enqueue('myQueue', { task: 'doSomething' }, (err) => {
  if (err) throw err;
  console.log('Task added to the queue.');
});

// 处理队列中的任务
queue.dequeue('myQueue', (err, task) => {
  if (err) throw err;
  console.log(`Processing task: ${task.task}`);
  
  // 完成任务
  queue.complete('myQueue', task._id, (err) => {
    if (err) throw err;
    console.log('Task completed.');
  });
});

这段代码演示了如何使用 Node-queue 模块来添加、处理和完成任务。每个步骤都包含错误处理,以确保应用程序的健壮性。

回到顶部