| 57 | |
| 58 | // 'name:type?[]:attribute' => Field |
| 59 | static async parse(input: string, schema?: ast.Schema): Promise<Field[]> { |
| 60 | debug(`parsing "Field" for input ${input}`) |
| 61 | const [_fieldName, _fieldType = "String", _attribute] = input.split(":") |
| 62 | let attribute = _attribute as string |
| 63 | let fieldName = uncapitalize(_fieldName as string) |
| 64 | let fieldType = capitalize(_fieldType) |
| 65 | const isId = fieldName === "id" |
| 66 | let isRequired = true |
| 67 | let isList = false |
| 68 | let isUpdatedAt = false |
| 69 | let isUnique = false |
| 70 | let defaultValue = undefined |
| 71 | let relationFromFields = undefined |
| 72 | let relationToFields = undefined |
| 73 | let maybeIdField = undefined |
| 74 | |
| 75 | if (fieldType.includes("?")) { |
| 76 | fieldType = fieldType.replace("?", "") |
| 77 | isRequired = false |
| 78 | } |
| 79 | |
| 80 | const {prismaType, default: defaultConfigValue} = |
| 81 | (await Field.getConfigForPrismaType(fieldType)) ?? {} |
| 82 | if (prismaType) { |
| 83 | fieldType = prismaType |
| 84 | } |
| 85 | if (defaultConfigValue) { |
| 86 | attribute = `default=${defaultConfigValue}` |
| 87 | } |
| 88 | |
| 89 | if (fieldType.includes("[]")) { |
| 90 | fieldType = fieldType.replace("[]", "") |
| 91 | fieldName = uncapitalize(fieldName) |
| 92 | isList = true |
| 93 | } |
| 94 | if (fieldType === FieldType.Uuid) { |
| 95 | fieldType = FieldType.String |
| 96 | attribute = "default=uuid" |
| 97 | } |
| 98 | // use original unmodified field name in case the list handling code |
| 99 | // has modified fieldName |
| 100 | if (typeof _fieldName === "string" && isRelation(_fieldName)) { |
| 101 | // this field is an object type, not a scalar type |
| 102 | const relationType = Relation[_fieldName as keyof typeof Relation] |
| 103 | // translate the type into the name since they should stay in sync |
| 104 | fieldName = uncapitalize(fieldType) |
| 105 | fieldType = singlePascal(fieldType) |
| 106 | |
| 107 | switch (relationType) { |
| 108 | case Relation.belongsTo: |
| 109 | // current model gets two fields: |
| 110 | // modelName ModelName @relation(fields: [modelNameId], references: [id]) |
| 111 | // modelNameId ModelIdType |
| 112 | const idFieldName = `${fieldName}Id` |
| 113 | relationFromFields = [idFieldName] |
| 114 | relationToFields = ["id"] |
| 115 | |
| 116 | const relationModel = schema?.list.find(function (component): component is ast.Model { |