Skip to content

Commit b43e0f5

Browse files
Merge pull request #28 from dannleed/feature/HCK-4140-add-azure-cli-fe-script-format
Feature - add azure cli fe script format
2 parents 4ce57a5 + 9eb16b3 commit b43e0f5

File tree

10 files changed

+431
-71
lines changed

10 files changed

+431
-71
lines changed

forward_engineering/api.js

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,55 @@
1-
const applyToInstanceHelper = require('./applyToInstance');
1+
const applyToInstanceHelper = require('./applyToInstance/applyToInstanceHelper');
22
const getIndexPolicyScript = require('./helpers/getIndexPolicyScript');
3-
const { PARTITION_KEY_DEFINITION_VERSION, PARTITION_KEY_KIND } = require('../shared/constants');
3+
const { getUniqueKeyPolicyScript } = require('./helpers/getUniqueKeyPolicyScript');
4+
const { buildAzureCLIScript } = require('./helpers/azureCLIScriptHelpers/buildAzureCLIScript');
5+
const getPartitionKey = require('./helpers/getPartitionKey');
46

57
module.exports = {
68
generateContainerScript(data, logger, callback, app) {
79
try {
810
const _ = app.require('lodash');
9-
const insertSamplesOption = _.get(data, 'options.additionalOptions', []).find(option => option.id === 'INCLUDE_SAMPLES') || {};
11+
const insertSamplesOption =
12+
_.get(data, 'options.additionalOptions', []).find(option => option.id === 'INCLUDE_SAMPLES') || {};
1013
const withSamples = data.options.origin !== 'ui';
11-
const samples = data.entities.map(entityId => updateSample(
12-
JSON.parse(data.jsonData[entityId]),
13-
data.containerData[0],
14-
(data.entityData[entityId] || [])[0] || {},
15-
));
16-
const scriptData = {
17-
partitionKey: getPartitionKey(_)(data.containerData),
18-
indexingPolicy: getIndexPolicyScript(_)(data.containerData),
19-
...(withSamples && { sample: samples }),
20-
...addItems(_)(data.containerData),
21-
};
22-
const script = JSON.stringify(scriptData, null, 2);
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 && 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+
shellName: data.options.targetScriptOptions.keyword.split('azureCli')[1].toLowerCase(),
44+
});
45+
2346
if (withSamples || !insertSamplesOption.value) {
2447
return callback(null, script);
2548
}
2649

2750
return callback(null, [
28-
{ title: 'CosmosDB script', script },
29-
{
30-
title: 'Sample data',
31-
script: JSON.stringify(samples, null, 2),
32-
},
51+
{ title: 'Azure CLI script', script },
52+
{ title: 'Sample data', script: JSON.stringify(samples, null, 2) },
3353
]);
3454
} catch (e) {
3555
const error = { message: e.message, stack: e.stack };
@@ -40,14 +60,13 @@ module.exports = {
4060
generateScript(data, logger, callback, app) {
4161
try {
4262
const _ = app.require('lodash');
63+
const uniqueKeys = _.get(data.containerData, '[0].uniqueKey', []);
64+
4365
const script = {
4466
partitionKey: getPartitionKey(_)(data.containerData),
4567
indexingPolicy: getIndexPolicyScript(_)(data.containerData),
46-
sample: updateSample(
47-
JSON.parse(data.jsonData),
48-
data.containerData[0],
49-
data.entityData[0],
50-
),
68+
...(uniqueKeys.length && getUniqueKeyPolicyScript(uniqueKeys)),
69+
sample: updateSample(JSON.parse(data.jsonData), data.containerData[0], data.entityData[0]),
5170
...addItems(_)(data.containerData),
5271
};
5372
return callback(null, JSON.stringify(script, null, 2));
@@ -75,23 +94,7 @@ const updateSample = (sample, containerData, entityData) => {
7594
};
7695
};
7796

78-
const getPartitionKey = (_) => (containerData) => {
79-
const fixNamePath = (key) => (key?.name || '').trim().replace(/\/$/, '');
80-
const partitionKeys = _.get(containerData, '[0].partitionKey', []);
81-
const isHierarchical = _.get(containerData, '[0].hierarchicalPartitionKey', false);
82-
83-
if (!isHierarchical) {
84-
return fixNamePath(partitionKeys[0]);
85-
}
86-
87-
return {
88-
paths: partitionKeys.map(fixNamePath),
89-
version: PARTITION_KEY_DEFINITION_VERSION.v2,
90-
kind: PARTITION_KEY_KIND.MultiHash,
91-
};
92-
};
93-
94-
const add = (key, items, mapper) => (script) => {
97+
const add = (key, items, mapper) => script => {
9598
if (!items.length) {
9699
return script;
97100
}
@@ -102,15 +105,15 @@ const add = (key, items, mapper) => (script) => {
102105
};
103106
};
104107

105-
const addItems = (_) => (containerData) => {
108+
const addItems = _ => containerData => {
106109
return _.flow(
107110
add('Stored Procedures', _.get(containerData, '[2].storedProcs', []), mapStoredProcs),
108111
add('User Defined Functions', _.get(containerData, '[4].udfs', []), mapUDFs),
109112
add('Triggers', _.get(containerData, '[3].triggers', []), mapTriggers),
110113
)();
111114
};
112115

113-
const mapUDFs = (udfs) => {
116+
const mapUDFs = udfs => {
114117
return udfs.map(udf => {
115118
return {
116119
id: udf.udfID,
@@ -119,18 +122,18 @@ const mapUDFs = (udfs) => {
119122
});
120123
};
121124

122-
const mapTriggers = (triggers) => {
125+
const mapTriggers = triggers => {
123126
return triggers.map(trigger => {
124127
return {
125128
id: trigger.triggerID,
126129
body: trigger.triggerFunction,
127130
triggerOperation: trigger.triggerOperation,
128-
triggerType: trigger.prePostTrigger === 'Pre-Trigger' ? 'Pre' : 'Post'
131+
triggerType: trigger.prePostTrigger === 'Pre-Trigger' ? 'Pre' : 'Post',
129132
};
130133
});
131134
};
132135

133-
const mapStoredProcs = (storedProcs) => {
136+
const mapStoredProcs = storedProcs => {
134137
return storedProcs.map(proc => {
135138
return {
136139
id: proc.storedProcID,

forward_engineering/applyToInstance/index.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,6 @@ const updateIndexingPolicy = (indexes) => {
6464
return result;
6565
};
6666

67-
const getUniqueKeys = (uniqueKeys) => {
68-
if (!uniqueKeys) {
69-
return [];
70-
}
71-
72-
return uniqueKeys.filter(uniqueKey => uniqueKey.attributePath && Array.isArray(uniqueKey.attributePath) && uniqueKey.attributePath.length).map((uniqueKey) => {
73-
return {
74-
paths: uniqueKey.attributePath.map(path => '/' + path.name.split('.').slice(1).join('/')).filter(Boolean)
75-
};
76-
}).filter(path => path.paths.length);
77-
};
78-
7967
module.exports = {
8068
testConnection: reApi.testConnection,
8169
async applyToInstance(connectionInfo, logger, callback, app) {
@@ -109,8 +97,8 @@ module.exports = {
10997
id: containerId,
11098
partitionKey: script.partitionKey,
11199
defaultTtl: helper.getTTL(containerData),
100+
...(script.uniqueKeyPolicy && { uniqueKeyPolicy: script.uniqueKeyPolicy }),
112101
...helper.getContainerThroughputProps(containerData),
113-
114102
});
115103

116104
progress('Add sample documents ...');
@@ -127,8 +115,7 @@ module.exports = {
127115
progress('Update indexing policy ...');
128116

129117
await container.replace({
130-
id: containerId,
131-
partitionKey: containerDef.partitionKey,
118+
...containerDef,
132119
indexingPolicy: updateIndexingPolicy(script.indexingPolicy),
133120
});
134121

forward_engineering/config.json

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,69 @@
88
"container": true,
99
"view": false
1010
},
11-
"additionalOptions": [{
12-
"id": "INCLUDE_SAMPLES",
13-
"value": true,
14-
"name": "Include sample data",
15-
"align": "right",
16-
"level": {
17-
"entity": false
11+
"additionalOptions": [
12+
{
13+
"id": "INCLUDE_SAMPLES",
14+
"value": true,
15+
"name": "Include sample data",
16+
"align": "right",
17+
"level": {
18+
"entity": false
19+
},
20+
"targetFEKeywords": [
21+
"containerSettingsJson"
22+
]
1823
}
19-
}],
24+
],
25+
"options": [
26+
{
27+
"name": "Container settings JSON",
28+
"keyword": "containerSettingsJson",
29+
"fileExtensions": [
30+
{
31+
"label": "application/json",
32+
"value": "json"
33+
}
34+
]
35+
},
36+
{
37+
"name": "Azure CLI script (Windows PowerShell)",
38+
"keyword": "azureCliPowerShell",
39+
"disableApplyScriptToInstance": true,
40+
"fileExtensions": [
41+
{
42+
"label": "application/txt",
43+
"value": "txt"
44+
}
45+
]
46+
},
47+
{
48+
"name": "Azure CLI script (zsh)",
49+
"keyword": "azureCliZsh",
50+
"disableApplyScriptToInstance": true,
51+
"fileExtensions": [
52+
{
53+
"label": "application/txt",
54+
"value": "txt"
55+
}
56+
]
57+
},
58+
{
59+
"name": "Azure CLI script (bash)",
60+
"keyword": "azureCliBash",
61+
"disableApplyScriptToInstance": true,
62+
"fileExtensions": [
63+
{
64+
"label": "application/txt",
65+
"value": "txt"
66+
}
67+
]
68+
}
69+
],
2070
"splitView": {
21-
"byAdditionalOptions": ["INCLUDE_SAMPLES"]
71+
"byAdditionalOptions": [
72+
"INCLUDE_SAMPLES"
73+
]
2274
},
2375
"applyScriptToInstance": true,
2476
"refresh": true
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const CLI = 'az cosmosdb sql';
2+
const DATABASE = 'database';
3+
const CONTAINER = 'container';
4+
const STORED_PROCEDURE = 'stored-procedure';
5+
const TRIGGER = 'trigger';
6+
const USER_DEFINED_FUNCTION = 'user-defined-function';
7+
const CREATE = 'create';
8+
9+
module.exports = {
10+
CLI,
11+
DATABASE,
12+
CREATE,
13+
CONTAINER,
14+
STORED_PROCEDURE,
15+
TRIGGER,
16+
USER_DEFINED_FUNCTION,
17+
};

0 commit comments

Comments
 (0)