# All files / indexer-management/models cost-model.ts

81.82% Statements 18/22
83.33% Branches 15/18
100% Functions 5/5
81.82% Lines 18/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127

    1x
    1x

    1x

    27x
    27x

    27x

    1x

    37x

    1x
    14x

    17x

    17x
    17x

    21x
    2x

    19x
    19x

    14x

     /* eslint-disable @typescript-eslint/no-empty-interface */

    import { Optional, Model, DataTypes, Sequelize } from 'sequelize'
    import { utils } from 'ethers'

    export interface GraphQLCostModel {
      deployment: string
      model: string | null | undefined
      variables: string | null | undefined
    }

    export const parseGraphQLCostModel = (
      costModel: GraphQLCostModel,
    ): CostModelCreationAttributes => {
      try {
        const variables = !costModel.variables
          ? costModel.variables
          : JSON.parse(costModel.variables)

        return {
          deployment: costModel.deployment,
          model: costModel.model || null,
          variables: variables,
        }
      } catch (error) {
        throw new Error(`Failed to parse GraphQL cost model: ${error}`)
      }
    }

    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    export type CostModelVariables = { [key: string]: any }

    export interface CostModelAttributes {
      id: number
      deployment: string
      model: string | null
      variables: CostModelVariables | null
    }

    export interface CostModelCreationAttributes
      extends Optional<CostModelAttributes, 'id' | 'deployment'> {}

    export class CostModel
      extends Model<CostModelAttributes, CostModelCreationAttributes>
      implements CostModelAttributes {
      public id!: number
      public deployment!: string
      public model!: string | null
      public variables!: CostModelVariables | null

      public createdAt!: Date
      public updatedAt!: Date

      // eslint-disable-next-line @typescript-eslint/ban-types
      public toGraphQL(): object {
        return {
          ...this.toJSON(),
          variables:
            this.variables === null ? this.variables : JSON.stringify(this.variables),
          __typename: 'CostModel',
        }
      }
    }

    export interface CostModelModels {
      CostModel: typeof CostModel
    }

    export const defineCostModelModels = (sequelize: Sequelize): CostModelModels => {
      CostModel.init(
        {
          id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            unique: true,
          },
          deployment: {
            type: DataTypes.STRING,
            allowNull: true,
            primaryKey: true,
            validate: {
              // eslint-disable-next-line @typescript-eslint/no-explicit-any
              isDeploymentID: (value: any) => {
                Iif (typeof value !== 'string') {
                  throw new Error('Deployment ID must be a string')
                }

                // "0x..." is ok
                Eif (utils.isHexString(value, 32)) {
                  return
                }

                throw new Error(`Deployment ID must be a valid subgraph deployment ID`)
              },
            },
          },
          model: {
            type: DataTypes.TEXT,
            allowNull: true,
          },
          variables: {
            type: DataTypes.JSONB,
            allowNull: true,
            validate: {
              isObject: (value: unknown | null) => {
                if (value === null || value === undefined) {
                  return
                }

                Eif (value instanceof Object && !(value instanceof Array)) {
                  return
                }

                throw new Error(`Variables must be a valid object or null`)
              },
            },
          },
        },
        {
          modelName: 'CostModel',
          sequelize,
        },
      )

      return { ['CostModel']: CostModel }
    }