Skip to content

Commit 0d90a99

Browse files
committed
HCK-4140: add FE Azure CLI script generation
1 parent 3878732 commit 0d90a99

File tree

8 files changed

+400
-58
lines changed

8 files changed

+400
-58
lines changed

forward_engineering/api.js

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +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');
43
const { getUniqueKeyPolicyScript } = require('./helpers/getUniqueKeyPolicyScript');
4+
const { buildAzureCLIScript } = require('./helpers/azureCLIScriptHelpers/buildAzureCLIScript');
5+
const getPartitionKey = require('./helpers/getPartitionKey');
56

67
module.exports = {
78
generateContainerScript(data, logger, callback, app) {
89
try {
910
const _ = app.require('lodash');
10-
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') || {};
1113
const withSamples = data.options.origin !== 'ui';
12-
const samples = data.entities.map(entityId => updateSample(
13-
JSON.parse(data.jsonData[entityId]),
14-
data.containerData[0],
15-
(data.entityData[entityId] || [])[0] || {},
16-
));
17-
const uniqueKeys = _.get(data.containerData, '[0].uniqueKey', []);
18-
const scriptData = {
19-
partitionKey: getPartitionKey(_)(data.containerData),
20-
...(uniqueKeys.length && getUniqueKeyPolicyScript(uniqueKeys)),
21-
indexingPolicy: getIndexPolicyScript(_)(data.containerData),
22-
...(withSamples && { sample: samples }),
23-
...addItems(_)(data.containerData),
24-
};
25-
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+
2646
if (withSamples || !insertSamplesOption.value) {
2747
return callback(null, script);
2848
}
2949

3050
return callback(null, [
31-
{ title: 'CosmosDB script', script },
32-
{
33-
title: 'Sample data',
34-
script: JSON.stringify(samples, null, 2),
35-
},
51+
{ title: 'Azure CLI script', script },
52+
{ title: 'Sample data', script: JSON.stringify(samples, null, 2) },
3653
]);
3754
} catch (e) {
3855
const error = { message: e.message, stack: e.stack };
@@ -43,14 +60,13 @@ module.exports = {
4360
generateScript(data, logger, callback, app) {
4461
try {
4562
const _ = app.require('lodash');
63+
const uniqueKeys = _.get(data.containerData, '[0].uniqueKey', []);
64+
4665
const script = {
4766
partitionKey: getPartitionKey(_)(data.containerData),
4867
indexingPolicy: getIndexPolicyScript(_)(data.containerData),
49-
sample: updateSample(
50-
JSON.parse(data.jsonData),
51-
data.containerData[0],
52-
data.entityData[0],
53-
),
68+
...(uniqueKeys.length && getUniqueKeyPolicyScript(uniqueKeys)),
69+
sample: updateSample(JSON.parse(data.jsonData), data.containerData[0], data.entityData[0]),
5470
...addItems(_)(data.containerData),
5571
};
5672
return callback(null, JSON.stringify(script, null, 2));
@@ -78,23 +94,7 @@ const updateSample = (sample, containerData, entityData) => {
7894
};
7995
};
8096

81-
const getPartitionKey = (_) => (containerData) => {
82-
const fixNamePath = (key) => (key?.name || '').trim().replace(/\/$/, '');
83-
const partitionKeys = _.get(containerData, '[0].partitionKey', []);
84-
const isHierarchical = _.get(containerData, '[0].hierarchicalPartitionKey', false);
85-
86-
if (!isHierarchical) {
87-
return fixNamePath(partitionKeys[0]);
88-
}
89-
90-
return {
91-
paths: partitionKeys.map(fixNamePath),
92-
version: PARTITION_KEY_DEFINITION_VERSION.v2,
93-
kind: PARTITION_KEY_KIND.MultiHash,
94-
};
95-
};
96-
97-
const add = (key, items, mapper) => (script) => {
97+
const add = (key, items, mapper) => script => {
9898
if (!items.length) {
9999
return script;
100100
}
@@ -105,15 +105,15 @@ const add = (key, items, mapper) => (script) => {
105105
};
106106
};
107107

108-
const addItems = (_) => (containerData) => {
108+
const addItems = _ => containerData => {
109109
return _.flow(
110110
add('Stored Procedures', _.get(containerData, '[2].storedProcs', []), mapStoredProcs),
111111
add('User Defined Functions', _.get(containerData, '[4].udfs', []), mapUDFs),
112112
add('Triggers', _.get(containerData, '[3].triggers', []), mapTriggers),
113113
)();
114114
};
115115

116-
const mapUDFs = (udfs) => {
116+
const mapUDFs = udfs => {
117117
return udfs.map(udf => {
118118
return {
119119
id: udf.udfID,
@@ -122,18 +122,18 @@ const mapUDFs = (udfs) => {
122122
});
123123
};
124124

125-
const mapTriggers = (triggers) => {
125+
const mapTriggers = triggers => {
126126
return triggers.map(trigger => {
127127
return {
128128
id: trigger.triggerID,
129129
body: trigger.triggerFunction,
130130
triggerOperation: trigger.triggerOperation,
131-
triggerType: trigger.prePostTrigger === 'Pre-Trigger' ? 'Pre' : 'Post'
131+
triggerType: trigger.prePostTrigger === 'Pre-Trigger' ? 'Pre' : 'Post',
132132
};
133133
});
134134
};
135135

136-
const mapStoredProcs = (storedProcs) => {
136+
const mapStoredProcs = storedProcs => {
137137
return storedProcs.map(proc => {
138138
return {
139139
id: proc.storedProcID,

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)