prisma-prefixed-ids 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/src/__tests__/index.test.ts +50 -3
- package/src/index.ts +6 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { PrismaClient } from "@prisma/client";
|
|
2
2
|
type ModelName = string;
|
|
3
3
|
export type PrefixConfig<ModelName extends string> = {
|
|
4
|
-
prefixes: Record<ModelName, string
|
|
4
|
+
prefixes: Partial<Record<ModelName, string>>;
|
|
5
5
|
idGenerator?: (prefix: string) => string;
|
|
6
6
|
};
|
|
7
7
|
type QueryArgs = {
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,7 @@ exports.createPrefixedIdsExtension = createPrefixedIdsExtension;
|
|
|
4
4
|
exports.extendPrismaClient = extendPrismaClient;
|
|
5
5
|
const nanoid_1 = require("nanoid");
|
|
6
6
|
const defaultIdGenerator = (prefix) => {
|
|
7
|
-
const nanoid = (0, nanoid_1.customAlphabet)("
|
|
7
|
+
const nanoid = (0, nanoid_1.customAlphabet)("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 24);
|
|
8
8
|
return `${prefix}_${nanoid()}`;
|
|
9
9
|
};
|
|
10
10
|
function createPrefixedIdsExtension(config) {
|
package/package.json
CHANGED
|
@@ -1,13 +1,30 @@
|
|
|
1
1
|
import { jest } from "@jest/globals";
|
|
2
2
|
|
|
3
3
|
import { PrismaClient } from "@prisma/client";
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
createPrefixedIdsExtension,
|
|
6
|
+
extendPrismaClient,
|
|
7
|
+
PrefixConfig,
|
|
8
|
+
} from "../index";
|
|
9
|
+
|
|
10
|
+
type MockUser = {
|
|
11
|
+
id: string;
|
|
12
|
+
name: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type CreateArgs = {
|
|
16
|
+
data: Record<string, unknown>;
|
|
17
|
+
};
|
|
5
18
|
|
|
6
19
|
// Mock PrismaClient
|
|
7
20
|
jest.mock("@prisma/client", () => {
|
|
8
21
|
return {
|
|
9
22
|
PrismaClient: jest.fn().mockImplementation(() => ({
|
|
10
23
|
$extends: jest.fn(),
|
|
24
|
+
user: {
|
|
25
|
+
create: jest.fn(),
|
|
26
|
+
findUnique: jest.fn(),
|
|
27
|
+
},
|
|
11
28
|
})),
|
|
12
29
|
};
|
|
13
30
|
});
|
|
@@ -18,12 +35,12 @@ jest.mock("nanoid", () => ({
|
|
|
18
35
|
}));
|
|
19
36
|
|
|
20
37
|
describe("PrefixedIdsExtension", () => {
|
|
21
|
-
let prisma: PrismaClient
|
|
38
|
+
let prisma: jest.Mocked<PrismaClient>;
|
|
22
39
|
const mockQuery = jest.fn((args: any) => Promise.resolve(args));
|
|
23
40
|
|
|
24
41
|
beforeEach(() => {
|
|
25
42
|
jest.clearAllMocks();
|
|
26
|
-
prisma = new PrismaClient()
|
|
43
|
+
prisma = new PrismaClient() as jest.Mocked<PrismaClient>;
|
|
27
44
|
});
|
|
28
45
|
|
|
29
46
|
describe("createPrefixedIdsExtension", () => {
|
|
@@ -115,6 +132,36 @@ describe("PrefixedIdsExtension", () => {
|
|
|
115
132
|
expect(result.data[0]).toHaveProperty("id");
|
|
116
133
|
expect(result.data[1]).toHaveProperty("id");
|
|
117
134
|
});
|
|
135
|
+
|
|
136
|
+
it("should generate IDs with uppercase letters", async (): Promise<void> => {
|
|
137
|
+
const prefixConfig: PrefixConfig<"User"> = {
|
|
138
|
+
prefixes: {
|
|
139
|
+
User: "usr",
|
|
140
|
+
},
|
|
141
|
+
// Force an ID with uppercase for testing
|
|
142
|
+
idGenerator: (prefix: string): string => {
|
|
143
|
+
return `${prefix}_ABC123DEF`;
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const extension = createPrefixedIdsExtension(prefixConfig);
|
|
148
|
+
prisma.$extends(extension);
|
|
149
|
+
|
|
150
|
+
// Mock the create operation
|
|
151
|
+
prisma.user.create.mockResolvedValueOnce({
|
|
152
|
+
id: "usr_ABC123DEF",
|
|
153
|
+
name: "Test User",
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const user = await prisma.user.create({
|
|
157
|
+
data: {
|
|
158
|
+
name: "Test User",
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// Test that the ID matches our pattern including uppercase
|
|
163
|
+
expect(user.id).toMatch(/^usr_[A-Z0-9]+$/);
|
|
164
|
+
});
|
|
118
165
|
});
|
|
119
166
|
|
|
120
167
|
describe("extendPrismaClient", () => {
|
package/src/index.ts
CHANGED
|
@@ -5,12 +5,15 @@ import { customAlphabet } from "nanoid";
|
|
|
5
5
|
type ModelName = string;
|
|
6
6
|
|
|
7
7
|
export type PrefixConfig<ModelName extends string> = {
|
|
8
|
-
prefixes: Record<ModelName, string
|
|
8
|
+
prefixes: Partial<Record<ModelName, string>>;
|
|
9
9
|
idGenerator?: (prefix: string) => string;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
const defaultIdGenerator = (prefix: string): string => {
|
|
13
|
-
const nanoid = customAlphabet(
|
|
13
|
+
const nanoid = customAlphabet(
|
|
14
|
+
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
15
|
+
24,
|
|
16
|
+
);
|
|
14
17
|
return `${prefix}_${nanoid()}`;
|
|
15
18
|
};
|
|
16
19
|
|
|
@@ -35,7 +38,7 @@ export function createPrefixedIdsExtension<ModelName extends string>(
|
|
|
35
38
|
|
|
36
39
|
const prefixedId = (modelName: ModelName): string | null => {
|
|
37
40
|
if (modelName in prefixes) {
|
|
38
|
-
return idGenerator(prefixes[modelName]);
|
|
41
|
+
return idGenerator(prefixes[modelName] as string);
|
|
39
42
|
}
|
|
40
43
|
return null;
|
|
41
44
|
};
|