File tree 5 files changed +71
-25
lines changed
5 files changed +71
-25
lines changed Original file line number Diff line number Diff line change @@ -107,10 +107,11 @@ Ref: Post.authorId > User.id
107
107
108
108
## Additional Options
109
109
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 ` |
114
115
115
116
Use additional options in the ` prisma.schema `
116
117
Original file line number Diff line number Diff line change @@ -183,4 +183,34 @@ Table AuthorToBook {
183
183
184
184
expect ( dbml ) . toEqual ( expectedDbml ) ;
185
185
} ) ;
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
+ } ) ;
186
216
} ) ;
Original file line number Diff line number Diff line change 2
2
//// THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
3
3
//// ------------------------------------------------------
4
4
5
- Table Post {
5
+ Table User {
6
6
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']
8
14
}
9
15
10
- Table Category {
16
+ Table Profile {
11
17
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'
13
23
}
14
24
15
- Table Author {
25
+ Table Post {
16
26
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
18
32
}
19
33
20
- Table Book {
21
- id String [pk]
22
- authors Author
34
+ Enum Role {
35
+ ADMIN
36
+ USER
23
37
}
24
38
25
- Table CategoryToPost {
26
- categoryId Int [ref: > Category.id]
27
- postId Int [ref: > Post.id]
28
- }
39
+ Ref: Profile.userId - User.id
29
40
30
- Table AuthorToBook {
31
- bookId String [ref: > Book.id]
32
- authorId Int [ref: > Author.id]
33
- }
41
+ Ref: Post.authorId > User.id
Original file line number Diff line number Diff line change @@ -12,12 +12,14 @@ export async function generate(options: GeneratorOptions) {
12
12
const dbmlFileName =
13
13
options . generator . config . outputName || defaultDBMLFileName ;
14
14
15
+ const allowManyToMany = options . generator . config . manyToMany || true ;
16
+
15
17
try {
16
18
await mkdir ( outputDir , { recursive : true } ) ;
17
19
18
20
// await writeFile('./test.json', JSON.stringify(options.dmmf.datamodel));
19
21
20
- const dbmlSchema = generateDBMLSchema ( options . dmmf ) ;
22
+ const dbmlSchema = generateDBMLSchema ( options . dmmf , manyToMany ) ;
21
23
22
24
await writeFile ( join ( outputDir , dbmlFileName ) , dbmlSchema ) ;
23
25
} catch ( e ) {
Original file line number Diff line number Diff line change 1
1
import { DMMF } from '@prisma/generator-helper' ;
2
2
import { generateTables } from './table' ;
3
3
import { generateEnums } from './enums' ;
4
- import { generateRelations } from './relations' ;
4
+ import { generateRelations , manyToOne } from './relations' ;
5
5
import { generateManyToManyTables } from './many-to-many-tables' ;
6
6
7
7
export const autoGeneratedComment = `//// ------------------------------------------------------
8
8
//// THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
9
9
//// ------------------------------------------------------` ;
10
10
11
- export function generateDBMLSchema ( dmmf : DMMF . Document ) : string {
11
+ export function generateDBMLSchema (
12
+ dmmf : DMMF . Document ,
13
+ allowManyToMany : boolean = true
14
+ ) : string {
12
15
const tables = generateTables ( dmmf . datamodel . models ) ;
13
- const manyToManyTables = generateManyToManyTables ( dmmf . datamodel . models ) ;
16
+ const manyToManyTables = allowManyToMany
17
+ ? generateManyToManyTables ( dmmf . datamodel . models )
18
+ : [ ] ;
14
19
const enums = generateEnums ( dmmf . datamodel . enums ) ;
15
20
const refs = generateRelations ( dmmf . datamodel . models ) ;
16
21
You can’t perform that action at this time.
0 commit comments