Skip to content

Commit fa40419

Browse files
committed
Inline and fix Flow
1 parent ce48940 commit fa40419

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

packages/react-pg/src/ReactPostgres.js

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,38 +74,30 @@ export function Pool(options: mixed) {
7474
};
7575
}
7676

77-
function getInnerMap(
78-
outerMap: Map<string, mixed>,
79-
query: string,
80-
values?: Array<mixed>,
81-
) {
82-
if (values == null || values.length === 0) {
83-
return [outerMap, query];
84-
}
85-
// If we have parameters, each becomes as a nesting layer for Maps.
86-
// We want to find (or create as needed) the innermost Map, and return that.
87-
let innerMap = outerMap;
77+
Pool.prototype.query = function(query: string, values?: Array<mixed>) {
78+
const pool = this.pool;
79+
const outerMap = unstable_getCacheForType(this.createResultMap);
80+
81+
let innerMap: Map<any, any> = outerMap;
8882
let key = query;
89-
for (let i = 0; i < values.length; i++) {
90-
let nextMap = innerMap.get(key);
91-
if (nextMap === undefined) {
92-
nextMap = new Map();
93-
innerMap.set(key, nextMap);
83+
if (values != null) {
84+
// If we have parameters, each becomes as a nesting layer for Maps.
85+
// We want to find (or create as needed) the innermost Map, and return that.
86+
for (let i = 0; i < values.length; i++) {
87+
let nextMap = innerMap.get(key);
88+
if (nextMap === undefined) {
89+
nextMap = new Map();
90+
innerMap.set(key, nextMap);
91+
}
92+
innerMap = nextMap;
93+
// Postgres bindings convert everything to strings:
94+
// https://node-postgres.com/features/queries#parameterized-query
95+
// We reuse their algorithm instead of reimplementing.
96+
key = prepareValue(values[i]);
9497
}
95-
innerMap = nextMap;
96-
// Postgres bindings convert everything to strings:
97-
// https://node-postgres.com/features/queries#parameterized-query
98-
// We reuse their algorithm instead of reimplementing.
99-
key = prepareValue(values[i]);
10098
}
101-
return [innerMap, key];
102-
}
10399

104-
Pool.prototype.query = function(query: string, values?: Array<mixed>) {
105-
const pool = this.pool;
106-
const outerMap = unstable_getCacheForType(this.createResultMap);
107-
const [innerMap, key] = getInnerMap(outerMap, query, values);
108-
let entry = innerMap.get(key);
100+
let entry: Result | void = innerMap.get(key);
109101
if (!entry) {
110102
const thenable = pool.query(query, values);
111103
entry = toResult(thenable);

scripts/flow/environment.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@ declare module 'pg' {
7777
query: (query: string, values?: Array<mixed>) => void,
7878
};
7979
}
80+
81+
declare module 'pg/lib/utils' {
82+
declare module.exports: {
83+
prepareValue(val: any): mixed,
84+
};
85+
}

0 commit comments

Comments
 (0)