0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-27 21:39:16 -05:00

refactor(core): users and users roles query factories

This commit is contained in:
Gao Sun 2023-01-09 16:41:14 +08:00
parent 4b21b7d16e
commit a68b34971a
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
2 changed files with 294 additions and 225 deletions

View file

@ -2,9 +2,10 @@ import type { User, CreateUser, UserWithRoleNames } from '@logto/schemas';
import { SearchJointMode, Users } from '@logto/schemas';
import type { OmitAutoSetFields } from '@logto/shared';
import { conditionalSql, convertToIdentifiers } from '@logto/shared';
import type { CommonQueryMethods } from 'slonik';
import { sql } from 'slonik';
import { buildUpdateWhere } from '#src/database/update-where.js';
import { buildUpdateWhereWithPool } from '#src/database/update-where.js';
import envSet from '#src/env-set/index.js';
import { DeletionError } from '#src/errors/SlonikError/index.js';
import type { Search } from '#src/utils/search.js';
@ -15,29 +16,30 @@ import { findUsersRolesByRoleId, findUsersRolesByUserId } from './users-roles.js
const { table, fields } = convertToIdentifiers(Users);
export const findUserByUsername = async (username: string) =>
envSet.pool.maybeOne<User>(sql`
const createUserQueries = (pool: CommonQueryMethods) => {
const findUserByUsername = async (username: string) =>
pool.maybeOne<User>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.username}=${username}
`);
export const findUserByEmail = async (email: string) =>
envSet.pool.maybeOne<User>(sql`
const findUserByEmail = async (email: string) =>
pool.maybeOne<User>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where lower(${fields.primaryEmail})=lower(${email})
`);
export const findUserByPhone = async (phone: string) =>
envSet.pool.maybeOne<User>(sql`
const findUserByPhone = async (phone: string) =>
pool.maybeOne<User>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.primaryPhone}=${phone}
`);
export const findUserById = async (id: string): Promise<UserWithRoleNames> => {
const user = await envSet.pool.one<User>(sql`
const findUserById = async (id: string): Promise<UserWithRoleNames> => {
const user = await pool.one<User>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.id}=${id}
@ -51,10 +53,10 @@ export const findUserById = async (id: string): Promise<UserWithRoleNames> => {
...user,
roleNames: roles.map(({ name }) => name),
};
};
};
export const findUserByIdentity = async (target: string, userId: string) =>
envSet.pool.maybeOne<User>(
const findUserByIdentity = async (target: string, userId: string) =>
pool.maybeOne<User>(
sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
@ -62,39 +64,39 @@ export const findUserByIdentity = async (target: string, userId: string) =>
`
);
export const hasUser = async (username: string, excludeUserId?: string) =>
envSet.pool.exists(sql`
const hasUser = async (username: string, excludeUserId?: string) =>
pool.exists(sql`
select ${fields.id}
from ${table}
where ${fields.username}=${username}
${conditionalSql(excludeUserId, (id) => sql`and ${fields.id}<>${id}`)}
`);
export const hasUserWithId = async (id: string) =>
envSet.pool.exists(sql`
const hasUserWithId = async (id: string) =>
pool.exists(sql`
select ${fields.id}
from ${table}
where ${fields.id}=${id}
`);
export const hasUserWithEmail = async (email: string, excludeUserId?: string) =>
envSet.pool.exists(sql`
const hasUserWithEmail = async (email: string, excludeUserId?: string) =>
pool.exists(sql`
select ${fields.primaryEmail}
from ${table}
where lower(${fields.primaryEmail})=lower(${email})
${conditionalSql(excludeUserId, (id) => sql`and ${fields.id}<>${id}`)}
`);
export const hasUserWithPhone = async (phone: string, excludeUserId?: string) =>
envSet.pool.exists(sql`
const hasUserWithPhone = async (phone: string, excludeUserId?: string) =>
pool.exists(sql`
select ${fields.primaryPhone}
from ${table}
where ${fields.primaryPhone}=${phone}
${conditionalSql(excludeUserId, (id) => sql`and ${fields.id}<>${id}`)}
`);
export const hasUserWithIdentity = async (target: string, userId: string) =>
envSet.pool.exists(
const hasUserWithIdentity = async (target: string, userId: string) =>
pool.exists(
sql`
select ${fields.id}
from ${table}
@ -102,7 +104,7 @@ export const hasUserWithIdentity = async (target: string, userId: string) =>
`
);
const buildUserConditions = (search: Search, excludeUserIds: string[]) => {
const buildUserConditions = (search: Search, excludeUserIds: string[]) => {
const hasSearch = search.matches.length > 0;
const searchFields = [
Users.fields.id,
@ -128,27 +130,24 @@ const buildUserConditions = (search: Search, excludeUserIds: string[]) => {
hasSearch,
() => sql`where ${buildConditionsFromSearch(search, searchFields)}`
);
};
};
export const defaultUserSearch = { matches: [], isCaseSensitive: false, joint: SearchJointMode.Or };
const defaultUserSearch = { matches: [], isCaseSensitive: false, joint: SearchJointMode.Or };
export const countUsers = async (
search: Search = defaultUserSearch,
excludeUserIds: string[] = []
) =>
envSet.pool.one<{ count: number }>(sql`
const countUsers = async (search: Search = defaultUserSearch, excludeUserIds: string[] = []) =>
pool.one<{ count: number }>(sql`
select count(*)
from ${table}
${buildUserConditions(search, excludeUserIds)}
`);
export const findUsers = async (
const findUsers = async (
limit: number,
offset: number,
search: Search,
excludeUserIds: string[] = []
) =>
envSet.pool.any<User>(
) =>
pool.any<User>(
sql`
select ${sql.join(
Object.values(fields).map((field) => sql`${table}.${field}`),
@ -161,25 +160,23 @@ export const findUsers = async (
`
);
export const findUsersByIds = async (userIds: string[]) =>
userIds.length > 0
? envSet.pool.any<User>(sql`
const findUsersByIds = async (userIds: string[]) =>
pool.any<User>(sql`
select ${sql.join(Object.values(fields), sql`, `)}
from ${table}
where ${fields.id} in (${sql.join(userIds, sql`, `)})
`)
: [];
`);
const updateUser = buildUpdateWhere<CreateUser, User>(Users, true);
const updateUser = buildUpdateWhereWithPool(pool)<CreateUser, User>(Users, true);
export const updateUserById = async (
const updateUserById = async (
id: string,
set: Partial<OmitAutoSetFields<CreateUser>>,
jsonbMode: 'replace' | 'merge' = 'merge'
) => updateUser({ set, where: { id }, jsonbMode });
) => updateUser({ set, where: { id }, jsonbMode });
export const deleteUserById = async (id: string) => {
const { rowCount } = await envSet.pool.query(sql`
const deleteUserById = async (id: string) => {
const { rowCount } = await pool.query(sql`
delete from ${table}
where ${fields.id}=${id}
`);
@ -187,28 +184,28 @@ export const deleteUserById = async (id: string) => {
if (rowCount < 1) {
throw new DeletionError(Users.table, id);
}
};
};
export const deleteUserIdentity = async (userId: string, target: string) =>
envSet.pool.one<User>(sql`
const deleteUserIdentity = async (userId: string, target: string) =>
pool.one<User>(sql`
update ${table}
set ${fields.identities}=${fields.identities}::jsonb-${target}
where ${fields.id}=${userId}
returning *
`);
export const hasActiveUsers = async () =>
envSet.pool.exists(sql`
const hasActiveUsers = async () =>
pool.exists(sql`
select ${fields.id}
from ${table}
limit 1
`);
export const getDailyNewUserCountsByTimeInterval = async (
const getDailyNewUserCountsByTimeInterval = async (
startTimeExclusive: number,
endTimeInclusive: number
) =>
envSet.pool.any<{ date: string; count: number }>(sql`
) =>
pool.any<{ date: string; count: number }>(sql`
select date(${fields.createdAt}), count(*)
from ${table}
where ${fields.createdAt} > to_timestamp(${startTimeExclusive}::double precision / 1000)
@ -216,7 +213,7 @@ export const getDailyNewUserCountsByTimeInterval = async (
group by date(${fields.createdAt})
`);
export const findUsersByRoleName = async (roleName: string) => {
const findUsersByRoleName = async (roleName: string) => {
const role = await findRoleByRoleName(roleName);
if (!role) {
@ -225,5 +222,55 @@ export const findUsersByRoleName = async (roleName: string) => {
const usersRoles = await findUsersRolesByRoleId(role.id);
if (usersRoles.length === 0) {
return [];
}
return findUsersByIds(usersRoles.map(({ userId }) => userId));
};
return {
findUserByUsername,
findUserByEmail,
findUserByPhone,
findUserById,
findUserByIdentity,
hasUser,
hasUserWithId,
hasUserWithEmail,
hasUserWithPhone,
hasUserWithIdentity,
countUsers,
findUsers,
findUsersByIds,
updateUserById,
deleteUserById,
deleteUserIdentity,
hasActiveUsers,
getDailyNewUserCountsByTimeInterval,
findUsersByRoleName,
};
};
/** @deprecated Will be removed soon. Use createUserQueries() factory instead. */
export const {
findUserByUsername,
findUserByEmail,
findUserByPhone,
findUserById,
findUserByIdentity,
hasUser,
hasUserWithId,
hasUserWithEmail,
hasUserWithPhone,
hasUserWithIdentity,
countUsers,
findUsers,
findUsersByIds,
updateUserById,
deleteUserById,
deleteUserIdentity,
hasActiveUsers,
getDailyNewUserCountsByTimeInterval,
findUsersByRoleName,
} = createUserQueries(envSet.pool);

View file

@ -1,37 +1,24 @@
import type { UsersRole } from '@logto/schemas';
import { UsersRoles } from '@logto/schemas';
import { conditionalSql, convertToIdentifiers } from '@logto/shared';
import type { CommonQueryMethods } from 'slonik';
import { sql } from 'slonik';
import envSet from '#src/env-set/index.js';
const { table, fields } = convertToIdentifiers(UsersRoles);
export const findUsersRolesByUserId = async (userId: string) =>
envSet.pool.any<UsersRole>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.userId}=${userId}
`);
export const findUsersRolesByRoleId = async (roleId: string, limit?: number) =>
envSet.pool.any<UsersRole>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.roleId}=${roleId}
${conditionalSql(limit, (value) => sql`limit ${value}`)}
`);
export const countUsersRolesByRoleId = async (roleId: string) =>
envSet.pool.one<{ count: number }>(sql`
export const createUsersRolesQueries = (pool: CommonQueryMethods) => {
const countUsersRolesByRoleId = async (roleId: string) =>
pool.one<{ count: number }>(sql`
select count(*)
from ${table}
where ${fields.roleId}=${roleId}
`);
export const findFirstUsersRolesByRoleIdAndUserIds = async (roleId: string, userIds: string[]) =>
const findFirstUsersRolesByRoleIdAndUserIds = async (roleId: string, userIds: string[]) =>
userIds.length > 0
? envSet.pool.maybeOne<UsersRole>(sql`
? pool.maybeOne<UsersRole>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.roleId}=${roleId}
@ -40,8 +27,23 @@ export const findFirstUsersRolesByRoleIdAndUserIds = async (roleId: string, user
`)
: null;
export const insertUsersRoles = async (usersRoles: UsersRole[]) =>
envSet.pool.query(sql`
const findUsersRolesByUserId = async (userId: string) =>
pool.any<UsersRole>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.userId}=${userId}
`);
const findUsersRolesByRoleId = async (roleId: string, limit?: number) =>
pool.any<UsersRole>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.roleId}=${roleId}
${conditionalSql(limit, (value) => sql`limit ${value}`)}
`);
const insertUsersRoles = async (usersRoles: UsersRole[]) =>
pool.query(sql`
insert into ${table} (${fields.userId}, ${fields.roleId}) values
${sql.join(
usersRoles.map(({ userId, roleId }) => sql`(${userId}, ${roleId})`),
@ -49,9 +51,29 @@ export const insertUsersRoles = async (usersRoles: UsersRole[]) =>
)}
`);
export const deleteUsersRolesByUserIdAndRoleId = async (userId: string, roleId: string) => {
await envSet.pool.query(sql`
const deleteUsersRolesByUserIdAndRoleId = async (userId: string, roleId: string) => {
await pool.query(sql`
delete from ${table}
where ${fields.userId} = ${userId} and ${fields.roleId} = ${roleId}
`);
};
return {
countUsersRolesByRoleId,
findFirstUsersRolesByRoleIdAndUserIds,
findUsersRolesByUserId,
findUsersRolesByRoleId,
insertUsersRoles,
deleteUsersRolesByUserIdAndRoleId,
};
};
/** @deprecated Will be removed soon. Use createUsersRolesQueries() factory instead. */
export const {
countUsersRolesByRoleId,
findFirstUsersRolesByRoleIdAndUserIds,
findUsersRolesByUserId,
findUsersRolesByRoleId,
insertUsersRoles,
deleteUsersRolesByUserIdAndRoleId,
} = createUsersRolesQueries(envSet.pool);