Skip to content

Commit

Permalink
fix: omit internal slonikqueryindex
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed May 14, 2024
1 parent bce5110 commit 8a120a5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-plants-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@slonik/dataloaders": minor
---

omit internal slonikqueryindex
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ const getInfo = (

const PersonConnectionLoader = createConnectionLoaderClass({
query: sql.type(
z.object({
id: z.number(),
name: z.string(),
uid: z.string(),
}),
z
.object({
id: z.number(),
name: z.string(),
uid: z.string(),
})
.strict(),
)`
SELECT
id,
Expand Down Expand Up @@ -353,7 +355,7 @@ describe('createConnectionLoaderClass', () => {
where: ({ name }) => sql.fragment`${name} = 'ccc'`,
});

expect(result.count).toEqual(2n);
expect(result.count).toEqual(2);
});

it('gets the count without fetching edges', async () => {
Expand All @@ -363,7 +365,7 @@ describe('createConnectionLoaderClass', () => {
where: ({ name }) => sql.fragment`${name} = 'ccc'`,
});

expect(result.count).toEqual(2n);
expect(result.count).toEqual(2);
expect(result.edges.length).toEqual(0);
});

Expand All @@ -380,9 +382,9 @@ describe('createConnectionLoaderClass', () => {
}),
]);

expect(results[0].count).toEqual(2n);
expect(results[0].count).toEqual(2);
expect(results[0].edges.length).toEqual(0);
expect(results[1].count).toEqual(2n);
expect(results[1].count).toEqual(2);
expect(results[1].edges.length).toEqual(0);
});

Expand All @@ -401,7 +403,7 @@ describe('createConnectionLoaderClass', () => {
]);

expect(results[0].count).toEqual(0);
expect(results[1].count).toEqual(2n);
expect(results[1].count).toEqual(2);
});

it('gets the edges without fetching edges', async () => {
Expand Down Expand Up @@ -492,21 +494,30 @@ describe('createConnectionLoaderClass (with validation)', () => {
}
});

it('loads all records with row validation', async () => {
const loader = new PersonConnectionLoader(pool);
const result = await loader.load({});

expect(result.edges).toHaveLength(10);
});

it('fails with schema validation error', async () => {
const BadConnectionLoader = createConnectionLoaderClass({
query: sql.type(
z.object({
id: z.number(),
id: z.string(),
uid: z.string(),
}),
)`
SELECT
*
id,
uid
FROM person
`,
});

const loader = new BadConnectionLoader(pool);

await expect(loader.load({})).rejects.toThrowError(SchemaValidationError);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
sql,
type SqlToken,
} from 'slonik';
import { z, type ZodTypeAny } from 'zod';
import { type AnyZodObject, z, type ZodTypeAny } from 'zod';

type DataLoaderKey<TResult> = {
cursor?: string | null;
Expand Down Expand Up @@ -87,7 +87,7 @@ export const createConnectionLoaderClass = <T extends ZodTypeAny>(config: {
countQueries.push(
sql.unsafe`(
-- @count-query
SELECT count(*) count
SELECT count(*)::int4 count
FROM (
${query}
) ${sql.identifier([TABLE_ALIAS])}
Expand Down Expand Up @@ -192,12 +192,11 @@ export const createConnectionLoaderClass = <T extends ZodTypeAny>(config: {
}
}

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

if ('shape' in query.parser) {
edgeSchema = z
.object({
slonikqueryindex: z.number(),
[SORT_COLUMN_ALIAS]: z.array(z.any()),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...(query.parser as any).shape,
Expand All @@ -207,7 +206,6 @@ export const createConnectionLoaderClass = <T extends ZodTypeAny>(config: {

const countSchema = z.object({
count: z.number(),
slonikqueryindex: z.number(),
});

const [edgeResults, countResults] = await Promise.all([
Expand Down Expand Up @@ -249,6 +247,7 @@ export const createConnectionLoaderClass = <T extends ZodTypeAny>(config: {

while (true) {
const value = record[SORT_COLUMN_ALIAS]?.[index];

if (value === undefined) {
break;
} else {
Expand Down
19 changes: 13 additions & 6 deletions packages/slonik-dataloaders/src/utilities/batchQueries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type CommonQueryMethods, type QuerySqlToken, sql } from 'slonik';
import { type z, type ZodTypeAny } from 'zod';
import { type AnyZodObject, z } from 'zod';

/**
* Uses UNION to batch multiple queries that have the same shape.
Expand All @@ -9,7 +9,7 @@ import { type z, type ZodTypeAny } from 'zod';
* This approach also has the benefit that it is compatible with
* Slonik interceptors that validate the shape of the result set.
*/
export const batchQueries = async <T extends ZodTypeAny>(
export const batchQueries = async <T extends AnyZodObject>(
pool: CommonQueryMethods,
zodSchema: T,
queries: readonly QuerySqlToken[],
Expand All @@ -18,7 +18,9 @@ export const batchQueries = async <T extends ZodTypeAny>(
return [];
}

const results = await pool.any(sql.type(zodSchema)`
const results = await pool.any(sql.type(
zodSchema.extend({ slonikqueryindex: z.string() }),
)`
${sql.join(
queries.map((query, index) => {
return sql.fragment`
Expand All @@ -33,8 +35,13 @@ export const batchQueries = async <T extends ZodTypeAny>(
`);

return queries.map((query, index) => {
return results.filter(
(result) => result.slonikqueryindex === String(index),
);
return results
.filter((result) => result.slonikqueryindex === String(index))
.map((result) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { slonikqueryindex, ...rest } = result;

return rest;
});
});
};
6 changes: 3 additions & 3 deletions packages/slonik/src/routines/executeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@ export const executeQuery = async (
const { fields } = result;

const rows: QueryResultRow[] = await Promise.all(
result.rows.map((row) =>
transformRow(executionContext, actualQuery, row, fields),
),
result.rows.map((row) => {
return transformRow(executionContext, actualQuery, row, fields);
}),
);

result = {
Expand Down

0 comments on commit 8a120a5

Please sign in to comment.