Skip to content

Commit

Permalink
chore!: rename methods and types
Browse files Browse the repository at this point in the history
  • Loading branch information
SegaraRai committed Jan 8, 2025
1 parent c0a5d80 commit d46c49a
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 145 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-beers-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fraci": minor
---

**BREAKING CHANGE** Renamed methods and types.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ Step 3. Use the Prisma extension of `fraci`.
```typescript
import { PrismaClient } from "@prisma/client";
import { BASE64 } from "fraci";
import { createFractionalIndexingExtension } from "fraci/prisma";
import { fraciExtension } from "fraci/prisma";

const prisma = new PrismaClient().$extends(
createFractionalIndexingExtension({
fraciExtension({
fields: {
// Here, we define the fractional index column.
// The notation is as follows: `table.column`.
Expand Down Expand Up @@ -106,17 +106,17 @@ Step 4. CRUD operations.
```typescript
// Get the helper object.
// Only predefined table and column name combinations are available due to strong typing.
const afi = prisma.article.fractionalIndexing("fi");
// ^table ^column
const afi = prisma.article.fraci("fi");
// ^ Table ^ Column

/**
* Create (append)
* Append a new article to the end.
*/
async function append() {
// Get the fractional indices to generate the new one that represents the last index.
const indices = await afi.getIndicesForLast({ userId: 1 });
// ^ Here, it's required to specify all columns specified in the `group` property above.
const indices = await afi.indicesForLast({ userId: 1 });
// ^ Here, it's required to specify all columns specified in the `group` property above.

// Generate a new fractional index.
// Note that the `generateKeyBetween` method is a generator to handle conflicts.
Expand Down Expand Up @@ -167,7 +167,7 @@ async function list() {
* Move article 3 to the position after article 4.
*/
async function move() {
const indices = await afi.getIndicesAfter({ id: 4 }, { userId: 1 });
const indices = await afi.indicesForAfter({ id: 4 }, { userId: 1 });
// ^ Here, one or more properties must be specified that uniquely identify the row.
// ^ Here, it's required to specify all columns specified in the `group` property above.
if (!indices) {
Expand Down
Binary file modified prisma/dev.db
Binary file not shown.
14 changes: 7 additions & 7 deletions src/examples/prisma/server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Prisma, PrismaClient } from "@prisma/client";
import { zValidator } from "@hono/zod-validator";
import { createFractionalIndexingExtension } from "fraci/prisma";
import { fraciExtension } from "fraci/prisma";
import { Hono } from "hono";
import * as z from "zod";
import { BASE64 } from "../../bases";

const prisma = new PrismaClient().$extends(
createFractionalIndexingExtension({
fraciExtension({
fields: {
"exampleItem.fi": {
group: ["groupId"],
Expand Down Expand Up @@ -65,8 +65,8 @@ const app = new Hono()
const groupId = Number(c.req.param("groupId"));
const { name } = c.req.valid("json");

const fiHelper = prisma.exampleItem.fractionalIndexing("fi");
const indices = await fiHelper.getIndicesForLast({ groupId });
const fiHelper = prisma.exampleItem.fraci("fi");
const indices = await fiHelper.indicesForLast({ groupId });

const delay = Number(c.req.query("delay") ?? "0");
if (delay > 0) {
Expand Down Expand Up @@ -124,12 +124,12 @@ const app = new Hono()
const itemId = Number(c.req.param("itemId"));
const { before, after } = c.req.valid("json");

const fiHelper = prisma.exampleItem.fractionalIndexing("fi");
const fiHelper = prisma.exampleItem.fraci("fi");

const indices =
before != null
? await fiHelper.getIndicesBefore({ id: before }, { groupId })
: await fiHelper.getIndicesAfter({ id: after }, { groupId });
? await fiHelper.indicesForBefore({ id: before }, { groupId })
: await fiHelper.indicesForAfter({ id: after }, { groupId });
if (!indices) {
return c.json({ error: "Reference item not found" }, 404);
}
Expand Down
16 changes: 8 additions & 8 deletions src/factory.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { describe, expect, it } from "bun:test";
import { createFractionalIndexing, DEFAULT_MAX_LENGTH } from "./factory.js";
import { fraci, DEFAULT_MAX_LENGTH } from "./factory.js";

describe("createFractionalIndexing", () => {
describe("fraci", () => {
const digitBase = "0123456789";
const lengthBase = "0123456789";

it("should create a FractionalIndexing instance with default options", () => {
const indexing = createFractionalIndexing({ digitBase, lengthBase });
it("should create a fraci instance with default options", () => {
const indexing = fraci({ digitBase, lengthBase });
expect(indexing.digitBase).toBe(digitBase);
expect(indexing.lengthBase).toBe(lengthBase);
});

it("should generate a key between two indices", () => {
const indexing = createFractionalIndexing({ digitBase, lengthBase });
const indexing = fraci({ digitBase, lengthBase });
const generator = indexing.generateKeyBetween(null, null);
const key = generator.next().value;
expect(typeof key).toBe("string");
expect(key!.length).toBeLessThanOrEqual(DEFAULT_MAX_LENGTH);
});

it("should generate multiple keys between two indices", () => {
const indexing = createFractionalIndexing({ digitBase, lengthBase });
const indexing = fraci({ digitBase, lengthBase });
const generator = indexing.generateNKeysBetween(null, null, 5);
const keys = generator.next().value;
expect(Array.isArray(keys)).toBe(true);
Expand All @@ -32,7 +32,7 @@ describe("createFractionalIndexing", () => {
});

it("should throw an error if maximum length is exceeded", () => {
const indexing = createFractionalIndexing({
const indexing = fraci({
digitBase,
lengthBase,
maxLength: 5,
Expand All @@ -45,7 +45,7 @@ describe("createFractionalIndexing", () => {
});

it("should stop generation when reached maxRetries", () => {
const indexing = createFractionalIndexing({
const indexing = fraci({
digitBase,
lengthBase,
maxRetries: 3,
Expand Down
22 changes: 6 additions & 16 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const DEFAULT_MAX_RETRIES = 10;
/**
* Fractional index pattern for demonstration.
*/
type FractionalIndexPattern<D extends string, L extends string> = `${L}${D}...`;
type IndexPattern<D extends string, L extends string> = `${L}${D}...`;

/**
* A helper type to convert a string to a union of characters.
Expand All @@ -25,11 +25,8 @@ type StringToUnion<T extends string> = T extends `${infer First}${infer Rest}`
/**
* Fractional indexing utility.
*/
export interface FractionalIndexing<D extends string, L extends string, X> {
readonly __EXAMPLE__?: FractionalIndexPattern<
StringToUnion<D>,
StringToUnion<L>
>;
export interface Fraci<D extends string, L extends string, X> {
readonly __EXAMPLE__?: IndexPattern<StringToUnion<D>, StringToUnion<L>>;

readonly digitBase: D;
readonly lengthBase: L;
Expand All @@ -47,10 +44,7 @@ export interface FractionalIndexing<D extends string, L extends string, X> {
/**
* Fractional indexing factory options.
*/
export interface FractionalIndexingFactoryOptions<
D extends string,
L extends string
> {
export interface FraciOptions<D extends string, L extends string> {
digitBase: D;
lengthBase: L;
maxLength?: number;
Expand All @@ -63,16 +57,12 @@ export interface FractionalIndexingFactoryOptions<
* @param options Options
* @returns Fractional indexing utility
*/
export function createFractionalIndexing<
D extends string,
L extends string,
X
>({
export function fraci<D extends string, L extends string, X>({
digitBase,
lengthBase,
maxLength = DEFAULT_MAX_LENGTH,
maxRetries = DEFAULT_MAX_RETRIES,
}: FractionalIndexingFactoryOptions<D, L>): FractionalIndexing<D, L, X> {
}: FraciOptions<D, L>): Fraci<D, L, X> {
type F = FractionalIndex<D, L, X>;

const [digBaseForward, digBaseReverse] = createDigitBaseMap(digitBase);
Expand Down
Loading

0 comments on commit d46c49a

Please sign in to comment.