Skip to content

Commit cdf38aa

Browse files
committed
feat(column-definition): add column defintion to table fields
* pk * unique * defaults: increment, now, string, boolean, number, not null * note
1 parent 8fdb9bb commit cdf38aa

File tree

8 files changed

+449
-299
lines changed

8 files changed

+449
-299
lines changed

prisma/dbml/schema.dbml

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
Table User {
2-
id Int
3-
email String
2+
id Int [pk, increment]
3+
createdAt DateTime [default: `now()`, not null]
4+
updatedAt DateTime [not null]
5+
email String [unique, not null]
46
name String
57
posts Post
68
profile Profile
7-
role Role
9+
role Role [not null, note: 'user role']
810
}
911

1012
Table Profile {
11-
id Int
13+
id Int [pk, increment]
1214
bio String
13-
user User
14-
userId Int
15+
user User [not null]
16+
userId Int [unique, not null]
1517
}
1618

1719
Table Post {
18-
id Int
19-
title String
20+
id Int [pk, increment]
21+
title String [not null]
2022
content String
21-
published Boolean
23+
published Boolean [not null, default: false]
2224
author User
2325
authorId Int
2426
}

prisma/migrations/20200818212722-init/README.md renamed to prisma/migrations/20200820181412-init/README.md

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Migration `20200818212722-init`
1+
# Migration `20200820181412-init`
22

3-
This migration has been generated by Marc Stammerjohann at 8/18/2020, 11:27:22 PM.
3+
This migration has been generated by Marc Stammerjohann at 8/20/2020, 8:14:12 PM.
44
You can check out the [state of the schema](./schema.prisma) after the migration.
55

66
## Database Steps
@@ -10,6 +10,8 @@ CREATE TYPE "Role" AS ENUM ('ADMIN', 'USER')
1010

1111
CREATE TABLE "blog"."User" (
1212
"id" SERIAL,
13+
"createdAt" timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
14+
"updatedAt" timestamp(3) NOT NULL ,
1315
"email" text NOT NULL ,
1416
"name" text ,
1517
"role" "Role" NOT NULL ,
@@ -45,10 +47,10 @@ ALTER TABLE "blog"."Post" ADD FOREIGN KEY ("authorId")REFERENCES "blog"."User"("
4547

4648
```diff
4749
diff --git schema.prisma schema.prisma
48-
migration ..20200818212722-init
50+
migration ..20200820181412-init
4951
--- datamodel.dml
5052
+++ datamodel.dml
51-
@@ -1,0 +1,45 @@
53+
@@ -1,0 +1,50 @@
5254
+// This is your Prisma schema file,
5355
+// learn more about it in the docs: https://pris.ly/d/prisma-schema
5456
+
@@ -66,14 +68,18 @@ migration ..20200818212722-init
6668
+}
6769
+
6870
+model User {
69-
+ id Int @id @default(autoincrement())
70-
+ email String @unique
71-
+ name String?
72-
+ posts Post[]
73-
+ profile Profile?
74-
+ role Role
71+
+ id Int @id @default(autoincrement())
72+
+ createdAt DateTime @default(now())
73+
+ updatedAt DateTime @updatedAt
74+
+ email String @unique
75+
+ name String?
76+
+ posts Post[]
77+
+ profile Profile?
78+
+ /// user role
79+
+ role Role
7580
+}
7681
+
82+
+/// User profile
7783
+model Profile {
7884
+ id Int @default(autoincrement()) @id
7985
+ bio String?
@@ -90,8 +96,9 @@ migration ..20200818212722-init
9096
+ authorId Int?
9197
+}
9298
+
99+
+/// user role
93100
+enum Role {
94-
+ ADMIN
101+
+ ADMIN /// allowed to do everything
95102
+ USER
96103
+}
97104
```

prisma/migrations/20200818212722-init/schema.prisma renamed to prisma/migrations/20200820181412-init/schema.prisma

+12-7
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ generator dbml {
1515
}
1616

1717
model User {
18-
id Int @id @default(autoincrement())
19-
email String @unique
20-
name String?
21-
posts Post[]
22-
profile Profile?
23-
role Role
18+
id Int @id @default(autoincrement())
19+
createdAt DateTime @default(now())
20+
updatedAt DateTime @updatedAt
21+
email String @unique
22+
name String?
23+
posts Post[]
24+
profile Profile?
25+
/// user role
26+
role Role
2427
}
2528

29+
/// User profile
2630
model Profile {
2731
id Int @default(autoincrement()) @id
2832
bio String?
@@ -39,7 +43,8 @@ model Post {
3943
authorId Int?
4044
}
4145

46+
/// user role
4247
enum Role {
43-
ADMIN
48+
ADMIN /// allowed to do everything
4449
USER
4550
}

prisma/migrations/20200818212722-init/steps.json renamed to prisma/migrations/20200820181412-init/steps.json

+50
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,56 @@
7878
"argument": "",
7979
"value": "autoincrement()"
8080
},
81+
{
82+
"tag": "CreateField",
83+
"model": "User",
84+
"field": "createdAt",
85+
"type": "DateTime",
86+
"arity": "Required"
87+
},
88+
{
89+
"tag": "CreateDirective",
90+
"location": {
91+
"path": {
92+
"tag": "Field",
93+
"model": "User",
94+
"field": "createdAt"
95+
},
96+
"directive": "default"
97+
}
98+
},
99+
{
100+
"tag": "CreateArgument",
101+
"location": {
102+
"tag": "Directive",
103+
"path": {
104+
"tag": "Field",
105+
"model": "User",
106+
"field": "createdAt"
107+
},
108+
"directive": "default"
109+
},
110+
"argument": "",
111+
"value": "now()"
112+
},
113+
{
114+
"tag": "CreateField",
115+
"model": "User",
116+
"field": "updatedAt",
117+
"type": "DateTime",
118+
"arity": "Required"
119+
},
120+
{
121+
"tag": "CreateDirective",
122+
"location": {
123+
"path": {
124+
"tag": "Field",
125+
"model": "User",
126+
"field": "updatedAt"
127+
},
128+
"directive": "updatedAt"
129+
}
130+
},
81131
{
82132
"tag": "CreateField",
83133
"model": "User",

prisma/migrations/migrate.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Prisma Migrate lockfile v1
22

3-
20200818212722-init
3+
20200820181412-init

prisma/schema.prisma

+12-7
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ generator dbml {
1515
}
1616

1717
model User {
18-
id Int @id @default(autoincrement())
19-
email String @unique
20-
name String?
21-
posts Post[]
22-
profile Profile?
23-
role Role
18+
id Int @id @default(autoincrement())
19+
createdAt DateTime @default(now())
20+
updatedAt DateTime @updatedAt
21+
email String @unique
22+
name String?
23+
posts Post[]
24+
profile Profile?
25+
/// user role
26+
role Role
2427
}
2528

29+
/// User profile
2630
model Profile {
2731
id Int @default(autoincrement()) @id
2832
bio String?
@@ -39,7 +43,8 @@ model Post {
3943
authorId Int?
4044
}
4145

46+
/// user role
4247
enum Role {
43-
ADMIN
48+
ADMIN /// allowed to do everything
4449
USER
4550
}

src/index.ts

+47-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ generatorHandler({
2121
try {
2222
await mkdir(options.generator.output, { recursive: true });
2323

24+
// await writeFile('./test.json', JSON.stringify(options.dmmf.datamodel));
2425
const tables = generateTables(options.dmmf.datamodel.models);
2526
const enums = generateEnums(options.dmmf.datamodel.enums);
2627
const refs = generateRefs(options.dmmf.datamodel.models);
@@ -48,7 +49,52 @@ const generateTables = (models: DMMF.Model[]): string[] => {
4849
};
4950

5051
const generateFields = (fields: DMMF.Field[]): string => {
51-
return fields.map((field) => `\t${field.name} ${field.type}`).join('\n');
52+
return fields
53+
.map(
54+
(field) =>
55+
`\t${field.name} ${field.type}${generateColumnDefinition(field)}`
56+
)
57+
.join('\n');
58+
};
59+
60+
const generateColumnDefinition = (field: DMMF.Field): string => {
61+
const columnDefinition = [];
62+
if (field.isId) {
63+
columnDefinition.push('pk');
64+
}
65+
66+
if ((field.default as DMMF.FieldDefault)?.name === 'autoincrement') {
67+
columnDefinition.push('increment');
68+
}
69+
70+
if ((field.default as DMMF.FieldDefault)?.name === 'now') {
71+
columnDefinition.push('default: `now()`');
72+
}
73+
74+
if (field.isUnique) {
75+
columnDefinition.push('unique');
76+
}
77+
78+
if (field.isRequired && !field.isId) {
79+
columnDefinition.push('not null');
80+
}
81+
82+
if (
83+
typeof field.default === 'string' ||
84+
typeof field.default === 'boolean' ||
85+
typeof field.default === 'number'
86+
) {
87+
columnDefinition.push(`default: ${field.default}`);
88+
}
89+
90+
if ((field as any).documentation) {
91+
columnDefinition.push(`note: '${(field as any).documentation}'`);
92+
}
93+
94+
if (columnDefinition.length) {
95+
return ' [' + columnDefinition.join(', ') + ']';
96+
}
97+
return '';
5298
};
5399

54100
const generateEnums = (enums: DMMF.DatamodelEnum[]): string[] => {

0 commit comments

Comments
 (0)