added azure related code
[it/otf.git] / otf-frontend / server / src / feathers / hooks / insertShardKey.js
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 module.exports = function (service) { \r
18     return async context => {\r
19         // If the method is find, get, or create, return\r
20         if(context.method == 'find' || context.method == 'get' || context.method == 'create'){\r
21             return context;\r
22         }\r
23 \r
24         // If the id or service does not exist, return\r
25         if(!context.id || !context.service){\r
26             return context;\r
27         }\r
28 \r
29         let serviceString;\r
30         if(service){\r
31             serviceString = context.app.get('base-path') + service;\r
32         }else{\r
33             serviceString = context.path;\r
34         }\r
35 \r
36         if(!context.app.services[serviceString].Model){\r
37             return context;\r
38         }\r
39 \r
40         // If the entity data hasnt been set, get and set it\r
41         if(!context.params.entityData){\r
42             context.params.entityData = await context.app.services[serviceString].get(context.id, { provider: undefined});\r
43         }\r
44 \r
45         // Find the shard key from the model\r
46         let shardKeys = {};\r
47         Object.keys(context.app.services[serviceString].Model.schema.options.shardKey).forEach(key => {\r
48             shardKeys[key] = context.params.entityData[key];\r
49         })\r
50 \r
51         // Add the shard keys to the query\r
52         Object.assign(context.params.query, shardKeys);\r
53 \r
54         return context;\r
55 \r
56     }\r
57 }