From b059a3081797da69abcf43c6475ae130fa0af8b4 Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Fri, 13 Oct 2023 19:35:27 +0800 Subject: [PATCH] refactor(core): refactor organization queries --- .../core/src/queries/organization-roles.ts | 20 ------------------- .../core/src/queries/organization-scopes.ts | 20 ------------------- packages/core/src/queries/organizations.ts | 20 +++++++++++++++++++ .../core/src/routes/organization-roles.ts | 13 +++++++----- .../src/routes/organization-scopes.test.ts | 11 ++++------ .../core/src/routes/organization-scopes.ts | 9 ++++----- packages/core/src/tenants/Queries.ts | 18 ----------------- 7 files changed, 36 insertions(+), 75 deletions(-) delete mode 100644 packages/core/src/queries/organization-roles.ts delete mode 100644 packages/core/src/queries/organization-scopes.ts diff --git a/packages/core/src/queries/organization-roles.ts b/packages/core/src/queries/organization-roles.ts deleted file mode 100644 index 1ba7c111f..000000000 --- a/packages/core/src/queries/organization-roles.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { - type OrganizationRoleKeys, - OrganizationRoles, - type CreateOrganizationRole, - type OrganizationRole, -} from '@logto/schemas'; -import { type CommonQueryMethods } from 'slonik'; - -import SchemaQueries from '#src/utils/SchemaQueries.js'; - -/** Class of queries for roles in the organization template. */ -export default class OrganizationRoleQueries extends SchemaQueries< - OrganizationRoleKeys, - CreateOrganizationRole, - OrganizationRole -> { - constructor(pool: CommonQueryMethods) { - super(pool, OrganizationRoles); - } -} diff --git a/packages/core/src/queries/organization-scopes.ts b/packages/core/src/queries/organization-scopes.ts deleted file mode 100644 index 9d50608fa..000000000 --- a/packages/core/src/queries/organization-scopes.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { - type OrganizationScopeKeys, - OrganizationScopes, - type CreateOrganizationScope, - type OrganizationScope, -} from '@logto/schemas'; -import { type CommonQueryMethods } from 'slonik'; - -import SchemaQueries from '#src/utils/SchemaQueries.js'; - -/** Class of queries for scopes in the organization template. */ -export default class OrganizationScopeQueries extends SchemaQueries< - OrganizationScopeKeys, - CreateOrganizationScope, - OrganizationScope -> { - constructor(pool: CommonQueryMethods) { - super(pool, OrganizationScopes); - } -} diff --git a/packages/core/src/queries/organizations.ts b/packages/core/src/queries/organizations.ts index 517a898b5..08c085317 100644 --- a/packages/core/src/queries/organizations.ts +++ b/packages/core/src/queries/organizations.ts @@ -3,9 +3,13 @@ import { type CreateOrganization, type OrganizationKeys, Organizations, + OrganizationRoles, + OrganizationScopes, + OrganizationRoleScopeRelations, } from '@logto/schemas'; import { type CommonQueryMethods } from 'slonik'; +import RelationQueries from '#src/utils/RelationQueries.js'; import SchemaQueries from '#src/utils/SchemaQueries.js'; export default class OrganizationQueries extends SchemaQueries< @@ -13,6 +17,22 @@ export default class OrganizationQueries extends SchemaQueries< CreateOrganization, Organization > { + /** Queries for roles in the organization template. */ + roles = new SchemaQueries(this.pool, OrganizationRoles); + /** Queries for scopes in the organization template. */ + scopes = new SchemaQueries(this.pool, OrganizationScopes); + + /** Queries for relations that connected with organization-related entities. */ + relations = { + /** Queries for organization role - organization scope relations. */ + rolesScopes: new RelationQueries( + this.pool, + OrganizationRoleScopeRelations.table, + OrganizationRoles.table, + OrganizationScopes.table + ), + }; + constructor(pool: CommonQueryMethods) { super(pool, Organizations); } diff --git a/packages/core/src/routes/organization-roles.ts b/packages/core/src/routes/organization-roles.ts index cecada6bd..ee83270a1 100644 --- a/packages/core/src/routes/organization-roles.ts +++ b/packages/core/src/routes/organization-roles.ts @@ -37,11 +37,16 @@ export default function organizationRoleRoutes( ...[ originalRouter, { - queries: { organizationRoles, organizationRoleScopeRelations }, + queries: { + organizations: { + roles, + relations: { rolesScopes }, + }, + }, }, ]: RouterInitArgs ) { - const actions = new OrganizationRoleActions(organizationRoles); + const actions = new OrganizationRoleActions(roles); const router = new SchemaRouter(OrganizationRoles, actions, { disabled: { post: true } }); /** Allows to carry an initial set of scopes for creating a new organization role. */ @@ -68,9 +73,7 @@ export default function organizationRoleRoutes( const role = await actions.post(data); if (scopeIds.length > 0) { - await organizationRoleScopeRelations.insert( - ...scopeIds.map<[string, string]>((id) => [role.id, id]) - ); + await rolesScopes.insert(...scopeIds.map<[string, string]>((id) => [role.id, id])); } ctx.body = role; diff --git a/packages/core/src/routes/organization-scopes.test.ts b/packages/core/src/routes/organization-scopes.test.ts index a8216ac4b..7ad5f0094 100644 --- a/packages/core/src/routes/organization-scopes.test.ts +++ b/packages/core/src/routes/organization-scopes.test.ts @@ -1,20 +1,17 @@ import { UniqueIntegrityConstraintViolationError } from 'slonik'; import RequestError from '#src/errors/RequestError/index.js'; -import { MockTenant } from '#src/test-utils/tenant.js'; import { OrganizationScopeActions } from './organization-scopes.js'; describe('OrganizationScopeActions', () => { it('should throw RequestError if UniqueIntegrityConstraintViolationError is thrown inside', async () => { - const tenantContext = new MockTenant(undefined, { - organizationScopes: { - insert: async () => { - throw new UniqueIntegrityConstraintViolationError(new Error('test'), 'unique'); - }, + // @ts-expect-error for testing + const actions = new OrganizationScopeActions({ + insert: async () => { + throw new UniqueIntegrityConstraintViolationError(new Error('test'), 'unique'); }, }); - const actions = new OrganizationScopeActions(tenantContext.queries.organizationScopes); await expect(actions.post({ name: 'test' })).rejects.toThrowError( new RequestError({ code: 'entity.duplicate_value_of_unique_field', field: 'name' }) diff --git a/packages/core/src/routes/organization-scopes.ts b/packages/core/src/routes/organization-scopes.ts index 092fc8581..09ee6e5ff 100644 --- a/packages/core/src/routes/organization-scopes.ts +++ b/packages/core/src/routes/organization-scopes.ts @@ -35,14 +35,13 @@ export default function organizationScopeRoutes( ...[ originalRouter, { - queries: { organizationScopes }, + queries: { + organizations: { scopes }, + }, }, ]: RouterInitArgs ) { - const router = new SchemaRouter( - OrganizationScopes, - new OrganizationScopeActions(organizationScopes) - ); + const router = new SchemaRouter(OrganizationScopes, new OrganizationScopeActions(scopes)); originalRouter.use(router.routes()); } diff --git a/packages/core/src/tenants/Queries.ts b/packages/core/src/tenants/Queries.ts index f841a2ba6..577c4053c 100644 --- a/packages/core/src/tenants/Queries.ts +++ b/packages/core/src/tenants/Queries.ts @@ -1,8 +1,3 @@ -import { - OrganizationRoleScopeRelations, - OrganizationRoles, - OrganizationScopes, -} from '@logto/schemas'; import type { CommonQueryMethods } from 'slonik'; import { type WellKnownCache } from '#src/caches/well-known.js'; @@ -16,8 +11,6 @@ import { createHooksQueries } from '#src/queries/hooks.js'; import { createLogQueries } from '#src/queries/log.js'; import { createLogtoConfigQueries } from '#src/queries/logto-config.js'; import { createOidcModelInstanceQueries } from '#src/queries/oidc-model-instance.js'; -import OrganizationRoleQueries from '#src/queries/organization-roles.js'; -import OrganizationScopeQueries from '#src/queries/organization-scopes.js'; import OrganizationQueries from '#src/queries/organizations.js'; import { createPasscodeQueries } from '#src/queries/passcode.js'; import { createResourceQueries } from '#src/queries/resource.js'; @@ -28,7 +21,6 @@ import { createSignInExperienceQueries } from '#src/queries/sign-in-experience.j import { createUserQueries } from '#src/queries/user.js'; import { createUsersRolesQueries } from '#src/queries/users-roles.js'; import { createVerificationStatusQueries } from '#src/queries/verification-status.js'; -import RelationQueries from '#src/utils/RelationQueries.js'; export default class Queries { applications = createApplicationQueries(this.pool); @@ -51,16 +43,6 @@ export default class Queries { domains = createDomainsQueries(this.pool); dailyActiveUsers = createDailyActiveUsersQueries(this.pool); organizations = new OrganizationQueries(this.pool); - /** Organization template scope queries. */ - organizationScopes = new OrganizationScopeQueries(this.pool); - /** Organization template role queries. */ - organizationRoles = new OrganizationRoleQueries(this.pool); - organizationRoleScopeRelations = new RelationQueries( - this.pool, - OrganizationRoleScopeRelations.table, - OrganizationRoles.table, - OrganizationScopes.table - ); constructor( public readonly pool: CommonQueryMethods,