11const _ = require ( 'lodash' ) ;
22const applyToInstanceHelper = require ( './applyToInstanceHelper' ) ;
3+ const getIndexPolicyScript = require ( './getIndexPolicyScript' ) ;
4+ const scriptHelper = require ( './scriptHelper' ) ;
35
46const DEFAULT_INDENT = ' ' ;
57let graphName = 'g' ;
68
79module . exports = {
810 generateContainerScript ( data , logger , cb ) {
9- let { collections, relationships, jsonData, containerData } = data ;
11+ let { collections, relationships, jsonData, containerData, options } = data ;
12+ const scriptId = _ . get ( options , 'targetScriptOptions.keyword' ) ;
1013 logger . clear ( ) ;
1114 try {
15+ if ( scriptId === 'cosmosdb' ) {
16+ return cb (
17+ null ,
18+ JSON . stringify (
19+ {
20+ indexingPolicy : getIndexPolicyScript ( _ ) ( containerData ) ,
21+ ...scriptHelper . addItems ( _ ) ( containerData ) ,
22+ } ,
23+ null ,
24+ 2
25+ )
26+ ) ;
27+ }
28+
1229 let resultScript = '' ;
1330 const traversalSource = _ . get ( containerData , [ 0 , 'traversalSource' ] , 'g' ) ;
1431 graphName = transformToValidGremlinName ( traversalSource ) ;
@@ -60,6 +77,7 @@ module.exports = {
6077 if ( ! data . containerData ) {
6178 return cb ( { message : 'Graph wasn\'t specified' } ) ;
6279 }
80+ const targetScriptOptions = data . targetScriptOptions || { } ;
6381 const containerProps = _ . get ( data . containerData , '[0]' , { } ) ;
6482 if ( ! containerProps . dbId ) {
6583 return cb ( { message : 'Database id wasn\'t specified' } ) ;
@@ -68,46 +86,79 @@ module.exports = {
6886 if ( ! graphName ) {
6987 return cb ( { message : 'Graph name wasn\'t specified' } ) ;
7088 }
89+ const progress = createLogger ( logger , containerProps . dbId , graphName ) ;
7190
7291 const cosmosClient = applyToInstanceHelper . setUpDocumentClient ( data ) ;
92+
93+ progress ( 'Create database if not exists ...' ) ;
94+
7395 await cosmosClient . databases . createIfNotExists ( {
7496 id : containerProps . dbId
7597 } ) ;
98+
99+ progress ( 'Create container if not exists ...' ) ;
100+
76101 const containerResponse = await cosmosClient
77102 . database ( containerProps . dbId )
78103 . containers . createIfNotExists ( {
79104 id : graphName ,
80- partitionKey : containerProps . partitionKey ,
105+ partitionKey : getPartitionKey ( _ ) ( data . containerData ) ,
81106 ...( containerProps . autopilot
82107 ? { maxThroughput : containerProps . throughput || 400 }
83108 : { throughput : containerProps . throughput || 400 } ) ,
84109 defaultTtl : applyToInstanceHelper . getTTL ( containerProps ) ,
85110 } ) ;
86- if ( containerResponse . statusCode === 201 ) {
87- const containerInstance = cosmosClient . database ( containerProps . dbId ) . container ( graphName ) ;
88111
89- const storedProcs = _ . get ( data . containerData , '[2].storedProcs' , [ ] ) ;
112+ let functionsScripts ;
113+
114+ if ( targetScriptOptions . id === 'cosmosdb' ) {
115+ progress ( 'Applying Cosmos DB script ...' ) ;
116+ const script = JSON . parse ( data . script ) ;
117+
118+ progress ( 'Update indexing policy ...' ) ;
119+
120+ await containerResponse . container . replace ( {
121+ id : graphName ,
122+ partitionKey : containerResponse . resource . partitionKey ,
123+ indexingPolicy : updateIndexingPolicy ( script . indexingPolicy ) ,
124+ } ) ;
125+
126+ const storedProcs = _ . get ( script , 'Stored Procedures' , [ ] ) ;
90127 if ( storedProcs . length ) {
91- await applyToInstanceHelper . createStoredProcs ( storedProcs , containerInstance ) ;
128+ progress ( 'Upload stored procs ...' ) ;
129+ await applyToInstanceHelper . createStoredProcs ( storedProcs , containerResponse . container ) ;
92130 }
93131
94- const udfs = _ . get ( data . containerData , '[3].udfs ' , [ ] ) ;
132+ const udfs = _ . get ( script , 'User Defined Functions ' , [ ] ) ;
95133 if ( udfs . length ) {
96- await applyToInstanceHelper . createUDFs ( udfs , containerInstance ) ;
134+ progress ( 'Upload user defined functions ...' ) ;
135+ await applyToInstanceHelper . createUDFs ( udfs , containerResponse . container ) ;
97136 }
98- const triggers = _ . get ( data . containerData , '[4].triggers' , [ ] ) ;
137+
138+ const triggers = _ . get ( script , 'Triggers' , [ ] ) ;
99139 if ( triggers . length ) {
100- await applyToInstanceHelper . createTriggers ( triggers , containerInstance ) ;
140+ progress ( 'Upload triggers ...' ) ;
141+ await applyToInstanceHelper . createTriggers ( triggers , containerResponse . container ) ;
101142 }
102- }
103143
104- const { labels, edges } = applyToInstanceHelper . parseScriptStatements ( data . script ) ;
105- const gremlinClient = await applyToInstanceHelper . getGremlinClient ( data , containerProps . dbId , graphName ) ;
106- await applyToInstanceHelper . runGremlinQueries ( gremlinClient , labels ) ;
107- await applyToInstanceHelper . runGremlinQueries ( gremlinClient , edges ) ;
144+ } else {
145+ progress ( 'Applying Gremlin script ...' ) ;
146+
147+ const { labels, edges } = applyToInstanceHelper . parseScriptStatements ( data . script ) ;
148+ const gremlinClient = await applyToInstanceHelper . getGremlinClient ( data , containerProps . dbId , graphName ) ;
108149
150+ progress ( 'Uploading labels ...' ) ;
151+
152+ await applyToInstanceHelper . runGremlinQueries ( gremlinClient , labels ) ;
153+
154+ progress ( 'Uploading edges ...' ) ;
155+
156+ await applyToInstanceHelper . runGremlinQueries ( gremlinClient , edges ) ;
157+ }
158+
109159 cb ( ) ;
110160 } catch ( err ) {
161+ logger . log ( 'error' , mapError ( err ) ) ;
111162 cb ( mapError ( err ) ) ;
112163 }
113164 } ,
@@ -126,6 +177,71 @@ module.exports = {
126177 }
127178} ;
128179
180+ const getPartitionKey = ( _ ) => ( containerData ) => {
181+ const partitionKey = _ . get ( containerData , '[0].partitionKey[0].name' ) ;
182+
183+ if ( ! partitionKey ) {
184+ return ;
185+ }
186+
187+ return '/' + partitionKey . split ( '.' ) . slice ( 1 ) . join ( '/' ) ;
188+ } ;
189+
190+ const updateIndexingPolicy = ( indexes ) => {
191+ const result = { ...indexes } ;
192+
193+ if ( Array . isArray ( result . includedPaths ) ) {
194+ result . includedPaths = addDataType ( result . includedPaths ) ;
195+ }
196+
197+ if ( Array . isArray ( result . excludedPaths ) ) {
198+ result . excludedPaths = addDataType ( result . excludedPaths ) ;
199+ }
200+
201+ if ( Array . isArray ( result . spatialIndexes ) ) {
202+ result . spatialIndexes = result . spatialIndexes . map ( addSpatialTypes ) ;
203+ }
204+
205+ return result ;
206+ } ;
207+
208+ const addDataType = ( indexes ) => {
209+ return indexes . map ( index => {
210+ if ( ! Array . isArray ( index . indexes ) ) {
211+ return index ;
212+ }
213+
214+ return {
215+ ...index ,
216+ indexes : index . indexes . map ( item => ( {
217+ ...item ,
218+ dataType : item . dataType || 'String' ,
219+ } ) ) ,
220+ } ;
221+ } ) ;
222+ } ;
223+
224+ const addSpatialTypes = ( spatialIndex ) => {
225+ if ( Array . isArray ( spatialIndex . types ) && spatialIndex . types . length ) {
226+ return spatialIndex ;
227+ }
228+
229+ return {
230+ ...spatialIndex ,
231+ types : [
232+ "Point" ,
233+ "LineString" ,
234+ "Polygon" ,
235+ "MultiPolygon"
236+ ]
237+ } ;
238+ } ;
239+
240+ const createLogger = ( logger , containerName , entityName ) => ( message ) => {
241+ logger . progress ( { message, containerName, entityName } ) ;
242+ logger . log ( 'info' , { message } , 'Applying to instance' ) ;
243+ } ;
244+
129245const generateVariables = variables => {
130246 return variables . reduce ( ( script , variable ) => {
131247 const key = variable . graphVariableKey ;
0 commit comments