@@ -10,38 +10,38 @@ import { singularize, isPlural } from './rwPluralize'
10
10
import { getPaths } from './'
11
11
12
12
/**
13
- * Used to memoize results from `getSchema` so we don't have to go through
14
- * the work of open the file and parsing it from scratch each time getSchema()
13
+ * Used to memoize results from `getSchema() ` so we don't have to go through
14
+ * the work of opening and parsing the file from scratch each time ` getSchema()`
15
15
* is called with the same model name.
16
16
*/
17
17
const schemaMemo = { }
18
18
19
19
/**
20
- * Searches for the given model (ignoring case) in schema.prisma
20
+ * Searches for the given model (ignoring case) in ` schema.prisma`
21
21
* and returns the name as it is written by the user, or
22
22
* `undefined` if no model could be found
23
23
*/
24
24
const getExistingModelName = async ( name ) => {
25
25
if ( ! name ) {
26
26
return undefined
27
27
}
28
- // Support PascalCase, camelCase, kebab-case, UPPER_CASE, and lowercase model
29
- // names
30
- const modelName = name . replace ( / [ _ - ] / g, '' ) . toLowerCase ( )
31
28
29
+ // Support PascalCase, camelCase, kebab-case, UPPER_CASE,
30
+ // and lowercase model names
31
+ const modelName = name . replace ( / [ _ - ] / g, '' ) . toLowerCase ( )
32
32
for ( let model of Object . values ( schemaMemo ) ) {
33
33
if ( model . name . toLowerCase ( ) === modelName ) {
34
34
return model . name
35
35
}
36
36
}
37
37
38
- const schema = await getSchemaDefinitions ( )
39
-
40
- for ( let model of schema . datamodel . models ) {
38
+ const schema = ( await getSchemaDefinitions ( ) ) . datamodel
39
+ for ( let model of schema . models ) {
41
40
if ( model . name . toLowerCase ( ) === modelName ) {
42
41
return model . name
43
42
}
44
43
}
44
+
45
45
return undefined
46
46
}
47
47
@@ -51,64 +51,63 @@ const getExistingModelName = async (name) => {
51
51
* entire schema is returned.
52
52
*/
53
53
export const getSchema = async ( name ) => {
54
- if ( name ) {
55
- const modelName = await getExistingModelName ( name )
56
- if ( ! modelName ) {
57
- throw new Error (
58
- `No schema definition found for \`${ name } \` in schema.prisma file` ,
59
- )
60
- }
61
- if ( ! schemaMemo [ modelName ] ) {
62
- const schema = await getSchemaDefinitions ( )
63
- const model = schema . datamodel . models . find ( ( model ) => {
64
- return model . name === modelName
65
- } )
66
-
67
- if ( model ) {
68
- // look for any fields that are enums and attach the possible enum values
69
- // so we can put them in generated test files
70
- model . fields . forEach ( ( field ) => {
71
- const fieldEnum = schema . datamodel . enums . find ( ( e ) => {
72
- return field . type === e . name
73
- } )
74
- if ( fieldEnum ) {
75
- field . enumValues = fieldEnum . values
76
- }
77
- } )
78
-
79
- // memoize based on the model name
80
- schemaMemo [ modelName ] = model
81
- }
82
- }
54
+ const schema = ( await getSchemaDefinitions ( ) ) . datamodel
55
+
56
+ if ( ! name ) {
57
+ return schema
58
+ }
59
+
60
+ const modelName = await getExistingModelName ( name )
61
+ if ( ! modelName ) {
62
+ throw new Error (
63
+ `No schema definition found for \`${ name } \` in schema.prisma file` ,
64
+ )
65
+ }
66
+
67
+ if ( schemaMemo [ modelName ] ) {
83
68
return schemaMemo [ modelName ]
84
- } else {
85
- return ( await getSchemaDefinitions ( ) ) . datamodel
86
69
}
70
+
71
+ const model = schema . models . find ( ( model ) => model . name === modelName )
72
+ if ( ! model ) {
73
+ // TODO: Can this happen, and if yes, should we prefer throwing an error?
74
+ return undefined
75
+ }
76
+
77
+ // Look for any fields that are enums and attach the possible enum values
78
+ // so we can put them in generated test files
79
+ model . fields . forEach ( ( field ) => {
80
+ const fieldEnum = schema . enums . find ( ( e ) => field . type === e . name )
81
+ if ( fieldEnum ) {
82
+ field . enumValues = fieldEnum . values
83
+ }
84
+ } )
85
+
86
+ // Memoize based on the model name
87
+ schemaMemo [ modelName ] = model
88
+
89
+ return model
87
90
}
88
91
89
92
/**
90
- * Returns the enum defined with the given `name` parsed from
91
- * the schema.prisma of the target application. If no `name` is given then the
92
- * all enum definitions are returned
93
+ * Returns the enum defined with the given `name` parsed from the
94
+ * ` schema.prisma` of the target application. If no `name` is given
95
+ * then all enum definitions are returned
93
96
*/
94
97
export const getEnum = async ( name ) => {
95
98
const schema = await getSchemaDefinitions ( )
99
+ if ( ! name ) {
100
+ return schema . metadata . datamodel . enums
101
+ }
96
102
97
- if ( name ) {
98
- const model = schema . datamodel . enums . find ( ( model ) => {
99
- return model . name === name
100
- } )
101
-
102
- if ( model ) {
103
- return model
104
- } else {
105
- throw new Error (
106
- `No enum schema definition found for \`${ name } \` in schema.prisma file` ,
107
- )
108
- }
103
+ const model = schema . datamodel . enums . find ( ( model ) => model . name === name )
104
+ if ( ! model ) {
105
+ throw new Error (
106
+ `No enum schema definition found for \`${ name } \` in schema.prisma file` ,
107
+ )
109
108
}
110
109
111
- return schema . metadata . datamodel . enums
110
+ return model
112
111
}
113
112
114
113
/*
@@ -128,11 +127,10 @@ export const getSchemaDefinitions = () => {
128
127
/*
129
128
* Returns the config info defined in `schema.prisma` (provider, datasource, etc.)
130
129
*/
131
- export const getSchemaConfig = ( ) => {
132
- return getConfig ( {
130
+ export const getSchemaConfig = ( ) =>
131
+ getConfig ( {
133
132
datamodel : getDataModel ( ) ,
134
133
} )
135
- }
136
134
137
135
export async function verifyModelName ( options ) {
138
136
const modelName =
0 commit comments