简体   繁体   English

数据库中node.js对象的自动持久性

[英]Automatic persistance of node.js objects in database

While experimenting with some data indexing using node.js objects (arrays, maps...) that takes some time to populate (from DB data) at every startup of the script, I wished my node.js objects could be automatically and transparently persisted in my database. 在尝试使用node.js对象(数组,映射......)进行一些数据索引时,在每次启动脚本时需要花费一些时间来填充(从DB数据),我希望我的node.js对象可以自动且透明地持久化在我的数据库中。

Having used MongoDB from node.js as well as other databases (including SQL-based) for some time now, I'm quite aware that the query/update mechanisms are very different between javascript objects (synchronous access, no queries) and database records (asynchronous access, queries, ...). 从node.js以及其他数据库(包括基于SQL)使用MongoDB已经有一段时间了,我非常清楚javascript对象(同步访问,没有查询)和数据库记录之间的查询/更新机制是非常不同的(异步访问,查询,......)。 However, I'm still hoping that a solution to make a javascript var persisted , at least for indices, can exist and be helpful. 但是,我仍然希望的解决方案,使一个JavaScript变种持续 ,至少在指数方面,可以存在并有所帮助。

Basically, I'm thinking of something like HTML5's LocalStorage, but for node.js servers. 基本上,我正在考虑像HTML5的LocalStorage,但对于node.js服务器。

Do you think this idea is interesting, feasible, or maybe it already exists? 你认为这个想法是有趣的,可行的,还是已经存在?

EDIT: Work in progress: https://github.com/adrienjoly/persistent-harmony 编辑:正在进行的工作: https//github.com/adrienjoly/persistent-harmony

A first thing to make clear, is that databases serve two purposes: persistence and convenient/efficient querying. 首先要明确的是,数据库有两个目的:持久性和方便/高效的查询。

If you only need the first because you absolutely know up front that no other program is going to access your persistent data, you could take a look at the literature on orthogonal persistence , which is exactly the concept that you're suggesting here. 如果您只需要第一个,因为您完全知道没有其他程序可以访问您的持久数据,您可以查看正交持久性的文献,这正是您在此建议的概念。 An example is the KEN protocol that was successfully implemented in the WaterKen Java server. 一个例子是在WaterKen Java服务器中成功实现的KEN协议。 There is some work to integrate the protocol into Google's V8 JavaScript runtime, which could lead to Nodeken, a Node.js with orthogonal persistence. 将协议集成到Google的V8 JavaScript运行时有一些工作 ,这可能会导致Nodeken,一个具有正交持久性的Node.js。

One of the difficulties of getting orthogonal persistence right is to map transactional semantics to a fe object-oriented programming system. 获得正交持久性的一个困难是将事务语义映射到fe面向对象的编程系统。 The approach taken by V8-ken is to treat a single event loop execution of your JavaScript runtime as a transaction. V8-ken采用的方法是将JavaScript运行时的单个事件循环执行视为事务。 In other words, the state of the virtual machine is persisted at the end of each "turn" in response of some event (incoming web request, server reply, user interface event, all asynchronous operations (IO), etc.). 换句话说,响应于某些事件(传入的Web请求,服务器回复,用户界面事件,所有异步操作(IO)等),虚拟机的状态在每个“转向”结束时被持久化。 This however requires a modified runtime such as V8-ken, but evolutions in ECMAScript, such as proxies, look promising to be able to implement such features more conveniently. 然而,这需要修改的运行时间,例如V8-ken,但ECMAScript中的演进(例如代理)看起来很有希望能够更方便地实现这些特征。

In many cases (think web applications) though, persistent data needs to be accessible by different programs, requiring a "real" database for data to be easily exported, migrated, queried, etc. Hybrid approaches could of course be possible... 但在许多情况下(想想Web应用程序),持久数据需要不同的程序才能访问,需要一个“真实”的数据库来轻松导出,迁移,查询等数据。当然,混合方法也是可能的......

%> npm search persistent storage
closet                JSON persistent storage with methods chainability and callbacks for asynchronous use. =ganglio 2013-01-29 18:41  0.0.7  json persistent storag
ewdDOM                Persistent lightweight DOM using Mumps Global Storage         =robtweed        2013-02-02 14:39  0.0.4
fs-persistent-object  Tiny Node library for persisting small runtime objects on filesystem =oleksiyk 2013-04-09 09:13  0.0.1  persistent tiny storage
headstorage           A persistent storage for Node.js                              =headhsu2568     2012-11-20 13:41  0.0.0  storage
level-store           A streaming storage engine based on LevelDB.                  =juliangruber    2013-06-21 19:55  3.3.2  leveldb levelup stream persistent
node-persist          Super-easy (and fast) persistent data structures in Node.js, modeled after HTML5 localStorage =benmonro 2013-04-09 17:33  0.0.1  node persist
persistent-hash-trie  Pure string:val storage, using structural sharing             =hughfdjackson   2013-05-24 19:24  0.4.1  persistent hash trie pure functional d
perstore              Perstore is a cross-platform JavaScript object store interface for mapping persistent objects to various different storage mediums using an in
shelf.js              A modular, powerful wrapper library for persistent objects in the browser and Node.js =shakty 2013-05-24 08:10  0.4.7  persistance localStorag
stay                  Persistent scuttlebutt instances for browser and node         =juliangruber    2012-12-11 21:54  0.1.0  persistent scuttlebutt persistence loc

Looks like the closest match would be node-persist 看起来最接近的匹配是node-persist

=) =)

EDIT: Here may be a better alternative solution... 编辑:这可能是一个更好的替代解决方案......

@adrienjoly You know prototyping is still fairly high level and may not be (in the long run) as efficient as you are thinking. @adrienjoly你知道原型设计仍然是相当高的水平,可能不会(从长远来看)和你想的那样高效。

You may be better off developing a module in C/C++ exposing a high level API for node.js to take advantage of. 你可能最好在C / C ++中开发一个模块,为node.js公开一个高级API来利用它。

I think I have a post about getting your feet wet with this type of node.js development (it stemmed from an original tutorial I followed here ) 我想我有一篇关于这种类型的node.js开发的帖子 (它来自我在这里遵循的原始教程)

I do believe that method is however outdated and a newer method is to use the node-gyp tool. 我确实认为该方法已经过时,而更新的方法是使用node-gyp工具。 Some additional resources and examples: node-gyp projects , uRSA (I have a small pull request with this one), bcrypt etc.. 一些额外的资源和示例: node- gyp 项目uRSA (我有一个小拉请求 ), bcrypt等。

My assumption in this is that you may bind the module extension to a db api such as oracle or postgres etc., and by writing a low level module linking to a low level API while exposing a high level API for developers to implement the persistent configuration options with API calls for slicing, indices, etc the performance would be optimal vs. trying to have node.js interpret your prototyping shim 我的假设是你可以将模块扩展绑定到db api,例如oracle或postgres等,并通过编写链接到低级API的低级模块,同时为开发人员公开高级API以实现持久性配置使用API​​调用切片,索引等的选项,性能将是最佳的,而不是尝试让node.js解释您的原型设计垫片

Maybe this is what you're looking for? 也许这就是你要找的东西?

https://github.com/yangli1990/flydb https://github.com/yangli1990/flydb

or 要么

npm install flydb

Now in javascript 现在在javascript中

var flydb = require('flydb');

flydb.test = "hello world";  //flydb.test will now persist

To run this 要运行它

node --harmony-proxies <your commands>

eg 例如

node --harmony-proxies app node --harmony-proxies app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM