mirror of
https://github.com/logto-io/logto.git
synced 2025-03-24 22:41:28 -05:00
refactor(core): add findEntityById utils (#1448)
add findEntityById utils
This commit is contained in:
parent
d4e5a8dda9
commit
ead79eab72
7 changed files with 52 additions and 39 deletions
38
packages/core/src/database/find-entity-by-id.ts
Normal file
38
packages/core/src/database/find-entity-by-id.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
import { SchemaLike, GeneratedSchema } from '@logto/schemas';
|
||||
import { sql, NotFoundError } from 'slonik';
|
||||
|
||||
import { convertToIdentifiers } from '@/database/utils';
|
||||
import envSet from '@/env-set';
|
||||
import RequestError from '@/errors/RequestError';
|
||||
import assertThat from '@/utils/assert-that';
|
||||
import { isKeyOf } from '@/utils/schema';
|
||||
|
||||
export const buildFindEntityById = <Schema extends SchemaLike, ReturnType extends SchemaLike>(
|
||||
schema: GeneratedSchema<Schema & { id: string }>
|
||||
) => {
|
||||
const { table, fields } = convertToIdentifiers(schema);
|
||||
const isKeyOfSchema = isKeyOf(schema);
|
||||
|
||||
// Make sure id is key of the schema
|
||||
assertThat(isKeyOfSchema('id'), 'entity.not_exists');
|
||||
|
||||
return async (id: string) => {
|
||||
try {
|
||||
return await envSet.pool.one<ReturnType>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
where ${fields.id}=${id}
|
||||
`);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof NotFoundError) {
|
||||
throw new RequestError({
|
||||
code: 'entity.not_exists_with_id',
|
||||
name: schema.table,
|
||||
id,
|
||||
status: 404,
|
||||
});
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
};
|
|
@ -1,6 +1,7 @@
|
|||
import { Application, CreateApplication, Applications } from '@logto/schemas';
|
||||
import { sql } from 'slonik';
|
||||
|
||||
import { buildFindEntityById } from '@/database/find-entity-by-id';
|
||||
import { buildInsertInto } from '@/database/insert-into';
|
||||
import { getTotalRowCount } from '@/database/row-count';
|
||||
import { buildUpdateWhere } from '@/database/update-where';
|
||||
|
@ -28,12 +29,9 @@ export const findAllApplications = async (limit: number, offset: number) =>
|
|||
`)
|
||||
);
|
||||
|
||||
export const findApplicationById = async (id: string) =>
|
||||
envSet.pool.one<Application>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
where ${fields.id}=${id}
|
||||
`);
|
||||
export const findApplicationById = buildFindEntityById<CreateApplication, Application>(
|
||||
Applications
|
||||
);
|
||||
|
||||
export const insertApplication = buildInsertInto<CreateApplication, Application>(Applications, {
|
||||
returning: true,
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { CreateLog, Log, Logs, LogType } from '@logto/schemas';
|
||||
import { sql } from 'slonik';
|
||||
|
||||
import { buildFindEntityById } from '@/database/find-entity-by-id';
|
||||
import { buildInsertInto } from '@/database/insert-into';
|
||||
import { conditionalSql, convertToIdentifiers } from '@/database/utils';
|
||||
import envSet from '@/env-set';
|
||||
|
@ -46,12 +47,7 @@ export const findLogs = async (limit: number, offset: number, logCondition: LogC
|
|||
offset ${offset}
|
||||
`);
|
||||
|
||||
export const findLogById = async (id: string) =>
|
||||
envSet.pool.one<Log>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
where ${fields.id}=${id}
|
||||
`);
|
||||
export const findLogById = buildFindEntityById<CreateLog, Log>(Logs);
|
||||
|
||||
const registerLogTypes: LogType[] = [
|
||||
'RegisterUsernamePassword',
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { Resource, CreateResource, Resources } from '@logto/schemas';
|
||||
import { sql } from 'slonik';
|
||||
|
||||
import { buildFindEntityById } from '@/database/find-entity-by-id';
|
||||
import { buildInsertInto } from '@/database/insert-into';
|
||||
import { getTotalRowCount } from '@/database/row-count';
|
||||
import { buildUpdateWhere } from '@/database/update-where';
|
||||
|
@ -34,12 +35,7 @@ export const findResourceByIndicator = async (indicator: string) =>
|
|||
where ${fields.indicator}=${indicator}
|
||||
`);
|
||||
|
||||
export const findResourceById = async (id: string) =>
|
||||
envSet.pool.one<Resource>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
where ${fields.id}=${id}
|
||||
`);
|
||||
export const findResourceById = buildFindEntityById<CreateResource, Resource>(Resources);
|
||||
|
||||
export const insertResource = buildInsertInto<CreateResource, Resource>(Resources, {
|
||||
returning: true,
|
||||
|
|
|
@ -1,20 +1,13 @@
|
|||
import { Setting, CreateSetting, Settings } from '@logto/schemas';
|
||||
import { sql } from 'slonik';
|
||||
|
||||
import { buildFindEntityById } from '@/database/find-entity-by-id';
|
||||
import { buildUpdateWhere } from '@/database/update-where';
|
||||
import { convertToIdentifiers, OmitAutoSetFields } from '@/database/utils';
|
||||
import envSet from '@/env-set';
|
||||
import { OmitAutoSetFields } from '@/database/utils';
|
||||
|
||||
export const defaultSettingId = 'default';
|
||||
|
||||
const { table, fields } = convertToIdentifiers(Settings);
|
||||
|
||||
export const getSetting = async () =>
|
||||
envSet.pool.one<Setting>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
where ${fields.id}=${defaultSettingId}
|
||||
`);
|
||||
buildFindEntityById<CreateSetting, Setting>(Settings)(defaultSettingId);
|
||||
|
||||
export const updateSetting = async (setting: Partial<OmitAutoSetFields<CreateSetting>>) => {
|
||||
return buildUpdateWhere<CreateSetting, Setting>(
|
||||
|
|
|
@ -34,7 +34,7 @@ describe('sign-in-experience query', () => {
|
|||
const expectSql = `
|
||||
select "id", "color", "branding", "language_info", "terms_of_use", "sign_in_methods", "social_sign_in_connector_targets", "sign_in_mode"
|
||||
from "sign_in_experiences"
|
||||
where "id" = $1
|
||||
where "id"=$1
|
||||
`;
|
||||
/* eslint-enable sql/no-unsafe-query */
|
||||
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
import { SignInExperience, CreateSignInExperience, SignInExperiences } from '@logto/schemas';
|
||||
import { sql } from 'slonik';
|
||||
|
||||
import { buildFindEntityById } from '@/database/find-entity-by-id';
|
||||
import { buildUpdateWhere } from '@/database/update-where';
|
||||
import { convertToIdentifiers } from '@/database/utils';
|
||||
import envSet from '@/env-set';
|
||||
|
||||
const { table, fields } = convertToIdentifiers(SignInExperiences);
|
||||
|
||||
const updateSignInExperience = buildUpdateWhere<CreateSignInExperience, SignInExperience>(
|
||||
SignInExperiences,
|
||||
|
@ -18,8 +14,4 @@ export const updateDefaultSignInExperience = async (set: Partial<CreateSignInExp
|
|||
updateSignInExperience({ set, where: { id }, jsonbMode: 'replace' });
|
||||
|
||||
export const findDefaultSignInExperience = async () =>
|
||||
envSet.pool.one<SignInExperience>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
where ${fields.id} = ${id}
|
||||
`);
|
||||
buildFindEntityById<CreateSignInExperience, SignInExperience>(SignInExperiences)(id);
|
||||
|
|
Loading…
Add table
Reference in a new issue