Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 698
rafwlodar Avatar asked Oct 01 '22 12:10

rafwlodar


1 Answers

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.

like image 90
sgress454 Avatar answered Oct 03 '22 00:10

sgress454