1
1
/*
2
- * Licensed to the Apache Software Foundation (ASF) under one or more
3
- * contributor license agreements. See the NOTICE file distributed with
4
- * this work for additional information regarding copyright ownership.
5
- * The ASF licenses this file to You under the Apache License, Version 2.0
6
- * (the "License"); you may not use this file except in compliance with
7
- * the License. You may obtain a copy of the License at
8
- *
9
- * http://www.apache.org/licenses/LICENSE-2.0
10
- *
11
- * Unless required by applicable law or agreed to in writing, software
12
- * distributed under the License is distributed on an "AS IS" BASIS,
13
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- * See the License for the specific language governing permissions and
15
- * limitations under the License.
16
- */
2
+ * Licensed to the Apache Software Foundation (ASF) under one or more
3
+ * contributor license agreements. See the NOTICE file distributed with
4
+ * this work for additional information regarding copyright ownership.
5
+ * The ASF licenses this file to You under the Apache License, Version 2.0
6
+ * (the "License"); you may not use this file except in compliance with
7
+ * the License. You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
17
18
const common = require ( './common' ) ;
18
19
19
20
module . exports = function ( endpoint , masterKey ) {
20
21
var DocumentClient = require ( 'documentdb' ) . DocumentClient ;
21
22
var client = new DocumentClient ( endpoint , { masterKey : masterKey } ) ;
22
-
23
+
23
24
this . client = client ;
24
25
var utilsDB = this ;
25
-
26
+
26
27
this . init = function ( databaseName , collectionName ) {
27
28
console . log ( "cosmosdb.init" ) ;
28
29
let querySpec = {
@@ -44,7 +45,7 @@ module.exports = function(endpoint, masterKey) {
44
45
} ) ;
45
46
} ) ;
46
47
} ;
47
-
48
+
48
49
//get collection in cosmosdb terminology
49
50
this . getDatabase = function ( collectionName ) {
50
51
console . log ( `cosmosdb.getDatabase:` , collectionName ) ;
@@ -70,12 +71,12 @@ module.exports = function(endpoint, masterKey) {
70
71
utilsDB . collectionLink = results [ 0 ] . _self ;
71
72
resolve ( results ) ;
72
73
}
73
-
74
+
74
75
}
75
76
} ) ;
76
77
} ) ;
77
78
} ;
78
-
79
+
79
80
//create collection in cosmosdb terminology
80
81
this . createDatabase = function ( collectionName ) {
81
82
console . log ( `cosmosdb.createDatabase:` , collectionName ) ;
@@ -93,24 +94,24 @@ module.exports = function(endpoint, masterKey) {
93
94
} ) ;
94
95
} ) ;
95
96
} ;
96
-
97
+
97
98
this . getWorkerID = function ( availabeWorkers ) {
98
-
99
+
99
100
return new Promise ( ( resolve , reject ) => {
100
101
var workerID = availabeWorkers [ 0 ] || 'worker0' ;
101
102
resolve ( workerID ) ;
102
103
} ) ;
103
104
} ;
104
-
105
+
105
106
this . createTrigger = function ( triggerID , newTrigger ) {
106
107
console . log ( `cosmosdb.createTrigger:` , triggerID ) ;
107
108
if ( ! newTrigger . id ) {
108
109
console . log ( "add id to doc" ) ;
109
110
newTrigger . id = triggerID ;
110
111
}
111
-
112
+
112
113
return new Promise ( function ( resolve , reject ) {
113
-
114
+
114
115
client . createDocument ( utilsDB . collectionLink , newTrigger , function ( err , document ) {
115
116
if ( err ) {
116
117
console . error ( `cosmosdb.createTrigger failed:` , err ) ;
@@ -122,15 +123,15 @@ module.exports = function(endpoint, masterKey) {
122
123
} ) ;
123
124
} ) ;
124
125
} ;
125
-
126
+
126
127
this . getTrigger = function ( triggerID , retry = true ) {
127
128
console . log ( `cosmosdb.getTrigger:` , triggerID ) ;
128
129
return new Promise ( function ( resolve , reject ) {
129
130
let querySpec = {
130
131
query : 'SELECT * FROM root r WHERE r.id = @id' ,
131
132
parameters : [ { name : '@id' , value : triggerID } ]
132
133
} ;
133
-
134
+
134
135
client . queryDocuments ( utilsDB . collectionLink , querySpec ) . toArray ( function ( err , results ) {
135
136
if ( err ) {
136
137
if ( retry ) {
@@ -145,7 +146,7 @@ module.exports = function(endpoint, masterKey) {
145
146
reject ( common . sendError ( err . statusCode , 'could not find trigger ' + triggerID + ' in the database' ) ) ;
146
147
}
147
148
}
148
-
149
+
149
150
if ( results . length == 0 )
150
151
resolve ( ) ;
151
152
else {
@@ -155,7 +156,7 @@ module.exports = function(endpoint, masterKey) {
155
156
} ) ;
156
157
} ) ;
157
158
} ;
158
-
159
+
159
160
this . updateTrigger = function ( triggerID , trigger , params , retryCount ) {
160
161
console . log ( `cosmosdb.updateTrigger:` , triggerID ) ;
161
162
if ( retryCount === 0 ) {
@@ -168,7 +169,7 @@ module.exports = function(endpoint, masterKey) {
168
169
} ;
169
170
trigger . status = status ;
170
171
}
171
-
172
+
172
173
return new Promise ( function ( resolve , reject ) {
173
174
utilsDB . getTrigger ( triggerID )
174
175
. then ( ( doc ) => {
@@ -197,15 +198,15 @@ module.exports = function(endpoint, masterKey) {
197
198
} ) ;
198
199
} ) ;
199
200
} ;
200
-
201
+
201
202
this . disableTrigger = function ( triggerID , trigger , retryCount , crudMessage ) {
202
203
console . log ( `cosmosdb.disableTrigger:` , triggerID ) ;
203
204
if ( retryCount === 0 ) {
204
205
//check if it is already disabled
205
206
if ( trigger . status && trigger . status . active === false ) {
206
207
return Promise . resolve ( triggerID ) ;
207
208
}
208
-
209
+
209
210
var message = `Automatically disabled trigger while ${ crudMessage } ` ;
210
211
var status = {
211
212
'active' : false ,
@@ -214,10 +215,10 @@ module.exports = function(endpoint, masterKey) {
214
215
} ;
215
216
trigger . status = status ;
216
217
}
217
-
218
+
218
219
return utilsDB . updateTrigger ( triggerID , trigger , { } , retryCount ) ;
219
220
} ;
220
-
221
+
221
222
this . deleteTrigger = function ( triggerID ) {
222
223
console . log ( `cosmosdb.deleteTrigger` , triggerID ) ;
223
224
return new Promise ( function ( resolve , reject ) {
0 commit comments