Limbo: Simple access to remote databases

Source: Internet
Author: User
Tags findone magic call

Simple and efficient project collaboration tool Teambition has been working to reshape people's work style with technology since its inception, hoping to bring the best collaborative experience to people's work styles, and in addition to teambition, we have a schedule management tool [Today], this time, We have a new product line chat (talk.ai) to go online, as its slogan"simple chat, easy collaboration" expressed, it is a slack-style, enterprise-based easy chat tool. Although simple, but is for the efficiency and collaboration of the Enterprise IM tool, it inherits the Teambition enterprise-level gene, through the Project collaboration tool Teambition, users can directly through the Teambition account login. It uses the topic-centered group to discuss the chat mode, promotes the communication with the slack way, the enterprise internal communication, the task, the personnel, the document and so on various elements and the process to open. Benefit from goal-based, increased focus, complete tasks, and filter out the most effective information in team chats. If there are topics that require the participation of other outsiders, simply send an email to invite the other person to join the discussion.


Welcome to try our Jane chat talk.ai, the first version on-line is inevitably very insufficient, please be enthusiastic to ask for the needs and advice, help us move forward.


Xu Jing Xin is responsible for the development of Jane Chat Talk.ai engineers, the following this article is his experience in the development of simple chat this application process.

Limbo: Simple access to remote databases

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/47/EC/wKioL1QEAq-QWSK-AAKjkW7jurg654.jpg "title=" screen shot 2014-09-01 a.m. 10.23.25.png "alt=" Wkiol1qeaq-qwsk-aakjkw7jurg654.jpg "/>

Jane, talk. Easy collaboration


For Nodejs ecology, the use of mongoose as the Model module is one of the best things, its main feature is a concise and elegant Schema definition, provides each key value type validation, data validation, index declaration, virtual key, and the extension of the instantiation method, The cost of development is greatly reduced. But when it comes to open data, it's not all that good.


In the process of building a simple chat application, we are actually experiencing this problem. Because of the need to use Teambition user and team data, and when the user data has been updated by Jane Chat, these updates can be pushed to the user in real-time in teambition. As a rule, we initially used a restful interface.


First stage, using the RESTful interface


RESTful interface is the most widely used, but there are still many shortcomings, such as the interface in the parameters and structure of the more restrictive, when considering the modification of the interface API, often worry about client compatibility, and once the client program has new requirements, you need to wait for the interface update. Another troublesome place is the need to do signature verification, for internal applications, we can completely control the access of a particular IP port through a firewall, where the signature is a bit redundant.


Second stage, separate unpacking Schema


Then we think of the Schema is unpacked into a separate warehouse, Nodejs has good module management, in different applications, we just need to introduce these modules, both synchronous update, and do DRY. The disadvantage with respect to the RESTful interface is that there are too many calls to the data, and the applications are not aware of each other. For example, in the chat with update user data, in the teambition is not known, and pushed to other clients.


Third stage, remote procedure call (RPC)


This phase is similar to the RESTful interface, where we expose some interface methods in the back-end process of teambition, so that our client programs can invoke these interfaces via a simple RPC method. For example, we have exported the user.update method, and the corresponding procedure can be invoked using rpc.call (' user.update ', params, callback) in the client code. Such invocation behavior is no different from using native code, and may be the simplest and most straightforward way to find it now.


Phase IV, the combination of RPC and mongoose


Things can be simpler because the purpose is primarily to manipulate the database, so we have developed a module limbo that exposes all the methods in the Mongoose model and divides it into namespaces to achieve the same experience in client and server-side programs.


For example, we use Limbo to connect MongoDB in a server-side program, only the following declaration is required: (The following code is coffeescript as an example)



Limbo = Require ' limbo '


# define Schema

Userschame = (Schema)

# The Schema here is mongoose. Schema

New Schema

Name:string

Email:string


# The Use method is used as a namespace to differentiate between different database connections, and the general parameter selects the database name.

db = Limbo.use (' Test '). Connect (' Mongodb://localhost:27017/test '). Load ' User ', Userschema



The way you use it is consistent with mongoose.



user = Db.user

# User is an object in the limbo used to encapsulate the model, you can directly call the Mongoose model using User.model

User.findone _id: ' xxxx '

User.create name: ' xxx ', email: ' YYY '



Here's the most exciting place in limbo, you can export all the methods in a collection to the RPC server, just through a simple declaration



Limbo.use (' Test '). Bind (7001). ENABLERPC ()



Here's how to call these methods in the client program



# in the client also need to initialize a limbo namespace, need to be consistent with the server, the link to the server domain name and port number

db = Limbo.use (' Test '). Connect (' tcp://localhost:7001 ')


# There are two ways to use RPC

# 1. Using the Call method

Db.call ' User.findone ', _id: ' xxxx ',

# 2. Using the method chain

Db.user.findOne _id: ' xxxx ',

# There is a delay in the second way, which must be used after the limbo and the service-side program handshake are successful.

# Otherwise throws an exception that does not exist for an object, but in a general application,

# The time required to initialize will be longer than the time required for this link, so the delay is negligible

```


It can be seen that the second way above and the service side in the local use of mongoose the same way, this kind of black magic call method should be the vast number of farmers loved.


Limbo Another commendable feature is the ability to listen to these remotely invoked events on a server-side program, thanks to the Nodejs event object, which limbo itself inherits from the Eventemitter object, so we trigger an event to the service-side program after each remote call. And on the server only need to simply listen to this event can



Limbo.on ' Test.user.findOne ', (user), ...



It is this RPC plus event feedback mechanism, so that Jane chat and teambition can achieve simple real-time data exchange. We will limbo hosted on GitHub Open source, is aware that there are many things that can be improved, so vulgar to say, welcome issue and Pr~


---


Finally, welcome to our new product chat, a lightweight, topic-based collaborative application.


This article is from the "Writing essays" blog, please be sure to keep this source http://teambition.blog.51cto.com/9334569/1547366

Limbo: Simple access to remote databases

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.