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

This commit is contained in:
Dan Abramov
2020-12-04 20:11:00 +00:00
committed by GitHub
parent 60e4a76fa8
commit 9b8060041b
2 changed files with 22 additions and 3 deletions

View File

@@ -12,6 +12,7 @@ import type {Wakeable} from 'shared/ReactTypes';
import {unstable_getCacheForType} from 'react';
import {Pool as PostgresPool} from 'pg';
import {prepareValue} from 'pg/lib/utils';
import invariant from 'shared/invariant';
const Pending = 0;
const Resolved = 1;
@@ -74,11 +75,13 @@ export function Pool(options: mixed) {
};
}
type NestedMap = Map<any, Result | NestedMap>;
Pool.prototype.query = function(query: string, values?: Array<mixed>) {
const pool = this.pool;
const outerMap = unstable_getCacheForType(this.createResultMap);
let innerMap: Map<any, any> = outerMap;
let innerMap: NestedMap = outerMap;
let key = query;
if (values != null) {
// 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>) {
if (nextMap === undefined) {
nextMap = new Map();
innerMap.set(key, nextMap);
} else if (!(nextMap instanceof Map)) {
invariant(
false,
'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.',
);
}
innerMap = nextMap;
// Postgres bindings convert everything to strings:
@@ -97,11 +107,18 @@ Pool.prototype.query = function(query: string, values?: Array<mixed>) {
}
}
let entry: Result | void = innerMap.get(key);
let entry = innerMap.get(key);
if (!entry) {
const thenable = pool.query(query, values);
entry = toResult(thenable);
innerMap.set(key, entry);
} else if (entry instanceof Map) {
invariant(
false,
'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.',
);
}
return readResult(entry);
};

View File

@@ -369,5 +369,7 @@
"378": "Type %s is not supported in client component props. Remove %s from this object, or avoid the entire object: %s",
"379": "Refs cannot be used in server components, nor passed to client components.",
"380": "Reading the cache is only supported while rendering.",
"381": "This feature is not supported by ReactSuspenseTestUtils."
"381": "This feature is not supported by ReactSuspenseTestUtils.",
"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.",
"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."
}