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

31 lines
835 B
TypeScript
Raw Normal View History

import { UserDBEntry, Users } from '@logto/schemas';
import { sql } from 'slonik';
2021-07-02 09:55:14 -05:00
import pool from '@/database/pool';
2021-07-04 04:41:46 -05:00
import { convertToIdentifiers, insertInto } from '@/database/utils';
const { table, fields } = convertToIdentifiers(Users);
export const findUserById = async (id: string) =>
pool.one<UserDBEntry>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
2021-07-04 04:41:46 -05:00
where ${fields.id}=${id}
`);
2021-07-04 04:41:46 -05: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}
`);
export const insertUser = async (user: UserDBEntry) =>
pool.query(insertInto(table, fields, Users.fieldKeys, user));