fix(create-adapter): transformWhere should account for customTransformInput (#2437)

This commit is contained in:
Maxwell
2025-06-28 15:55:02 +10:00
committed by GitHub
parent 3119cadf0d
commit c13d1867a8
2 changed files with 41 additions and 5 deletions

View File

@@ -486,6 +486,8 @@ export const createAdapter =
? undefined
: CleanedWhere[] => {
if (!where) return undefined as any;
const newMappedKeys = config.mapKeysTransformInput ?? {};
return where.map((w) => {
const {
field: unsafe_field,
@@ -504,11 +506,13 @@ export const createAdapter =
field: unsafe_field,
model,
});
const fieldName: string =
newMappedKeys[defaultFieldName] ||
getFieldName({
field: defaultFieldName,
model: defaultModelName,
});
const fieldName = getFieldName({
field: defaultFieldName,
model: defaultModelName,
});
const fieldAttr = getFieldAttributes({
field: defaultFieldName,
model: defaultModelName,

View File

@@ -1,6 +1,10 @@
import { describe, test, expect } from "vitest";
import { createAdapter } from "..";
import type { AdapterConfig, CreateCustomAdapter } from "../types";
import type {
AdapterConfig,
CleanedWhere,
CreateCustomAdapter,
} from "../types";
import type { BetterAuthOptions, User, Where } from "../../../types";
import { betterAuth } from "../../../auth";
@@ -559,6 +563,7 @@ describe("Create Adapter Helper", async () => {
};
},
});
const res = (await adapter.create({
model: "user",
data: { email: "test@test.com" },
@@ -572,6 +577,33 @@ describe("Create Adapter Helper", async () => {
expect(parameters.data.email_address).toEqual("test@test.com");
});
test("Should allow custom transform input to transform the where clause", async () => {
const parameters: CleanedWhere[] = await new Promise(async (r) => {
const adapter = await createTestAdapter({
config: {
debugLogs: {},
mapKeysTransformInput: {
id: "_id",
},
},
adapter(args_0) {
return {
async findOne({ model, where, select }) {
r(where);
return {} as any;
},
};
},
});
adapter.findOne({
model: "user",
where: [{ field: "id", value: "123" }],
});
});
expect(parameters[0]!.field).toEqual("_id");
});
test("Should allow custom map output key transformation", async () => {
const parameters: {
data: { email: string };