From 364b1062938039c441a723794c1e442057f7839b Mon Sep 17 00:00:00 2001 From: SegaraRai Date: Mon, 6 Jan 2025 21:51:00 +0900 Subject: [PATCH] feat!: remove result extension --- .changeset/loud-lizards-sparkle.md | 5 +++ prisma/dev.db | Bin 69632 -> 69632 bytes src/prisma.test.ts | 39 ++++------------------- src/prisma.ts | 48 +---------------------------- 4 files changed, 12 insertions(+), 80 deletions(-) create mode 100644 .changeset/loud-lizards-sparkle.md diff --git a/.changeset/loud-lizards-sparkle.md b/.changeset/loud-lizards-sparkle.md new file mode 100644 index 0000000..3b6c618 --- /dev/null +++ b/.changeset/loud-lizards-sparkle.md @@ -0,0 +1,5 @@ +--- +"fraci": minor +--- + +**BREAKING CHANGE:** Removed result extension. diff --git a/prisma/dev.db b/prisma/dev.db index b47a27d99adddd33cdbf8f3c9e501c6493e213e8..2bf8e399ce3fa1969d73a15db19fd5fa75d6ba28 100644 GIT binary patch delta 38 lcmZozz|ydQWrDO|GXn#IAP~cV<3t@}#^%O^tqF_^_yL$32lM~{ delta 38 lcmZozz|ydQWrDO|Ap--0AP~cV{X`vO#=^#gtqF_^_yLt?2hIQh diff --git a/src/prisma.test.ts b/src/prisma.test.ts index 7241c51..c18e73e 100644 --- a/src/prisma.test.ts +++ b/src/prisma.test.ts @@ -8,7 +8,6 @@ const basePrisma = new PrismaClient(); const prisma = basePrisma.$extends( createFractionalIndexingExtension({ - prefix: "__fi_prefix_" as const, fields: { "article.fi": { group: ["userId"], @@ -274,44 +273,18 @@ test("instantiation type check", () => { describe("basic", () => { test("photo.fi", async () => { - const photo = await prisma.photo.findUniqueOrThrow({ where: { id: 1 } }); - expect(photo.__fi_prefix_fi as string).toBe(photo.fi); - expect(prisma.photo.fractionalIndexing("fi")).toBeObject(); }); test("should not exist in photo.title", async () => { - const photo = await prisma.photo.findUniqueOrThrow({ where: { id: 1 } }); - - // @ts-expect-error - expect(photo.__fi_prefix_title).toBeUndefined(); - // @ts-expect-error expect(prisma.photo.fractionalIndexing("title")).toBeUndefined(); }); test("should not exist in tag.name", async () => { - const tag = await prisma.tag.findUniqueOrThrow({ where: { id: 1 } }); - - // @ts-expect-error - expect(tag.__fi_prefix_name).toBeUndefined(); - // @ts-expect-error expect(prisma.tag.fractionalIndexing("name")).toBeUndefined(); }); - - test("tagsOnPhotos", async () => { - const item = await prisma.tagsOnPhotos.findUniqueOrThrow({ - where: { id: 1 }, - }); - - expect(item.__fi_prefix_tagFI as string).toBe(item.tagFI); - - expect(item.__fi_prefix_photoFI as string).toBe(item.photoFI); - - // @ts-expect-error - item.__fi_prefix_tagFI === item.__fi_prefix_photoFI; - }); }); describe("getIndicesBefore and getIndicesAfter", () => { @@ -329,7 +302,7 @@ describe("getIndicesBefore and getIndicesAfter", () => { .fractionalIndexing("fi") .getIndicesBefore(null, { userId: 1 }); expect(indicesForLast1).toBeArrayOfSize(2); - expect(indicesForLast1[0]).toBe(articles[1].__fi_prefix_fi); + expect(indicesForLast1[0] as string).toBe(articles[1].fi); expect(indicesForLast1[1]).toBeNull(); const indicesForLast2 = await prisma.article @@ -342,7 +315,7 @@ describe("getIndicesBefore and getIndicesAfter", () => { .getIndicesAfter(null, { userId: 1 }); expect(indicesForFirst1).toBeArrayOfSize(2); expect(indicesForFirst1[0]).toBeNull(); - expect(indicesForFirst1[1]).toBe(articles[0].__fi_prefix_fi); + expect(indicesForFirst1[1] as string).toBe(articles[0].fi); const indicesForFirst2 = await prisma.article .fractionalIndexing("fi") @@ -353,8 +326,8 @@ describe("getIndicesBefore and getIndicesAfter", () => { .fractionalIndexing("fi") .getIndicesBefore({ id: articles[1].id }, { userId: 1 }); expect(indicesForMiddle1).toBeArrayOfSize(2); - expect(indicesForMiddle1?.[0]).toBe(articles[0].__fi_prefix_fi); - expect(indicesForMiddle1?.[1]).toBe(articles[1].__fi_prefix_fi); + expect(indicesForMiddle1?.[0] as string).toBe(articles[0].fi); + expect(indicesForMiddle1?.[1] as string).toBe(articles[1].fi); const indicesForMiddle2 = await prisma.article .fractionalIndexing("fi") @@ -375,7 +348,7 @@ describe("getIndicesBefore and getIndicesAfter", () => { .fractionalIndexing("fi") .getIndicesBefore(null, { articleId: 2, userId: 1 }); expect(indicesForLast1).toBeArrayOfSize(2); - expect(indicesForLast1[0]).toBe(photos[0].__fi_prefix_fi); + expect(indicesForLast1[0] as string).toBe(photos[0].fi); expect(indicesForLast1[1]).toBeNull(); const indicesForLast2 = await prisma.photo @@ -388,7 +361,7 @@ describe("getIndicesBefore and getIndicesAfter", () => { .getIndicesAfter(null, { articleId: 2, userId: 1 }); expect(indicesForFirst1).toBeArrayOfSize(2); expect(indicesForFirst1[0]).toBeNull(); - expect(indicesForFirst1[1]).toBe(photos[0].__fi_prefix_fi); + expect(indicesForFirst1[1] as string).toBe(photos[0].fi); const indicesForFirst2 = await prisma.photo .fractionalIndexing("fi") diff --git a/src/prisma.ts b/src/prisma.ts index a32d426..c037868 100644 --- a/src/prisma.ts +++ b/src/prisma.ts @@ -31,8 +31,6 @@ type PrismaBrand = { [PRISMA_BRAND]: { model: M; field: F }; }; -type WithDefault = Extract extends never ? F : Extract; - /** * A union of all model names. * @@ -206,13 +204,6 @@ type FieldOptionsRecord = { }; export interface FractionalIndexingExtensionOptions { - /** - * The prefix for the fractional index fields. - * - * @default "__fi_" - */ - readonly prefix?: string; - /** * The maximum number of retries to generate a fractional index. * @@ -240,12 +231,6 @@ type FieldsUnion = { [K in keyof O["fields"]]: [K, O["fields"][K]]; }[keyof O["fields"]]; -type Prefix = WithDefault< - O["prefix"], - string, - "__fi_" ->; - type FieldInfo = { readonly I: I; readonly value: FractionalIndex; @@ -280,23 +265,6 @@ type PerModelFieldInfo = { }; }; -/** - * [result component](https://www.prisma.io/docs/orm/prisma-client/client-extensions/result) of the Prisma extension. - */ -type ExtensionResult = { - [M in keyof PerModelFieldInfo]: { - [F in Extract< - keyof PerModelFieldInfo[M], - string - > as `${Prefix}${F}`]: { - needs: { [K in F]: true }; - compute: (args: { - [K in F]: string; - }) => PerModelFieldInfo[M][F]["value"]; - }; - }; -}; - /** * [model component](https://www.prisma.io/docs/orm/prisma-client/client-extensions/model) of the Prisma extension. */ @@ -311,7 +279,6 @@ type ExtensionModel = { type Extension = { name: typeof EXTENSION_NAME; model: ExtensionModel; - result: ExtensionResult; }; function isIndexConflictError( @@ -333,7 +300,6 @@ export function createFractionalIndexingExtension< Options extends FractionalIndexingExtensionOptions >({ fields, - prefix = "__fi_", maxLength = DEFAULT_MAX_LENGTH, maxRetries = DEFAULT_MAX_RETRIES, }: Options) { @@ -341,10 +307,7 @@ export function createFractionalIndexingExtension< type HelperValue = FractionalIndexingForPrisma; const helperMap = new Map(); - const extensionResult = Object.create(null) as Record< - ModelKey, - Record any }> - >; + for (const [modelAndField, { lengthBase, digitBase }] of Object.entries( fields ) as [string, FieldOptions][]) { @@ -364,14 +327,6 @@ export function createFractionalIndexingExtension< maxRetries, }); - extensionResult[model] ??= Object.create(null); - extensionResult[model][`${prefix}${field}`] = { - needs: { [field]: true }, - compute({ [field]: value }) { - return value; - }, - }; - const getIndicesAfter = async (cursor: any, where: any): Promise => { if (!cursor) { const firstItem = await (client as any)[model].findFirst({ @@ -452,7 +407,6 @@ export function createFractionalIndexingExtension< return client.$extends({ name: EXTENSION_NAME, model: extensionModel, - result: extensionResult, } as unknown as Extension); }); }