Skip to content

Commit 9b80600

Browse files
authored
Error when the number of parameters to a query changes (#20379)
1 parent 60e4a76 commit 9b80600

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

packages/react-pg/src/ReactPostgres.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {Wakeable} from 'shared/ReactTypes';
1212
import {unstable_getCacheForType} from 'react';
1313
import {Pool as PostgresPool} from 'pg';
1414
import {prepareValue} from 'pg/lib/utils';
15+
import invariant from 'shared/invariant';
1516

1617
const Pending = 0;
1718
const Resolved = 1;
@@ -74,11 +75,13 @@ export function Pool(options: mixed) {
7475
};
7576
}
7677

78+
type NestedMap = Map<any, Result | NestedMap>;
79+
7780
Pool.prototype.query = function(query: string, values?: Array<mixed>) {
7881
const pool = this.pool;
7982
const outerMap = unstable_getCacheForType(this.createResultMap);
8083

81-
let innerMap: Map<any, any> = outerMap;
84+
let innerMap: NestedMap = outerMap;
8285
let key = query;
8386
if (values != null) {
8487
// 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>) {
8891
if (nextMap === undefined) {
8992
nextMap = new Map();
9093
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+
);
91101
}
92102
innerMap = nextMap;
93103
// Postgres bindings convert everything to strings:
@@ -97,11 +107,18 @@ Pool.prototype.query = function(query: string, values?: Array<mixed>) {
97107
}
98108
}
99109

100-
let entry: Result | void = innerMap.get(key);
110+
let entry = innerMap.get(key);
101111
if (!entry) {
102112
const thenable = pool.query(query, values);
103113
entry = toResult(thenable);
104114
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+
);
105122
}
106123
return readResult(entry);
107124
};

scripts/error-codes/codes.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,5 +369,7 @@
369369
"378": "Type %s is not supported in client component props. Remove %s from this object, or avoid the entire object: %s",
370370
"379": "Refs cannot be used in server components, nor passed to client components.",
371371
"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."
373375
}

0 commit comments

Comments
 (0)