Skip to content

Commit

Permalink
feat!: remove result extension
Browse files Browse the repository at this point in the history
  • Loading branch information
SegaraRai committed Jan 6, 2025
1 parent f4f1473 commit 364b106
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 80 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-lizards-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fraci": minor
---

**BREAKING CHANGE:** Removed result extension.
Binary file modified prisma/dev.db
Binary file not shown.
39 changes: 6 additions & 33 deletions src/prisma.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const basePrisma = new PrismaClient();

const prisma = basePrisma.$extends(
createFractionalIndexingExtension({
prefix: "__fi_prefix_" as const,
fields: {
"article.fi": {
group: ["userId"],
Expand Down Expand Up @@ -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", () => {
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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")
Expand Down
48 changes: 1 addition & 47 deletions src/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ type PrismaBrand<M extends string, F extends string> = {
[PRISMA_BRAND]: { model: M; field: F };
};

type WithDefault<T, E, F> = Extract<T, E> extends never ? F : Extract<T, E>;

/**
* A union of all model names.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -240,12 +231,6 @@ type FieldsUnion<O extends FractionalIndexingExtensionOptions> = {
[K in keyof O["fields"]]: [K, O["fields"][K]];
}[keyof O["fields"]];

type Prefix<O extends FractionalIndexingExtensionOptions> = WithDefault<
O["prefix"],
string,
"__fi_"
>;

type FieldInfo<I extends FieldOptions, M extends ModelKey, W, X> = {
readonly I: I;
readonly value: FractionalIndex<I["digitBase"], I["lengthBase"], X>;
Expand Down Expand Up @@ -280,23 +265,6 @@ type PerModelFieldInfo<O extends FractionalIndexingExtensionOptions> = {
};
};

/**
* [result component](https://www.prisma.io/docs/orm/prisma-client/client-extensions/result) of the Prisma extension.
*/
type ExtensionResult<O extends FractionalIndexingExtensionOptions> = {
[M in keyof PerModelFieldInfo<O>]: {
[F in Extract<
keyof PerModelFieldInfo<O>[M],
string
> as `${Prefix<O>}${F}`]: {
needs: { [K in F]: true };
compute: (args: {
[K in F]: string;
}) => PerModelFieldInfo<O>[M][F]["value"];
};
};
};

/**
* [model component](https://www.prisma.io/docs/orm/prisma-client/client-extensions/model) of the Prisma extension.
*/
Expand All @@ -311,7 +279,6 @@ type ExtensionModel<O extends FractionalIndexingExtensionOptions> = {
type Extension<O extends FractionalIndexingExtensionOptions> = {
name: typeof EXTENSION_NAME;
model: ExtensionModel<O>;
result: ExtensionResult<O>;
};

function isIndexConflictError(
Expand All @@ -333,18 +300,14 @@ export function createFractionalIndexingExtension<
Options extends FractionalIndexingExtensionOptions
>({
fields,
prefix = "__fi_",
maxLength = DEFAULT_MAX_LENGTH,
maxRetries = DEFAULT_MAX_RETRIES,
}: Options) {
return Prisma.defineExtension((client) => {
type HelperValue = FractionalIndexingForPrisma<any, any, any, any, any>;

const helperMap = new Map<string, HelperValue>();
const extensionResult = Object.create(null) as Record<
ModelKey,
Record<string, { needs: {}; compute: (args: any) => any }>
>;

for (const [modelAndField, { lengthBase, digitBase }] of Object.entries(
fields
) as [string, FieldOptions][]) {
Expand All @@ -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<any> => {
if (!cursor) {
const firstItem = await (client as any)[model].findFirst({
Expand Down Expand Up @@ -452,7 +407,6 @@ export function createFractionalIndexingExtension<
return client.$extends({
name: EXTENSION_NAME,
model: extensionModel,
result: extensionResult,
} as unknown as Extension<Options>);
});
}

0 comments on commit 364b106

Please sign in to comment.