Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an error for query parameter count mismatch #20379

Merged
merged 1 commit into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/react-pg/src/ReactPostgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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);
};
4 changes: 3 additions & 1 deletion scripts/error-codes/codes.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}