|
1 | | -const applyToInstanceHelper = require('./applyToInstance'); |
2 | | -const getIndexPolicyScript = require('./helpers/getIndexPolicyScript'); |
3 | | -const { getUniqueKeyPolicyScript } = require('./helpers/getUniqueKeyPolicyScript'); |
4 | | -const { buildAzureCLIScript } = require('./helpers/azureCLIScriptHelpers/buildAzureCLIScript'); |
5 | | -const getPartitionKey = require('./helpers/getPartitionKey'); |
| 1 | +const { generateScript } = require('./generateScript'); |
| 2 | +const { generateContainerScript } = require('./generateContainerScript'); |
| 3 | +const { applyToInstance, testConnection } = require('./applyToInstance'); |
6 | 4 |
|
7 | 5 | module.exports = { |
8 | | - generateContainerScript(data, logger, callback, app) { |
9 | | - try { |
10 | | - const _ = app.require('lodash'); |
11 | | - const insertSamplesOption = |
12 | | - _.get(data, 'options.additionalOptions', []).find(option => option.id === 'INCLUDE_SAMPLES') || {}; |
13 | | - const withSamples = data.options.origin !== 'ui'; |
14 | | - const samples = data.entities.map(entityId => |
15 | | - updateSample( |
16 | | - JSON.parse(data.jsonData[entityId]), |
17 | | - data.containerData[0], |
18 | | - (data.entityData[entityId] || [])[0] || {}, |
19 | | - ), |
20 | | - ); |
21 | | - if (data.options?.targetScriptOptions?.keyword === 'containerSettingsJson') { |
22 | | - const uniqueKeys = _.get(data.containerData, '[0].uniqueKey', []); |
23 | | - const scriptData = { |
24 | | - partitionKey: getPartitionKey(_)(data.containerData), |
25 | | - ...(uniqueKeys.length && getUniqueKeyPolicyScript(uniqueKeys)), |
26 | | - indexingPolicy: getIndexPolicyScript(_)(data.containerData), |
27 | | - ...(withSamples && { sample: samples }), |
28 | | - ...addItems(_)(data.containerData), |
29 | | - }; |
30 | | - const script = JSON.stringify(scriptData, null, 2); |
31 | | - if (withSamples || !insertSamplesOption.value) { |
32 | | - return callback(null, script); |
33 | | - } |
34 | | - |
35 | | - return callback(null, [ |
36 | | - { title: 'CosmosDB script', script }, |
37 | | - { title: 'Sample data', script: JSON.stringify(samples, null, 2) }, |
38 | | - ]); |
39 | | - } |
40 | | - |
41 | | - const script = buildAzureCLIScript(_)({ |
42 | | - ...data, |
43 | | - }); |
44 | | - |
45 | | - if (withSamples || !insertSamplesOption.value) { |
46 | | - return callback(null, script); |
47 | | - } |
48 | | - |
49 | | - return callback(null, [ |
50 | | - { title: 'Azure CLI script', script }, |
51 | | - { title: 'Sample data', script: JSON.stringify(samples, null, 2) }, |
52 | | - ]); |
53 | | - } catch (e) { |
54 | | - const error = { message: e.message, stack: e.stack }; |
55 | | - logger.log('error', error, 'CosmosDB w\\ SQL API forward engineering error'); |
56 | | - callback(error); |
57 | | - } |
58 | | - }, |
59 | | - generateScript(data, logger, callback, app) { |
60 | | - try { |
61 | | - const _ = app.require('lodash'); |
62 | | - const uniqueKeys = _.get(data.containerData, '[0].uniqueKey', []); |
63 | | - |
64 | | - const script = { |
65 | | - partitionKey: getPartitionKey(_)(data.containerData), |
66 | | - indexingPolicy: getIndexPolicyScript(_)(data.containerData), |
67 | | - ...(uniqueKeys.length && getUniqueKeyPolicyScript(uniqueKeys)), |
68 | | - sample: updateSample(JSON.parse(data.jsonData), data.containerData[0], data.entityData[0]), |
69 | | - ...addItems(_)(data.containerData), |
70 | | - }; |
71 | | - return callback(null, JSON.stringify(script, null, 2)); |
72 | | - } catch (e) { |
73 | | - const error = { message: e.message, stack: e.stack }; |
74 | | - logger.log('error', error, 'CosmosDB w\\ SQL API forward engineering error'); |
75 | | - callback(error); |
76 | | - } |
77 | | - }, |
78 | | - applyToInstance: applyToInstanceHelper.applyToInstance, |
79 | | - |
80 | | - testConnection: applyToInstanceHelper.testConnection, |
81 | | -}; |
82 | | - |
83 | | -const updateSample = (sample, containerData, entityData) => { |
84 | | - const docType = containerData?.docTypeName; |
85 | | - |
86 | | - if (!docType) { |
87 | | - return sample; |
88 | | - } |
89 | | - |
90 | | - return { |
91 | | - ...sample, |
92 | | - [docType]: entityData.code || entityData.collectionName, |
93 | | - }; |
94 | | -}; |
95 | | - |
96 | | -const add = (key, items, mapper) => script => { |
97 | | - if (!items.length) { |
98 | | - return script; |
99 | | - } |
100 | | - |
101 | | - return { |
102 | | - ...script, |
103 | | - [key]: mapper(items), |
104 | | - }; |
105 | | -}; |
106 | | - |
107 | | -const addItems = _ => containerData => { |
108 | | - return _.flow( |
109 | | - add('Stored Procedures', _.get(containerData, '[2].storedProcs', []), mapStoredProcs), |
110 | | - add('User Defined Functions', _.get(containerData, '[4].udfs', []), mapUDFs), |
111 | | - add('Triggers', _.get(containerData, '[3].triggers', []), mapTriggers), |
112 | | - )(); |
113 | | -}; |
114 | | - |
115 | | -const mapUDFs = udfs => { |
116 | | - return udfs.map(udf => { |
117 | | - return { |
118 | | - id: udf.udfID, |
119 | | - body: udf.udfFunction, |
120 | | - }; |
121 | | - }); |
122 | | -}; |
123 | | - |
124 | | -const mapTriggers = triggers => { |
125 | | - return triggers.map(trigger => { |
126 | | - return { |
127 | | - id: trigger.triggerID, |
128 | | - body: trigger.triggerFunction, |
129 | | - triggerOperation: trigger.triggerOperation, |
130 | | - triggerType: trigger.prePostTrigger === 'Pre-Trigger' ? 'Pre' : 'Post', |
131 | | - }; |
132 | | - }); |
133 | | -}; |
134 | | - |
135 | | -const mapStoredProcs = storedProcs => { |
136 | | - return storedProcs.map(proc => { |
137 | | - return { |
138 | | - id: proc.storedProcID, |
139 | | - body: proc.storedProcFunction, |
140 | | - }; |
141 | | - }); |
| 6 | + generateScript, |
| 7 | + generateContainerScript, |
| 8 | + applyToInstance, |
| 9 | + testConnection, |
142 | 10 | }; |
0 commit comments