File tree Expand file tree Collapse file tree 1 file changed +53
-2
lines changed
.stacks/core/database/src/migrations Expand file tree Collapse file tree 1 file changed +53
-2
lines changed Original file line number Diff line number Diff line change 1
- function migrations ( ) {
1
+ interface Column {
2
+ name : string ;
3
+ type : string ;
4
+ required ?: boolean ;
5
+ unique ?: boolean ;
6
+ default ?: string ;
7
+ }
8
+
9
+ interface Model {
10
+ name : string ;
11
+ columns : Column [ ] ;
12
+ }
2
13
14
+ function generatePrismaSchema ( models : Model [ ] ) : string {
15
+ let schema = `datasource db {
16
+ provider = "postgresql"
17
+ url = env("DATABASE_URL")
3
18
}
4
19
5
- export { migrations }
20
+ generator client {
21
+ provider = "prisma-client-js"
22
+ }
23
+
24
+ ` ;
25
+
26
+ for ( const model of models ) {
27
+ schema += `model ${ model . name } {
28
+ id Int @id @default(autoincrement())
29
+ createdAt DateTime @default(now())
30
+ updatedAt DateTime @updatedAt()
31
+ ` ;
32
+
33
+ for ( const column of model . columns ) {
34
+ let columnSchema = ` ${ column . name } ${ column . type } ` ;
35
+
36
+ if ( column . required ) {
37
+ columnSchema += ' @required' ;
38
+ }
39
+
40
+ if ( column . unique ) {
41
+ columnSchema += ' @unique' ;
42
+ }
43
+
44
+ if ( column . default ) {
45
+ columnSchema += ` @default(${ column . default } )` ;
46
+ }
47
+
48
+ columnSchema += '\n' ;
49
+ schema += columnSchema ;
50
+ }
51
+
52
+ schema += '}\n\n' ;
53
+ }
54
+
55
+ return schema ;
56
+ }
You can’t perform that action at this time.
0 commit comments