admin管理员组文章数量:1318129
Does anyone have ideas on maintaining multiple versions of your API when using sails.js? Imagine a simple example like:
// Request
GET /api/v1/catVids?min_view_count=10000
// Response
[{"video_title": "top cat fails"}, {"video_title": "funny-ass cats"}]
Users are actively consuming v1 of the API but something has now changed in the requirements that will break existing functionality. For example, an attribute name change. So now we need to utilize a different controller to fulfill requests for this new behavior. What I would like to do is have both APIs co-exist so backwards patibility is not broken.
// Request
GET /api/v2/catVids?minimum_view_count=10000
// Response
[{"title": "top cat fails"}, {"title": "funny-ass cats"}]
However, I'm unsure on the best way to implement this. One way I think it could work is to use the following directory setup within the sails app:
api/
|-- controllers/
|---- v1/
|------ CatController.js
|---- v2/
|------ CatController.js
|-- models/
|---- v1/
|------ Cat.js
|---- v2/
|------ Cat.js
I'm just wondering if anyone else has ran into a similar scenario or has any suggestions on the topic.
Does anyone have ideas on maintaining multiple versions of your API when using sails.js? Imagine a simple example like:
// Request
GET /api/v1/catVids?min_view_count=10000
// Response
[{"video_title": "top cat fails"}, {"video_title": "funny-ass cats"}]
Users are actively consuming v1 of the API but something has now changed in the requirements that will break existing functionality. For example, an attribute name change. So now we need to utilize a different controller to fulfill requests for this new behavior. What I would like to do is have both APIs co-exist so backwards patibility is not broken.
// Request
GET /api/v2/catVids?minimum_view_count=10000
// Response
[{"title": "top cat fails"}, {"title": "funny-ass cats"}]
However, I'm unsure on the best way to implement this. One way I think it could work is to use the following directory setup within the sails app:
api/
|-- controllers/
|---- v1/
|------ CatController.js
|---- v2/
|------ CatController.js
|-- models/
|---- v1/
|------ Cat.js
|---- v2/
|------ Cat.js
I'm just wondering if anyone else has ran into a similar scenario or has any suggestions on the topic.
Share Improve this question asked Oct 2, 2014 at 19:12 Jason SimsJason Sims 1,1582 gold badges11 silver badges22 bronze badges 6- So your saying just your controllers need versioning, not the entire app? – Meeker Commented Oct 3, 2014 at 15:08
- Yea just the API would need to be versioned for backwards patibility. – Jason Sims Commented Oct 6, 2014 at 5:40
- Are you running blueprint routes for you all your actions or do you have custom routes? Also, are your running any type of file version control right now? Like Git or SVN? Cause you can prefix all of your blueprint routes in sailsjs (sailsjs/#/documentation/anatomy/myApp/config/…) and then you can tag different versions in what ever version control system your running. Then you just have to figure out deployment. You would have to run a sails server for every version. – Meeker Commented Oct 6, 2014 at 14:19
- I'm using the routes built by sails on boot. I am using git as a VCS but I should be able to run multiple versions of the API simultaneously from the same app. /api/v1 routes would just go to different controllers than /api/v2 routes. – Jason Sims Commented Oct 6, 2014 at 19:57
- I don't think Model can be run from sub folders at this time, and I know that the current blueprints will no do this as well. Your ment "should be able to run multiple versions of the API ... from the same app", is a little assertive. Why should you be able to? You can easily write your own blueprints and simply create models that are suffixed. UserV1, UserV2, UserV3. But like I said you would have to create your own blueprints. – Meeker Commented Oct 6, 2014 at 22:30
1 Answer
Reset to default 10You can get away with putting your controllers in subdirectories as in your example, because nested controllers are fully supported by Sails. However, nested models are not fully supported because of the ambiguity they would cause (see this ment on the matter). If you put two model files both named Cat.js in separate subfolders, they'll collide and the second one will overwrite the first in memory when Sails lifts.
That's sort of an academic point though, since you'll need some way to distinguish between your two model versions anyway in code. That is, in your v1 controller you'll want to make sure you're referencing your v1 Cat
model, and likewise for v2. The simplest solution would be to go with a scheme like you have in your example, but add a suffix to your models (or at least everything after v1):
api/
|-- controllers/
|---- v1/
|------ CatController.js
|---- v2/
|------ CatController.js
|-- models/
|---- v1/
|------ Cat.js
|---- v2/
|------ Cat_v2.js
The subfolder for the models will be ignored by Sails, so to make sure your blueprints work the way you want, you can add a _config
property to your controllers to force them to use the correct model.
api/controllers/v1/CatController.js:
module.exports = {
_config: {
model: 'cat'
},
...
}
api/controllers/v2/CatController.js:
module.exports = {
_config: {
model: 'cat_v2'
},
...
}
Update (for Sails 1.0)
The usage of _config
in a controller is no longer valid in Sails 1.0. Instead, you can use the parseBlueprintOptions
config function to set which model a blueprint operates on, e.g.:
parseBlueprintOptions: function(req) {
// Get the default query options.
var queryOptions = req._sails.hooks.blueprints.parseBlueprintOptions(req);
// Add the _v2 suffix to the `using` property.
queryOptions.using = queryOptions.using + '_v2';
return queryOptions;
}
本文标签: javascriptHow to maintain multiple API versions in sailsjsStack Overflow
版权声明:本文标题:javascript - How to maintain multiple API versions in sails.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741984676a2408603.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论