Skip to content

Commit 1d50b51

Browse files
committed
Add README
1 parent 1e8cea1 commit 1d50b51

File tree

1 file changed

+233
-1
lines changed

1 file changed

+233
-1
lines changed

README.md

Lines changed: 233 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,235 @@
11
# prisma-db-comments-generator
22

3-
Generate database comments from Prisma schema.
3+
Generate database comments from Prisma schema.
4+
5+
It is based on the following code idea. Thank you @Jyrno42 .
6+
7+
- https://github.com/prisma/prisma/issues/8703#issuecomment-1614360386
8+
9+
## Features
10+
11+
- Create a migration SQL for the `COMMENT ON` statement based on the information in the `schema.prisma` file.
12+
- Comments written with triple slashes (`///`) are eligible.
13+
- Create a `COMMENT ON` statement only for the part of the difference.
14+
- The previous information is stored as `prisma/migrations/comments-latest.json`.
15+
- Support for table and column comments.
16+
- Comments you do not want to create can be excluded using regular expressions.
17+
- For example, a VIEW is to be created manually, and you do not want to create a comment statement from the `schema.prisma`. In such cases, you can exclude them by specifying the pattern of the VIEW.
18+
- Enum information can be added to column comments.
19+
20+
## Usage
21+
22+
Install this package.
23+
24+
```
25+
npm install --save-dev @onozaty/prisma-db-comments-generator
26+
```
27+
28+
Add the generator to the `schema.prisma`
29+
30+
```prisma
31+
generator comments {
32+
provider = "prisma-db-comments-generator"
33+
}
34+
```
35+
36+
Run `npx prisma generate` to trigger the generator.
37+
A SQL file for `COMMENT ON` is generated in the migrations folder.
38+
39+
```
40+
$ npx prisma generate
41+
Environment variables loaded from .env
42+
Prisma schema loaded from prisma/schema.prisma
43+
Comments generation completed: 20240804142142_update_comments
44+
45+
✔ Generated Prisma Client (v5.17.0) to ./node_modules/@prisma/client in 73ms
46+
47+
✔ Generated Prisma Database Comments (v1.0.1) to ./prisma/migrations in 11ms
48+
```
49+
50+
## Example
51+
52+
For example, from the following `schema.prisma`,
53+
54+
```prisma
55+
generator client {
56+
provider = "prisma-client-js"
57+
}
58+
59+
generator comments {
60+
provider = "prisma-db-comments-generator"
61+
}
62+
63+
datasource db {
64+
provider = "postgresql"
65+
url = env("DATABASE_URL")
66+
}
67+
68+
/// Product type enumeration
69+
enum ProductType {
70+
BOOK
71+
TOY
72+
FASHION
73+
74+
@@map("enum_product_type")
75+
}
76+
77+
/// Customer
78+
model Customer {
79+
/// Customer ID
80+
customerId Int @id @default(autoincrement()) @map("customer_id")
81+
/// Customer Name
82+
name String
83+
/// e-mail
84+
email String @unique
85+
createdAt DateTime @default(dbgenerated("statement_timestamp()")) @map("created_at") @db.Timestamptz()
86+
sales Sale[]
87+
88+
@@map("customers")
89+
}
90+
91+
/// Product
92+
model Product {
93+
/// Product ID
94+
productId Int @id @default(autoincrement()) @map("product_id")
95+
/// Product Name
96+
name String
97+
/// Product Type
98+
type ProductType
99+
/// Product Description
100+
description String?
101+
/// Price
102+
price Float
103+
createdAt DateTime @default(dbgenerated("statement_timestamp()")) @map("created_at") @db.Timestamptz()
104+
sales Sale[]
105+
106+
@@map("products")
107+
}
108+
109+
model Sale {
110+
/// Sale ID
111+
saleId Int @id @default(autoincrement()) @map("sale_id")
112+
customer Customer @relation(fields: [customerId], references: [customerId])
113+
/// Customer ID
114+
customerId Int @map("customer_id")
115+
product Product @relation(fields: [productId], references: [productId])
116+
/// Product ID
117+
productId Int @map("product_id")
118+
/// Quantity
119+
quantity Int
120+
/// Total Price
121+
totalPrice Float @map("total_price")
122+
createdAt DateTime @default(dbgenerated("statement_timestamp()")) @map("created_at") @db.Timestamptz()
123+
124+
@@map("sales")
125+
}
126+
```
127+
128+
A migration SQL file will be generated as shown below.
129+
130+
```sql
131+
-- Prisma Database Comments Generator v1.0.1
132+
133+
-- customers comments
134+
COMMENT ON TABLE "customers" IS 'Customer';
135+
COMMENT ON COLUMN "customers"."customer_id" IS 'Customer ID';
136+
COMMENT ON COLUMN "customers"."name" IS 'Customer Name';
137+
COMMENT ON COLUMN "customers"."email" IS 'e-mail';
138+
139+
-- products comments
140+
COMMENT ON TABLE "products" IS 'Product';
141+
COMMENT ON COLUMN "products"."product_id" IS 'Product ID';
142+
COMMENT ON COLUMN "products"."name" IS 'Product Name';
143+
COMMENT ON COLUMN "products"."type" IS 'Product Type';
144+
COMMENT ON COLUMN "products"."description" IS 'Product Description';
145+
COMMENT ON COLUMN "products"."price" IS 'Price';
146+
147+
-- sales comments
148+
COMMENT ON COLUMN "sales"."sale_id" IS 'Sale ID';
149+
COMMENT ON COLUMN "sales"."customer_id" IS 'Customer ID';
150+
COMMENT ON COLUMN "sales"."product_id" IS 'Product ID';
151+
COMMENT ON COLUMN "sales"."quantity" IS 'Quantity';
152+
COMMENT ON COLUMN "sales"."total_price" IS 'Total Price';
153+
```
154+
155+
## Options
156+
157+
### targets
158+
159+
You can select the target with `targets`.
160+
The default is both `table` and `column`. (`targets = ["table", "column"]`)
161+
162+
For example, the following will only create comments on columns.
163+
164+
```prisma
165+
generator comments {
166+
provider = "prisma-db-comments-generator"
167+
targets = ["column"]
168+
}
169+
```
170+
171+
### ignorePattern
172+
173+
Specify the model to be excluded from making comments as a regular expression with `ignorePattern`.
174+
The name specified here is the name in the database. Therefore, if `@@map` is specified, it will be the name in `@@map`.
175+
176+
For example, the following is a model with `_view` as a suffix that is excluded.
177+
178+
```prisma
179+
generator comments {
180+
provider = "prisma-db-comments-generator"
181+
ignorePattern = "_view$"
182+
}
183+
```
184+
185+
### includeEnumInFeildComment
186+
187+
If `includeEnumInFeildComment` is set to true, information about the enum is appended to the column comment.
188+
Default is `false`.
189+
190+
```prisma
191+
generator comments {
192+
provider = "prisma-db-comments-generator"
193+
includeEnumInFeildComment = true
194+
}
195+
```
196+
197+
If `includeEnumInFeildComment` is set to `true` with the following definition,
198+
199+
```prisma
200+
enum ProductType {
201+
BOOK
202+
TOY
203+
FASHION
204+
205+
@@map("enum_product_type")
206+
}
207+
208+
/// Product
209+
model Product {
210+
/// Product Type
211+
type ProductType
212+
213+
// others...
214+
}
215+
```
216+
217+
The following comment is generated.
218+
219+
```sql
220+
COMMENT ON COLUMN "products"."type" IS E'Product Type\nenum: enum_product_type(BOOK, TOY, FASHION)';
221+
```
222+
223+
## Supported Databases
224+
225+
- PostgreSQL
226+
227+
Other databases may be available, but the above is the only one checked.
228+
229+
## License
230+
231+
MIT
232+
233+
## Author
234+
235+
[onozaty](https://github.com/onozaty)

0 commit comments

Comments
 (0)