[PR #1258] [MERGED] feat: add total users table/collection count to admin plugin list-users endpoint #3721

Closed
opened 2026-03-13 11:08:00 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/1258
Author: @D3visionNL
Created: 1/21/2025
Status: Merged
Merged: 2/14/2025
Merged by: @Bekacru

Base: v1.2Head: feat/listusers-provide-totals


📝 Commits (6)

  • ad0f387 Added total, offset and limit return propertiest to listUsers endpoint for admin plugin. This required modifying all db adapters. Also updated the init.test.ts.snap file to reflect adding of the count function in the adapter and the countTotalUsers function in the internalAdapter.
  • 973a47c Resolved all issues in review. We now use count wherever possible.
  • 8ba4cf3 linter was complaining
  • 257a7a7 literal on adapter and 1 more test
  • a6c02ac lint
  • 118dc9c admin plugin listUsers pagination documentation

📊 Changes

12 files changed (+129 additions, -3 deletions)

View changed files

📝 docs/components/sidebar-content.tsx (+0 -1)
📝 docs/content/docs/plugins/admin.mdx (+39 -0)
📝 packages/better-auth/src/__snapshots__/init.test.ts.snap (+2 -0)
📝 packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts (+11 -1)
📝 packages/better-auth/src/adapters/kysely-adapter/kysely-adapter.ts (+16 -0)
📝 packages/better-auth/src/adapters/memory-adapter/memory-adapter.ts (+3 -0)
📝 packages/better-auth/src/adapters/mongodb-adapter/mongodb-adapter.ts (+7 -0)
📝 packages/better-auth/src/adapters/prisma-adapter/prisma-adapter.ts (+13 -0)
📝 packages/better-auth/src/db/internal-adapter.ts (+6 -0)
📝 packages/better-auth/src/plugins/admin/admin.test.ts (+14 -1)
📝 packages/better-auth/src/plugins/admin/index.ts (+14 -0)
📝 packages/better-auth/src/types/adapter.ts (+4 -0)

📄 Description

closes #1222 and various feature requests in the discord.

This feature will return the total amount of users as well as the limit and offset that were used for the list-users search. This will allow you to do proper pagination without requiring additional API calls.

Types:

{
  users: [User],
  total: number,
  limit: number | undefined
  offset: number | undefined
}

I purposefully made limit and offset be undefined when they aren't queried. This way you can easily check if a limit or offset were used or not. If making them undefined is undesirable I can always change them to, for example, -1.

Example response:

{
  "users": [
    {
      "id": "678faf201d309d6892b774c0",
      "name": "d3vision",
      "email": "info@d3vision.dev",
      "emailVerified": true,
      "image": "https://cdn.discordapp.com/embed/avatars/3.png",
      "createdAt": "2025-01-21T14:28:48.355Z",
      "updatedAt": "2025-01-21T14:28:48.355Z",
      "role": "user"
    }
  ],
  "total": 2,
  "limit": 1,
  "offset": 1
}

This is for a users collection with 2 users.

If the query errors, it will return:

{
  "users": [],
  "total": 0,
}

Lastly, I updated the init.test.ts.snap file to reflect my adding of the count function in the adapter and the countTotalUsers function in the internalAdapter.

As usual, please let me know if I need to make any changes or if there are things I should add!


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/1258 **Author:** [@D3visionNL](https://github.com/D3visionNL) **Created:** 1/21/2025 **Status:** ✅ Merged **Merged:** 2/14/2025 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `v1.2` ← **Head:** `feat/listusers-provide-totals` --- ### 📝 Commits (6) - [`ad0f387`](https://github.com/better-auth/better-auth/commit/ad0f3877d494c4f55d7d38f4f4f6b4a0f6e08c0e) Added total, offset and limit return propertiest to listUsers endpoint for admin plugin. This required modifying all db adapters. Also updated the init.test.ts.snap file to reflect adding of the count function in the adapter and the countTotalUsers function in the internalAdapter. - [`973a47c`](https://github.com/better-auth/better-auth/commit/973a47cf0267ddf797f7ad61dfe63e40bfa0584a) Resolved all issues in review. We now use count wherever possible. - [`8ba4cf3`](https://github.com/better-auth/better-auth/commit/8ba4cf3e0f44963c579cb3a4700cb6e7a9dcd156) linter was complaining - [`257a7a7`](https://github.com/better-auth/better-auth/commit/257a7a7f31c9afaa3b65ace96870891d0e3fab05) literal on adapter and 1 more test - [`a6c02ac`](https://github.com/better-auth/better-auth/commit/a6c02acab435856e60fdffefb48889d3998a15fb) lint - [`118dc9c`](https://github.com/better-auth/better-auth/commit/118dc9c45c568960596cb515cd55d89c942578c5) admin plugin listUsers pagination documentation ### 📊 Changes **12 files changed** (+129 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `docs/components/sidebar-content.tsx` (+0 -1) 📝 `docs/content/docs/plugins/admin.mdx` (+39 -0) 📝 `packages/better-auth/src/__snapshots__/init.test.ts.snap` (+2 -0) 📝 `packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts` (+11 -1) 📝 `packages/better-auth/src/adapters/kysely-adapter/kysely-adapter.ts` (+16 -0) 📝 `packages/better-auth/src/adapters/memory-adapter/memory-adapter.ts` (+3 -0) 📝 `packages/better-auth/src/adapters/mongodb-adapter/mongodb-adapter.ts` (+7 -0) 📝 `packages/better-auth/src/adapters/prisma-adapter/prisma-adapter.ts` (+13 -0) 📝 `packages/better-auth/src/db/internal-adapter.ts` (+6 -0) 📝 `packages/better-auth/src/plugins/admin/admin.test.ts` (+14 -1) 📝 `packages/better-auth/src/plugins/admin/index.ts` (+14 -0) 📝 `packages/better-auth/src/types/adapter.ts` (+4 -0) </details> ### 📄 Description closes #1222 and various feature requests in the discord. This feature will return the total amount of users as well as the limit and offset that were used for the list-users search. This will allow you to do proper pagination without requiring additional API calls. Types: ``` { users: [User], total: number, limit: number | undefined offset: number | undefined } ``` I purposefully made `limit` and `offset` be undefined when they aren't queried. This way you can easily check if a limit or offset were used or not. If making them undefined is undesirable I can always change them to, for example, -1. Example response: ```json { "users": [ { "id": "678faf201d309d6892b774c0", "name": "d3vision", "email": "info@d3vision.dev", "emailVerified": true, "image": "https://cdn.discordapp.com/embed/avatars/3.png", "createdAt": "2025-01-21T14:28:48.355Z", "updatedAt": "2025-01-21T14:28:48.355Z", "role": "user" } ], "total": 2, "limit": 1, "offset": 1 } ``` _This is for a users collection with 2 users._ If the query errors, it will return: ```json { "users": [], "total": 0, } ``` Lastly, I updated the `init.test.ts.snap` file to reflect my adding of the count function in the adapter and the countTotalUsers function in the internalAdapter. **As usual, please let me know if I need to make any changes or if there are things I should add!** --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 11:08:00 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#3721