2021-07-02 21:14:18 +08:00
|
|
|
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';
|
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) =>
|
|
|
|
pool.one<UserDBEntry>(sql`
|
|
|
|
select ${sql.join(Object.values(fields), sql`,`)}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.username}=${username}
|
|
|
|
`);
|
|
|
|
|
2021-07-02 21:14:18 +08:00
|
|
|
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-02 21:14:18 +08:00
|
|
|
`);
|
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 });
|