various fixes: refactored imports, introduced fromDriver/toDriver to improve compat

This commit is contained in:
dswbx
2025-06-13 21:15:29 +02:00
parent cc038a0a9a
commit 2ada4e9f20
15 changed files with 100 additions and 35 deletions

View File

@@ -278,6 +278,10 @@ export class EntityManager<TBD extends object = DefaultDB> {
row[key] = field.getDefault();
}
// transform from driver
value = this.connection.fromDriver(value, field);
// transform from field
row[key] = field.transformRetrieve(value as any);
} catch (e: any) {
throw new TransformRetrieveFailedException(

View File

@@ -1,13 +1,15 @@
import { $console, type DB as DefaultDB, type PrimaryFieldType } from "core";
import type { DB as DefaultDB, PrimaryFieldType } from "core";
import { type EmitsEvents, EventManager } from "core/events";
import type { DeleteQueryBuilder, InsertQueryBuilder, UpdateQueryBuilder } from "kysely";
import { type TActionContext, WhereBuilder } from "../..";
import type { TActionContext } from "../..";
import { WhereBuilder } from "../query/WhereBuilder";
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";
import { transformObject } from "core/utils";
type MutatorQB =
| InsertQueryBuilder<any, any, any>
@@ -86,7 +88,11 @@ export class Mutator<
throw new Error(`Field "${key}" is not fillable on entity "${entity.name}"`);
}
// transform from field
validatedData[key] = await field.transformPersist(data[key], this.em, context);
// transform to driver
validatedData[key] = this.em.connection.toDriver(validatedData[key], field);
}
if (Object.keys(validatedData).length === 0) {
@@ -283,6 +289,10 @@ export class Mutator<
): Promise<MutatorResult<Output[]>> {
const entity = this.entity;
const validatedData = await this.getValidatedData(data, "update");
console.log("updateWhere", {
entity,
validatedData,
});
// @todo: add a way to delete all by adding force?
if (!where || typeof where !== "object" || Object.keys(where).length === 0) {

View File

@@ -5,6 +5,7 @@ import { Result, type ResultJSON, type ResultOptions } from "../Result";
export type MutatorResultOptions = ResultOptions & {
silent?: boolean;
logParams?: boolean;
};
export type MutatorResultJSON<T = EntityData[]> = ResultJSON<T>;
@@ -19,7 +20,10 @@ export class MutatorResult<T = EntityData[]> extends Result<T> {
hydrator: (rows) => em.hydrate(entity.name, rows as any),
beforeExecute: (compiled) => {
if (!options?.silent) {
$console.debug(`[Mutation]\n${compiled.sql}\n`);
$console.debug(
`[Mutation]\n${compiled.sql}\n`,
options?.logParams ? compiled.parameters : undefined,
);
}
},
onError: (error) => {

View File

@@ -246,8 +246,10 @@ export class Repository<TBD extends object = DefaultDB, TB extends keyof TBD = a
qb = WhereBuilder.addClause(qb, options.where);
}
if (!ignore.includes("limit")) qb = qb.limit(options.limit ?? defaults.limit);
if (!ignore.includes("offset")) qb = qb.offset(options.offset ?? defaults.offset);
if (!ignore.includes("limit")) {
qb = qb.limit(options.limit ?? defaults.limit);
if (!ignore.includes("offset")) qb = qb.offset(options.offset ?? defaults.offset);
}
// sorting
if (!ignore.includes("sort")) {