Skip to content

Commit 50ef8cf

Browse files
committed
add map db name option
1 parent 9e3fa6f commit 50ef8cf

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ Ref: Post.authorId > User.id
145145
| `output` | Output directory for the DBML file | `string` | `./dbml` |
146146
| `outputName` | Name for the DBML file | `string` | `dbml.schema` |
147147
| `manyToMany` | Create Many-To-Many join table | `boolean` | `true` |
148+
| `mapToDbSchema` | Use mapped table name | `boolean` | `true` |
148149

149150
Use additional options in the `schema.prisma`
150151

prisma/schema.prisma

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,29 @@
22
// learn more about it in the docs: https://pris.ly/d/prisma-schema
33

44
datasource db {
5-
provider = "sqlite"
5+
provider = "mysql"
66
url = env("DATABASE_URL")
77
}
88

99
generator client {
10-
provider = "prisma-client-js"
10+
provider = "prisma-client-js"
1111
}
1212

1313
generator dbml {
14-
provider = "node ./dist/generator.js"
14+
provider = "node ./dist/generator.js"
15+
mapToDbSchema = "false"
1516
}
1617

1718
model User {
1819
id Int @id @default(autoincrement())
19-
createdAt DateTime @default(now())
20-
updatedAt DateTime @updatedAt
20+
createdAt DateTime @default(now()) @db.DateTime(0)
21+
updatedAt DateTime @updatedAt @map("updated_at")
2122
email String @unique
22-
name String?
23+
name String? @db.VarChar(255)
2324
posts Post[]
2425
profile Profile?
26+
27+
@@map("user")
2528
}
2629

2730
/// User profile
@@ -30,6 +33,8 @@ model Profile {
3033
bio String?
3134
user User @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: Cascade)
3235
userId Int @unique
36+
37+
@@map("profile")
3338
}
3439

3540
model Post {

src/cli/dbml-generator.ts

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export async function generate(options: GeneratorOptions) {
1414
const outputDir = parseEnvValue(output!);
1515
const dbmlFileName = config.outputName || defaultDBMLFileName;
1616
const allowManyToMany = config.manyToMany === 'false' ? false : true;
17+
const mapToDbSchema = config.mapToDbSchema === 'false' ? false: true;
1718
const projectOptions = await getProjectOptions(config);
1819

1920
try {
@@ -24,6 +25,7 @@ export async function generate(options: GeneratorOptions) {
2425

2526
const dbmlSchema = generateDBMLSchema(
2627
options.dmmf,
28+
mapToDbSchema,
2729
allowManyToMany,
2830
projectOptions
2931
);

src/generator/dbml.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ export const autoGeneratedComment = `//// --------------------------------------
1111

1212
export function generateDBMLSchema(
1313
dmmf: DMMF.Document,
14+
mapToDbSchema: boolean,
1415
allowManyToMany: boolean = true,
1516
projectOptions?: ProjectOptions
1617
): string {
17-
const tables = generateTables(dmmf.datamodel.models);
18+
const tables = generateTables(dmmf.datamodel.models, mapToDbSchema);
1819
const manyToManyTables = allowManyToMany
1920
? generateManyToManyTables(dmmf.datamodel.models)
2021
: [];

src/generator/table.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import { DBMLKeywords, PrismaScalars } from './../keywords';
22
import { DMMF } from '@prisma/generator-helper';
33

4-
export function generateTables(models: DMMF.Model[]): string[] {
5-
return models.map(
6-
(model) =>
7-
`${DBMLKeywords.Table} ${model.name} {\n` +
4+
export function generateTables(
5+
models: DMMF.Model[],
6+
mapToDbSchema: boolean
7+
): string[] {
8+
return models.map((model) => {
9+
let modelName = model.name;
10+
11+
if (mapToDbSchema && model.dbName) {
12+
modelName = model.dbName;
13+
}
14+
15+
return (
16+
`${DBMLKeywords.Table} ${modelName} {\n` +
817
generateFields(model.fields) +
918
generateTableIndexes(model) +
1019
generateTableDocumentation(model) +
1120
'\n}'
12-
);
21+
);
22+
});
1323
}
1424

1525
const generateTableIndexes = (model: DMMF.Model): string => {

0 commit comments

Comments
 (0)