# All files / dist/indexer-management/models cost-model.js

29.17% Statements 7/24
0% Branches 0/18
0% Functions 0/5
29.17% Lines 7/24

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

    3x
    3x
    3x
    3x
    3x

    3x
    3x

     "use strict";
    /* eslint-disable @typescript-eslint/no-empty-interface */
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.defineCostModelModels = exports.CostModel = exports.parseGraphQLCostModel = void 0;
    const sequelize_1 = require("sequelize");
    const ethers_1 = require("ethers");
    exports.parseGraphQLCostModel = (costModel) => {
        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}`);
        }
    };
    class CostModel extends sequelize_1.Model {
        // eslint-disable-next-line @typescript-eslint/ban-types
        toGraphQL() {
            return Object.assign(Object.assign({}, this.toJSON()), { variables: this.variables === null ? this.variables : JSON.stringify(this.variables), __typename: 'CostModel' });
        }
    }
    exports.CostModel = CostModel;
    exports.defineCostModelModels = (sequelize) => {
        CostModel.init({
            id: {
                type: sequelize_1.DataTypes.INTEGER,
                autoIncrement: true,
                unique: true,
            },
            deployment: {
                type: sequelize_1.DataTypes.STRING,
                allowNull: true,
                primaryKey: true,
                validate: {
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    isDeploymentID: (value) => {
                        if (typeof value !== 'string') {
                            throw new Error('Deployment ID must be a string');
                        }
                        // "0x..." is ok
                        if (ethers_1.utils.isHexString(value, 32)) {
                            return;
                        }
                        throw new Error(`Deployment ID must be a valid subgraph deployment ID`);
                    },
                },
            },
            model: {
                type: sequelize_1.DataTypes.TEXT,
                allowNull: true,
            },
            variables: {
                type: sequelize_1.DataTypes.JSONB,
                allowNull: true,
                validate: {
                    isObject: (value) => {
                        if (value === null || value === undefined) {
                            return;
                        }
                        if (value instanceof Object && !(value instanceof Array)) {
                            return;
                        }
                        throw new Error(`Variables must be a valid object or null`);
                    },
                },
            },
        }, {
            modelName: 'CostModel',
            sequelize,
        });
        return { ['CostModel']: CostModel };
    };
    //# sourceMappingURL=cost-model.js.map