|
1 | 1 | const _ = require('lodash'); |
| 2 | +const applyToInstanceHelper = require('./applyToInstanceHelper'); |
2 | 3 |
|
3 | 4 | const DEFAULT_INDENT = ' '; |
4 | 5 | let graphName = 'g'; |
@@ -45,6 +46,83 @@ module.exports = { |
45 | 46 | }, 150); |
46 | 47 | return; |
47 | 48 | } |
| 49 | + }, |
| 50 | + |
| 51 | + async applyToInstance(data, logger, cb, app) { |
| 52 | + try { |
| 53 | + logger.clear(); |
| 54 | + logger.log('info', data, data.hiddenKeys); |
| 55 | + |
| 56 | + if (!data.script) { |
| 57 | + return cb({ message: 'Empty script' }); |
| 58 | + } |
| 59 | + |
| 60 | + if (!data.containerData) { |
| 61 | + return cb({ message: 'Graph wasn\'t specified' }); |
| 62 | + } |
| 63 | + const containerProps = _.get(data.containerData, '[0]', {}); |
| 64 | + if (!containerProps.dbId) { |
| 65 | + return cb({ message: 'Database id wasn\'t specified' }); |
| 66 | + } |
| 67 | + const graphName = containerProps.code || containerProps.name; |
| 68 | + if (!graphName) { |
| 69 | + return cb({ message: 'Graph name wasn\'t specified' }); |
| 70 | + } |
| 71 | + |
| 72 | + const cosmosClient = applyToInstanceHelper.setUpDocumentClient(data); |
| 73 | + await cosmosClient.databases.createIfNotExists({ |
| 74 | + id: containerProps.dbId |
| 75 | + }); |
| 76 | + const containerResponse = await cosmosClient |
| 77 | + .database(containerProps.dbId) |
| 78 | + .containers.createIfNotExists({ |
| 79 | + id: graphName, |
| 80 | + partitionKey: containerProps.partitionKey, |
| 81 | + ...(containerProps.autopilot |
| 82 | + ? { maxThroughput: containerProps.throughput || 400 } |
| 83 | + : { throughput: containerProps.throughput || 400 }), |
| 84 | + defaultTtl: applyToInstanceHelper.getTTL(containerProps), |
| 85 | + }); |
| 86 | + if (containerResponse.statusCode === 201) { |
| 87 | + const containerInstance = cosmosClient.database(containerProps.dbId).container(graphName); |
| 88 | + |
| 89 | + const storedProcs = _.get(data.containerData, '[2].storedProcs', []); |
| 90 | + if (storedProcs.length) { |
| 91 | + await applyToInstanceHelper.createStoredProcs(storedProcs, containerInstance); |
| 92 | + } |
| 93 | + |
| 94 | + const udfs = _.get(data.containerData, '[3].udfs', []); |
| 95 | + if (udfs.length) { |
| 96 | + await applyToInstanceHelper.createUDFs(udfs, containerInstance); |
| 97 | + } |
| 98 | + const triggers = _.get(data.containerData, '[4].triggers', []); |
| 99 | + if (triggers.length) { |
| 100 | + await applyToInstanceHelper.createTriggers(triggers, containerInstance); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + const { labels, edges } = applyToInstanceHelper.parseScriptStatements(data.script); |
| 105 | + const gremlinClient = await applyToInstanceHelper.getGremlinClient(data, containerProps.dbId, graphName); |
| 106 | + await applyToInstanceHelper.runGremlinQueries(gremlinClient, labels); |
| 107 | + await applyToInstanceHelper.runGremlinQueries(gremlinClient, edges); |
| 108 | + |
| 109 | + cb(); |
| 110 | + } catch(err) { |
| 111 | + cb(mapError(err)); |
| 112 | + } |
| 113 | + }, |
| 114 | + |
| 115 | + async testConnection(connectionInfo, logger, cb, app) { |
| 116 | + logger.clear(); |
| 117 | + logger.log('info', connectionInfo, 'Test connection', connectionInfo.hiddenKeys); |
| 118 | + try { |
| 119 | + const client = applyToInstanceHelper.setUpDocumentClient(connectionInfo); |
| 120 | + await applyToInstanceHelper.testConnection(client); |
| 121 | + return cb(); |
| 122 | + } catch(err) { |
| 123 | + logger.log('error', mapError(err), 'Connection failed'); |
| 124 | + return cb(mapError(err)); |
| 125 | + } |
48 | 126 | } |
49 | 127 | }; |
50 | 128 |
|
@@ -479,3 +557,10 @@ const generateIndexes = indexesData => { |
479 | 557 |
|
480 | 558 | return script + ';'; |
481 | 559 | }; |
| 560 | + |
| 561 | +const mapError = (error) => { |
| 562 | + return { |
| 563 | + message: error.message, |
| 564 | + stack: error.stack |
| 565 | + }; |
| 566 | +}; |
0 commit comments