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

39 lines
1 KiB
TypeScript
Raw Normal View History

import { UserDBEntry, Users } from '@logto/schemas';
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-08-18 00:24:00 +08:00
import { convertToIdentifiers } from '@/database/utils';
const { table, fields } = convertToIdentifiers(Users);
export const findUserByUsername = async (username: string) =>
pool.one<UserDBEntry>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.username}=${username}
`);
export const findUserById = async (id: string) =>
pool.one<UserDBEntry>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
2021-07-04 17:41:46 +08:00
where ${fields.id}=${id}
`);
2021-07-04 17:41:46 +08:00
export const hasUser = async (username: string) =>
pool.exists(sql`
select ${fields.id}
from ${table}
where ${fields.username}=${username}
`);
export const hasUserWithId = async (id: string) =>
pool.exists(sql`
select ${fields.id}
from ${table}
where ${fields.id}=${id}
`);
2021-08-18 00:24:00 +08:00
export const insertUser = buildInsertInto<UserDBEntry>(pool, Users, { returning: true });