Skip to content

Commit

Permalink
Error if the number of parameters changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Dec 4, 2020
1 parent 60e4a76 commit 48fa205
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
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 compared to ' +
'the last time the same query was used. Always pass the exact number of ' +
'parameters that the query needs, every time you use it.',
);
}
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 compared to ' +
'the last time the same query was used. Always pass the exact number of ' +
'parameters that the query needs, every time you use it.',
);
}
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 compared to the last time the same query was used. Always pass the exact number of parameters that the query needs, every time you use it.",
"383": "This query has received fewer parameters compared to the last time the same query was used. Always pass the exact number of parameters that the query needs, every time you use it."
}

0 comments on commit 48fa205

Please sign in to comment.