Skip to content

Commit b4615e7

Browse files
committed
feat(many-to-many): option to disable m-n table
* add test if disabled
1 parent f0e2923 commit b4615e7

File tree

5 files changed

+71
-25
lines changed

5 files changed

+71
-25
lines changed

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,11 @@ Ref: Post.authorId > User.id
107107

108108
## Additional Options
109109

110-
| Option |  Description | Type |  Default |
111-
| ------------ | ---------------------------------- | -------- | ------------- |
112-
| `output` | Output directory for the DBML file | `string` | `./dbml` |
113-
| `outputName` | Name for the DBML file | `string` | `dbml.schema` |
110+
| Option |  Description | Type |  Default |
111+
| ------------ | ---------------------------------- | --------- | ------------- |
112+
| `output` | Output directory for the DBML file | `string` | `./dbml` |
113+
| `outputName` | Name for the DBML file | `string` | `dbml.schema` |
114+
| `manyToMany` | Create Many-To-Many join table | `boolean` | `true` |
114115

115116
Use additional options in the `prisma.schema`
116117

__tests__/dbml.test.ts

+30
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,34 @@ Table AuthorToBook {
183183

184184
expect(dbml).toEqual(expectedDbml);
185185
});
186+
187+
test('generating dbml schema with many-to-many relationship', async () => {
188+
const dmmf = await generateDMMF(datamodelDbmlManyToMany);
189+
190+
const expectedDbml = `${autoGeneratedComment}
191+
192+
Table Post {
193+
id Int [pk, increment]
194+
categories Category
195+
}
196+
197+
Table Category {
198+
id Int [pk, increment]
199+
posts Post
200+
}
201+
202+
Table Author {
203+
id Int [pk, increment]
204+
books Book
205+
}
206+
207+
Table Book {
208+
id Int [pk, increment]
209+
authors Author
210+
}`;
211+
212+
const dbml = generateDBMLSchema(dmmf, false);
213+
214+
expect(dbml).toEqual(expectedDbml);
215+
});
186216
});

prisma/dbml/schema.dbml

+25-17
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,40 @@
22
//// THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
33
//// ------------------------------------------------------
44

5-
Table Post {
5+
Table User {
66
id Int [pk, increment]
7-
categories Category
7+
createdAt DateTime [default: `now()`, not null]
8+
updatedAt DateTime [not null]
9+
email String [unique, not null]
10+
name String
11+
posts Post
12+
profile Profile
13+
role Role [not null, default: 'USER', note: 'user role']
814
}
915

10-
Table Category {
16+
Table Profile {
1117
id Int [pk, increment]
12-
posts Post
18+
bio String
19+
user User [not null]
20+
userId Int [unique, not null]
21+
22+
Note: 'User profile'
1323
}
1424

15-
Table Author {
25+
Table Post {
1626
id Int [pk, increment]
17-
books Book
27+
title String [not null, default: '']
28+
content String
29+
published Boolean [not null, default: false]
30+
author User
31+
authorId Int
1832
}
1933

20-
Table Book {
21-
id String [pk]
22-
authors Author
34+
Enum Role {
35+
ADMIN
36+
USER
2337
}
2438

25-
Table CategoryToPost {
26-
categoryId Int [ref: > Category.id]
27-
postId Int [ref: > Post.id]
28-
}
39+
Ref: Profile.userId - User.id
2940

30-
Table AuthorToBook {
31-
bookId String [ref: > Book.id]
32-
authorId Int [ref: > Author.id]
33-
}
41+
Ref: Post.authorId > User.id

src/cli/dbml-generator.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ export async function generate(options: GeneratorOptions) {
1212
const dbmlFileName =
1313
options.generator.config.outputName || defaultDBMLFileName;
1414

15+
const allowManyToMany = options.generator.config.manyToMany || true;
16+
1517
try {
1618
await mkdir(outputDir, { recursive: true });
1719

1820
// await writeFile('./test.json', JSON.stringify(options.dmmf.datamodel));
1921

20-
const dbmlSchema = generateDBMLSchema(options.dmmf);
22+
const dbmlSchema = generateDBMLSchema(options.dmmf, manyToMany);
2123

2224
await writeFile(join(outputDir, dbmlFileName), dbmlSchema);
2325
} catch (e) {

src/generator/dbml.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import { DMMF } from '@prisma/generator-helper';
22
import { generateTables } from './table';
33
import { generateEnums } from './enums';
4-
import { generateRelations } from './relations';
4+
import { generateRelations, manyToOne } from './relations';
55
import { generateManyToManyTables } from './many-to-many-tables';
66

77
export const autoGeneratedComment = `//// ------------------------------------------------------
88
//// THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
99
//// ------------------------------------------------------`;
1010

11-
export function generateDBMLSchema(dmmf: DMMF.Document): string {
11+
export function generateDBMLSchema(
12+
dmmf: DMMF.Document,
13+
allowManyToMany: boolean = true
14+
): string {
1215
const tables = generateTables(dmmf.datamodel.models);
13-
const manyToManyTables = generateManyToManyTables(dmmf.datamodel.models);
16+
const manyToManyTables = allowManyToMany
17+
? generateManyToManyTables(dmmf.datamodel.models)
18+
: [];
1419
const enums = generateEnums(dmmf.datamodel.enums);
1520
const refs = generateRelations(dmmf.datamodel.models);
1621

0 commit comments

Comments
 (0)