Using sails.js with an existing postgres database

2019-04-05 01:18发布

问题:

I was looking at using Sails for an app that we are developing.

I'm using the sails-postgresql adapter which uses the waterline orm.

I have an existing database that I want to connect to.

If I create a model using generate something

and then in my model I have

attributes:{
    title:{type:'String'}
}

If I browse to localhost/something the orm deletes all the columns in the something table except title.

Is there a way to stop it from doing this? This app should not delete columns on this database.

Thanks!

回答1:

I am the author of Sails-Postgresql. Sails has an ORM called Waterline that it uses for managing data. The default setting assumes that you would want to auto-migrate your database to match your model attributes. Because Postgresql is a SQL database the Sails-Postgresql adapter has a setting called syncable that defaults to true. This would be false in a NoSQL database like redis.

This is easy to turn off if you want to manage your database columns yourself. You can add migrate: safe to your model and it won't try and update your database schema when you start Sails.

module.exports = {
  adapter: 'postgresql',
  migrate: 'safe',
  attributes: {
    title: { type: 'string' }
  }
};

Sails doesn't have anything like migrations in Rails. It uses auto-migrations to attempt to remove this from your development process and then leaves updating your production schema to you.