Skip to content

Commit 71085f0

Browse files
authored
Fix backward compatibility bug introduced in 4.5.3 that causes query to return a differnt type (#633)
* Fix backward compatibility bug introduced in 4.5.3 that causes query to return a differnt type * Update CHANGELOG.md * remove metrics from queryoptions type * Fix client test with invalid metrics flag * Fix test * Fix test * Deprecate 4.5.3
1 parent 20e66a4 commit 71085f0

File tree

8 files changed

+27
-31
lines changed

8 files changed

+27
-31
lines changed

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 4.5.4
2+
- Disable ability to configure a client and the query method from returning metrics when calling query - fixing bug introduced in 4.5.3 that breaks backward compatibility. Continue supporting queryWithMetrics. [#633](https://github.com/fauna/faunadb-js/pull/633).
3+
14
## 4.5.3
25
- Enable the client to return metrics on queries [#625](https://github.com/fauna/faunadb-js/pull/625) [#628](https://github.com/fauna/faunadb-js/pull/628)
36

Diff for: README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ let client = new faunadb.Client({
137137
})
138138
```
139139

140-
The `response` object is shaped differently for clients when the `metrics` option
141-
is/not set. The default client setting returns the `response` body at root level.
142-
When the client is configured to return `metrics`, the `response` object is
143-
structured as follows:
140+
#### Querying and Returning the metrics of your queries
141+
142+
The `response` object is shaped differently for clients when calling `queryWithMetrics`;
143+
it includes the value of the response along with a metrics field giving data on ops,
144+
time, and transaction retires consumed by your query:
144145

145146
```javascript
146147
{

Diff for: concourse/scripts/publish.sh

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ then
1919
echo "Publishing a new version..."
2020
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc
2121
npm publish
22+
npm deprecate [email protected] "4.5.3 is is deprecated as it contains a bug that changed the type returned by query for typescript users"
2223
rm .npmrc
2324

2425
echo "faunadb-js@$PACKAGE_VERSION published to npm" > ../slack-message/publish

Diff for: package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "faunadb",
3-
"version": "4.5.3",
3+
"version": "4.5.4",
44
"apiVersion": "4",
55
"description": "FaunaDB Javascript driver for Node.JS and Browsers",
66
"homepage": "https://fauna.com",

Diff for: src/Client.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ function Client(options) {
184184
queryTimeout: null,
185185
http2SessionIdleTime: http2SessionIdleTime.value,
186186
checkNewVersion: false,
187-
metrics: false,
188187
})
189188

190189
if (http2SessionIdleTime.shouldOverride) {
@@ -194,7 +193,6 @@ function Client(options) {
194193
this._observer = options.observer
195194
this._http = new http.HttpClient(options)
196195
this.stream = stream.StreamAPI(this)
197-
this._globalQueryOptions = { metrics: options.metrics }
198196
}
199197

200198
/**
@@ -298,13 +296,10 @@ Client.prototype.close = function(opts) {
298296
* @return {external:Promise<Object>} {value, metrics} An object containing the FaunaDB response object and the list of query metrics incurred by the request.
299297
*/
300298
Client.prototype.queryWithMetrics = function(expression, options) {
301-
options = Object.assign({}, this._globalQueryOptions, options, {
302-
metrics: true,
303-
})
304-
return this._execute('POST', '', query.wrap(expression), null, options)
299+
return this._execute('POST', '', query.wrap(expression), null, options, true)
305300
}
306301

307-
Client.prototype._execute = function(method, path, data, query, options) {
302+
Client.prototype._execute = function(method, path, data, query, options, returnMetrics = false) {
308303
query = util.defaults(query, null)
309304

310305
if (
@@ -358,7 +353,7 @@ Client.prototype._execute = function(method, path, data, query, options) {
358353
'x-txn-retries',
359354
]
360355

361-
if (options && options.metrics) {
356+
if (returnMetrics) {
362357
return {
363358
value: responseObject['resource'],
364359
metrics: Object.fromEntries(Array.from(Object.entries(response.headers)).

Diff for: src/types/Client.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface QueryOptions
3131
extends Partial<
3232
Pick<
3333
ClientConfig,
34-
'secret' | 'queryTimeout' | 'fetch' | 'observer' | 'metrics'
34+
'secret' | 'queryTimeout' | 'fetch' | 'observer'
3535
>
3636
> {
3737
signal?: AbortSignal
@@ -62,7 +62,7 @@ interface MetricsResponse<T = object> {
6262

6363
export default class Client {
6464
constructor(opts?: ClientConfig)
65-
query<T = object>(expr: ExprArg, options?: QueryOptions): Promise<T> | Promise<MetricsResponse<T>>
65+
query<T = object>(expr: ExprArg, options?: QueryOptions): Promise<T>
6666
queryWithMetrics<T = object>(
6767
expr: ExprArg,
6868
options?: QueryOptions

Diff for: test/client.test.js

+10-14
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,31 @@ describe('Client', () => {
4040
expect(client._http._baseUrl.endsWith(':0')).toBeFalsy()
4141
})
4242

43-
test('returns query metrics if the metrics flag is set', async () => {
44-
var metricsClient = util.getClient({ metrics: true })
45-
const response = await metricsClient.query(query.Add(1, 1))
46-
assertMetric(response.metrics, 'x-compute-ops')
47-
assertMetric(response.metrics, 'x-byte-read-ops')
48-
assertMetric(response.metrics, 'x-byte-write-ops')
49-
assertMetric(response.metrics, 'x-query-time')
50-
assertMetric(response.metrics, 'x-txn-retries')
43+
test('the client does not support a metrics flag', async () => {
44+
expect(() => util.getClient({ metrics: true })).toThrow(new Error('No such option metrics'))
5145
})
5246

53-
test('returns query metrics if using the queryWithMetrics function', async () => {
47+
test('query does not support a metrics flag', async () => {
48+
const response = await client.query(query.Add(1, 1))
49+
expect(response).toEqual(2)
50+
})
51+
52+
test('queryWithMetrics returns the metrics and the response value', async () => {
5453
const response = await client.queryWithMetrics(query.Add(1, 1))
54+
expect(response.value).toEqual(2)
5555
assertMetric(response.metrics, 'x-compute-ops')
5656
assertMetric(response.metrics, 'x-byte-read-ops')
5757
assertMetric(response.metrics, 'x-byte-write-ops')
5858
assertMetric(response.metrics, 'x-query-time')
5959
assertMetric(response.metrics, 'x-txn-retries')
6060
})
6161

62-
test('query metrics response has correct structure', async () => {
62+
test('queryWithMetrics returns the metrics', async () => {
6363
const response = await client.queryWithMetrics(query.Add(1, 1))
6464
expect(Object.keys(response).sort()).
6565
toEqual(['metrics', 'value'])
6666
})
6767

68-
test('client with metrics returns expected value', async () => {
69-
const response = await client.queryWithMetrics(query.Add(1, 1))
70-
expect(response.value).toEqual(2)
71-
})
7268

7369
test('paginates', () => {
7470
return createDocument().then(function(document) {

0 commit comments

Comments
 (0)