The persistence of node. js with Sails~model and ORM

Source: Internet
Author: User

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:

    1. Safe-never auto-migrate My database (s). I will does it myself (by hand) [does not automatically merge data and requires manual control]
    2. 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]
    3. 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

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.