@@ -3,20 +3,28 @@ import { ZModelCodeGenerator } from '@zenstackhq/sdk';
3
3
import fs from 'node:fs' ;
4
4
import path from 'node:path' ;
5
5
import { execPackage } from '../utils/exec-utils' ;
6
- import { generateTempPrismaSchema , getSchemaFile , handleSubProcessError , loadSchemaDocumentWithServices } from './action-utils' ;
6
+ import {
7
+ generateTempPrismaSchema ,
8
+ getSchemaFile ,
9
+ handleSubProcessError ,
10
+ loadSchemaDocumentWithServices ,
11
+ } from './action-utils' ;
7
12
import { syncEnums , syncRelation , syncTable , type Relation } from './pull' ;
8
13
import { providers } from './pull/provider' ;
9
14
import { getDatasource } from './pull/utils' ;
15
+ import { config } from '@dotenvx/dotenvx' ;
10
16
11
17
type PushOptions = {
12
18
schema ?: string ;
13
19
acceptDataLoss ?: boolean ;
14
20
forceReset ?: boolean ;
15
21
} ;
16
22
17
- type PullOptions = {
23
+ export type PullOptions = {
18
24
schema ?: string ;
19
25
out ?: string ;
26
+ naming ?: 'pascal' | 'camel' | 'snake' | 'kebab' | 'none' ;
27
+ alwaysMap ?: boolean ;
20
28
} ;
21
29
22
30
/**
@@ -63,62 +71,57 @@ async function runPush(options: PushOptions) {
63
71
async function runPull ( options : PullOptions ) {
64
72
const schemaFile = getSchemaFile ( options . schema ) ;
65
73
const { model, services } = await loadSchemaDocumentWithServices ( schemaFile ) ;
66
- await import ( "@dotenvx/dotenvx/ config" )
67
- const SUPPORTED_PROVIDERS = [ 'sqlite' , 'postgresql' ]
68
- const datasource = getDatasource ( model )
74
+ config ( ) ;
75
+ const SUPPORTED_PROVIDERS = [ 'sqlite' , 'postgresql' ] ;
76
+ const datasource = getDatasource ( model ) ;
69
77
70
78
if ( ! datasource ) {
71
- throw new Error ( 'No datasource found in the schema.' )
79
+ throw new Error ( 'No datasource found in the schema.' ) ;
72
80
}
73
81
74
82
if ( ! SUPPORTED_PROVIDERS . includes ( datasource . provider ) ) {
75
- throw new Error ( `Unsupported datasource provider: ${ datasource . provider } ` )
83
+ throw new Error ( `Unsupported datasource provider: ${ datasource . provider } ` ) ;
76
84
}
77
85
78
86
const provider = providers [ datasource . provider ] ;
79
87
80
88
if ( ! provider ) {
81
- throw new Error (
82
- `No introspection provider found for: ${ datasource . provider } `
83
- )
89
+ throw new Error ( `No introspection provider found for: ${ datasource . provider } ` ) ;
84
90
}
85
91
86
- const { enums, tables } = await provider . introspect ( datasource . url )
92
+ const { enums, tables } = await provider . introspect ( datasource . url ) ;
87
93
88
94
const newModel : Model = {
89
95
$type : 'Model' ,
90
96
$container : undefined ,
91
97
$containerProperty : undefined ,
92
98
$containerIndex : undefined ,
93
- declarations : [ ...model . declarations . filter ( d => [ " DataSource" ] . includes ( d . $type ) ) ] ,
99
+ declarations : [ ...model . declarations . filter ( ( d ) => [ ' DataSource' ] . includes ( d . $type ) ) ] ,
94
100
imports : [ ] ,
95
101
} ;
96
102
103
+ syncEnums ( { dbEnums : enums , model : newModel , services, options } ) ;
97
104
98
- syncEnums ( { dbEnums : enums , model : newModel , services } )
99
-
100
-
101
-
102
- const resolvedRelations : Relation [ ] = [ ]
105
+ const resolvedRelations : Relation [ ] = [ ] ;
103
106
for ( const table of tables ) {
104
- const relations = syncTable ( { table, model : newModel , provider, services } )
105
- resolvedRelations . push ( ...relations )
107
+ const relations = syncTable ( { table, model : newModel , provider, services, options } ) ;
108
+ resolvedRelations . push ( ...relations ) ;
106
109
}
107
110
108
111
for ( const relation of resolvedRelations ) {
109
- syncRelation ( { model : newModel , relation, services } ) ;
112
+ syncRelation ( { model : newModel , relation, services, options } ) ;
110
113
}
111
114
112
115
//TODO: diff models and apply changes only
113
116
114
- const generator = await new ZModelCodeGenerator ( ) ;
117
+ const generator = new ZModelCodeGenerator ( ) ;
115
118
116
- const zmodelSchema = await generator . generate ( newModel )
119
+ const zmodelSchema = generator . generate ( newModel ) ;
117
120
118
121
console . log ( options . out ? `Writing to ${ options . out } ` : schemaFile ) ;
119
122
120
123
const outPath = options . out ? path . resolve ( options . out ) : schemaFile ;
121
124
console . log ( outPath ) ;
122
125
123
- fs . writeFileSync ( outPath , zmodelSchema )
126
+ fs . writeFileSync ( outPath , zmodelSchema ) ;
124
127
}
0 commit comments