Skip to content

Commit 9cab0f2

Browse files
committed
[prisma-client-types-generator] v1 Aliases as file
1 parent f6df644 commit 9cab0f2

File tree

17 files changed

+2820
-2390
lines changed

17 files changed

+2820
-2390
lines changed

packages/config/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"jest-server.js"
1010
],
1111
"dependencies": {
12-
"next": "14.1.4",
13-
"react": "18.2.0",
14-
"react-dom": "18.2.0",
15-
"typescript": "^5.4.3"
12+
"next": "15.2.3",
13+
"react": "19.0.0",
14+
"react-dom": "19.0.0",
15+
"typescript": "^5.8.2"
1616
}
1717
}

packages/eslint-config-custom/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"version": "0.0.0",
55
"private": true,
66
"dependencies": {
7-
"eslint": "^8.57.0",
8-
"eslint-config-next": "^14.1.4",
9-
"eslint-config-prettier": "^9.1.0",
10-
"eslint-config-turbo": "^1.13.0",
11-
"eslint-plugin-react": "^7.34.1"
7+
"eslint": "^9.23.0",
8+
"eslint-config-next": "^15.2.3",
9+
"eslint-config-prettier": "^10.1.1",
10+
"eslint-config-turbo": "^2.4.4",
11+
"eslint-plugin-react": "^7.37.4"
1212
}
1313
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changesets
2+
3+
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
4+
with multi-package repos, or single-package repos to help you version and publish your code. You can
5+
find the full documentation for it [in our repository](https://github.com/changesets/changesets)
6+
7+
We have a quick list of common questions to get you started engaging with this project in
8+
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
3+
"changelog": "@changesets/cli/changelog",
4+
"commit": false,
5+
"fixed": [],
6+
"linked": [],
7+
"access": "restricted",
8+
"baseBranch": "main",
9+
"updateInternalDependencies": "patch",
10+
"ignore": []
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"prisma-client-types-generator": major
3+
---
4+
5+
Aliases as file

packages/prisma-client-types-generator/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# prisma-client-types-generator
22

3+
## 1.0.0
4+
5+
### Major Changes
6+
7+
- Aliases as file
8+
39
## 0.1.1
410

511
### Patch Changes
Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,149 @@
11
# Prisma Client Types Generator
22

3-
Docs coming soon...
3+
A tool to generate TypeScript types from your Prisma schema, can be safely imported on the browser.
4+
5+
## what does it do
6+
7+
This package generates TypeScript types from your Prisma schema file that can be safely used in browser environments. Unlike the types generated by Prisma Client, these types:
8+
9+
- Are pure TypeScript types with no runtime dependencies
10+
- Can be imported directly in browser code without bundling issues
11+
- Don't include any Prisma-specific runtime code or decorators
12+
- Only include the type information needed for type checking and IDE support
13+
14+
This is particularly useful when you want to share types between your backend and frontend code without bringing in the full Prisma Client library to the browser.
15+
16+
For example, if you have a Prisma model it will generate:
17+
18+
- ModelValues => the scalars
19+
- ModelKeys => the unique keys
20+
- Model => keys and values, as they are represented in the database
21+
- ModelExtended => the model with all the relations
22+
23+
## Usage Examples
24+
25+
### 1. Basic Setup
26+
27+
First, add the generator to your `schema.prisma`:
28+
29+
```prisma
30+
generator types {
31+
provider = "prisma-client-types-generator"
32+
output = "./generated/types.ts"
33+
}
34+
```
35+
36+
### 2. Example Prisma Schema
37+
38+
```prisma
39+
model User {
40+
id Int @id @default(autoincrement())
41+
email String @unique
42+
name String?
43+
posts Post[]
44+
createdAt DateTime @default(now())
45+
}
46+
47+
model Post {
48+
id Int @id @default(autoincrement())
49+
title String
50+
content String?
51+
author User @relation(fields: [authorId], references: [id])
52+
authorId Int
53+
createdAt DateTime @default(now())
54+
}
55+
```
56+
57+
### 3. Generated Types Usage
58+
59+
After running `prisma generate`, you can use the generated types in your frontend code:
60+
61+
```typescript
62+
import type { User, UserExtended, Post, PostExtended } from "./generated/types";
63+
64+
// Basic type usage
65+
const user: User = {
66+
id: 1,
67+
68+
name: "John Doe",
69+
createdAt: new Date(),
70+
};
71+
72+
// Using extended types with relations
73+
const postWithAuthor: PostExtended = {
74+
id: 1,
75+
title: "My First Post",
76+
content: "Hello World!",
77+
authorId: 1,
78+
createdAt: new Date(),
79+
author: {
80+
id: 1,
81+
82+
name: "John Doe",
83+
createdAt: new Date(),
84+
},
85+
};
86+
87+
// Type-safe API responses
88+
async function fetchUser(id: number): Promise<UserExtended> {
89+
const response = await fetch(`/api/users/${id}`);
90+
return response.json();
91+
}
92+
```
93+
94+
### 4. Configuration Options
95+
96+
You can customize the generator output using configuration options in your `schema.prisma`:
97+
98+
```prisma
99+
generator types {
100+
provider = "prisma-client-types-generator"
101+
output = "./generated/types.ts"
102+
config = {
103+
pascalCase = true
104+
aliases = "./prisma/aliases.ts"
105+
}
106+
}
107+
```
108+
109+
Then create a file `prisma/aliases.ts` with your type aliases:
110+
111+
```typescript
112+
// prisma/aliases.ts
113+
// CommonJS style (recommended for compatibility with require)
114+
module.exports = {
115+
outdated_legacy_table: "NewModelName",
116+
outdated_legacy_enum: "NewEnumName",
117+
};
118+
119+
// Or ESM style (if you're using ESM modules)
120+
export default {
121+
outdated_legacy_table: "NewModelName",
122+
outdated_legacy_enum: "NewEnumName",
123+
};
124+
```
125+
126+
The aliases file should export a default object where:
127+
128+
- Keys are the model names from your Prisma schema
129+
- Values are the desired type names in the generated output
130+
131+
### 5. Type Safety in API Routes
132+
133+
```typescript
134+
import type { User, Post } from "./generated/types";
135+
136+
// Type-safe API route handler
137+
export async function createPost(
138+
data: Omit<Post, "id" | "createdAt">
139+
): Promise<Post> {
140+
// Your API logic here
141+
return {
142+
id: 1,
143+
...data,
144+
createdAt: new Date(),
145+
};
146+
}
147+
```
148+
149+
These types can be safely used in both frontend and backend code, providing type safety without bringing in the full Prisma Client runtime.
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "prisma-client-types-generator",
3-
"version": "0.1.1",
3+
"version": "1.0.0",
44
"description": "Generate safe types for the browser and other uses",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",
@@ -10,10 +10,11 @@
1010
},
1111
"scripts": {
1212
"build": "tsup src/index.ts --format esm,cjs --dts --treeshake",
13+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
1314
"dev": "pnpm build --watch",
1415
"lint": "eslint src --fix",
15-
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
1616
"prepublish": "pnpm test && pnpm build",
17+
"release": "pnpm changeset && pnpm changeset version",
1718
"test": "sh ./test/test.sh"
1819
},
1920
"files": [
@@ -34,28 +35,28 @@
3435
"author": "Orlando Groppo <[email protected]>",
3536
"license": "MIT",
3637
"peerDependencies": {
37-
"@prisma/client": ">=5",
38-
"prisma": ">=5"
38+
"@prisma/client": ">=6",
39+
"prisma": ">=6"
3940
},
4041
"dependencies": {
41-
"@prisma/generator-helper": "^5.22.0"
42+
"@prisma/generator-helper": "^6.5.0"
4243
},
4344
"devDependencies": {
44-
"@babel/core": "7.26.0",
45-
"@babel/preset-env": "^7.26.0",
45+
"@babel/core": "7.26.10",
46+
"@babel/preset-env": "^7.26.9",
4647
"@babel/preset-typescript": "^7.26.0",
47-
"@changesets/cli": "^2.27.10",
48-
"@prisma/client": "^5.22.0",
48+
"@changesets/cli": "^2.28.1",
49+
"@prisma/client": "^6.5.0",
4950
"@types/jest": "^29.5.14",
50-
"@types/node": "^22.9.3",
51-
"deverything": "^1.10.1",
51+
"@types/node": "^22.13.11",
52+
"deverything": "^3.0.0",
5253
"eslint-config-custom": "workspace:*",
5354
"jest": "^29.7.0",
54-
"prisma": "^5.22.0",
55+
"prisma": "^6.5.0",
5556
"ts-node": "^10.9.2",
5657
"tsd": "^0.31.2",
57-
"tsup": "^8.3.5",
58-
"tsx": "^4.19.2",
59-
"typescript": "^5.7.2"
58+
"tsup": "^8.4.0",
59+
"tsx": "^4.19.3",
60+
"typescript": "^5.8.2"
6061
}
6162
}

packages/prisma-client-types-generator/src/formatEntityName.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { PrismaClientTypesGeneratorConfig } from "./onGenerate";
2-
3-
let aliasesMap: Record<string, string> | undefined;
1+
import { InternalGeneratorOptions } from "./onGenerate";
42

53
export const formatEntityName = (
64
rawEntityName: string,
7-
{ aliases, pascalCase }: PrismaClientTypesGeneratorConfig
5+
{ aliasMap, pascalCase }: InternalGeneratorOptions
86
) => {
9-
if (aliases) {
10-
aliasesMap = aliasesMap || JSON.parse(aliases);
11-
if (rawEntityName in aliasesMap) {
12-
return aliasesMap[rawEntityName];
7+
if (aliasMap) {
8+
if (rawEntityName in aliasMap) {
9+
return aliasMap[rawEntityName];
1310
}
1411
}
1512

packages/prisma-client-types-generator/src/generateEnum.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { DMMF } from "@prisma/generator-helper";
22
import { formatEntityName } from "./formatEntityName";
3-
import { PrismaClientTypesGeneratorConfig } from "./onGenerate";
3+
import { InternalGeneratorOptions } from "./onGenerate";
44

55
export const generateEnum = (
66
prismaEnum: DMMF.DatamodelEnum,
7-
config: PrismaClientTypesGeneratorConfig
7+
config: InternalGeneratorOptions
88
) => {
99
let out = "";
1010

0 commit comments

Comments
 (0)