2021-07-02 08:14:18 -05:00
|
|
|
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';
|
2021-07-02 08:14:18 -05:00
|
|
|
|
|
|
|
const { table, fields } = convertToIdentifiers(Users);
|
|
|
|
|
2021-07-26 09:32:18 -05:00
|
|
|
export const findUserByUsername = async (username: string) =>
|
|
|
|
pool.one<UserDBEntry>(sql`
|
|
|
|
select ${sql.join(Object.values(fields), sql`,`)}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.username}=${username}
|
|
|
|
`);
|
|
|
|
|
2021-07-02 08:14:18 -05:00
|
|
|
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-02 08:14:18 -05:00
|
|
|
`);
|
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));
|