File tree Expand file tree Collapse file tree 1 file changed +31
-1
lines changed
.stacks/core/database/src/migrations Expand file tree Collapse file tree 1 file changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,10 @@ interface Model {
15
15
columns : Column [ ]
16
16
}
17
17
18
+ interface ModelData {
19
+ [ key : string ] : any ;
20
+ }
21
+
18
22
function generatePrismaSchema ( models : Model [ ] , path : string ) : void {
19
23
let schema = `datasource db {
20
24
provider = "postgresql"
@@ -52,7 +56,9 @@ generator client {
52
56
53
57
schema += '}\n\n'
54
58
}
59
+
55
60
path = '.stacks/database/schema.prisma'
61
+
56
62
fs . writeFile ( path , schema , ( err ) => {
57
63
if ( err ) {
58
64
console . error ( `Error writing schema file: ${ err . message } ` )
@@ -62,4 +68,28 @@ generator client {
62
68
} )
63
69
}
64
70
65
- export { generatePrismaSchema }
71
+ function readModelsFromFolder ( folderPath : string ) : ModelData [ ] {
72
+ const models : ModelData [ ] = [ ] ;
73
+
74
+ fs . readdirSync ( folderPath ) . forEach ( ( file ) => {
75
+ if ( file . endsWith ( '.ts' ) ) {
76
+ const filePath = `${ folderPath } /${ file } ` ;
77
+ const fileContents = fs . readFileSync ( filePath , 'utf-8' ) ;
78
+
79
+ const regex = / r e t u r n \s * { ( [ ^ } ] * ) } / m;
80
+ const match = fileContents . match ( regex ) ;
81
+
82
+ if ( match ) {
83
+ const modelData = eval ( `({${ match [ 1 ] } })` ) ;
84
+ models . push ( modelData ) ;
85
+ }
86
+ }
87
+ } ) ;
88
+
89
+ return models ;
90
+ }
91
+
92
+ export {
93
+ generatePrismaSchema ,
94
+ readModelsFromFolder
95
+ }
You can’t perform that action at this time.
0 commit comments