MCPcopy Create free account
hub / github.com/subquery/subql / getColumnOption

Function getColumnOption

packages/node-core/src/utils/graphql.ts:43–146  ·  view source on GitHub ↗
(
  field: GraphQLEntityField,
  enums: Map<string, EnumType>,
  schema: string
)

Source from the content-addressed store, hash-verified

41}
42
43export 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 () {

Callers 2

createColumnMethod · 0.90

Calls 6

getTypeByScalarNameFunction · 0.90
enumNameToHashFunction · 0.85
BigIntInterface · 0.85
processGetJsonFunction · 0.85
processSetJsonFunction · 0.85
mapMethod · 0.80

Tested by

no test coverage detected