Before I saw Sails.js's waterline provided a declarative relational object with a DB mapper, the surprise of heaven, can be said to greatly improve the efficiency.
Using the Waterline object-relational model, users can define relational databases directly using the JavaScript language, meaning that we no longer need to declare the model as in the Java environment, and then the specific relational operations require the user to handle the code in the business logic, Instead, it provides a relational form of declaration to create model, supporting one way relation, one-one relation, One-many relation, Many-many relation. Its syntax is as follows:
One-relation:
#myApp/api/models/pet.jsmodule.exports = { attributes: { name:' STRING ', color:' STRING ' }}
#myApp/api/models/user.jsmodule.exports = { attributes: { name:' STRING ', age :' INTEGER ', pony:{ ' pet ' }}
One-many relation:
#myApp/api/models/pet.jsmodule.exports = { attributes: { name:' STRING ', color:' STRING ', owner:{ model:' user '}} } #myApp/api/models/user.jsmodule.exports = { attributes: { name:' STRING ', age :' INTEGER ', pets:{ ' Pet ', ' owner ' }}
There are also many-many relation:
#myApp/api/models/pet.jsmodule.exports ={attributes: {name:' STRING ', Color:' STRING ', //Add A reference to Userowners: {collection:' User ', via:' Pets ', Dominant:true} }} #myApp/api/models/user.jsmodule.exports ={attributes: {name:' STRING ', Age:' INTEGER ', //Add A reference to Petpets:{Collection:' Pet ', via:' Owners ' } }}
As you can see, all the database relationships are defined by the object model, and also support pipelining (waterline) operation:
User.find (). Populate (' pets '). EXEC (function (err,r) {Console.log (R[0].tojson ())});//The following is the returned json{pets: [{name: ' Pinkie Pie ', color: ' Pink ', id:2, createdat:tue Feb 17:58:04 GMT-0600 (CST), updatedat:tue Feb 17:58:04 GMT-0600 (CST), owner:1}, {name: ' Applejack ', color: ' Orange ', id:4, createdat: Tue Feb 18:02:58 GMT-0600 (CST), updatedat:tue Feb 18:02:58 GMT-0600 (CST), owner:1}], Name: ' Mike ', age:21, createdat:tue 17:49:04 GMT-0600 (CST), Updatedat:tue Feb 11 2014 17:49 : GMT-0600 (CST), id:1}
Seeing that the JavaScript ecosystem is thriving, I have to lament that my big python has no such passionate framework, and bemoaned. So I intend to create one, but because of the level and time limit, still did not start, but opened a repo on GitHub.
Today, a Python package named Pony is found to be functionally equivalent to waterline, with a more eco-friendly environment and a UI tool for developers to use.
Reference:
1. Pony ORM Official Website: http://ponyorm.com/
2. Pony demo:http://www.blog.pythonlibrary.org/2014/07/21/python-101-an-intro-to-pony-orm/
3. Pony Github:https://github.com/ponyorm/pony
4. Pony docs:http://doc.ponyorm.com/
5. Pony editor:https://editor.ponyorm.com/
Python-declared object-Relational db mapper--pony