Basic introduction to Waterline
what is waterline
Basic Concepts of ORM
Object Relational Mapping
Map a document in the document database, a row in a relational database table, to an object in JavaScript
The operation object can complete the operation of the database
Waterline Features and Benefits
Supports most mainstream databases
out of SQL
Use the same code to operate different databases
easy-to-understand symbols //mathematical symbols
Rich methods //Add, delete, modify and check more than 20
Diverse data types
Comparison of Waterline vs Mongoose
Support for more databases
Provides richer CURD methods than Mongoose
Richer data verification methods
Waterline main concepts
adapter
Function: Convert the unified operation code into a database operation supported by a certain database
Code to create:
var mysqlAdapter = require('sails-mysql');
var mongoAdapter = require('sails-mongo');
var adapters = {
mongo : mongoAdapter,
mysql : mysqlAdapter,
default : mongo
}
connect
Establish an actual connection to the database through an adapter and the corresponding connection information
Code display:
//连接配置
var connections = {
mongo : {
adapter : 'mongo',
url : 'mongodb://localhost/watereline'
},
mysql : {
adapter : 'mysql',
url : 'mysql://root:@localhost/watereline'
}
};
Data collection
Define specific data types
Similar to Model in mongoose
Specifically corresponding to the table in the relational database, and the collection in the document database
validator
Perform data checks
Anchor is used https://github.com/sailsjs/anchor
Predefined data validators, support general check, time check, latitude and longitude coordinate check, Email address check, etc.
Support custom data validator
lifecycle callback
创建时:beforeValidate/afterValidate/beforeCreate/afterCreate
更新时:beforeValidate/afterValidate/beforeUpdate/afterUpdate
When deleting: beforeDestroy/afterDestroy