mirror of
https://github.com/logto-io/logto.git
synced 2025-03-31 22:51:25 -05:00
feat(core): get /users
(#237)
This commit is contained in:
parent
eb319a3b89
commit
ee49880845
2 changed files with 19 additions and 9 deletions
|
@ -1,10 +1,11 @@
|
|||
import { User, CreateUser, Users } from '@logto/schemas';
|
||||
import { sql } from 'slonik';
|
||||
|
||||
import { buildFindMany } from '@/database/find-many';
|
||||
import { buildInsertInto } from '@/database/insert-into';
|
||||
import pool from '@/database/pool';
|
||||
import { buildUpdateWhere } from '@/database/update-where';
|
||||
import { convertToIdentifiers, OmitAutoSetFields } from '@/database/utils';
|
||||
import { convertToIdentifiers, getTotalRowCount, OmitAutoSetFields } from '@/database/utils';
|
||||
import { DeletionError } from '@/errors/SlonikError';
|
||||
|
||||
const { table, fields } = convertToIdentifiers(Users);
|
||||
|
@ -85,11 +86,12 @@ export const hasUserWithIdentity = async (connectorId: string, userId: string) =
|
|||
|
||||
export const insertUser = buildInsertInto<CreateUser, User>(pool, Users, { returning: true });
|
||||
|
||||
export const findAllUsers = async () =>
|
||||
pool.many<User>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
`);
|
||||
export const findTotalNumberOfUsers = async () => getTotalRowCount(table);
|
||||
|
||||
const findUserMany = buildFindMany<CreateUser, User>(pool, Users);
|
||||
|
||||
export const findAllUsers = async (limit: number, offset: number) =>
|
||||
findUserMany({ limit, offset });
|
||||
|
||||
const updateUser = buildUpdateWhere<CreateUser, User>(pool, Users, true);
|
||||
|
||||
|
|
|
@ -4,14 +4,22 @@ import { InvalidInputError } from 'slonik';
|
|||
import { object, string } from 'zod';
|
||||
|
||||
import koaGuard from '@/middleware/koa-guard';
|
||||
import koaPagination from '@/middleware/koa-pagination';
|
||||
import { findRolesByRoleNames } from '@/queries/roles';
|
||||
import { findAllUsers, findUserById, updateUserById } from '@/queries/user';
|
||||
import { findAllUsers, findTotalNumberOfUsers, findUserById, updateUserById } from '@/queries/user';
|
||||
|
||||
import { AuthedRouter } from './types';
|
||||
|
||||
export default function adminUserRoutes<T extends AuthedRouter>(router: T) {
|
||||
router.get('/users', async (ctx, next) => {
|
||||
const users = await findAllUsers();
|
||||
router.get('/users', koaPagination(), async (ctx, next) => {
|
||||
const { limit, offset } = ctx.pagination;
|
||||
|
||||
const [{ count }, users] = await Promise.all([
|
||||
findTotalNumberOfUsers(),
|
||||
findAllUsers(limit, offset),
|
||||
]);
|
||||
|
||||
ctx.pagination.totalCount = count;
|
||||
ctx.body = users.map((user) => pick(user, ...userInfoSelectFields));
|
||||
|
||||
return next();
|
||||
|
|
Loading…
Add table
Reference in a new issue