Search code examples
node.jspostgresqlsails.jswaterline

Database schema with sails.js and sails-postgresql


Is there any way to set database schema with sails-postgresql waterline adapter?

By default postgres adapter allways choose default public schema in database but I want to connect it to another schema.

For example I have database dev, schema test in database dev and table users in schema test. Now I want to select all data from table users, in sql syntax I can simply write:

SELECT * FROM test.users

How to make it work in sails ?

When I write a model that uses postgres adapter, method Users.find() will look for the table users in default public schema. I want to change it to look in schema test without interactions with my postgres database.

Is it possible?


Solution

  • There is support for this, although it is as-yet undocumented. You can set the schema name for a model using the meta.schemaName property, eg:

    module.exports = {
    
      tableName: 'users',
      meta: {
         schemaName: 'test'
      },
    
      attributes: {
         ...
      }
    
    };
    

    Update

    It turns out this functionality was essentially broken for several versions, but it has been revamped and released in Sails-Postgresql v0.11.0. The syntax is the same as above. The main caveat is that it will not work with multiple Waterline models sharing the same tableName.