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