Skip to content

Commit

Permalink
Run findMany and aggregate simultaneously
Browse files Browse the repository at this point in the history
  • Loading branch information
Kareem-Medhat committed Nov 19, 2023
1 parent 8362b79 commit f5e5e8e
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ export async function findManyCursorConnection<
const skip = cursor ? 1 : undefined

// Execute the underlying query operations
records = await findMany({ cursor, take, skip })
totalCount = hasRequestedField('totalCount') ? await aggregate() : -1
const results = await Promise.all([findMany({ cursor, take, skip }), hasRequestedField('totalCount') ? aggregate() : Promise.resolve(-1)])
records = results[0]
totalCount = results[1]

// See if we are "after" another record, indicating a previous page
hasPreviousPage = !!args.after
Expand All @@ -64,8 +65,9 @@ export async function findManyCursorConnection<
const skip = cursor ? 1 : undefined

// Execute the underlying query operations
records = await findMany({ cursor, take, skip })
totalCount = hasRequestedField('totalCount') ? await aggregate() : -1
const results = await Promise.all([findMany({ cursor, take, skip }), hasRequestedField('totalCount') ? aggregate() : Promise.resolve(-1)])
records = results[0]
totalCount = results[1]

// See if we are "before" another record, indicating a next page
hasNextPage = !!args.before
Expand All @@ -77,8 +79,12 @@ export async function findManyCursorConnection<
if (hasPreviousPage) records.shift()
} else {
// Execute the underlying query operations
records = hasRequestedField('edges') || hasRequestedField('nodes') ? await findMany({}) : []
totalCount = hasRequestedField('totalCount') ? await aggregate() : -1
const results = await Promise.all([
hasRequestedField('edges') || hasRequestedField('nodes') ? findMany({}) : Promise.resolve([]),
hasRequestedField('totalCount') ? aggregate() : Promise.resolve(-1)
])
records = results[0]
totalCount = results[1]

// Since we are getting all records, there are no pages
hasNextPage = false
Expand Down

0 comments on commit f5e5e8e

Please sign in to comment.