Skip to content

Commit 7a3f700

Browse files
steebchenclaude
andcommitted
refactor: replace nanoid with @steebchen/id
Migrate from nanoid to @steebchen/id library. Use id() for 20-char time-sortable IDs and random() for longer lengths. Updated to v1.3.0 which provides better ID generation with nanosecond precision. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent f0801cf commit 7a3f700

File tree

9 files changed

+59
-82
lines changed

9 files changed

+59
-82
lines changed

apps/api/src/auth/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Redis } from "ioredis";
77

88
import { sendTransactionalEmail } from "@/utils/email.js";
99

10-
import { db, eq, tables, shortid } from "@llmgateway/db";
10+
import { db, eq, tables, random } from "@llmgateway/db";
1111
import { logger } from "@llmgateway/logger";
1212

1313
const apiUrl = process.env.API_URL || "http://localhost:4002";
@@ -721,7 +721,7 @@ export const apiAuth: ReturnType<typeof betterAuth> = instrumentBetterAuth(
721721
// Generate a token with a prefix for better identification
722722
const prefix =
723723
process.env.NODE_ENV === "development" ? `llmgdev_` : "llmgtwy_";
724-
const token = prefix + shortid(40);
724+
const token = prefix + random(40);
725725

726726
await tx.insert(tables.apiKey).values({
727727
projectId: project.id,

apps/api/src/routes/keys-api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { z } from "zod";
44

55
import { maskToken } from "@/lib/maskToken.js";
66

7-
import { eq, db, shortid, tables } from "@llmgateway/db";
7+
import { eq, db, random, tables } from "@llmgateway/db";
88

99
import type { ServerTypes } from "@/vars.js";
1010

@@ -250,7 +250,7 @@ keysApi.openapi(create, async (c) => {
250250
// Generate a token with a prefix for better identification
251251
const prefix =
252252
process.env.NODE_ENV === "development" ? `llmgdev_` : "llmgtwy_";
253-
const token = prefix + shortid(40);
253+
const token = prefix + random(40);
254254

255255
// Create the API key
256256
const [apiKey] = await db

apps/api/src/routes/playground.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
22
import { getCookie, setCookie } from "hono/cookie";
33
import { HTTPException } from "hono/http-exception";
44

5-
import { db, tables, shortid } from "@llmgateway/db";
5+
import { db, tables, random } from "@llmgateway/db";
66

77
import type { ServerTypes } from "@/vars.js";
88

@@ -76,7 +76,7 @@ playground.openapi(ensureKey, async (c) => {
7676
if (!key) {
7777
const prefix =
7878
process.env.NODE_ENV === "development" ? `llmgdev_` : "llmgtwy_";
79-
const token = prefix + shortid(40);
79+
const token = prefix + random(40);
8080
[key] = await db
8181
.insert(tables.apiKey)
8282
.values({

apps/gateway/src/chat/chat.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
getProviderMetricsForCombinations,
2222
isCachingEnabled,
2323
type InferSelectModel,
24-
shortid,
24+
random,
2525
type tables,
2626
} from "@llmgateway/db";
2727
import { logger } from "@llmgateway/logger";
@@ -359,7 +359,7 @@ const completions = createRoute({
359359

360360
chat.openapi(completions, async (c) => {
361361
// Extract or generate request ID
362-
const requestId = c.req.header("x-request-id") || shortid(40);
362+
const requestId = c.req.header("x-request-id") || random(40);
363363

364364
// Parse JSON manually even if it's malformed
365365
let rawBody: unknown;

apps/playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@radix-ui/react-tabs": "^1.1.13",
3939
"@radix-ui/react-tooltip": "^1.2.8",
4040
"@radix-ui/react-use-controllable-state": "1.2.2",
41+
"@steebchen/id": "1.3.0",
4142
"@stripe/react-stripe-js": "5.2.0",
4243
"@stripe/stripe-js": "8.1.0",
4344
"@tanstack/react-query": "^5.90.5",
@@ -52,7 +53,6 @@
5253
"date-fns": "4.1.0",
5354
"embla-carousel-react": "8.6.0",
5455
"lucide-react": "0.548.0",
55-
"nanoid": "5.1.6",
5656
"next": "16.0.1",
5757
"next-themes": "^0.4.6",
5858
"openapi-fetch": "0.15.0",

apps/playground/src/components/ai-elements/prompt-input.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use client";
22

3+
import { id } from "@steebchen/id";
34
import {
45
ImageIcon,
56
Loader2Icon,
@@ -10,7 +11,6 @@ import {
1011
SquareIcon,
1112
XIcon,
1213
} from "lucide-react";
13-
import { nanoid } from "nanoid";
1414
import {
1515
type ChangeEvent,
1616
type ChangeEventHandler,
@@ -146,7 +146,7 @@ export function PromptInputProvider({
146146
setAttachements((prev) =>
147147
prev.concat(
148148
incoming.map((file) => ({
149-
id: nanoid(),
149+
id: id(),
150150
type: "file" as const,
151151
url: URL.createObjectURL(file),
152152
mediaType: file.type,
@@ -520,7 +520,7 @@ export const PromptInput = ({
520520
const next: (FileUIPart & { id: string })[] = [];
521521
for (const file of capped) {
522522
next.push({
523-
id: nanoid(),
523+
id: id(),
524524
type: "file",
525525
url: URL.createObjectURL(file),
526526
mediaType: file.type,

packages/db/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
"@llmgateway/cache": "workspace:*",
3131
"@llmgateway/logger": "workspace:*",
3232
"@opentelemetry/api": "1.9.0",
33+
"@steebchen/id": "1.3.0",
3334
"drizzle-orm": "1.0.0-beta.1",
34-
"nanoid": "5.1.5",
3535
"pg": "^8.16.3",
3636
"zod": "3.25.75"
3737
},

packages/db/src/schema.ts

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { id } from "@steebchen/id";
12
import { sql } from "drizzle-orm";
23
import {
34
boolean,
@@ -12,11 +13,12 @@ import {
1213
timestamp,
1314
unique,
1415
} from "drizzle-orm/pg-core";
15-
import { customAlphabet } from "nanoid";
1616

1717
import type { errorDetails, tools, toolChoice, toolResults } from "./types.js";
1818
import type z from "zod";
1919

20+
export { id, random } from "@steebchen/id";
21+
2022
export const UnifiedFinishReason = {
2123
COMPLETED: "completed",
2224
LENGTH_LIMIT: "length_limit",
@@ -32,14 +34,8 @@ export const UnifiedFinishReason = {
3234
export type UnifiedFinishReason =
3335
(typeof UnifiedFinishReason)[keyof typeof UnifiedFinishReason];
3436

35-
const generate = customAlphabet(
36-
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
37-
);
38-
39-
export const shortid = (size = 20) => generate(size);
40-
4137
export const user = pgTable("user", {
42-
id: text().primaryKey().$defaultFn(shortid),
38+
id: text().primaryKey().$defaultFn(id),
4339
createdAt: timestamp().notNull().defaultNow(),
4440
updatedAt: timestamp()
4541
.notNull()
@@ -55,7 +51,7 @@ export const user = pgTable("user", {
5551
export const session = pgTable(
5652
"session",
5753
{
58-
id: text().primaryKey().$defaultFn(shortid),
54+
id: text().primaryKey().$defaultFn(id),
5955
expiresAt: timestamp().notNull().defaultNow(),
6056
token: text().notNull().unique(),
6157
createdAt: timestamp().notNull().defaultNow(),
@@ -75,7 +71,7 @@ export const session = pgTable(
7571
export const account = pgTable(
7672
"account",
7773
{
78-
id: text().primaryKey().$defaultFn(shortid),
74+
id: text().primaryKey().$defaultFn(id),
7975
accountId: text().notNull(),
8076
providerId: text().notNull(),
8177
userId: text()
@@ -98,7 +94,7 @@ export const account = pgTable(
9894
);
9995

10096
export const verification = pgTable("verification", {
101-
id: text().primaryKey().$defaultFn(shortid),
97+
id: text().primaryKey().$defaultFn(id),
10298
identifier: text().notNull(),
10399
value: text().notNull(),
104100
expiresAt: timestamp().notNull().defaultNow(),
@@ -107,7 +103,7 @@ export const verification = pgTable("verification", {
107103
});
108104

109105
export const organization = pgTable("organization", {
110-
id: text().primaryKey().notNull().$defaultFn(shortid),
106+
id: text().primaryKey().notNull().$defaultFn(id),
111107
createdAt: timestamp().notNull().defaultNow(),
112108
updatedAt: timestamp()
113109
.notNull()
@@ -148,7 +144,7 @@ export const organization = pgTable("organization", {
148144
export const transaction = pgTable(
149145
"transaction",
150146
{
151-
id: text().primaryKey().notNull().$defaultFn(shortid),
147+
id: text().primaryKey().notNull().$defaultFn(id),
152148
createdAt: timestamp().notNull().defaultNow(),
153149
updatedAt: timestamp()
154150
.notNull()
@@ -185,7 +181,7 @@ export const transaction = pgTable(
185181
export const userOrganization = pgTable(
186182
"user_organization",
187183
{
188-
id: text().primaryKey().notNull().$defaultFn(shortid),
184+
id: text().primaryKey().notNull().$defaultFn(id),
189185
createdAt: timestamp().notNull().defaultNow(),
190186
updatedAt: timestamp()
191187
.notNull()
@@ -212,7 +208,7 @@ export const userOrganization = pgTable(
212208
export const project = pgTable(
213209
"project",
214210
{
215-
id: text().primaryKey().notNull().$defaultFn(shortid),
211+
id: text().primaryKey().notNull().$defaultFn(id),
216212
createdAt: timestamp().notNull().defaultNow(),
217213
updatedAt: timestamp()
218214
.notNull()
@@ -239,7 +235,7 @@ export const project = pgTable(
239235
export const apiKey = pgTable(
240236
"api_key",
241237
{
242-
id: text().primaryKey().notNull().$defaultFn(shortid),
238+
id: text().primaryKey().notNull().$defaultFn(id),
243239
createdAt: timestamp().notNull().defaultNow(),
244240
updatedAt: timestamp()
245241
.notNull()
@@ -268,7 +264,7 @@ export const apiKey = pgTable(
268264
export const apiKeyIamRule = pgTable(
269265
"api_key_iam_rule",
270266
{
271-
id: text().primaryKey().notNull().$defaultFn(shortid),
267+
id: text().primaryKey().notNull().$defaultFn(id),
272268
createdAt: timestamp().notNull().defaultNow(),
273269
updatedAt: timestamp()
274270
.notNull()
@@ -323,7 +319,7 @@ export interface ProviderKeyOptions {
323319
export const providerKey = pgTable(
324320
"provider_key",
325321
{
326-
id: text().primaryKey().notNull().$defaultFn(shortid),
322+
id: text().primaryKey().notNull().$defaultFn(id),
327323
createdAt: timestamp().notNull().defaultNow(),
328324
updatedAt: timestamp()
329325
.notNull()
@@ -350,7 +346,7 @@ export const providerKey = pgTable(
350346
export const log = pgTable(
351347
"log",
352348
{
353-
id: text().primaryKey().notNull().$defaultFn(shortid),
349+
id: text().primaryKey().notNull().$defaultFn(id),
354350
requestId: text().notNull(),
355351
createdAt: timestamp().notNull().defaultNow(),
356352
updatedAt: timestamp()
@@ -453,7 +449,7 @@ export const log = pgTable(
453449
export const passkey = pgTable(
454450
"passkey",
455451
{
456-
id: text().primaryKey().$defaultFn(shortid),
452+
id: text().primaryKey().$defaultFn(id),
457453
createdAt: timestamp().notNull().defaultNow(),
458454
updatedAt: timestamp()
459455
.notNull()
@@ -476,7 +472,7 @@ export const passkey = pgTable(
476472
export const paymentMethod = pgTable(
477473
"payment_method",
478474
{
479-
id: text().primaryKey().$defaultFn(shortid),
475+
id: text().primaryKey().$defaultFn(id),
480476
createdAt: timestamp().notNull().defaultNow(),
481477
updatedAt: timestamp().notNull().defaultNow(),
482478
stripePaymentMethodId: text().notNull(),
@@ -494,7 +490,7 @@ export const paymentMethod = pgTable(
494490
export const organizationAction = pgTable(
495491
"organization_action",
496492
{
497-
id: text().primaryKey().$defaultFn(shortid),
493+
id: text().primaryKey().$defaultFn(id),
498494
createdAt: timestamp().notNull().defaultNow(),
499495
updatedAt: timestamp()
500496
.notNull()
@@ -515,7 +511,7 @@ export const organizationAction = pgTable(
515511
);
516512

517513
export const lock = pgTable("lock", {
518-
id: text().primaryKey().$defaultFn(shortid),
514+
id: text().primaryKey().$defaultFn(id),
519515
createdAt: timestamp().notNull().defaultNow(),
520516
updatedAt: timestamp()
521517
.notNull()
@@ -527,7 +523,7 @@ export const lock = pgTable("lock", {
527523
export const chat = pgTable(
528524
"chat",
529525
{
530-
id: text().primaryKey().$defaultFn(shortid),
526+
id: text().primaryKey().$defaultFn(id),
531527
createdAt: timestamp().notNull().defaultNow(),
532528
updatedAt: timestamp()
533529
.notNull()
@@ -548,7 +544,7 @@ export const chat = pgTable(
548544
export const message = pgTable(
549545
"message",
550546
{
551-
id: text().primaryKey().$defaultFn(shortid),
547+
id: text().primaryKey().$defaultFn(id),
552548
createdAt: timestamp().notNull().defaultNow(),
553549
updatedAt: timestamp()
554550
.notNull()
@@ -570,7 +566,7 @@ export const message = pgTable(
570566
);
571567

572568
export const installation = pgTable("installation", {
573-
id: text().primaryKey().$defaultFn(shortid),
569+
id: text().primaryKey().$defaultFn(id),
574570
createdAt: timestamp().notNull().defaultNow(),
575571
updatedAt: timestamp()
576572
.notNull()
@@ -648,7 +644,7 @@ export const model = pgTable(
648644
export const modelProviderMapping = pgTable(
649645
"model_provider_mapping",
650646
{
651-
id: text().primaryKey().$defaultFn(shortid),
647+
id: text().primaryKey().$defaultFn(id),
652648
createdAt: timestamp().notNull().defaultNow(),
653649
updatedAt: timestamp()
654650
.notNull()
@@ -703,7 +699,7 @@ export const modelProviderMapping = pgTable(
703699
export const modelProviderMappingHistory = pgTable(
704700
"model_provider_mapping_history",
705701
{
706-
id: text().primaryKey().$defaultFn(shortid),
702+
id: text().primaryKey().$defaultFn(id),
707703
createdAt: timestamp().notNull().defaultNow(),
708704
updatedAt: timestamp()
709705
.notNull()
@@ -752,7 +748,7 @@ export const modelProviderMappingHistory = pgTable(
752748
export const modelHistory = pgTable(
753749
"model_history",
754750
{
755-
id: text().primaryKey().$defaultFn(shortid),
751+
id: text().primaryKey().$defaultFn(id),
756752
createdAt: timestamp().notNull().defaultNow(),
757753
updatedAt: timestamp()
758754
.notNull()

0 commit comments

Comments
 (0)