0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00
logto/packages/core/src/queries/user.ts

157 lines
4.6 KiB
TypeScript
Raw Normal View History

import { User, CreateUser, Users } from '@logto/schemas';
import { sql } from 'slonik';
2021-08-30 11:30:54 +08:00
import { buildInsertInto } from '@/database/insert-into';
2021-11-18 11:26:34 +08:00
import { buildUpdateWhere } from '@/database/update-where';
import { conditionalSql, convertToIdentifiers, OmitAutoSetFields } from '@/database/utils';
import envSet from '@/env-set';
import { DeletionError, UpdateError } from '@/errors/SlonikError';
const { table, fields } = convertToIdentifiers(Users);
export const findUserByUsername = async (username: string) =>
envSet.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.one<User>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.primaryEmail}=${email}
`);
export const findUserByPhone = async (phone: string) =>
envSet.pool.one<User>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.primaryPhone}=${phone}
`);
export const findUserById = async (id: string) =>
envSet.pool.one<User>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.id}=${id}
`);
2021-07-04 17:41:46 +08:00
2022-02-11 15:19:18 +08:00
export const findUserByIdentity = async (connectorId: string, userId: string) =>
envSet.pool.one<User>(
2022-02-11 15:19:18 +08:00
sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.identities}::json#>>'{${sql.identifier([connectorId])},userId}' = ${userId}
`
);
2021-07-04 17:41:46 +08:00
export const hasUser = async (username: string) =>
envSet.pool.exists(sql`
select ${fields.id}
from ${table}
where ${fields.username}=${username}
`);
2021-07-04 17:41:46 +08:00
export const hasUserWithId = async (id: string) =>
envSet.pool.exists(sql`
select ${fields.id}
from ${table}
where ${fields.id}=${id}
`);
2021-07-04 17:41:46 +08:00
export const hasUserWithEmail = async (email: string) =>
envSet.pool.exists(sql`
select ${fields.primaryEmail}
from ${table}
where ${fields.primaryEmail}=${email}
`);
export const hasUserWithPhone = async (phone: string) =>
envSet.pool.exists(sql`
select ${fields.primaryPhone}
from ${table}
where ${fields.primaryPhone}=${phone}
`);
2022-02-11 15:19:18 +08:00
export const hasUserWithIdentity = async (connectorId: string, userId: string) =>
envSet.pool.exists(
2022-02-11 15:19:18 +08:00
sql`
select ${fields.id}
from ${table}
where ${fields.identities}::json#>>'{${sql.identifier([connectorId])},userId}' = ${userId}
`
);
export const insertUser = buildInsertInto<CreateUser, User>(Users, {
returning: true,
});
2021-11-18 11:26:34 +08:00
const buildUserSearchConditionSql = (search: string) => {
const searchFields = [fields.primaryEmail, fields.primaryPhone, fields.username, fields.name];
const conditions = searchFields.map((filedName) => sql`${filedName} like ${'%' + search + '%'}`);
2022-02-16 15:55:08 +08:00
return sql`${sql.join(conditions, sql` or `)}`;
};
2022-02-16 15:55:08 +08:00
export const countUsers = async (search?: string) =>
envSet.pool.one<{ count: number }>(sql`
select count(*)
from ${table}
${conditionalSql(search, (search) => sql`where ${buildUserSearchConditionSql(search)}`)}
`);
export const findUsers = async (limit: number, offset: number, search?: string) =>
envSet.pool.any<User>(
sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
${conditionalSql(search, (search) => sql`where ${buildUserSearchConditionSql(search)}`)}
limit ${limit}
offset ${offset}
`
);
2021-11-18 11:26:34 +08:00
const updateUser = buildUpdateWhere<CreateUser, User>(Users, true);
2021-11-18 11:26:34 +08:00
export const updateUserById = async (id: string, set: Partial<OmitAutoSetFields<CreateUser>>) =>
2021-11-18 11:26:34 +08:00
updateUser({ set, where: { id } });
export const deleteUserById = async (id: string) => {
const { rowCount } = await envSet.pool.query(sql`
delete from ${table}
where ${fields.id}=${id}
`);
2022-01-27 19:26:34 +08:00
2021-11-18 11:26:34 +08:00
if (rowCount < 1) {
throw new DeletionError(Users.table, id);
2021-11-18 11:26:34 +08:00
}
};
export const clearUserCustomDataById = async (id: string) => {
const { rowCount } = await envSet.pool.query<User>(sql`
update ${table}
set ${fields.customData}='{}'::jsonb
where ${fields.id}=${id}
`);
if (rowCount < 1) {
throw new UpdateError(Users, { set: { customData: {} }, where: { id } });
}
};
export const deleteUserIdentity = async (userId: string, connectorId: string) =>
envSet.pool.one<User>(sql`
update ${table}
set ${fields.identities}=${fields.identities}::jsonb-${connectorId}
where ${fields.id}=${userId}
returning *
`);
export const hasActiveUsers = async () =>
envSet.pool.exists(sql`
select ${fields.id}
from ${table}
limit 1
`);