Back to Catalog
The last one talked about defining the model and related parameters in sails, this talk about how to persist your model to file, relational database and NoSQL database, in the persistence of this point, sails is unified management, which is an advantage, but also a disadvantage; The advantage is the integration of the entrance, With which media persistence, a setting is OK; The disadvantage is also very obvious, is in a sails project, there can only be a persistent method , you want to let MySQL and mongodb coexistence, that is not, hehe!
The persistence of data is mainly divided into the following steps, the following one by one explains
1 Install the missing driver, by default, both MongoDB and SQL Server need to be installed, NPM Install command
Enter the following command in the Command prompt window to install
NPM Install sails-mongodbnpm install sails-sqlserver
2 Adding database connection information/config/connection.js, for example, MongoDB and SQL Server
Somemongodbserver: { adapter: ' Sails-mongo ', host: ' 192.168.2.21 ', port:27017, // // Password: ' Password ', database: ' Testnodejs ' }, Somesqlserver: {adapter: ' Sails-sqlserver ', Host: ' 192.168.2.71 ', User: ' sa ', Password: ' zzl123 ', Database: ' Testnodejs ' } /c13>
3 Set the type of database used by the model for persistence/config/model.js
Module.exports.models = { /*************************************************************************** * * * Your app ' s default connection. i.e. the name of one of the Your app's * * connections (see ' config/connections.js ') * * * ******************************************* ********************************/ //connection: ' Localdiskdb ',Connection: ' Somesqlserver ',//connection: ' Somemongodbserver ', /*************************************************************************** * * How and whether Sails would attempt to automatically rebuild the * * tables /collections/etc. In your schema. * * * * See http://sailsjs.org/#!/documentation /concepts/orm/model-settings.html * * * ****** *********************************************************************/Migrate:' Alter '//automatically merge without erasing the original data};
Here are some instructions for migrate:
- Safe-never auto-migrate My database (s). I will does it myself (by hand) [does not automatically merge data and requires manual control]
- Alter-auto-migrate, but attempt to keep my existing data (experimental) [automatically merged with old data, when a new field is added, the datasheet will not be deleted, it is recommended]
- Drop-wipe/drop all my data and rebuild models every time I lift sails[Delete datasheet, create new table, insert new data]
After the above settings, run your app.js, if there is no error, it means that your data can be persisted, hehe!
Small knowledge:
MongoDB it to automatically create databases and data tables
SQL Server It needs to manually select the build database, the data table is automatically created
Back to Catalog
The persistence of node. js with Sails~model and ORM