Skip to content

Commit

Permalink
Inline and fix Flow
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Dec 4, 2020
1 parent ce48940 commit fa40419
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
48 changes: 20 additions & 28 deletions packages/react-pg/src/ReactPostgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,38 +74,30 @@ export function Pool(options: mixed) {
};
}

function getInnerMap(
outerMap: Map<string, mixed>,
query: string,
values?: Array<mixed>,
) {
if (values == null || values.length === 0) {
return [outerMap, query];
}
// If we have parameters, each becomes as a nesting layer for Maps.
// We want to find (or create as needed) the innermost Map, and return that.
let innerMap = outerMap;
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 key = query;
for (let i = 0; i < values.length; i++) {
let nextMap = innerMap.get(key);
if (nextMap === undefined) {
nextMap = new Map();
innerMap.set(key, nextMap);
if (values != null) {
// If we have parameters, each becomes as a nesting layer for Maps.
// We want to find (or create as needed) the innermost Map, and return that.
for (let i = 0; i < values.length; i++) {
let nextMap = innerMap.get(key);
if (nextMap === undefined) {
nextMap = new Map();
innerMap.set(key, nextMap);
}
innerMap = nextMap;
// Postgres bindings convert everything to strings:
// https://node-postgres.com/features/queries#parameterized-query
// We reuse their algorithm instead of reimplementing.
key = prepareValue(values[i]);
}
innerMap = nextMap;
// Postgres bindings convert everything to strings:
// https://node-postgres.com/features/queries#parameterized-query
// We reuse their algorithm instead of reimplementing.
key = prepareValue(values[i]);
}
return [innerMap, key];
}

Pool.prototype.query = function(query: string, values?: Array<mixed>) {
const pool = this.pool;
const outerMap = unstable_getCacheForType(this.createResultMap);
const [innerMap, key] = getInnerMap(outerMap, query, values);
let entry = innerMap.get(key);
let entry: Result | void = innerMap.get(key);
if (!entry) {
const thenable = pool.query(query, values);
entry = toResult(thenable);
Expand Down
6 changes: 6 additions & 0 deletions scripts/flow/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,9 @@ declare module 'pg' {
query: (query: string, values?: Array<mixed>) => void,
};
}

declare module 'pg/lib/utils' {
declare module.exports: {
prepareValue(val: any): mixed,
};
}

0 comments on commit fa40419

Please sign in to comment.