@abtnode/nedb-multi 中文文档教程

发布于 4年前 浏览 20 项目主页 更新于 3年前

nedb-multi

Nedb (https://github.com/louischatriot/nedb) 不支持多进程并发访问。 试图解决这个问题的一个模块是 nedb-party (https://github.com/allain/nedb-party)。 但是,它不支持返回游标的方法。 它还依赖于每个进程在同一端口上启动一个 http 服务器,并且设法开始侦听的那个成为主服务器,其他进程连接到它。 但是,如果您使用的是 cluster 模块(或 PM2),那么这将不起作用,因为在这种情况下,端口在子进程之间共享,并且它们都能够成为主人。 我提交了一个补丁请求,该请求被接受了,它使用了基于目录的锁定。 这足以让它工作,但管理锁可能很棘手。

我决定尝试一种类似但无锁的方法,使用 axon 框架 (https://github.com/tj/axon)。 其他连接到的仍然只有一个主进程,但没有锁。 此外,还支持基于回调和基于游标的方法。 Datastore#persistence 对象的 setAutocompactionIntervalstopAutocompactioncompactDatafile 方法也受支持,除了 compaction.done 调用 compactDatafile 后触发的事件。

Installation

npm install --save @abtnode/nedb-multi

Usage

您需要单独启动实际访问数据库的进程。 它位于 /node_modules/nedb-multi/server.js。 我建议您使用进程管理器(例如 PM2)来执行此操作。

您可以通过将端口号作为子进程的第一个参数或通过设置环境变量 NEDB_MULTI_PORT 来传递服务器将侦听的端口号。

在您的其他进程中,通过传递您在上一步中设置的端口来创建数据存储。 支持所有可以序列化为 JSON 的选项字段,autoload 除外。

const Datastore = require('@abtnode/nedb-multi')(<port>);

const db = new Datastore({ filename: 'test.db' });

db.loadDatabase(function (err) {
  // ...
});

注意:在创建数据存储之前或之后启动服务器都没有关系。

Example

/example 文件夹中,您可以找到一个使用 @abtnode/nedb 的项目-multiPM2。 它将创建一个 example.db 文件,其中包含来自两个进程的插入。

运行它:npm install,然后是 npm start

Test

要运行测试,请执行:npm test

nedb-multi

Nedb (https://github.com/louischatriot/nedb) does not support concurrent access from multiple processes. One module which tries to solve this problem is nedb-party (https://github.com/allain/nedb-party). However, it does not support methods that return cursors. It also relies on each process starting a http server on the same port and whichever manages to start listening becomes the master and the others connect to it. This however does not work if you're using the cluster module (or PM2) as in that case the port is shared between the child processes and they are all able to become masters. I submitted a patch request, which was accepted, that instead used directory-based locking. This was enough to make it work but managing locks can be tricky.

I decided to try a similar, but lock-free approach, using the axon framework (https://github.com/tj/axon). There's still only one master process that the others connect to, but there are no locks. Also, both callback- and cursor-based methods are supported. The setAutocompactionInterval, stopAutocompaction and compactDatafile methods of the Datastore#persistence object are also supported, except for the compaction.done event fired after calling compactDatafile.

Installation

npm install --save @abtnode/nedb-multi

Usage

You need to start the process which actually accesses the database separately. It's located in <your-project-dir>/node_modules/nedb-multi/server.js. I suggest you use a process manager such as PM2 to do this.

You can pass the port number on which the server will listen by giving it as the first argument of the child process or by setting the env variable NEDB_MULTI_PORT.

In your other processes create the datastore by passing the port you set in the previous step. All options fields which can be serialized to JSON, are supported, except for autoload.

const Datastore = require('@abtnode/nedb-multi')(<port>);

const db = new Datastore({ filename: 'test.db' });

db.loadDatabase(function (err) {
  // ...
});

Note: It does not matter if you start the server before or after creating the datastore.

Example

In /example folder you can find a project which uses @abtnode/nedb-multi with PM2. It will create an example.db file that contains the inserts from two processes.

Run it with: npm install, then npm start

Test

To run the tests execute: npm test.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文