ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

RxDB——Web的实时数据库

2021-04-13 19:52:13  阅读:192  来源: 互联网

标签:Web 浏览器 name 数据库 RxDB 查询 使用 adapter


特性

  • —— 多平台支持:浏览器、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功能:可轻松处理数据代码关系并自定义文档和集合的功能。

平台支持

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

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

快速开始

安装:

`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(); });

https://pic4.zhimg.com/v2-a5b5404fe0bdf17f8503ea799c795933_b.jpg

多窗口/标签:

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

https://pic4.zhimg.com/v2-e3fe14881ea77f73887a1caf282e0abb_b.jpg

可复制:

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

https://pic1.zhimg.com/v2-c43de0916abbcb78d77ad25d96c05480_b.jpg

架构:

用于描述你的数据的架构通过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 ♛

https://pic2.zhimg.com/v2-17b9ab86e2f3edd4673912492bc7841d_b.jpg

压缩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

标签:Web,浏览器,name,数据库,RxDB,查询,使用,adapter
来源: https://blog.51cto.com/u_15166492/2703959

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有