Skip to content

Commit

Permalink
fix: dont dedupe array params in queries
Browse files Browse the repository at this point in the history
  • Loading branch information
leemhenson committed Oct 4, 2018
1 parent a319715 commit 1a398fe
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Typescript wrapper around node-postgres
:wrench: chore
:notebook: docs

#### v9.0.1
- :bug: Prevent array positional parameters from being deduped.

## v9.0.0
- :boom: An additional `context` param has been added to `connection.query` and `queryX`
functions. This is a blob of data that will be attached to query and transaction error instances.
Expand Down
6 changes: 4 additions & 2 deletions src/utils/sql.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NonEmptyArray } from "fp-ts/lib/NonEmptyArray";
import { None, Some } from "fp-ts/lib/Option";
import { mixed } from "io-ts";
import { forOwn, isEqual, isPlainObject } from "lodash";
import { forOwn, isPlainObject, isEqual } from "lodash";
import * as pg from "pg";

const normaliseValue = (value: any): any => {
Expand Down Expand Up @@ -52,7 +52,9 @@ export const SQL = (parts: TemplateStringsArray, ...inValues: any[]): pg.QueryCo
return `NULL`;
}

const found = outValues.findIndex(o => isEqual(o, normalisedValue));
const found = outValues.findIndex(
o => !Array.isArray(normalisedValue) && isEqual(o, normalisedValue),
);

if (found > -1) {
return `$${found + 1}`;
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,11 @@ describe("queries", () => {
expect(results).toEqual([{ json: { foo: [1, 2] } }]);
}),
));

test("queryAny with two empty arrays with different type assertions", () =>
connectionTest(
queryAny(t.any, SQL`SELECT ${[]}::uuid[] AS a, ${[]}::int[] as b`).map(results => {
expect(results).toEqual([{ a: [], b: [] }]);
}),
));
});
12 changes: 6 additions & 6 deletions tests/utils/sql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,22 @@ describe("Utils", () => {
it("matches empty arrays", () => {
value = [];
res = SQL`${value} = ${value}`;
expect(res).toHaveProperty("text", "$1 = $1");
expect(res).toHaveProperty("values", [value]);
expect(res).toHaveProperty("text", "$1 = $2");
expect(res).toHaveProperty("values", [value, value]);
});

it("matches populated arrays", () => {
value = [10];
res = SQL`${value} = ${value}`;
expect(res).toHaveProperty("text", "$1 = $1");
expect(res).toHaveProperty("values", [value]);
expect(res).toHaveProperty("text", "$1 = $2");
expect(res).toHaveProperty("values", [value, value]);
});

it("matches nested arrays", () => {
value = [[10]];
res = SQL`${value} = ${value}`;
expect(res).toHaveProperty("text", "$1 = $1");
expect(res).toHaveProperty("values", [value]);
expect(res).toHaveProperty("text", "$1 = $2");
expect(res).toHaveProperty("values", [value, value]);
});
});

Expand Down

0 comments on commit 1a398fe

Please sign in to comment.