@samitouri / QOS-React-2 / commits / 9b8060041b

Error when the number of parameters to a query changes (#20379)

Dan Abramov committed Dec 4, 2020 at 20:11 UTC 9b8060041b4dec3388c448f909e3d6387edefa69
2 files changed +22 -3
packages/react-pg/src/ReactPostgres.js
+19 -2
@@ -12,6 +12,7 @@ import type {Wakeable} from 'shared/ReactTypes';
12 import {unstable_getCacheForType} from 'react';
13 import {Pool as PostgresPool} from 'pg';
14 import {prepareValue} from 'pg/lib/utils';
15 +import invariant from 'shared/invariant';
16
17 const Pending = 0;
18 const Resolved = 1;
@@ -74,11 +75,13 @@ export function Pool(options: mixed) {
75 };
76 }
77
78 +type NestedMap = Map<any, Result | NestedMap>;
79 +
80 Pool.prototype.query = function(query: string, values?: Array<mixed>) {
81 const pool = this.pool;
82 const outerMap = unstable_getCacheForType(this.createResultMap);
83
81 - let innerMap: Map<any, any> = outerMap;
84 + let innerMap: NestedMap = outerMap;
85 let key = query;
86 if (values != null) {
87 // If we have parameters, each becomes as a nesting layer for Maps.
@@ -88,6 +91,13 @@ Pool.prototype.query = function(query: string, values?: Array<mixed>) {
91 if (nextMap === undefined) {
92 nextMap = new Map();
93 innerMap.set(key, nextMap);
94 + } else if (!(nextMap instanceof Map)) {
95 + invariant(
96 + false,
97 + 'This query has received more parameters than the last time ' +
98 + 'the same query was used. Always pass the exact number of ' +
99 + 'parameters that the query needs.',
100 + );
101 }
102 innerMap = nextMap;
103 // Postgres bindings convert everything to strings:
@@ -97,11 +107,18 @@ Pool.prototype.query = function(query: string, values?: Array<mixed>) {
107 }
108 }
109
100 - let entry: Result | void = innerMap.get(key);
110 + let entry = innerMap.get(key);
111 if (!entry) {
112 const thenable = pool.query(query, values);
113 entry = toResult(thenable);
114 innerMap.set(key, entry);
115 + } else if (entry instanceof Map) {
116 + invariant(
117 + false,
118 + 'This query has received fewer parameters than the last time ' +
119 + 'the same query was used. Always pass the exact number of ' +
120 + 'parameters that the query needs.',
121 + );
122 }
123 return readResult(entry);
124 };
scripts/error-codes/codes.json
+3 -1
@@ -369,5 +369,7 @@
369 "378": "Type %s is not supported in client component props. Remove %s from this object, or avoid the entire object: %s",
370 "379": "Refs cannot be used in server components, nor passed to client components.",
371 "380": "Reading the cache is only supported while rendering.",
372 - "381": "This feature is not supported by ReactSuspenseTestUtils."
372 + "381": "This feature is not supported by ReactSuspenseTestUtils.",
373 + "382": "This query has received more parameters than the last time the same query was used. Always pass the exact number of parameters that the query needs.",
374 + "383": "This query has received fewer parameters than the last time the same query was used. Always pass the exact number of parameters that the query needs."
375 }