简体   繁体   English

将sails.js与现有的postgres数据库一起使用

[英]Using sails.js with an existing postgres database

I was looking at using Sails for an app that we are developing. 我正在考虑将Sails用于我们正在开发的应用程序。

I'm using the sails-postgresql adapter which uses the waterline orm. 我正在使用使用水线orm的sails-postgresql适配器。

I have an existing database that I want to connect to. 我有一个我想连接的现有数据库。

If I create a model using generate something 如果我使用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. 如果我浏览到localhost / something,orm将删除除title之外的something表中的所有列。

Is there a way to stop it from doing this? 有没有办法阻止它这样做? This app should not delete columns on this database. 此应用程序不应删除此数据库上的列。

Thanks! 谢谢!

I am the author of Sails-Postgresql. 我是Sails-Postgresql的作者。 Sails has an ORM called Waterline that it uses for managing data. Sails有一个名为Waterline的ORM,用于管理数据。 The default setting assumes that you would want to auto-migrate your database to match your model attributes. 默认设置假定您希望auto-migrate数据库以匹配模型属性。 Because Postgresql is a SQL database the Sails-Postgresql adapter has a setting called syncable that defaults to true. 因为Postgresql是一个SQL数据库,所以Sails-Postgresql适配器有一个名为syncable的设置,默认为true。 This would be false in a NoSQL database like redis. 这在像redis这样的NoSQL数据库中是错误的。

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. 您可以将migrate: safe添加到模型中,并且在启动Sails时不会尝试更新数据库模式。

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

Sails doesn't have anything like migrations in Rails. Sails没有像Rails中的迁移那样的东西。 It uses auto-migrations to attempt to remove this from your development process and then leaves updating your production schema to you. 它使用自动迁移尝试从开发过程中删除它,然后将生产模式更新给您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM