@ragestudio/scylla-odm 0.22.2 → 0.22.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. package/batch/index.d.ts +3 -3
  2. package/batch/index.d.ts.map +1 -1
  3. package/client.d.ts +6 -5
  4. package/client.d.ts.map +1 -1
  5. package/client.js +7 -7
  6. package/client.js.map +1 -1
  7. package/cql_gen/create_table.d.ts +1 -1
  8. package/cql_gen/create_table.d.ts.map +1 -1
  9. package/document/index.d.ts +3 -3
  10. package/document/index.d.ts.map +1 -1
  11. package/driver/LICENSE.txt +177 -0
  12. package/driver/NOTICE.txt +67 -0
  13. package/driver/auth/index.d.ts +37 -0
  14. package/driver/auth/index.js +37 -0
  15. package/driver/auth/no-auth-provider.js +73 -0
  16. package/driver/auth/plain-text-auth-provider.js +81 -0
  17. package/driver/auth/provider.js +77 -0
  18. package/driver/client-options.js +442 -0
  19. package/driver/client.js +1267 -0
  20. package/driver/concurrent/index.d.ts +49 -0
  21. package/driver/concurrent/index.js +366 -0
  22. package/driver/connection.js +1034 -0
  23. package/driver/control-connection.js +1282 -0
  24. package/driver/encoder.js +2316 -0
  25. package/driver/errors.js +223 -0
  26. package/driver/execution-options.js +612 -0
  27. package/driver/execution-profile.js +274 -0
  28. package/driver/host-connection-pool.js +587 -0
  29. package/driver/host.js +699 -0
  30. package/driver/index.d.ts +387 -0
  31. package/driver/index.js +81 -0
  32. package/driver/mapping/cache.js +214 -0
  33. package/driver/mapping/doc-info-adapter.js +171 -0
  34. package/driver/mapping/index.d.ts +219 -0
  35. package/driver/mapping/index.js +57 -0
  36. package/driver/mapping/mapper.js +225 -0
  37. package/driver/mapping/mapping-handler.js +641 -0
  38. package/driver/mapping/model-batch-item.js +215 -0
  39. package/driver/mapping/model-batch-mapper.js +141 -0
  40. package/driver/mapping/model-mapper.js +315 -0
  41. package/driver/mapping/model-mapping-info.js +225 -0
  42. package/driver/mapping/object-selector.js +417 -0
  43. package/driver/mapping/q.js +156 -0
  44. package/driver/mapping/query-generator.js +556 -0
  45. package/driver/mapping/result-mapper.js +123 -0
  46. package/driver/mapping/result.js +139 -0
  47. package/driver/mapping/table-mappings.js +133 -0
  48. package/driver/mapping/tree.js +160 -0
  49. package/driver/metadata/aggregate.js +79 -0
  50. package/driver/metadata/client-state.js +119 -0
  51. package/driver/metadata/data-collection.js +182 -0
  52. package/driver/metadata/event-debouncer.js +174 -0
  53. package/driver/metadata/index.d.ts +276 -0
  54. package/driver/metadata/index.js +1156 -0
  55. package/driver/metadata/materialized-view.js +49 -0
  56. package/driver/metadata/schema-function.js +98 -0
  57. package/driver/metadata/schema-index.js +166 -0
  58. package/driver/metadata/schema-parser.js +1399 -0
  59. package/driver/metadata/table-metadata.js +77 -0
  60. package/driver/operation-state.js +206 -0
  61. package/driver/policies/address-resolution.js +145 -0
  62. package/driver/policies/index.d.ts +241 -0
  63. package/driver/policies/index.js +110 -0
  64. package/driver/policies/load-balancing.js +970 -0
  65. package/driver/policies/reconnection.js +166 -0
  66. package/driver/policies/retry.js +326 -0
  67. package/driver/policies/speculative-execution.js +150 -0
  68. package/driver/policies/timestamp-generation.js +176 -0
  69. package/driver/prepare-handler.js +347 -0
  70. package/driver/promise-utils.js +191 -0
  71. package/driver/readers.js +624 -0
  72. package/driver/request-execution.js +644 -0
  73. package/driver/request-handler.js +332 -0
  74. package/driver/requests.js +618 -0
  75. package/driver/stream-id-stack.js +209 -0
  76. package/driver/streams.js +745 -0
  77. package/driver/token.js +325 -0
  78. package/driver/tokenizer.js +631 -0
  79. package/driver/types/big-decimal.js +282 -0
  80. package/driver/types/duration.js +576 -0
  81. package/driver/types/index.d.ts +486 -0
  82. package/driver/types/index.js +733 -0
  83. package/driver/types/inet-address.js +262 -0
  84. package/driver/types/integer.js +818 -0
  85. package/driver/types/local-date.js +280 -0
  86. package/driver/types/local-time.js +299 -0
  87. package/driver/types/mutable-long.js +385 -0
  88. package/driver/types/protocol-version.js +391 -0
  89. package/driver/types/result-set.js +287 -0
  90. package/driver/types/result-stream.js +164 -0
  91. package/driver/types/row.js +85 -0
  92. package/driver/types/time-uuid.js +414 -0
  93. package/driver/types/tuple.js +103 -0
  94. package/driver/types/uuid.js +160 -0
  95. package/driver/types/vector.js +130 -0
  96. package/driver/types/version-number.js +153 -0
  97. package/driver/utils.js +1485 -0
  98. package/driver/writers.js +350 -0
  99. package/global.d.ts +1 -1
  100. package/global.d.ts.map +1 -1
  101. package/index.d.ts +6 -6
  102. package/index.d.ts.map +1 -1
  103. package/index.js +6 -6
  104. package/index.js.map +1 -1
  105. package/migrate/index.d.ts +1 -1
  106. package/migrate/index.d.ts.map +1 -1
  107. package/migrate/index.js +1 -1
  108. package/migrate/index.js.map +1 -1
  109. package/model/index.d.ts +6 -6
  110. package/model/index.d.ts.map +1 -1
  111. package/model/index.js +10 -10
  112. package/model/index.js.map +1 -1
  113. package/operations/countAll.d.ts +1 -1
  114. package/operations/countAll.d.ts.map +1 -1
  115. package/operations/delete.d.ts +3 -4
  116. package/operations/delete.d.ts.map +1 -1
  117. package/operations/delete.js +1 -1
  118. package/operations/delete.js.map +1 -1
  119. package/operations/find.d.ts +2 -2
  120. package/operations/find.d.ts.map +1 -1
  121. package/operations/find.js +1 -1
  122. package/operations/find.js.map +1 -1
  123. package/operations/findOne.d.ts +2 -2
  124. package/operations/findOne.d.ts.map +1 -1
  125. package/operations/findOne.js +1 -1
  126. package/operations/findOne.js.map +1 -1
  127. package/operations/insert.d.ts +3 -3
  128. package/operations/insert.d.ts.map +1 -1
  129. package/operations/insert.js +2 -2
  130. package/operations/insert.js.map +1 -1
  131. package/operations/sync.d.ts +1 -1
  132. package/operations/sync.d.ts.map +1 -1
  133. package/operations/sync.js +1 -1
  134. package/operations/sync.js.map +1 -1
  135. package/operations/tableExists.d.ts +1 -1
  136. package/operations/tableExists.d.ts.map +1 -1
  137. package/operations/update.d.ts +3 -3
  138. package/operations/update.d.ts.map +1 -1
  139. package/operations/update.js +2 -2
  140. package/operations/update.js.map +1 -1
  141. package/package.json +4 -12
  142. package/schema/index.d.ts +1 -1
  143. package/schema/index.d.ts.map +1 -1
  144. package/types.d.ts +4 -4
  145. package/types.d.ts.map +1 -1
  146. package/utils/queryParser.d.ts +1 -1
  147. package/utils/queryParser.d.ts.map +1 -1
  148. package/utils/queryParser.js +1 -1
  149. package/utils/queryParser.js.map +1 -1
  150. package/utils/typeChecker.d.ts +1 -1
  151. package/utils/typeChecker.d.ts.map +1 -1
  152. package/utils/typeChecker.js +1 -1
  153. package/utils/typeChecker.js.map +1 -1
@@ -0,0 +1,618 @@
1
+ /*
2
+ * Licensed to the Apache Software Foundation (ASF) under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. The ASF licenses this file
6
+ * to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ import util from "util"
20
+
21
+ import { FrameWriter } from "./writers.js"
22
+ import types from "./types/index.js"
23
+ import utils from "./utils.js"
24
+ import { ExecutionOptions } from "./execution-options.js"
25
+ import pkg from "../../package.json" with { type: "json" }
26
+
27
+ /**
28
+ * Options for the execution of the query / prepared statement
29
+ * @private
30
+ */
31
+ const queryFlag = {
32
+ values: 0x01,
33
+ skipMetadata: 0x02,
34
+ pageSize: 0x04,
35
+ withPagingState: 0x08,
36
+ withSerialConsistency: 0x10,
37
+ withDefaultTimestamp: 0x20,
38
+ withNameForValues: 0x40,
39
+ withKeyspace: 0x80,
40
+ withPageSizeBytes: 0x40000000,
41
+ withContinuousPaging: 0x80000000,
42
+ }
43
+
44
+ /**
45
+ * Options for the execution of a batch request from protocol v3 and above
46
+ * @private
47
+ */
48
+ const batchFlag = {
49
+ withSerialConsistency: 0x10,
50
+ withDefaultTimestamp: 0x20,
51
+ withNameForValues: 0x40,
52
+ withKeyspace: 0x80,
53
+ }
54
+
55
+ /**
56
+ * Options for execution of a prepare request from protocol DSE_V2 and above
57
+ * @private
58
+ */
59
+ const prepareFlag = {
60
+ withKeyspace: 0x01,
61
+ }
62
+
63
+ const batchType = {
64
+ logged: 0,
65
+ unlogged: 1,
66
+ counter: 2,
67
+ }
68
+
69
+ /**
70
+ * Abstract class Request
71
+ */
72
+ class Request {
73
+ constructor() {
74
+ this.length = 0
75
+ }
76
+
77
+ /**
78
+ * @abstract
79
+ * @param {Encoder} encoder
80
+ * @param {Number} streamId
81
+ * @throws {TypeError}
82
+ * @returns {Buffer}
83
+ */
84
+ write(encoder, streamId) {
85
+ throw new Error("Method must be implemented")
86
+ }
87
+
88
+ /**
89
+ * Creates a new instance using the same constructor as the current instance, copying the properties.
90
+ * @return {Request}
91
+ */
92
+ clone() {
93
+ const newRequest = new this.constructor()
94
+ const keysArray = Object.keys(this)
95
+ for (let i = 0; i < keysArray.length; i++) {
96
+ const key = keysArray[i]
97
+ newRequest[key] = this[key]
98
+ }
99
+ return newRequest
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Writes a execute query (given a prepared queryId)
105
+ * @param {String} query
106
+ * @param {Buffer} queryId
107
+ * @param {Array} params
108
+ * @param options
109
+ */
110
+ class ExecuteRequest extends Request {
111
+ /**
112
+ * @param {String} query
113
+ * @param queryId
114
+ * @param params
115
+ * @param {ExecutionOptions} execOptions
116
+ * @param meta
117
+ */
118
+ constructor(query, queryId, params, execOptions, meta) {
119
+ super()
120
+
121
+ this.query = query
122
+ this.queryId = queryId
123
+ this.params = params
124
+ this.meta = meta
125
+ this.options = execOptions || ExecutionOptions.empty()
126
+ this.timestamp = this.options.getOrGenerateTimestamp()
127
+ this.consistency =
128
+ this.options.getConsistency() || types.consistencies.one
129
+ // Only QUERY request parameters are encoded as named parameters
130
+ // EXECUTE request parameters are always encoded as positional parameters
131
+ this.namedParameters = false
132
+ }
133
+
134
+ getParamType(index) {
135
+ const columnInfo = this.meta.columns[index]
136
+ return columnInfo ? columnInfo.type : null
137
+ }
138
+
139
+ write(encoder, streamId) {
140
+ //v1: <queryId>
141
+ // <n><value_1>....<value_n><consistency>
142
+ //v2: <queryId>
143
+ // <consistency><flags>[<n><value_1>...<value_n>][<result_page_size>][<paging_state>][<serial_consistency>]
144
+ //v3: <queryId>
145
+ // <consistency><flags>[<n>[name_1]<value_1>...[name_n]<value_n>][<result_page_size>][<paging_state>][<serial_consistency>][<timestamp>]
146
+ const frameWriter = new FrameWriter(types.opcodes.execute)
147
+ let headerFlags = this.options.isQueryTracing()
148
+ ? types.frameFlags.tracing
149
+ : 0
150
+ if (this.options.getCustomPayload()) {
151
+ //The body may contain the custom payload
152
+ headerFlags |= types.frameFlags.customPayload
153
+ frameWriter.writeCustomPayload(this.options.getCustomPayload())
154
+ }
155
+ frameWriter.writeShortBytes(this.queryId)
156
+ if (
157
+ types.protocolVersion.supportsResultMetadataId(
158
+ encoder.protocolVersion,
159
+ )
160
+ ) {
161
+ frameWriter.writeShortBytes(this.meta.resultId)
162
+ }
163
+ this.writeQueryParameters(frameWriter, encoder)
164
+
165
+ // Record the length of the body of the request before writing it
166
+ this.length = frameWriter.bodyLength
167
+
168
+ return frameWriter.write(encoder.protocolVersion, streamId, headerFlags)
169
+ }
170
+
171
+ /**
172
+ * Writes v1 and v2 execute query parameters
173
+ * @param {FrameWriter} frameWriter
174
+ * @param {Encoder} encoder
175
+ * @param {Boolean} [isQuery] True if query, otherwise assumed to be execute request.
176
+ */
177
+ writeQueryParameters(frameWriter, encoder, isQuery) {
178
+ //v1: <n><value_1>....<value_n><consistency>
179
+ //v2: <consistency><flags>[<n><value_1>...<value_n>][<result_page_size>][<paging_state>][<serial_consistency>]
180
+ //v3: <consistency><flags>[<n>[name_1]<value_1>...[name_n]<value_n>][<result_page_size>][<paging_state>][<serial_consistency>][<timestamp>]
181
+ //dse_v1: <consistency><flags>[<n>[name_1]<value_1>...[name_n]<value_n>][<result_page_size>][<paging_state>]
182
+ // [<serial_consistency>][<timestamp>][continuous_paging_options]
183
+ //dse_v2: <consistency><flags>[<n>[name_1]<value_1>...[name_n]<value_n>][<result_page_size>][<paging_state>]
184
+ // [<serial_consistency>][<timestamp>][keyspace][continuous_paging_options]
185
+ let flags = 0
186
+
187
+ const timestamp = this.timestamp
188
+
189
+ if (types.protocolVersion.supportsPaging(encoder.protocolVersion)) {
190
+ flags |= this.params && this.params.length ? queryFlag.values : 0
191
+ flags |= this.options.getFetchSize() > 0 ? queryFlag.pageSize : 0
192
+ flags |= this.options.getPageState() ? queryFlag.withPagingState : 0
193
+ flags |= this.options.getSerialConsistency()
194
+ ? queryFlag.withSerialConsistency
195
+ : 0
196
+ flags |=
197
+ timestamp !== null && timestamp !== undefined
198
+ ? queryFlag.withDefaultTimestamp
199
+ : 0
200
+ flags |= this.namedParameters ? queryFlag.withNameForValues : 0
201
+
202
+ // Don't inject keyspace for EXECUTE requests as inherited from prepared statement.
203
+ const supportsKeyspace =
204
+ isQuery &&
205
+ types.protocolVersion.supportsKeyspaceInRequest(
206
+ encoder.protocolVersion,
207
+ )
208
+ flags |=
209
+ supportsKeyspace && this.options.getKeyspace()
210
+ ? queryFlag.withKeyspace
211
+ : 0
212
+
213
+ frameWriter.writeShort(this.consistency)
214
+ if (
215
+ types.protocolVersion.uses4BytesQueryFlags(
216
+ encoder.protocolVersion,
217
+ )
218
+ ) {
219
+ frameWriter.writeInt(flags)
220
+ } else {
221
+ frameWriter.writeByte(flags)
222
+ }
223
+ }
224
+
225
+ if (this.params && this.params.length) {
226
+ frameWriter.writeShort(this.params.length)
227
+ for (let i = 0; i < this.params.length; i++) {
228
+ let paramValue = this.params[i]
229
+ if (flags & queryFlag.withNameForValues) {
230
+ //parameter is composed by name / value
231
+ frameWriter.writeString(paramValue.name)
232
+ paramValue = paramValue.value
233
+ }
234
+ frameWriter.writeBytes(
235
+ encoder.encode(paramValue, this.getParamType(i)),
236
+ )
237
+ }
238
+ }
239
+
240
+ if (!types.protocolVersion.supportsPaging(encoder.protocolVersion)) {
241
+ if (!this.params || !this.params.length) {
242
+ //zero parameters
243
+ frameWriter.writeShort(0)
244
+ }
245
+ frameWriter.writeShort(this.consistency)
246
+ return
247
+ }
248
+ if (flags & queryFlag.pageSize) {
249
+ frameWriter.writeInt(this.options.getFetchSize())
250
+ }
251
+ if (flags & queryFlag.withPagingState) {
252
+ frameWriter.writeBytes(this.options.getPageState())
253
+ }
254
+ if (flags & queryFlag.withSerialConsistency) {
255
+ frameWriter.writeShort(this.options.getSerialConsistency())
256
+ }
257
+ if (flags & queryFlag.withDefaultTimestamp) {
258
+ frameWriter.writeLong(timestamp)
259
+ }
260
+ if (flags & queryFlag.withKeyspace) {
261
+ frameWriter.writeString(this.options.getKeyspace())
262
+ }
263
+ }
264
+ }
265
+
266
+ class QueryRequest extends ExecuteRequest {
267
+ /**
268
+ * @param {String} query
269
+ * @param params
270
+ * @param {ExecutionOptions} [execOptions]
271
+ * @param {Boolean} [namedParameters]
272
+ */
273
+ constructor(query, params, execOptions, namedParameters) {
274
+ super(query, null, params, execOptions, null)
275
+ this.hints = this.options.getHints() || utils.emptyArray
276
+ this.namedParameters = namedParameters
277
+ }
278
+
279
+ getParamType(index) {
280
+ return this.hints[index]
281
+ }
282
+
283
+ write(encoder, streamId) {
284
+ //v1: <query><consistency>
285
+ //v2: <query>
286
+ // <consistency><flags>[<n><value_1>...<value_n>][<result_page_size>][<paging_state>][<serial_consistency>]
287
+ //v3: <query>
288
+ // <consistency><flags>[<n>[name_1]<value_1>...[name_n]<value_n>][<result_page_size>][<paging_state>][<serial_consistency>][<timestamp>]
289
+ const frameWriter = new FrameWriter(types.opcodes.query)
290
+ let headerFlags = this.options.isQueryTracing()
291
+ ? types.frameFlags.tracing
292
+ : 0
293
+ if (this.options.getCustomPayload()) {
294
+ //The body may contain the custom payload
295
+ headerFlags |= types.frameFlags.customPayload
296
+ frameWriter.writeCustomPayload(this.options.getCustomPayload())
297
+ }
298
+
299
+ frameWriter.writeLString(this.query)
300
+
301
+ if (!types.protocolVersion.supportsPaging(encoder.protocolVersion)) {
302
+ frameWriter.writeShort(this.consistency)
303
+ } else {
304
+ //Use the same fields as the execute writer
305
+ this.writeQueryParameters(frameWriter, encoder, true)
306
+ }
307
+
308
+ // Record the length of the body of the request before writing it
309
+ this.length = frameWriter.bodyLength
310
+
311
+ return frameWriter.write(encoder.protocolVersion, streamId, headerFlags)
312
+ }
313
+ }
314
+
315
+ class PrepareRequest extends Request {
316
+ constructor(query, keyspace) {
317
+ super()
318
+ this.query = query
319
+ this.keyspace = keyspace
320
+ }
321
+
322
+ write(encoder, streamId) {
323
+ const frameWriter = new FrameWriter(types.opcodes.prepare)
324
+ frameWriter.writeLString(this.query)
325
+ if (
326
+ types.protocolVersion.supportsPrepareFlags(encoder.protocolVersion)
327
+ ) {
328
+ const flags =
329
+ this.keyspace &&
330
+ types.protocolVersion.supportsKeyspaceInRequest(
331
+ encoder.protocolVersion,
332
+ )
333
+ ? prepareFlag.withKeyspace
334
+ : 0
335
+ frameWriter.writeInt(flags)
336
+ if (flags & prepareFlag.withKeyspace) {
337
+ frameWriter.writeString(this.keyspace)
338
+ }
339
+ }
340
+ return frameWriter.write(encoder.protocolVersion, streamId)
341
+ }
342
+ }
343
+
344
+ class StartupRequest extends Request {
345
+ /**
346
+ * Creates a new instance of {@link StartupRequest}.
347
+ * @param {Object} [options]
348
+ * @param [options.cqlVersion]
349
+ * @param [options.noCompact]
350
+ * @param [options.clientId]
351
+ * @param [options.applicationName]
352
+ * @param [options.applicationVersion]
353
+ */
354
+ constructor(options) {
355
+ super()
356
+ this.options = options || {}
357
+ }
358
+
359
+ write(encoder, streamId) {
360
+ const frameWriter = new FrameWriter(types.opcodes.startup)
361
+
362
+ const startupOptions = {
363
+ CQL_VERSION: this.options.cqlVersion || "3.0.0",
364
+ DRIVER_NAME: pkg.description,
365
+ DRIVER_VERSION: pkg.version,
366
+ }
367
+
368
+ if (this.options.noCompact) {
369
+ startupOptions["NO_COMPACT"] = "true"
370
+ }
371
+
372
+ if (this.options.clientId) {
373
+ startupOptions["CLIENT_ID"] = this.options.clientId.toString()
374
+ }
375
+
376
+ if (this.options.applicationName) {
377
+ startupOptions["APPLICATION_NAME"] = this.options.applicationName
378
+ }
379
+
380
+ if (this.options.applicationVersion) {
381
+ startupOptions["APPLICATION_VERSION"] =
382
+ this.options.applicationVersion
383
+ }
384
+
385
+ frameWriter.writeStringMap(startupOptions)
386
+ return frameWriter.write(encoder.protocolVersion, streamId)
387
+ }
388
+ }
389
+
390
+ class RegisterRequest extends Request {
391
+ constructor(events) {
392
+ super()
393
+ this.events = events
394
+ }
395
+
396
+ write(encoder, streamId) {
397
+ const frameWriter = new FrameWriter(types.opcodes.register)
398
+ frameWriter.writeStringList(this.events)
399
+ return frameWriter.write(encoder.protocolVersion, streamId)
400
+ }
401
+ }
402
+
403
+ /**
404
+ * Represents an AUTH_RESPONSE request
405
+ * @param {Buffer} token
406
+ */
407
+ class AuthResponseRequest extends Request {
408
+ constructor(token) {
409
+ super()
410
+ this.token = token
411
+ }
412
+
413
+ write(encoder, streamId) {
414
+ const frameWriter = new FrameWriter(types.opcodes.authResponse)
415
+ frameWriter.writeBytes(this.token)
416
+ return frameWriter.write(encoder.protocolVersion, streamId)
417
+ }
418
+ }
419
+
420
+ /**
421
+ * Represents a protocol v1 CREDENTIALS request message
422
+ */
423
+ class CredentialsRequest extends Request {
424
+ constructor(username, password) {
425
+ super()
426
+ this.username = username
427
+ this.password = password
428
+ }
429
+
430
+ write(encoder, streamId) {
431
+ const frameWriter = new FrameWriter(types.opcodes.credentials)
432
+ frameWriter.writeStringMap({
433
+ username: this.username,
434
+ password: this.password,
435
+ })
436
+ return frameWriter.write(encoder.protocolVersion, streamId)
437
+ }
438
+ }
439
+
440
+ class BatchRequest extends Request {
441
+ /**
442
+ * Creates a new instance of BatchRequest.
443
+ * @param {Array.<{query, params, [info]}>} queries Array of objects with the properties query and params
444
+ * @param {ExecutionOptions} execOptions
445
+ */
446
+ constructor(queries, execOptions) {
447
+ super()
448
+ this.queries = queries
449
+ this.options = execOptions
450
+ this.timestamp = this.options.getOrGenerateTimestamp()
451
+ this.hints = execOptions.getHints() || utils.emptyArray
452
+ this.type = batchType.logged
453
+
454
+ if (execOptions.isBatchCounter()) {
455
+ this.type = batchType.counter
456
+ } else if (!execOptions.isBatchLogged()) {
457
+ this.type = batchType.unlogged
458
+ }
459
+ }
460
+
461
+ /**
462
+ * Writes a batch request
463
+ */
464
+ write(encoder, streamId) {
465
+ //v2: <type><n><query_1>...<query_n><consistency>
466
+ //v3: <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>]
467
+ //dseV1+: similar to v3/v4, flags is an int instead of a byte
468
+ if (!this.queries || !(this.queries.length > 0)) {
469
+ throw new TypeError(
470
+ util.format("Invalid queries provided %s", this.queries),
471
+ )
472
+ }
473
+ const frameWriter = new FrameWriter(types.opcodes.batch)
474
+ let headerFlags = this.options.isQueryTracing()
475
+ ? types.frameFlags.tracing
476
+ : 0
477
+ if (this.options.getCustomPayload()) {
478
+ //The body may contain the custom payload
479
+ headerFlags |= types.frameFlags.customPayload
480
+ frameWriter.writeCustomPayload(this.options.getCustomPayload())
481
+ }
482
+ frameWriter.writeByte(this.type)
483
+ frameWriter.writeShort(this.queries.length)
484
+ const self = this
485
+ this.queries.forEach(function eachQuery(item, i) {
486
+ const hints = self.hints[i]
487
+ const params = item.params || utils.emptyArray
488
+ let getParamType
489
+ if (item.queryId) {
490
+ // Contains prepared queries
491
+ frameWriter.writeByte(1)
492
+ frameWriter.writeShortBytes(item.queryId)
493
+ getParamType = (i) => item.meta.columns[i].type
494
+ } else {
495
+ // Contains string queries
496
+ frameWriter.writeByte(0)
497
+ frameWriter.writeLString(item.query)
498
+ getParamType = hints ? (i) => hints[i] : () => null
499
+ }
500
+
501
+ frameWriter.writeShort(params.length)
502
+ params.forEach((param, index) =>
503
+ frameWriter.writeBytes(
504
+ encoder.encode(param, getParamType(index)),
505
+ ),
506
+ )
507
+ }, this)
508
+
509
+ frameWriter.writeShort(this.options.getConsistency())
510
+
511
+ if (types.protocolVersion.supportsTimestamp(encoder.protocolVersion)) {
512
+ // Batch flags
513
+ let flags = this.options.getSerialConsistency()
514
+ ? batchFlag.withSerialConsistency
515
+ : 0
516
+ const timestamp = this.timestamp
517
+ flags |=
518
+ timestamp !== null && timestamp !== undefined
519
+ ? batchFlag.withDefaultTimestamp
520
+ : 0
521
+
522
+ flags |=
523
+ this.options.getKeyspace() &&
524
+ types.protocolVersion.supportsKeyspaceInRequest(
525
+ encoder.protocolVersion,
526
+ )
527
+ ? batchFlag.withKeyspace
528
+ : 0
529
+
530
+ if (
531
+ types.protocolVersion.uses4BytesQueryFlags(
532
+ encoder.protocolVersion,
533
+ )
534
+ ) {
535
+ frameWriter.writeInt(flags)
536
+ } else {
537
+ frameWriter.writeByte(flags)
538
+ }
539
+
540
+ if (flags & batchFlag.withSerialConsistency) {
541
+ frameWriter.writeShort(this.options.getSerialConsistency())
542
+ }
543
+
544
+ if (flags & batchFlag.withDefaultTimestamp) {
545
+ frameWriter.writeLong(timestamp)
546
+ }
547
+
548
+ if (flags & batchFlag.withKeyspace) {
549
+ frameWriter.writeString(this.options.getKeyspace())
550
+ }
551
+ }
552
+
553
+ // Set the length of the body of the request before writing it
554
+ this.length = frameWriter.bodyLength
555
+
556
+ return frameWriter.write(encoder.protocolVersion, streamId, headerFlags)
557
+ }
558
+
559
+ clone() {
560
+ return new BatchRequest(this.queries, this.options)
561
+ }
562
+ }
563
+
564
+ function CancelRequest(operationId) {
565
+ this.streamId = null
566
+ this.operationId = operationId
567
+ }
568
+
569
+ util.inherits(CancelRequest, Request)
570
+
571
+ CancelRequest.prototype.write = function (encoder, streamId) {
572
+ const frameWriter = new FrameWriter(types.opcodes.cancel)
573
+ frameWriter.writeInt(1)
574
+ frameWriter.writeInt(this.operationId)
575
+ return frameWriter.write(encoder.protocolVersion, streamId)
576
+ }
577
+
578
+ class OptionsRequest extends Request {
579
+ write(encoder, streamId) {
580
+ const frameWriter = new FrameWriter(types.opcodes.options)
581
+ return frameWriter.write(encoder.protocolVersion, streamId, 0)
582
+ }
583
+
584
+ clone() {
585
+ // since options has no unique state, simply return self.
586
+ return this
587
+ }
588
+ }
589
+
590
+ const options = new OptionsRequest()
591
+
592
+ export {
593
+ AuthResponseRequest,
594
+ BatchRequest,
595
+ CancelRequest,
596
+ CredentialsRequest,
597
+ ExecuteRequest,
598
+ options,
599
+ PrepareRequest,
600
+ QueryRequest,
601
+ RegisterRequest,
602
+ Request,
603
+ StartupRequest,
604
+ }
605
+
606
+ export default {
607
+ AuthResponseRequest,
608
+ BatchRequest,
609
+ CancelRequest,
610
+ CredentialsRequest,
611
+ ExecuteRequest,
612
+ options,
613
+ PrepareRequest,
614
+ QueryRequest,
615
+ RegisterRequest,
616
+ Request,
617
+ StartupRequest,
618
+ }