特性

  • —— 多平台支持:浏览器、nodejs、electron、cordova、react-navite以及其他所有 javascript-runtime的平台。
  • —— 响应式:基于RxJS的响应式数据处理。
  • —— 离线:当用户没有联网的时候你的应用依然可以使用。
  • —— 可复制:可以在客户端和服务端复制数据,与PouchDB、CouchDB、IBM Cloudant兼容
  • —— 架构:Schemas are defined viajsonschemaand are used to describe your data.Schema-basedwith the easy-to-learn standard of json-schema
  • —— Mango查询:与mongoDB和mongoose一样的查询方式。
  • —— 加密单个数据字段以保护用户数据。
  • —— 数据库状态(json)的导入/导出,对于使用TDD进行编码非常棒。
  • —— 多窗口支持:可以在不同的浏览器窗口或nodejs进程之间同步数据。
  • —— ORM功能:可轻松处理数据代码关系并自定义文档和集合的功能。

平台支持

你可以在以下平台中使用完全相同的代码

  • Chrome、Firefox、Safari、Edge、IE
  • NodeJS
  • Electron
  • React-Native
  • Cordova/Phonegap
  • Nativescript

在下列框架中可以直接使用RxDB

  • React
  • Angular/ng2
  • Ionic2
  • Vuejs

快速开始

安装:

`npm install rxdb --save

peerDependencies

npm install rxjs --save`

引入:

**var** RxDB **=** require('rxdb'); RxDB.create({ name**:** 'heroesdb', adapter**:** 'websql', password**:** 'myLongAndStupidPassword', *// 可选的* multiInstance**:** **true** *// 默认为:true* }) *// 创建数据库* .then(**function**(db) {**return** db.collection({name**:** 'heroes', schema**:** mySchema});}) *// 创建一个集合* .then(**function**(collection) {collection.insert({name**:** 'Bob'});})

功能展示

Mango查询:

想要在你的集合中查询数据,请使用mquery api创建链式的Mango查询,你可以在mongo和mongoose中了解这些。

myCollection .find() .where('name').ne('Alice') .where('age').gt(18).lt(67) .limit(10) .sort('-age') .exec().then( docs => { console.dir(docs); });

响应式:

RxDB使用rxjs以使你的数据是响应式的。这样可以始终轻松的在dom中实时显示数据库中的数据状态,而无需手动重新提交查询。

db.heroes .find() .sort('name') .$ *// <- returns observable of query* .subscribe( docs => { myDomElement.innerHTML **=** docs .map(doc => '<li>' **+** doc.name **+** '</li>') .join(); });

多窗口/标签:

当两个RxDB实例使用相同的存储引擎时,它们的数据状态和动作流将被广播。 这意味着使用两个浏览器窗口时,窗口#1的更改将自动影响窗口#2的数据,并且这完全是离线的。

可复制:

由于RxDB依赖于glorious的PouchDB,因此很容易在设备和服务器之间复制数据。 是的,changeEvents也是同步的。

架构:

用于描述你的数据的架构通过jsonschemaand定义。

**const** mySchema **=** { title**:** "hero schema", version**:** 0, *// <- incremental version-number* description**:** "describes a simple hero", type**:** "object", properties**:** { name**:** { type**:** "string", primary**:** **true** *// <- this means: unique, required, string and will be used as '_id'* }, secret**:** { type**:** "string", encrypted**:** **true** *// <- this means that the value of this field is stored encrypted* }, skills**:** { type**:** "array", maxItems**:** 5, uniqueItems**:** **true**, item**:** { type**:** "object", properties**:** { name**:** { type**:** "string" }, damage**:** { type**:** "number" } } } } }, required**:** ["color"] };

加密:

通过将schema-field设置为encrypted: true后,将以加密模式存储,如果没有密码则无法读取。当然,你也可以加密嵌套对象。 例如:

"secret"**:** { "type"**:** "string", "encrypted"**:** **true**}

适配器:

底层的pouchdb可以使用不同的适配器作为存储引擎。 因此,只需切换适配器即可在不同环境中使用RxDB。 例如,你可以在浏览器中使用websql,在移动浏览器中使用localstorage,在nodejs中使用leveldown-adapter。

*// this requires the indexeddb-adapter* RxDB.plugin(require('pouchdb-adapter-idb')); *// this creates a database with the indexeddb-adapter* **const** database **=** await RxDB.create({ name**:** 'mydatabase', adapter**:** 'idb' *// the name of your adapter* });

这里有一个你可以使用的多个的适配器的系统的描述。

导入/导出:

RxDB允许你将整个数据库或单个集合导入和导出到json对象中。这有助于跟踪应用程序中的错误或移动到测试中的指定状态。

`// export a single collection const jsonCol = await myCollection.dump();

// export the whole database const jsonDB = await myDatabase.dump();

// import the dump to the collection await emptyCollection.importDump(json);

// import the dump to the database await emptyDatabase.importDump(json);`

Leader-Election:

想象一下,你的网站需要每分钟从服务器获取一段数据。要完成此任务,需要创建websocket或pull-interval。如果你的用户现在在5个选项卡中并行打开该站点,它将运行interval或创建socket 5次。这是非常浪费资源,所以可以通过RxDB的LeaderElection来解决。

myRxDatabase.waitForLeadership() .then(() => { *// this will only run when the instance becomes leader.* mySocket **=** createWebSocket(); });

In this example the leader is marked with the crown ♛

压缩Key:

根据你使用RxDB的适配器和环境,某种方式下客户端存储会受到限制。为了节省磁盘空间,RxDB具有基于内部模式的key压缩,以最大限度地减少保存文档的大小。

例如:

`// when you save an object with big keys await myCollection.insert({ firstName**:** 'foo' lastName**:** 'bar' stupidLongKey**:** 5 });

// RxDB will internally transform it to { '|a': 'foo' '|b': 'bar' '|c': 5 }

// so instead of 46 chars, the compressed-version has only 28 // the compression works internally, so you can of course still access values via the original key.names console.log(myDoc.firstName); // 'foo'`

查询更改检测(QueryChangeDetection):

与Meteors oplog-observe-driver类似,RxDB有一个QueryChangeDetection来优化观察或重用的查询。这可确保在更新/插入/删除文档时,查询不必重新运行整个数据库,但新结果将根据事件计算。这样可以实现零成本的巨大性能提升。 QueryChangeDetection在内部工作,目前处于测试阶段(默认情况下禁用)。

浏览器支持

支持所有主要的浏览器和IE11。测试会在Firefox和Chrome中自动运行,但在VirtualBox for IE11和Edge中需要手动运行。

我们很快将切换到Browserstack并在所有主流浏览器中都运自动行测试。

由于RxDB严重依赖PouchDB,请参阅其浏览器支持以获取更多信息。另外请记住,不同的浏览器具有不同的存储限制,尤其是在移动设备上。

开始使用

立即开始阅读文档或探索示例项目。

以上内容来源于:https://github.com/pubkey/rxdb

©著作权归作者所有:来自51CTO博客作者mb607558ea4fef1的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 【数据库修复】.ReadInstrutions后缀勒索病毒的mdf数据库文件修
  2. 【数据库修复】.ROGER后缀勒索病毒的数据库.bck文件修复
  3. Python 爬虫JD数据
  4. Python连接SQLite数据库
  5. Echarts(1):Python爬取微博热搜并用Echarts词云展示
  6. Python3 常见数据类型的转换
  7. C# 8中的Async Streams
  8. 网络编程基础第四讲阻塞模型
  9. 基于OAC的员工离职数据分析

随机推荐

  1. Android中使用HorizontalScrollView横向
  2. Android使用 startActivityForResult 、
  3. NDK笔记---Android.mk文件
  4. Android绑定银行卡提现怎么做?
  5. 简单的DatePicker样式设置
  6. Android中findViewById()h获取EditText
  7. StringBuffer和String、 StringBuilder的
  8. android ListView 九大重要属性详细分析
  9. 如何在android 5.0(L)中运行应用程序活动
  10. 为什么我的Android应用程序偶尔可以非常