1
- const applyToInstanceHelper = require ( './applyToInstance' ) ;
1
+ const applyToInstanceHelper = require ( './applyToInstance/applyToInstanceHelper ' ) ;
2
2
const getIndexPolicyScript = require ( './helpers/getIndexPolicyScript' ) ;
3
- const { PARTITION_KEY_DEFINITION_VERSION , PARTITION_KEY_KIND } = require ( '../shared/constants' ) ;
4
3
const { getUniqueKeyPolicyScript } = require ( './helpers/getUniqueKeyPolicyScript' ) ;
4
+ const { buildAzureCLIScript } = require ( './helpers/azureCLIScriptHelpers/buildAzureCLIScript' ) ;
5
+ const getPartitionKey = require ( './helpers/getPartitionKey' ) ;
5
6
6
7
module . exports = {
7
8
generateContainerScript ( data , logger , callback , app ) {
8
9
try {
9
10
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' ) || { } ;
11
13
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
+
26
46
if ( withSamples || ! insertSamplesOption . value ) {
27
47
return callback ( null , script ) ;
28
48
}
29
49
30
50
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 ) } ,
36
53
] ) ;
37
54
} catch ( e ) {
38
55
const error = { message : e . message , stack : e . stack } ;
@@ -43,14 +60,13 @@ module.exports = {
43
60
generateScript ( data , logger , callback , app ) {
44
61
try {
45
62
const _ = app . require ( 'lodash' ) ;
63
+ const uniqueKeys = _ . get ( data . containerData , '[0].uniqueKey' , [ ] ) ;
64
+
46
65
const script = {
47
66
partitionKey : getPartitionKey ( _ ) ( data . containerData ) ,
48
67
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 ] ) ,
54
70
...addItems ( _ ) ( data . containerData ) ,
55
71
} ;
56
72
return callback ( null , JSON . stringify ( script , null , 2 ) ) ;
@@ -78,23 +94,7 @@ const updateSample = (sample, containerData, entityData) => {
78
94
} ;
79
95
} ;
80
96
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 => {
98
98
if ( ! items . length ) {
99
99
return script ;
100
100
}
@@ -105,15 +105,15 @@ const add = (key, items, mapper) => (script) => {
105
105
} ;
106
106
} ;
107
107
108
- const addItems = ( _ ) => ( containerData ) => {
108
+ const addItems = _ => containerData => {
109
109
return _ . flow (
110
110
add ( 'Stored Procedures' , _ . get ( containerData , '[2].storedProcs' , [ ] ) , mapStoredProcs ) ,
111
111
add ( 'User Defined Functions' , _ . get ( containerData , '[4].udfs' , [ ] ) , mapUDFs ) ,
112
112
add ( 'Triggers' , _ . get ( containerData , '[3].triggers' , [ ] ) , mapTriggers ) ,
113
113
) ( ) ;
114
114
} ;
115
115
116
- const mapUDFs = ( udfs ) => {
116
+ const mapUDFs = udfs => {
117
117
return udfs . map ( udf => {
118
118
return {
119
119
id : udf . udfID ,
@@ -122,18 +122,18 @@ const mapUDFs = (udfs) => {
122
122
} ) ;
123
123
} ;
124
124
125
- const mapTriggers = ( triggers ) => {
125
+ const mapTriggers = triggers => {
126
126
return triggers . map ( trigger => {
127
127
return {
128
128
id : trigger . triggerID ,
129
129
body : trigger . triggerFunction ,
130
130
triggerOperation : trigger . triggerOperation ,
131
- triggerType : trigger . prePostTrigger === 'Pre-Trigger' ? 'Pre' : 'Post'
131
+ triggerType : trigger . prePostTrigger === 'Pre-Trigger' ? 'Pre' : 'Post' ,
132
132
} ;
133
133
} ) ;
134
134
} ;
135
135
136
- const mapStoredProcs = ( storedProcs ) => {
136
+ const mapStoredProcs = storedProcs => {
137
137
return storedProcs . map ( proc => {
138
138
return {
139
139
id : proc . storedProcID ,
0 commit comments