Node嵌入式数据库:NeDB
jopen
10年前
用于Node.js应用的嵌入式持久数据库,采用Javascript开发,没有任何依赖(当然除了NPM模块)。你可以把它想象成用于Node.js项目的SQLite数据库。其 API 是MongoDB的一个子集。您可以使用它作为一个持久性或只存在内存中的数据存储。
NeDB并不旨在替换大型的数据库如MongoDB。它的目标是为你提供一个简洁,简单的方法来查询数据,并坚持久化到磁盘中,适合于那些没有很多并发连接的Web应用,比如一个持续集成和部署服务器与利用Node Webkit构建的桌面应用。
// Type 1: In-memory only datastore (no need to load the database) var Datastore = require('nedb') , db = new Datastore(); // Type 2: Persistent datastore with manual loading var Datastore = require('nedb') , db = new Datastore({ filename: 'path/to/datafile' }); db.loadDatabase(function (err) { // Callback is optional // Now commands will be executed }); // Type 3: Persistent datastore with automatic loading var Datastore = require('nedb') , db = new Datastore({ filename: 'path/to/datafile', autoload: true }); // You can issue commands right away // Type 4: Persistent datastore for a Node Webkit app called 'nwtest' // For example on Linux, the datafile will be ~/.config/nwtest/nedb-data/something.db var Datastore = require('nedb') , path = require('path') , db = new Datastore({ filename: path.join(require('nw.gui').App.dataPath, 'something.db') }); // Of course you can create multiple datastores if you need several // collections. In this case it's usually a good idea to use autoload for all collections. db = {}; db.users = new Datastore('path/to/users.db'); db.robots = new Datastore('path/to/robots.db'); // You need to load each database (here we do it asynchronously) db.users.loadDatabase(); db.robots.loadDatabase();