added azure related code
[it/otf.git] / otf-frontend / server / src / feathers / hooks / insertShardKey.js
diff --git a/otf-frontend/server/src/feathers/hooks/insertShardKey.js b/otf-frontend/server/src/feathers/hooks/insertShardKey.js
new file mode 100644 (file)
index 0000000..6a6831c
--- /dev/null
@@ -0,0 +1,57 @@
+/*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
+#                                                                              #\r
+#   Licensed under the Apache License, Version 2.0 (the "License");            #\r
+#   you may not use this file except in compliance with the License.           #\r
+#   You may obtain a copy of the License at                                    #\r
+#                                                                              #\r
+#       http://www.apache.org/licenses/LICENSE-2.0                             #\r
+#                                                                              #\r
+#   Unless required by applicable law or agreed to in writing, software        #\r
+#   distributed under the License is distributed on an "AS IS" BASIS,          #\r
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
+#   See the License for the specific language governing permissions and        #\r
+#   limitations under the License.                                             #\r
+##############################################################################*/\r
+\r
+\r
+module.exports = function (service) { \r
+    return async context => {\r
+        // If the method is find, get, or create, return\r
+        if(context.method == 'find' || context.method == 'get' || context.method == 'create'){\r
+            return context;\r
+        }\r
+\r
+        // If the id or service does not exist, return\r
+        if(!context.id || !context.service){\r
+            return context;\r
+        }\r
+\r
+        let serviceString;\r
+        if(service){\r
+            serviceString = context.app.get('base-path') + service;\r
+        }else{\r
+            serviceString = context.path;\r
+        }\r
+\r
+        if(!context.app.services[serviceString].Model){\r
+            return context;\r
+        }\r
+\r
+        // If the entity data hasnt been set, get and set it\r
+        if(!context.params.entityData){\r
+            context.params.entityData = await context.app.services[serviceString].get(context.id, { provider: undefined});\r
+        }\r
+\r
+        // Find the shard key from the model\r
+        let shardKeys = {};\r
+        Object.keys(context.app.services[serviceString].Model.schema.options.shardKey).forEach(key => {\r
+            shardKeys[key] = context.params.entityData[key];\r
+        })\r
+\r
+        // Add the shard keys to the query\r
+        Object.assign(context.params.query, shardKeys);\r
+\r
+        return context;\r
+\r
+    }\r
+}
\ No newline at end of file