( field: GraphQLEntityField, enums: Map<string, EnumType>, schema: string )
| 41 | } |
| 42 | |
| 43 | export function getColumnOption( |
| 44 | field: GraphQLEntityField, |
| 45 | enums: Map<string, EnumType>, |
| 46 | schema: string |
| 47 | ): ModelAttributeColumnOptions { |
| 48 | const allowNull = field.nullable; |
| 49 | |
| 50 | let enumType: string | null = null; |
| 51 | if (field.isEnum) { |
| 52 | const enumTypeName = enumNameToHash(field.type); |
| 53 | const enumTypeNameDeprecated = `${schema}_enum_${enumNameToHash(field.type)}`; |
| 54 | |
| 55 | [enumTypeName, enumTypeNameDeprecated].forEach((t) => { |
| 56 | if (enums.has(t)) { |
| 57 | enumType = t === enumTypeNameDeprecated ? `"${t}"` : `"${schema}"."${enumTypeName}"`; |
| 58 | } |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | if (field.isEnum && !enumType) throw new Error('Unable to get enum type'); |
| 63 | |
| 64 | const type = field.isEnum |
| 65 | ? `${enumType}${field.isArray ? '[]' : ''}` |
| 66 | : field.isArray || field.jsonInterface |
| 67 | ? getTypeByScalarName('Json')?.sequelizeType |
| 68 | : getTypeByScalarName(field.type)?.sequelizeType; |
| 69 | |
| 70 | if (type === undefined) { |
| 71 | throw new Error('Unable to get model type'); |
| 72 | } |
| 73 | |
| 74 | const columnOption: ModelAttributeColumnOptions<any> = { |
| 75 | type, |
| 76 | comment: field.description, |
| 77 | allowNull, |
| 78 | primaryKey: field.type === 'ID', |
| 79 | }; |
| 80 | if (field.type === 'BigInt') { |
| 81 | columnOption.get = function () { |
| 82 | const dataValue = this.getDataValue(field.name); |
| 83 | if (field.isArray) { |
| 84 | return dataValue ? dataValue.map((v: any) => BigInt(v)) : null; |
| 85 | } |
| 86 | return dataValue ? BigInt(dataValue) : null; |
| 87 | }; |
| 88 | columnOption.set = function (val: any) { |
| 89 | if (field.isArray) { |
| 90 | this.setDataValue( |
| 91 | field.name, |
| 92 | (val as any[])?.map((v) => v.toString()) |
| 93 | ); |
| 94 | } else { |
| 95 | this.setDataValue(field.name, val?.toString()); |
| 96 | } |
| 97 | }; |
| 98 | } |
| 99 | if (field.type === 'Bytes') { |
| 100 | columnOption.get = function () { |
no test coverage detected