Skip to content

Commit

Permalink
fix(code-gen): fix empty where xxIn behaviour in the sql generator (#722
Browse files Browse the repository at this point in the history
)

For some reason we skipped over empty `xxIn` arrays in the where generator. Now we handle the empty case correctly.
  • Loading branch information
dirkdev98 authored Feb 24, 2021
1 parent 519300e commit c251c3c
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 369 deletions.
18 changes: 8 additions & 10 deletions packages/code-gen/src/generator/sql/where-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,16 @@ export function getWherePartial(context, type) {
if (isQueryPart(where.${field.name})) {
strings.push(\` AND $\{tableName}"${field.key}" = ANY(\`, ")");
values.push(where.${field.name}, undefined);
} else if (Array.isArray(where.${field.name}) && where.${field.name}.length > 0) {
} else if (Array.isArray(where.${field.name})) {
strings.push(\` AND $\{tableName}"${field.key}" = ANY(ARRAY[\`);
for (let i = 0; i < where.${field.name}.length; ++i) {
values.push(where.${field.name}[i]);
if (i === where.${field.name}.length - 1) {
strings.push("]::${fieldType}[])");
values.push(undefined);
} else {
if (i !== where.${field.name}.length - 1) {
strings.push(", ");
}
}
strings.push("]::${fieldType}[])");
values.push(undefined);
}
`;
break;
Expand All @@ -228,17 +227,16 @@ export function getWherePartial(context, type) {
if (isQueryPart(where.${field.name})) {
strings.push(\` AND $\{tableName}"${field.key}" != ANY(\`, ")");
values.push(where.${field.name}, undefined);
} else if (Array.isArray(where.${field.name}) && where.${field.name}.length > 0) {
} else if (Array.isArray(where.${field.name})) {
strings.push(\` AND $\{tableName}"${field.key}" != ANY(ARRAY[\`);
for (let i = 0; i < where.${field.name}.length; ++i) {
values.push(where.${field.name}[i]);
if (i === where.${field.name}.length - 1) {
strings.push("]::${fieldType}[])");
values.push(undefined);
} else {
if (i !== where.${field.name}.length - 1) {
strings.push(", ");
}
}
strings.push("]::${fieldType}[])");
values.push(undefined);
}
`;
break;
Expand Down
8 changes: 8 additions & 0 deletions packages/code-gen/test/sql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ test("code-gen/e2e/sql", async (t) => {
t.equal(dbUser.email, "[email protected]");
});

t.test("query filter with empty idIn", async (t) => {
const categories = await client.queries.categorySelect(sql, {
idIn: [],
});

t.equal(categories.length, 0);
});

t.test("query same 'shortName' originally", async () => {
await client
.queryUser({
Expand Down
114 changes: 40 additions & 74 deletions packages/store/src/generated/database/file.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c251c3c

Please sign in to comment.