mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 04:46:05 +00:00
connection: rewrote query execution, batching, added generic sqlite, added node/bun sqlite, aligned repo/mutator results
This commit is contained in:
@@ -207,8 +207,9 @@ export class EntityManager<TBD extends object = DefaultDB> {
|
||||
|
||||
repository<E extends Entity | keyof TBD | string>(
|
||||
entity: E,
|
||||
opts: Omit<RepositoryOptions, "emgr"> = {},
|
||||
): Repository<TBD, EntitySchema<TBD, E>> {
|
||||
return this.repo(entity);
|
||||
return this.repo(entity, opts);
|
||||
}
|
||||
|
||||
repo<E extends Entity | keyof TBD | string>(
|
||||
|
||||
126
app/src/data/entities/Result.ts
Normal file
126
app/src/data/entities/Result.ts
Normal file
@@ -0,0 +1,126 @@
|
||||
import { isDebug } from "core";
|
||||
import { pick } from "core/utils";
|
||||
import type { Connection } from "data/connection";
|
||||
import type {
|
||||
Compilable,
|
||||
CompiledQuery,
|
||||
QueryResult as KyselyQueryResult,
|
||||
SelectQueryBuilder,
|
||||
} from "kysely";
|
||||
|
||||
export type ResultHydrator<T = any> = (rows: T[]) => any;
|
||||
export type ResultOptions<T = any> = {
|
||||
hydrator?: ResultHydrator<T>;
|
||||
beforeExecute?: (compiled: CompiledQuery) => void | Promise<void>;
|
||||
onError?: (error: Error) => void | Promise<void>;
|
||||
single?: boolean;
|
||||
};
|
||||
export type ResultJSON<T = any> = {
|
||||
data: T;
|
||||
meta: {
|
||||
items: number;
|
||||
time: number;
|
||||
sql?: string;
|
||||
parameters?: any[];
|
||||
[key: string]: any;
|
||||
};
|
||||
};
|
||||
|
||||
export interface QueryResult<T = any> extends Omit<KyselyQueryResult<T>, "rows"> {
|
||||
time: number;
|
||||
items: number;
|
||||
data: T;
|
||||
rows: unknown[];
|
||||
sql: string;
|
||||
parameters: any[];
|
||||
count?: number;
|
||||
total?: number;
|
||||
}
|
||||
|
||||
export class Result<T = unknown> {
|
||||
results: QueryResult<T>[] = [];
|
||||
time: number = 0;
|
||||
|
||||
constructor(
|
||||
protected conn: Connection,
|
||||
protected options: ResultOptions<T> = {},
|
||||
) {}
|
||||
|
||||
get(): QueryResult<T> {
|
||||
if (!this.results) {
|
||||
throw new Error("Result not executed");
|
||||
}
|
||||
|
||||
if (Array.isArray(this.results)) {
|
||||
return (this.results ?? []) as any;
|
||||
}
|
||||
|
||||
return this.results[0] as any;
|
||||
}
|
||||
|
||||
first(): QueryResult<T> {
|
||||
const res = this.get();
|
||||
const first = Array.isArray(res) ? res[0] : res;
|
||||
return first ?? ({} as any);
|
||||
}
|
||||
|
||||
get sql() {
|
||||
return this.first().sql;
|
||||
}
|
||||
|
||||
get parameters() {
|
||||
return this.first().parameters;
|
||||
}
|
||||
|
||||
get data() {
|
||||
if (this.options.single) {
|
||||
return this.first().data?.[0];
|
||||
}
|
||||
|
||||
return this.first().data ?? [];
|
||||
}
|
||||
|
||||
async execute(qb: Compilable | Compilable[]) {
|
||||
const qbs = Array.isArray(qb) ? qb : [qb];
|
||||
|
||||
for (const qb of qbs) {
|
||||
const compiled = qb.compile();
|
||||
await this.options.beforeExecute?.(compiled);
|
||||
try {
|
||||
const start = performance.now();
|
||||
const res = await this.conn.executeQuery(compiled);
|
||||
this.time = Number.parseFloat((performance.now() - start).toFixed(2));
|
||||
this.results.push({
|
||||
...res,
|
||||
data: this.options.hydrator?.(res.rows as T[]),
|
||||
items: res.rows.length,
|
||||
time: this.time,
|
||||
sql: compiled.sql,
|
||||
parameters: [...compiled.parameters],
|
||||
});
|
||||
} catch (e) {
|
||||
if (this.options.onError) {
|
||||
await this.options.onError(e as Error);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
protected additionalMetaKeys(): string[] {
|
||||
return [];
|
||||
}
|
||||
|
||||
toJSON(): ResultJSON<T> {
|
||||
const { rows, data, ...metaRaw } = this.first();
|
||||
const keys = isDebug() ? ["items", "time", "sql", "parameters"] : ["items", "time"];
|
||||
const meta = pick(metaRaw, [...keys, ...this.additionalMetaKeys()] as any);
|
||||
return {
|
||||
data: this.data,
|
||||
meta,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
export * from "./Entity";
|
||||
export * from "./EntityManager";
|
||||
export * from "./Mutator";
|
||||
export * from "./mutation/Mutator";
|
||||
export * from "./query/Repository";
|
||||
export * from "./query/WhereBuilder";
|
||||
export * from "./query/WithBuilder";
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { $console, type DB as DefaultDB, type PrimaryFieldType } from "core";
|
||||
import { type EmitsEvents, EventManager } from "core/events";
|
||||
import type { DeleteQueryBuilder, InsertQueryBuilder, UpdateQueryBuilder } from "kysely";
|
||||
import { type TActionContext, WhereBuilder } from "..";
|
||||
import type { Entity, EntityData, EntityManager } from "../entities";
|
||||
import { InvalidSearchParamsException } from "../errors";
|
||||
import { MutatorEvents } from "../events";
|
||||
import { RelationMutator } from "../relations";
|
||||
import type { RepoQuery } from "../server/query";
|
||||
import { type TActionContext, WhereBuilder } from "../..";
|
||||
import type { Entity, EntityData, EntityManager } from "../../entities";
|
||||
import { InvalidSearchParamsException } from "../../errors";
|
||||
import { MutatorEvents } from "../../events";
|
||||
import { RelationMutator } from "../../relations";
|
||||
import type { RepoQuery } from "../../server/query";
|
||||
import { MutatorResult, type MutatorResultOptions } from "./MutatorResult";
|
||||
|
||||
type MutatorQB =
|
||||
| InsertQueryBuilder<any, any, any>
|
||||
@@ -17,14 +18,6 @@ type MutatorUpdateOrDelete =
|
||||
| UpdateQueryBuilder<any, any, any, any>
|
||||
| DeleteQueryBuilder<any, any, any>;
|
||||
|
||||
export type MutatorResponse<T = EntityData[]> = {
|
||||
entity: Entity;
|
||||
sql: string;
|
||||
parameters: any[];
|
||||
result: EntityData[];
|
||||
data: T;
|
||||
};
|
||||
|
||||
export class Mutator<
|
||||
TBD extends object = DefaultDB,
|
||||
TB extends keyof TBD = any,
|
||||
@@ -103,35 +96,18 @@ export class Mutator<
|
||||
return validatedData as Given;
|
||||
}
|
||||
|
||||
protected async many(qb: MutatorQB): Promise<MutatorResponse> {
|
||||
const entity = this.entity;
|
||||
const { sql, parameters } = qb.compile();
|
||||
|
||||
try {
|
||||
const result = await qb.execute();
|
||||
|
||||
const data = this.em.hydrate(entity.name, result) as EntityData[];
|
||||
|
||||
return {
|
||||
entity,
|
||||
sql,
|
||||
parameters: [...parameters],
|
||||
result: result,
|
||||
data,
|
||||
};
|
||||
} catch (e) {
|
||||
// @todo: redact
|
||||
$console.error("[Error in query]", sql);
|
||||
throw e;
|
||||
}
|
||||
protected async performQuery<T = EntityData[]>(
|
||||
qb: MutatorQB,
|
||||
opts?: MutatorResultOptions,
|
||||
): Promise<MutatorResult<T>> {
|
||||
const result = new MutatorResult(this.em, this.entity, {
|
||||
silent: false,
|
||||
...opts,
|
||||
});
|
||||
return (await result.execute(qb)) as any;
|
||||
}
|
||||
|
||||
protected async single(qb: MutatorQB): Promise<MutatorResponse<EntityData>> {
|
||||
const { data, ...response } = await this.many(qb);
|
||||
return { ...response, data: data[0]! };
|
||||
}
|
||||
|
||||
async insertOne(data: Input): Promise<MutatorResponse<Output>> {
|
||||
async insertOne(data: Input): Promise<MutatorResult<Output>> {
|
||||
const entity = this.entity;
|
||||
if (entity.type === "system" && this.__unstable_disable_system_entity_creation) {
|
||||
throw new Error(`Creation of system entity "${entity.name}" is disabled`);
|
||||
@@ -174,7 +150,7 @@ export class Mutator<
|
||||
.values(validatedData)
|
||||
.returning(entity.getSelect());
|
||||
|
||||
const res = await this.single(query);
|
||||
const res = await this.performQuery(query, { single: true });
|
||||
|
||||
await this.emgr.emit(
|
||||
new Mutator.Events.MutatorInsertAfter({ entity, data: res.data, changed: validatedData }),
|
||||
@@ -183,7 +159,7 @@ export class Mutator<
|
||||
return res as any;
|
||||
}
|
||||
|
||||
async updateOne(id: PrimaryFieldType, data: Partial<Input>): Promise<MutatorResponse<Output>> {
|
||||
async updateOne(id: PrimaryFieldType, data: Partial<Input>): Promise<MutatorResult<Output>> {
|
||||
const entity = this.entity;
|
||||
if (!id) {
|
||||
throw new Error("ID must be provided for update");
|
||||
@@ -206,7 +182,7 @@ export class Mutator<
|
||||
.where(entity.id().name, "=", id)
|
||||
.returning(entity.getSelect());
|
||||
|
||||
const res = await this.single(query);
|
||||
const res = await this.performQuery(query, { single: true });
|
||||
|
||||
await this.emgr.emit(
|
||||
new Mutator.Events.MutatorUpdateAfter({
|
||||
@@ -220,7 +196,7 @@ export class Mutator<
|
||||
return res as any;
|
||||
}
|
||||
|
||||
async deleteOne(id: PrimaryFieldType): Promise<MutatorResponse<Output>> {
|
||||
async deleteOne(id: PrimaryFieldType): Promise<MutatorResult<Output>> {
|
||||
const entity = this.entity;
|
||||
if (!id) {
|
||||
throw new Error("ID must be provided for deletion");
|
||||
@@ -233,7 +209,7 @@ export class Mutator<
|
||||
.where(entity.id().name, "=", id)
|
||||
.returning(entity.getSelect());
|
||||
|
||||
const res = await this.single(query);
|
||||
const res = await this.performQuery(query, { single: true });
|
||||
|
||||
await this.emgr.emit(
|
||||
new Mutator.Events.MutatorDeleteAfter({ entity, entityId: id, data: res.data }),
|
||||
@@ -286,7 +262,7 @@ export class Mutator<
|
||||
}
|
||||
|
||||
// @todo: decide whether entries should be deleted all at once or one by one (for events)
|
||||
async deleteWhere(where: RepoQuery["where"]): Promise<MutatorResponse<Output[]>> {
|
||||
async deleteWhere(where: RepoQuery["where"]): Promise<MutatorResult<Output[]>> {
|
||||
const entity = this.entity;
|
||||
|
||||
// @todo: add a way to delete all by adding force?
|
||||
@@ -298,13 +274,13 @@ export class Mutator<
|
||||
entity.getSelect(),
|
||||
);
|
||||
|
||||
return (await this.many(qb)) as any;
|
||||
return await this.performQuery(qb);
|
||||
}
|
||||
|
||||
async updateWhere(
|
||||
data: Partial<Input>,
|
||||
where: RepoQuery["where"],
|
||||
): Promise<MutatorResponse<Output[]>> {
|
||||
): Promise<MutatorResult<Output[]>> {
|
||||
const entity = this.entity;
|
||||
const validatedData = await this.getValidatedData(data, "update");
|
||||
|
||||
@@ -317,10 +293,10 @@ export class Mutator<
|
||||
.set(validatedData as any)
|
||||
.returning(entity.getSelect());
|
||||
|
||||
return (await this.many(query)) as any;
|
||||
return await this.performQuery(query);
|
||||
}
|
||||
|
||||
async insertMany(data: Input[]): Promise<MutatorResponse<Output[]>> {
|
||||
async insertMany(data: Input[]): Promise<MutatorResult<Output[]>> {
|
||||
const entity = this.entity;
|
||||
if (entity.type === "system" && this.__unstable_disable_system_entity_creation) {
|
||||
throw new Error(`Creation of system entity "${entity.name}" is disabled`);
|
||||
@@ -352,6 +328,6 @@ export class Mutator<
|
||||
.values(validated)
|
||||
.returning(entity.getSelect());
|
||||
|
||||
return (await this.many(query)) as any;
|
||||
return await this.performQuery(query);
|
||||
}
|
||||
}
|
||||
33
app/src/data/entities/mutation/MutatorResult.ts
Normal file
33
app/src/data/entities/mutation/MutatorResult.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { $console } from "core/console";
|
||||
import type { Entity, EntityData } from "../Entity";
|
||||
import type { EntityManager } from "../EntityManager";
|
||||
import { Result, type ResultJSON, type ResultOptions } from "../Result";
|
||||
|
||||
export type MutatorResultOptions = ResultOptions & {
|
||||
silent?: boolean;
|
||||
};
|
||||
|
||||
export type MutatorResultJSON<T = EntityData[]> = ResultJSON<T>;
|
||||
|
||||
export class MutatorResult<T = EntityData[]> extends Result<T> {
|
||||
constructor(
|
||||
protected em: EntityManager<any>,
|
||||
public entity: Entity,
|
||||
options?: MutatorResultOptions,
|
||||
) {
|
||||
super(em.connection, {
|
||||
hydrator: (rows) => em.hydrate(entity.name, rows as any),
|
||||
beforeExecute: (compiled) => {
|
||||
if (!options?.silent) {
|
||||
$console.debug(`[Mutation]\n${compiled.sql}\n`, compiled.parameters);
|
||||
}
|
||||
},
|
||||
onError: (error) => {
|
||||
if (!options?.silent) {
|
||||
$console.error("[ERROR] Mutator:", error.message);
|
||||
}
|
||||
},
|
||||
...options,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -13,37 +13,11 @@ import {
|
||||
WithBuilder,
|
||||
} from "../index";
|
||||
import { JoinBuilder } from "./JoinBuilder";
|
||||
import { ensureInt } from "core/utils";
|
||||
import { RepositoryResult, type RepositoryResultOptions } from "./RepositoryResult";
|
||||
import type { ResultOptions } from "../Result";
|
||||
|
||||
export type RepositoryQB = SelectQueryBuilder<any, any, any>;
|
||||
|
||||
export type RepositoryRawResponse = {
|
||||
sql: string;
|
||||
parameters: any[];
|
||||
result: EntityData[];
|
||||
};
|
||||
export type RepositoryResponse<T = EntityData[]> = RepositoryRawResponse & {
|
||||
entity: Entity;
|
||||
data: T;
|
||||
meta: {
|
||||
items: number;
|
||||
total?: number;
|
||||
count?: number;
|
||||
time?: number;
|
||||
query?: {
|
||||
sql: string;
|
||||
parameters: readonly any[];
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
export type RepositoryCountResponse = RepositoryRawResponse & {
|
||||
count: number;
|
||||
};
|
||||
export type RepositoryExistsResponse = RepositoryRawResponse & {
|
||||
exists: boolean;
|
||||
};
|
||||
|
||||
export type RepositoryOptions = {
|
||||
silent?: boolean;
|
||||
includeCounts?: boolean;
|
||||
@@ -182,126 +156,18 @@ export class Repository<TBD extends object = DefaultDB, TB extends keyof TBD = a
|
||||
return validated;
|
||||
}
|
||||
|
||||
protected async executeQb(qb: RepositoryQB) {
|
||||
const compiled = qb.compile();
|
||||
if (this.options?.silent !== true) {
|
||||
$console.debug(`Repository: query\n${compiled.sql}\n`, compiled.parameters);
|
||||
}
|
||||
|
||||
let result: any;
|
||||
try {
|
||||
result = await qb.execute();
|
||||
} catch (e) {
|
||||
if (this.options?.silent !== true) {
|
||||
if (e instanceof Error) {
|
||||
$console.error("[ERROR] Repository.executeQb", e.message);
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
result,
|
||||
sql: compiled.sql,
|
||||
parameters: [...compiled.parameters],
|
||||
};
|
||||
}
|
||||
|
||||
protected async performQuery(qb: RepositoryQB): Promise<RepositoryResponse> {
|
||||
const entity = this.entity;
|
||||
const compiled = qb.compile();
|
||||
|
||||
const payload = {
|
||||
entity,
|
||||
sql: compiled.sql,
|
||||
parameters: [...compiled.parameters],
|
||||
result: [],
|
||||
data: [],
|
||||
meta: {
|
||||
total: 0,
|
||||
count: 0,
|
||||
items: 0,
|
||||
time: 0,
|
||||
query: { sql: compiled.sql, parameters: compiled.parameters },
|
||||
},
|
||||
};
|
||||
|
||||
// don't batch (add counts) if `includeCounts` is set to false
|
||||
// or when explicitly set to true and batching is not supported
|
||||
if (
|
||||
this.options?.includeCounts === false ||
|
||||
(this.options?.includeCounts === true && !this.em.connection.supports("batching"))
|
||||
) {
|
||||
const start = performance.now();
|
||||
const res = await this.executeQb(qb);
|
||||
const time = Number.parseFloat((performance.now() - start).toFixed(2));
|
||||
const result = res.result ?? [];
|
||||
const data = this.em.hydrate(entity.name, result);
|
||||
|
||||
return {
|
||||
...payload,
|
||||
result,
|
||||
data,
|
||||
meta: {
|
||||
...payload.meta,
|
||||
total: undefined,
|
||||
count: undefined,
|
||||
items: data.length,
|
||||
time,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (this.options?.silent !== true) {
|
||||
$console.debug(`Repository: query\n${compiled.sql}\n`, compiled.parameters);
|
||||
}
|
||||
|
||||
const selector = (as = "count") => this.conn.fn.countAll<number>().as(as);
|
||||
const countQuery = qb
|
||||
.clearSelect()
|
||||
.select(selector())
|
||||
.clearLimit()
|
||||
.clearOffset()
|
||||
.clearGroupBy()
|
||||
.clearOrderBy();
|
||||
const totalQuery = this.conn.selectFrom(entity.name).select(selector());
|
||||
|
||||
try {
|
||||
const start = performance.now();
|
||||
const [_count, _total, result] = await this.em.connection.batchQuery([
|
||||
countQuery,
|
||||
totalQuery,
|
||||
qb,
|
||||
]);
|
||||
|
||||
const time = Number.parseFloat((performance.now() - start).toFixed(2));
|
||||
const data = this.em.hydrate(entity.name, result);
|
||||
|
||||
return {
|
||||
...payload,
|
||||
result,
|
||||
data,
|
||||
meta: {
|
||||
...payload.meta,
|
||||
// parsing is important since pg returns string
|
||||
total: ensureInt(_total[0]?.count),
|
||||
count: ensureInt(_count[0]?.count),
|
||||
items: result.length,
|
||||
time,
|
||||
},
|
||||
};
|
||||
} catch (e) {
|
||||
if (this.options?.silent !== true) {
|
||||
if (e instanceof Error) {
|
||||
$console.error("[ERROR] Repository.performQuery", e.message);
|
||||
}
|
||||
|
||||
throw e;
|
||||
} else {
|
||||
return payload;
|
||||
}
|
||||
}
|
||||
protected async performQuery<T = EntityData[]>(
|
||||
qb: RepositoryQB,
|
||||
opts?: RepositoryResultOptions,
|
||||
execOpts?: { includeCounts?: boolean },
|
||||
): Promise<RepositoryResult<T>> {
|
||||
const result = new RepositoryResult(this.em, this.entity, {
|
||||
silent: this.options.silent,
|
||||
...opts,
|
||||
});
|
||||
return (await result.execute(qb, {
|
||||
includeCounts: execOpts?.includeCounts ?? this.options.includeCounts,
|
||||
})) as any;
|
||||
}
|
||||
|
||||
private async triggerFindBefore(entity: Entity, options: RepoQuery): Promise<void> {
|
||||
@@ -319,7 +185,7 @@ export class Repository<TBD extends object = DefaultDB, TB extends keyof TBD = a
|
||||
): Promise<void> {
|
||||
if (options.limit === 1) {
|
||||
await this.emgr.emit(
|
||||
new Repository.Events.RepositoryFindOneAfter({ entity, options, data: data[0]! }),
|
||||
new Repository.Events.RepositoryFindOneAfter({ entity, options, data }),
|
||||
);
|
||||
} else {
|
||||
await this.emgr.emit(
|
||||
@@ -331,12 +197,11 @@ export class Repository<TBD extends object = DefaultDB, TB extends keyof TBD = a
|
||||
protected async single(
|
||||
qb: RepositoryQB,
|
||||
options: RepoQuery,
|
||||
): Promise<RepositoryResponse<EntityData>> {
|
||||
): Promise<RepositoryResult<TBD[TB] | undefined>> {
|
||||
await this.triggerFindBefore(this.entity, options);
|
||||
const { data, ...response } = await this.performQuery(qb);
|
||||
|
||||
await this.triggerFindAfter(this.entity, options, data);
|
||||
return { ...response, data: data[0]! };
|
||||
const result = await this.performQuery(qb, { single: true });
|
||||
await this.triggerFindAfter(this.entity, options, result.data);
|
||||
return result as any;
|
||||
}
|
||||
|
||||
addOptionsToQueryBuilder(
|
||||
@@ -413,7 +278,7 @@ export class Repository<TBD extends object = DefaultDB, TB extends keyof TBD = a
|
||||
async findId(
|
||||
id: PrimaryFieldType,
|
||||
_options?: Partial<Omit<RepoQuery, "where" | "limit" | "offset">>,
|
||||
): Promise<RepositoryResponse<TBD[TB] | undefined>> {
|
||||
): Promise<RepositoryResult<TBD[TB] | undefined>> {
|
||||
const { qb, options } = this.buildQuery(
|
||||
{
|
||||
..._options,
|
||||
@@ -429,7 +294,7 @@ export class Repository<TBD extends object = DefaultDB, TB extends keyof TBD = a
|
||||
async findOne(
|
||||
where: RepoQuery["where"],
|
||||
_options?: Partial<Omit<RepoQuery, "where" | "limit" | "offset">>,
|
||||
): Promise<RepositoryResponse<TBD[TB] | undefined>> {
|
||||
): Promise<RepositoryResult<TBD[TB] | undefined>> {
|
||||
const { qb, options } = this.buildQuery({
|
||||
..._options,
|
||||
where,
|
||||
@@ -439,7 +304,7 @@ export class Repository<TBD extends object = DefaultDB, TB extends keyof TBD = a
|
||||
return (await this.single(qb, options)) as any;
|
||||
}
|
||||
|
||||
async findMany(_options?: Partial<RepoQuery>): Promise<RepositoryResponse<TBD[TB][]>> {
|
||||
async findMany(_options?: Partial<RepoQuery>): Promise<RepositoryResult<TBD[TB][]>> {
|
||||
const { qb, options } = this.buildQuery(_options);
|
||||
await this.triggerFindBefore(this.entity, options);
|
||||
|
||||
@@ -454,7 +319,7 @@ export class Repository<TBD extends object = DefaultDB, TB extends keyof TBD = a
|
||||
id: PrimaryFieldType,
|
||||
reference: string,
|
||||
_options?: Partial<Omit<RepoQuery, "limit" | "offset">>,
|
||||
): Promise<RepositoryResponse<EntityData>> {
|
||||
): Promise<RepositoryResult<EntityData>> {
|
||||
const entity = this.entity;
|
||||
const listable_relations = this.em.relations.listableRelationsOf(entity);
|
||||
const relation = listable_relations.find((r) => r.ref(reference).reference === reference);
|
||||
@@ -482,10 +347,10 @@ export class Repository<TBD extends object = DefaultDB, TB extends keyof TBD = a
|
||||
},
|
||||
};
|
||||
|
||||
return this.cloneFor(newEntity).findMany(findManyOptions);
|
||||
return this.cloneFor(newEntity).findMany(findManyOptions) as any;
|
||||
}
|
||||
|
||||
async count(where?: RepoQuery["where"]): Promise<RepositoryCountResponse> {
|
||||
async count(where?: RepoQuery["where"]): Promise<RepositoryResult<{ count: number }>> {
|
||||
const entity = this.entity;
|
||||
const options = this.getValidOptions({ where });
|
||||
|
||||
@@ -497,17 +362,18 @@ export class Repository<TBD extends object = DefaultDB, TB extends keyof TBD = a
|
||||
qb = WhereBuilder.addClause(qb, options.where);
|
||||
}
|
||||
|
||||
const { result, ...compiled } = await this.executeQb(qb);
|
||||
|
||||
return {
|
||||
sql: compiled.sql,
|
||||
parameters: [...compiled.parameters],
|
||||
result,
|
||||
count: result[0]?.count ?? 0,
|
||||
};
|
||||
return await this.performQuery(
|
||||
qb,
|
||||
{
|
||||
hydrator: (rows) => ({ count: rows[0]?.count ?? 0 }),
|
||||
},
|
||||
{ includeCounts: false },
|
||||
);
|
||||
}
|
||||
|
||||
async exists(where: Required<RepoQuery>["where"]): Promise<RepositoryExistsResponse> {
|
||||
async exists(
|
||||
where: Required<RepoQuery>["where"],
|
||||
): Promise<RepositoryResult<{ exists: boolean }>> {
|
||||
const entity = this.entity;
|
||||
const options = this.getValidOptions({ where });
|
||||
|
||||
@@ -517,13 +383,8 @@ export class Repository<TBD extends object = DefaultDB, TB extends keyof TBD = a
|
||||
// add mandatory where
|
||||
qb = WhereBuilder.addClause(qb, options.where!).limit(1);
|
||||
|
||||
const { result, ...compiled } = await this.executeQb(qb);
|
||||
|
||||
return {
|
||||
sql: compiled.sql,
|
||||
parameters: [...compiled.parameters],
|
||||
result,
|
||||
exists: result[0]!.count > 0,
|
||||
};
|
||||
return await this.performQuery(qb, {
|
||||
hydrator: (rows) => ({ exists: rows[0]?.count > 0 }),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
105
app/src/data/entities/query/RepositoryResult.ts
Normal file
105
app/src/data/entities/query/RepositoryResult.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import { $console } from "core/console";
|
||||
import type { Entity, EntityData } from "../Entity";
|
||||
import type { EntityManager } from "../EntityManager";
|
||||
import { Result, type ResultJSON, type ResultOptions } from "../Result";
|
||||
import type { Compilable, SelectQueryBuilder } from "kysely";
|
||||
import { ensureInt } from "core/utils";
|
||||
|
||||
export type RepositoryResultOptions = ResultOptions & {
|
||||
silent?: boolean;
|
||||
};
|
||||
|
||||
export type RepositoryResultJSON<T = EntityData[]> = ResultJSON<T>;
|
||||
|
||||
export class RepositoryResult<T = EntityData[]> extends Result<T> {
|
||||
constructor(
|
||||
protected em: EntityManager<any>,
|
||||
public entity: Entity,
|
||||
options?: RepositoryResultOptions,
|
||||
) {
|
||||
super(em.connection, {
|
||||
hydrator: (rows) => em.hydrate(entity.name, rows as any),
|
||||
beforeExecute: (compiled) => {
|
||||
if (!options?.silent) {
|
||||
$console.debug(`Query:\n${compiled.sql}\n`, compiled.parameters);
|
||||
}
|
||||
},
|
||||
onError: (error) => {
|
||||
if (options?.silent !== true) {
|
||||
$console.error("Repository:", String(error));
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
...options,
|
||||
});
|
||||
}
|
||||
|
||||
private shouldIncludeCounts(intent?: boolean) {
|
||||
if (intent === undefined) return this.conn.supports("softscans");
|
||||
return intent;
|
||||
}
|
||||
|
||||
override async execute(
|
||||
qb: SelectQueryBuilder<any, any, any>,
|
||||
opts?: { includeCounts?: boolean },
|
||||
) {
|
||||
const includeCounts = this.shouldIncludeCounts(opts?.includeCounts);
|
||||
|
||||
if (includeCounts) {
|
||||
const selector = (as = "count") => this.conn.kysely.fn.countAll<number>().as(as);
|
||||
const countQuery = qb
|
||||
.clearSelect()
|
||||
.select(selector())
|
||||
.clearLimit()
|
||||
.clearOffset()
|
||||
.clearGroupBy()
|
||||
.clearOrderBy();
|
||||
const totalQuery = this.conn.kysely.selectFrom(this.entity.name).select(selector());
|
||||
|
||||
const compiled = qb.compile();
|
||||
this.options.beforeExecute?.(compiled);
|
||||
|
||||
try {
|
||||
const start = performance.now();
|
||||
const [main, count, total] = await this.em.connection.executeQueries(
|
||||
compiled,
|
||||
countQuery,
|
||||
totalQuery,
|
||||
);
|
||||
this.time = Number.parseFloat((performance.now() - start).toFixed(2));
|
||||
this.results.push({
|
||||
...main,
|
||||
data: this.options.hydrator?.(main.rows as T[]),
|
||||
items: main.rows.length,
|
||||
count: ensureInt(count.rows[0]?.count ?? 0),
|
||||
total: ensureInt(total.rows[0]?.count ?? 0),
|
||||
time: this.time,
|
||||
sql: compiled.sql,
|
||||
parameters: [...compiled.parameters],
|
||||
});
|
||||
} catch (e) {
|
||||
if (this.options.onError) {
|
||||
await this.options.onError(e as Error);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
return await super.execute(qb);
|
||||
}
|
||||
|
||||
get count() {
|
||||
return this.first().count;
|
||||
}
|
||||
|
||||
get total() {
|
||||
return this.first().total;
|
||||
}
|
||||
|
||||
protected override additionalMetaKeys(): string[] {
|
||||
return ["count", "total"];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user