Skip to content

Commit 121a9b8

Browse files
PhilzenTobbe
andauthored
[CLI] Refactor & simplify schema helpers (#11896)
Co-authored-by: Tobbe Lundberg <[email protected]>
1 parent 56c712a commit 121a9b8

File tree

1 file changed

+57
-59
lines changed

1 file changed

+57
-59
lines changed

packages/cli/src/lib/schemaHelpers.js

+57-59
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,38 @@ import { singularize, isPlural } from './rwPluralize'
1010
import { getPaths } from './'
1111

1212
/**
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()`
1515
* is called with the same model name.
1616
*/
1717
const schemaMemo = {}
1818

1919
/**
20-
* Searches for the given model (ignoring case) in schema.prisma
20+
* Searches for the given model (ignoring case) in `schema.prisma`
2121
* and returns the name as it is written by the user, or
2222
* `undefined` if no model could be found
2323
*/
2424
const getExistingModelName = async (name) => {
2525
if (!name) {
2626
return undefined
2727
}
28-
// Support PascalCase, camelCase, kebab-case, UPPER_CASE, and lowercase model
29-
// names
30-
const modelName = name.replace(/[_-]/g, '').toLowerCase()
3128

29+
// Support PascalCase, camelCase, kebab-case, UPPER_CASE,
30+
// and lowercase model names
31+
const modelName = name.replace(/[_-]/g, '').toLowerCase()
3232
for (let model of Object.values(schemaMemo)) {
3333
if (model.name.toLowerCase() === modelName) {
3434
return model.name
3535
}
3636
}
3737

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) {
4140
if (model.name.toLowerCase() === modelName) {
4241
return model.name
4342
}
4443
}
44+
4545
return undefined
4646
}
4747

@@ -51,64 +51,63 @@ const getExistingModelName = async (name) => {
5151
* entire schema is returned.
5252
*/
5353
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]) {
8368
return schemaMemo[modelName]
84-
} else {
85-
return (await getSchemaDefinitions()).datamodel
8669
}
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
8790
}
8891

8992
/**
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
9396
*/
9497
export const getEnum = async (name) => {
9598
const schema = await getSchemaDefinitions()
99+
if (!name) {
100+
return schema.metadata.datamodel.enums
101+
}
96102

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+
)
109108
}
110109

111-
return schema.metadata.datamodel.enums
110+
return model
112111
}
113112

114113
/*
@@ -128,11 +127,10 @@ export const getSchemaDefinitions = () => {
128127
/*
129128
* Returns the config info defined in `schema.prisma` (provider, datasource, etc.)
130129
*/
131-
export const getSchemaConfig = () => {
132-
return getConfig({
130+
export const getSchemaConfig = () =>
131+
getConfig({
133132
datamodel: getDataModel(),
134133
})
135-
}
136134

137135
export async function verifyModelName(options) {
138136
const modelName =

0 commit comments

Comments
 (0)