mirror of
https://github.com/logto-io/logto.git
synced 2025-03-24 22:41:28 -05:00
refactor(core): generic schema queries and actions
This commit is contained in:
parent
7178657935
commit
c057024b7c
3 changed files with 89 additions and 51 deletions
|
@ -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<number> {
|
||||
const { count } = await this.#findTotalNumber();
|
||||
return count;
|
||||
}
|
||||
|
||||
async findAll(limit: number, offset: number): Promise<readonly Organization[]> {
|
||||
return this.#findAll(limit, offset);
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Readonly<Organization>> {
|
||||
return this.#findById(id);
|
||||
}
|
||||
|
||||
async insert(
|
||||
data: Omit<OmitAutoSetFields<CreateOrganization>, 'id'>
|
||||
): Promise<Readonly<Organization>> {
|
||||
return this.#insert({ id: generateStandardId(), ...data });
|
||||
}
|
||||
|
||||
async updateById(
|
||||
id: string,
|
||||
data: Partial<OmitAutoSetFields<Organization>>,
|
||||
jsonbMode: 'replace' | 'merge' = 'replace'
|
||||
): Promise<Readonly<Organization>> {
|
||||
return this.#updateById({ set: data, where: { id }, jsonbMode });
|
||||
}
|
||||
|
||||
async deleteById(id: string): Promise<void> {
|
||||
await this.#deleteById(id);
|
||||
export default class OrganizationQueries extends SchemaQueries<
|
||||
OrganizationKeys,
|
||||
CreateOrganization,
|
||||
Organization
|
||||
> {
|
||||
constructor(pool: CommonQueryMethods) {
|
||||
super(pool, Organizations);
|
||||
}
|
||||
}
|
||||
|
|
74
packages/core/src/utils/SchemaQueries.ts
Normal file
74
packages/core/src/utils/SchemaQueries.ts
Normal file
|
@ -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<SchemaLike<Key> & { id: string }>,
|
||||
Schema extends SchemaLike<Key> & { id: string },
|
||||
> {
|
||||
#findTotalNumber: () => Promise<{ count: number }>;
|
||||
#findAll: (limit: number, offset: number) => Promise<readonly Schema[]>;
|
||||
#findById: (id: string) => Promise<Readonly<Schema>>;
|
||||
#insert: (data: OmitAutoSetFields<CreateSchema>) => Promise<Readonly<Schema>>;
|
||||
|
||||
#updateById: <SetKey extends Key | 'id', WhereKey extends Key | 'id'>(
|
||||
data: UpdateWhereData<SetKey, WhereKey>
|
||||
) => Promise<Schema>;
|
||||
|
||||
#deleteById: (id: string) => Promise<void>;
|
||||
|
||||
constructor(
|
||||
public readonly pool: CommonQueryMethods,
|
||||
public readonly schema: GeneratedSchema<Key | 'id', CreateSchema, Schema>
|
||||
) {
|
||||
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<number> {
|
||||
const { count } = await this.#findTotalNumber();
|
||||
return count;
|
||||
}
|
||||
|
||||
async findAll(limit: number, offset: number): Promise<readonly Schema[]> {
|
||||
return this.#findAll(limit, offset);
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Readonly<Schema>> {
|
||||
return this.#findById(id);
|
||||
}
|
||||
|
||||
async insert(data: Omit<OmitAutoSetFields<CreateSchema>, 'id'>): Promise<Readonly<Schema>>;
|
||||
async insert(data: OmitAutoSetFields<CreateSchema>): Promise<Readonly<Schema>> {
|
||||
return this.#insert({ id: generateStandardId(), ...data });
|
||||
}
|
||||
|
||||
async updateById(
|
||||
id: string,
|
||||
data: Partial<Schema>,
|
||||
jsonbMode: 'replace' | 'merge' = 'replace'
|
||||
): Promise<Readonly<Schema>> {
|
||||
return this.#updateById({ set: data, where: { id }, jsonbMode });
|
||||
}
|
||||
|
||||
async deleteById(id: string): Promise<void> {
|
||||
await this.#deleteById(id);
|
||||
}
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
import { type CommonQueryMethods } from 'slonik';
|
||||
|
||||
export default class TenantQueries {
|
||||
constructor(public readonly pool: CommonQueryMethods) {}
|
||||
}
|
Loading…
Add table
Reference in a new issue