feat: Allow passing id in DB hook create (#3048)

* feat(database-hooks): Allow passing `id` in DB hook `create`

It's the same to using a custom `idGenerator`, except configurable by the database hook which would in theory provide more data.

A use-case is to generate the id based on user info in the user before DB hook.

Solves https://discord.com/channels/1288403910284935179/1379190465588367540/1384217435535835216

* chore: lint

* fix: tests failing
This commit is contained in:
Maxwell
2025-06-21 01:25:42 +10:00
committed by GitHub
parent f12e345beb
commit 9714131eaf
3 changed files with 5 additions and 2 deletions

View File

@@ -318,7 +318,10 @@ export const createAdapter =
!config.disableIdGeneration &&
!options.advanced?.database?.useNumberId
) {
fields.id = idField({ customModelName: unsafe_model, forceAllowId });
fields.id = idField({
customModelName: unsafe_model,
forceAllowId: forceAllowId && "id" in data,
});
}
for (const field in fields) {
const value = data[field];

View File

@@ -4,7 +4,6 @@ import { APIError } from "better-call";
import { getSessionFromCtx } from "./session";
import { setSessionCookie } from "../../cookies";
import type { GenericEndpointContext, User } from "../../types";
import { BASE_ERROR_CODES } from "../../error/codes";
import { jwtVerify, type JWTPayload, type JWTVerifyResult } from "jose";
import { signJWT } from "../../crypto/jwt";
import { originCheck } from "../middlewares";

View File

@@ -53,6 +53,7 @@ export function getWithHooks(
? await adapter.create<T>({
model,
data: actualData as any,
forceAllowId: true,
})
: customCreated;