From c057024b7c2ed30bdb907b48074fcd5fd6845680 Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Wed, 11 Oct 2023 17:04:45 +0800 Subject: [PATCH] refactor(core): generic schema queries and actions --- packages/core/src/queries/organizations.ts | 61 +++++------------- packages/core/src/utils/SchemaQueries.ts | 74 ++++++++++++++++++++++ packages/core/src/utils/TenantQueries.ts | 5 -- 3 files changed, 89 insertions(+), 51 deletions(-) create mode 100644 packages/core/src/utils/SchemaQueries.ts delete mode 100644 packages/core/src/utils/TenantQueries.ts diff --git a/packages/core/src/queries/organizations.ts b/packages/core/src/queries/organizations.ts index 5e15acdef..517a898b5 100644 --- a/packages/core/src/queries/organizations.ts +++ b/packages/core/src/queries/organizations.ts @@ -1,50 +1,19 @@ -import { type Organization, Organizations, type CreateOrganization } from '@logto/schemas'; -import { generateStandardId, type OmitAutoSetFields } from '@logto/shared'; +import { + type Organization, + type CreateOrganization, + type OrganizationKeys, + Organizations, +} from '@logto/schemas'; +import { type CommonQueryMethods } from 'slonik'; -import { buildDeleteByIdWithPool } from '#src/database/delete-by-id.js'; -import { buildFindAllEntitiesWithPool } from '#src/database/find-all-entities.js'; -import { buildFindEntityByIdWithPool } from '#src/database/find-entity-by-id.js'; -import { buildInsertIntoWithPool } from '#src/database/insert-into.js'; -import { buildGetTotalRowCountWithPool } from '#src/database/row-count.js'; -import { buildUpdateWhereWithPool } from '#src/database/update-where.js'; -import TenantQueries from '#src/utils/TenantQueries.js'; +import SchemaQueries from '#src/utils/SchemaQueries.js'; -export default class OrganizationQueries extends TenantQueries { - #findTotalNumber = buildGetTotalRowCountWithPool(this.pool, Organizations.table); - #findAll = buildFindAllEntitiesWithPool(this.pool)(Organizations); - #findById = buildFindEntityByIdWithPool(this.pool)(Organizations); - #insert = buildInsertIntoWithPool(this.pool)(Organizations, { returning: true }); - #updateById = buildUpdateWhereWithPool(this.pool)(Organizations, true); - #deleteById = buildDeleteByIdWithPool(this.pool, Organizations.table); - - async findTotalNumber(): Promise { - const { count } = await this.#findTotalNumber(); - return count; - } - - async findAll(limit: number, offset: number): Promise { - return this.#findAll(limit, offset); - } - - async findById(id: string): Promise> { - return this.#findById(id); - } - - async insert( - data: Omit, 'id'> - ): Promise> { - return this.#insert({ id: generateStandardId(), ...data }); - } - - async updateById( - id: string, - data: Partial>, - jsonbMode: 'replace' | 'merge' = 'replace' - ): Promise> { - return this.#updateById({ set: data, where: { id }, jsonbMode }); - } - - async deleteById(id: string): Promise { - await this.#deleteById(id); +export default class OrganizationQueries extends SchemaQueries< + OrganizationKeys, + CreateOrganization, + Organization +> { + constructor(pool: CommonQueryMethods) { + super(pool, Organizations); } } diff --git a/packages/core/src/utils/SchemaQueries.ts b/packages/core/src/utils/SchemaQueries.ts new file mode 100644 index 000000000..a01e0d802 --- /dev/null +++ b/packages/core/src/utils/SchemaQueries.ts @@ -0,0 +1,74 @@ +import { type GeneratedSchema } from '@logto/schemas'; +import { + generateStandardId, + type UpdateWhereData, + type OmitAutoSetFields, + type SchemaLike, +} from '@logto/shared'; +import { type CommonQueryMethods } from 'slonik'; + +import { buildDeleteByIdWithPool } from '#src/database/delete-by-id.js'; +import { buildFindAllEntitiesWithPool } from '#src/database/find-all-entities.js'; +import { buildFindEntityByIdWithPool } from '#src/database/find-entity-by-id.js'; +import { buildInsertIntoWithPool } from '#src/database/insert-into.js'; +import { buildGetTotalRowCountWithPool } from '#src/database/row-count.js'; +import { buildUpdateWhereWithPool } from '#src/database/update-where.js'; + +export default class SchemaQueries< + Key extends string, + CreateSchema extends Partial & { id: string }>, + Schema extends SchemaLike & { id: string }, +> { + #findTotalNumber: () => Promise<{ count: number }>; + #findAll: (limit: number, offset: number) => Promise; + #findById: (id: string) => Promise>; + #insert: (data: OmitAutoSetFields) => Promise>; + + #updateById: ( + data: UpdateWhereData + ) => Promise; + + #deleteById: (id: string) => Promise; + + constructor( + public readonly pool: CommonQueryMethods, + public readonly schema: GeneratedSchema + ) { + this.#findTotalNumber = buildGetTotalRowCountWithPool(this.pool, this.schema.table); + this.#findAll = buildFindAllEntitiesWithPool(this.pool)(this.schema); + this.#findById = buildFindEntityByIdWithPool(this.pool)(this.schema); + this.#insert = buildInsertIntoWithPool(this.pool)(this.schema, { returning: true }); + this.#updateById = buildUpdateWhereWithPool(this.pool)(this.schema, true); + this.#deleteById = buildDeleteByIdWithPool(this.pool, this.schema.table); + } + + async findTotalNumber(): Promise { + const { count } = await this.#findTotalNumber(); + return count; + } + + async findAll(limit: number, offset: number): Promise { + return this.#findAll(limit, offset); + } + + async findById(id: string): Promise> { + return this.#findById(id); + } + + async insert(data: Omit, 'id'>): Promise>; + async insert(data: OmitAutoSetFields): Promise> { + return this.#insert({ id: generateStandardId(), ...data }); + } + + async updateById( + id: string, + data: Partial, + jsonbMode: 'replace' | 'merge' = 'replace' + ): Promise> { + return this.#updateById({ set: data, where: { id }, jsonbMode }); + } + + async deleteById(id: string): Promise { + await this.#deleteById(id); + } +} diff --git a/packages/core/src/utils/TenantQueries.ts b/packages/core/src/utils/TenantQueries.ts deleted file mode 100644 index 88c023442..000000000 --- a/packages/core/src/utils/TenantQueries.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { type CommonQueryMethods } from 'slonik'; - -export default class TenantQueries { - constructor(public readonly pool: CommonQueryMethods) {} -}