Skip to content

Commit

Permalink
fix: handle sql tokens without parser
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed May 14, 2024
1 parent 8a120a5 commit 9bdba03
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/giant-grapes-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@slonik/dataloaders": patch
---

handle sql tokens without parser
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,21 @@ describe('createConnectionLoaderClass', () => {
expect(result.edges[9].node.id).toEqual(1);
});

it('loads records with ORDER expression (batch)', async () => {
const loader = new PersonConnectionLoader(pool);
const [a, b] = await Promise.all([
loader.load({
orderBy: ({ uid }) => [[uid, 'ASC']],
}),
loader.load({
orderBy: ({ uid }) => [[uid, 'DESC']],
}),
]);

expect(getNodeIds(a.edges)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
expect(getNodeIds(b.edges)).toEqual([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
});

it('loads records with multiple ORDER BY expressions', async () => {
const loader = new PersonConnectionLoader(pool);
const result = await loader.load({
Expand Down Expand Up @@ -183,6 +198,41 @@ describe('createConnectionLoaderClass', () => {
expect(getNodeIds(result.edges)).toEqual([9, 10]);
});

it('loads records with WHERE expression (batch)', async () => {
const loader = new PersonConnectionLoader(pool);
const [a, b] = await Promise.all([
loader.load({
where: ({ name }) => sql.fragment`${name} = 'aaa'`,
}),
loader.load({
where: ({ name }) => sql.fragment`${name} = 'bbb'`,
}),
]);

expect(getNodeIds(a.edges)).toEqual([1, 2]);
expect(getNodeIds(b.edges)).toEqual([3, 4]);
});

it('loads records with WHERE expression (batch; miss)', async () => {
const loader = new PersonConnectionLoader(pool);
// eslint-disable-next-line id-length
const [a, b, c] = await Promise.all([
loader.load({
where: ({ name }) => sql.fragment`${name} = 'aaa'`,
}),
loader.load({
where: ({ name }) => sql.fragment`${name} = 'xxx'`,
}),
loader.load({
where: ({ name }) => sql.fragment`${name} = 'bbb'`,
}),
]);

expect(getNodeIds(a.edges)).toEqual([1, 2]);
expect(getNodeIds(b.edges)).toEqual([]);
expect(getNodeIds(c.edges)).toEqual([3, 4]);
});

it('loads records with LIMIT', async () => {
const loader = new PersonConnectionLoader(pool);
const result = await loader.load({
Expand Down Expand Up @@ -501,6 +551,26 @@ describe('createConnectionLoaderClass (with validation)', () => {
expect(result.edges).toHaveLength(10);
});

it('loads all records with row validation (unsafe)', async () => {
const UnsafePersonConnectionLoader = createConnectionLoaderClass({
query: sql.unsafe`
SELECT
id,
uid,
name
FROM person
`,
});

const loader = new UnsafePersonConnectionLoader(pool);

const result = await loader.load({
orderBy: ({ uid }) => [[uid, 'ASC']],
});

expect(getNodeIds(result.edges)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
});

it('fails with schema validation error', async () => {
const BadConnectionLoader = createConnectionLoaderClass({
query: sql.type(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export const createConnectionLoaderClass = <T extends ZodTypeAny>(config: {
}
}

let edgeSchema: AnyZodObject = z.object({});
let edgeSchema: AnyZodObject;

if ('shape' in query.parser) {
edgeSchema = z
Expand All @@ -202,6 +202,12 @@ export const createConnectionLoaderClass = <T extends ZodTypeAny>(config: {
...(query.parser as any).shape,
})
.strict();
} else {
edgeSchema = z
.object({
[SORT_COLUMN_ALIAS]: z.array(z.any()),
})
.passthrough();
}

const countSchema = z.object({
Expand Down

0 comments on commit 9bdba03

Please sign in to comment.