ORM Library-waterline in node

Source: Internet
Author: User

Waterline is the more active ORM module in GitHub that supports most major databases, including relational and non-relational. In this article we take Sails-mongo (that is, using waterline to manipulate MongoDB) as an example, combining Koa2 to write a simple restful interface. In fact, it does not count as strict restful, because it does not make specific routing matches, nor does it implement all of the crud features, but their interfaces are described on GitHub, and it is not difficult to implement all of these functions with the following code.

To demonstrate convenience, not to confuse, only created 2 files, respectively, app.js and Server.js,app.js defined the KOA generated object app and middleware, server.js defined the database model, database establishment, server creation and startup. Let's look at Server.js first:

varWaterline = require ("Waterline");varMongoadapter = require ("Sails-mongo");//MONGO AdaptervarApp = require ("./app.js"));varHTTP = require ("http");varUsercollection = Waterline.Collection.extend ({//user model in databaseIdentity: "User", Connection:"Mymongo", attributes:{name:"String", Age:{type:"Integer", Required:false}}, Autocreatedat:false,//waterline will automatically add the Createdat and Updatedat fields to collection, where these two fields are not setAutoupdatedat:false});varConfig ={adapters:{"MONGO": Mongoadapter}, connections:{mymongo:{adapter:"MONGO", URL:"Mongodb://localhost:27017/test"//two ways to do it            //host: "LocalHost"            //database: "Test"        }    }};varWaterline =Newwaterline (); waterline.loadcollection (usercollection); Waterline.initialize (config,function(Err,models) {//Initialize the database first    varServer =Http.createserver (App.callback ()); App.models= Models.collections;//use the app to save the models in the database in order to be used in App.jsApp.connections =models.connections; Server.listen (8080);//Start the serverServer.on ("Listening",function() {Console.log ("Listening ...")    });});

in Server.js, the models in the database is attached to the app, and the app is accessible in App.js, so models is uploaded to the app.js. Next look at App.js:

varKoa = require ("Koa");varApp =NewKoa (); App.use (Async (Ctx,next)=>{//in KOA, the CTX of all middleware is the sameCtx.request.models = App.models;//save models in the app to Ctx.request for later middlewareawait next ();}); App.use (Async (Ctx,next)={    varUser = Ctx.request.models.user;//get user in Ctx.request    varDoc = await user.create ({name: "Yingge", age:18}); Ctx.body=json.stringify (DOC);}); Module.exports= app;

In fact, this example can be directly from the app to get App.models.user, not superfluous will app.models to ctx.request.models, so the purpose is to take into account the more routes, we will not all the routes are written in App.js, this time, the Files that are defined on other routes can only be accessed to CTX, which is the context of KOA2. We use CTX to store the public data. After the first middleware is finished, the second middleware is accessed, the model named user is obtained in the second middleware, and the created entries are returned.

The above is modeled after the example of waterline official online Express KOA Demo.  for learning and reference.

Orm Library-waterline in node

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.