Waterline.js Joins/Populate with existing database

与世无争的帅哥 提交于 2019-12-24 16:44:10

问题


I have an existing postgres database which I am using to build a sails.js driven website, utilising waterline for ORM.

I'm fine with using my database in its existing form for everything other than population i.e. joining tables.

When working with a non-production database I'm comfortable with how waterline can produce join tables for me, but I'm really unsure how to bypass this to work with the current tables I have and their foreign key relationships. To give an idea of the types of tables I would typically have I've shown an example below:

| Intel       |        |
|-------------|--------|
| Column      | Type   |
| id          | int PK |
| alliance_id | int FK |
| planet_id   | int FK |
| dist        | int    |
| bg          | string |
| amps        | int    |


| Alliance |        |
|----------|--------|
| Column   | Type   |
| id       | int PK |
| name     | string |
| score    | int    |
| value    | int    |
| size     | int    |


| Planet    |        |
|-----------|--------|
| Column    | Type   |
| id        | int PK |
| rulerName | string |
| score     | int    |
| value     | int    |
| size      | int    |

So in the above tables I would typically be able to join Intel --> Alliance and Intel --> Planet and access the data across each of these.

What would I need in my waterline model of Intel, Alliance, Planet to access this easily?

I'd love to do a:

Intel.find({alliance.name= 'test'})

or

Intel.find().populate('planet')

and then somehow be able to access intel.planet.score or intel.alliance.name etc

Thanks for any help. I can add more information if required just let me know in the comments.


回答1:


first create models for all your databases table , as mention here you can populate models and return joins results



来源:https://stackoverflow.com/questions/37742970/waterline-js-joins-populate-with-existing-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!