2022-01-18 12:44:08 +08:00
|
|
|
import { User, CreateUser, Users } from '@logto/schemas';
|
2021-07-02 21:14:18 +08:00
|
|
|
import { sql } from 'slonik';
|
2021-08-30 11:30:54 +08:00
|
|
|
|
|
|
|
import { buildInsertInto } from '@/database/insert-into';
|
2021-07-02 22:55:14 +08:00
|
|
|
import pool from '@/database/pool';
|
2021-11-18 11:26:34 +08:00
|
|
|
import { buildUpdateWhere } from '@/database/update-where';
|
|
|
|
import { convertToIdentifiers, OmitAutoSetFields } from '@/database/utils';
|
2022-01-14 10:19:43 +08:00
|
|
|
import { DeletionError } from '@/errors/SlonikError';
|
2021-07-02 21:14:18 +08:00
|
|
|
|
|
|
|
const { table, fields } = convertToIdentifiers(Users);
|
|
|
|
|
2021-07-26 22:32:18 +08:00
|
|
|
export const findUserByUsername = async (username: string) =>
|
2021-12-08 11:11:27 +08:00
|
|
|
pool.one<User>(sql`
|
2021-12-20 14:20:23 +08:00
|
|
|
select ${sql.join(Object.values(fields), sql`,`)}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.username}=${username}
|
|
|
|
`);
|
2021-07-26 22:32:18 +08:00
|
|
|
|
2022-01-31 11:04:55 +08:00
|
|
|
export const findUserByEmail = async (email: string) =>
|
|
|
|
pool.one<User>(sql`
|
|
|
|
select ${sql.join(Object.values(fields), sql`,`)}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.primaryEmail}=${email}
|
|
|
|
`);
|
|
|
|
|
2022-02-09 16:14:42 +08:00
|
|
|
export const findUserByPhone = async (phone: string) =>
|
|
|
|
pool.one<User>(sql`
|
|
|
|
select ${sql.join(Object.values(fields), sql`,`)}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.primaryPhone}=${phone}
|
|
|
|
`);
|
|
|
|
|
2021-07-02 21:14:18 +08:00
|
|
|
export const findUserById = async (id: string) =>
|
2021-12-08 11:11:27 +08:00
|
|
|
pool.one<User>(sql`
|
2021-12-20 14:20:23 +08:00
|
|
|
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) =>
|
|
|
|
pool.one<User>(
|
|
|
|
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) =>
|
|
|
|
pool.exists(sql`
|
2021-12-20 14:20:23 +08:00
|
|
|
select ${fields.id}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.username}=${username}
|
|
|
|
`);
|
2021-07-04 17:41:46 +08:00
|
|
|
|
|
|
|
export const hasUserWithId = async (id: string) =>
|
|
|
|
pool.exists(sql`
|
2021-12-20 14:20:23 +08:00
|
|
|
select ${fields.id}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.id}=${id}
|
|
|
|
`);
|
2021-07-04 17:41:46 +08:00
|
|
|
|
2022-02-07 14:14:42 +08:00
|
|
|
export const hasUserWithEmail = async (email: string) =>
|
|
|
|
pool.exists(sql`
|
|
|
|
select ${fields.primaryEmail}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.primaryEmail}=${email}
|
|
|
|
`);
|
|
|
|
|
2022-02-09 16:14:42 +08:00
|
|
|
export const hasUserWithPhone = async (phone: string) =>
|
|
|
|
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) =>
|
|
|
|
pool.exists(
|
|
|
|
sql`
|
|
|
|
select ${fields.id}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.identities}::json#>>'{${sql.identifier([connectorId])},userId}' = ${userId}
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
2022-01-18 12:44:08 +08:00
|
|
|
export const insertUser = buildInsertInto<CreateUser, User>(pool, Users, { returning: true });
|
2021-11-18 11:26:34 +08:00
|
|
|
|
|
|
|
export const findAllUsers = async () =>
|
2021-12-08 11:11:27 +08:00
|
|
|
pool.many<User>(sql`
|
2021-11-18 11:26:34 +08:00
|
|
|
select ${sql.join(Object.values(fields), sql`, `)}
|
|
|
|
from ${table}
|
|
|
|
`);
|
|
|
|
|
2022-01-18 12:44:08 +08:00
|
|
|
const updateUser = buildUpdateWhere<CreateUser, User>(pool, Users, true);
|
2021-11-18 11:26:34 +08:00
|
|
|
|
2022-01-18 12:44:08 +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 pool.query(sql`
|
2021-12-20 14:20:23 +08:00
|
|
|
delete from ${table}
|
|
|
|
where id=${id}
|
|
|
|
`);
|
2022-01-27 19:26:34 +08:00
|
|
|
|
2021-11-18 11:26:34 +08:00
|
|
|
if (rowCount < 1) {
|
2022-01-14 10:19:43 +08:00
|
|
|
throw new DeletionError();
|
2021-11-18 11:26:34 +08:00
|
|
|
}
|
|
|
|
};
|