text: const mongoose = require('mongoose');
  const { Implementation } = require('../../Implementation');
  const { MongooseFieldAdapter } = require('@keystone-alpha/adapter-mongoose');
  const { KnexFieldAdapter } = require('@keystone-alpha/adapter-knex');
  
  class Decimal extends Implementation {
    constructor() {
      super(...arguments);
    }
  
    get gqlOutputFields() {
      return [`${this.path}: String`];
    }
    get gqlOutputFieldResolvers() {
      return { [`${this.path}`]: item => item[this.path] };
    }
  
    get gqlQueryInputFields() {
      return [
        ...this.equalityInputFields('String'),
        ...this.orderingInputFields('String'),
        ...this.inInputFields('String'),
      ];
    }
    get gqlUpdateInputFields() {
      return [`${this.path}: String`];
    }
    get gqlCreateInputFields() {
      return [`${this.path}: String`];
    }
    extendAdminMeta(meta) {
      return {
        ...meta,
        symbol: this.config.symbol,
      };
    }
  }
  
  class MongoDecimalInterface extends MongooseFieldAdapter {
    addToMongooseSchema(schema) {
      const { mongooseOptions = {} } = this.config;
      const { isRequired } = mongooseOptions;
  
      const validator = a => typeof a === 'object' && /^-?\d*\.?\d*$/.test(a);
      const schemaOptions = {
        type: mongoose.Decimal128,
        validate: {
          validator: this.buildValidator(validator, isRequired),
          message: '{VALUE} is not a Decimal value',
        },
      };
      schema.add({ [this.path]: this.mergeSchemaOptions(schemaOptions, this.config) });
    }
  
    setupHooks({ addPreSaveHook, addPostReadHook }) {
      // Updates the relevant value in the item provided (by referrence)
      addPreSaveHook(item => {
        if (item[this.path] && typeof item[this.path] === 'string') {
          item[this.path] = mongoose.Types.Decimal128.fromString(item[this.path]);
        } else if (!item[this.path]) {
          item[this.path] = null;
        }
        // else: Must either be undefined or a Decimal128 object, so leave it alone.
        return item;
      });
      addPostReadHook(item => {
        if (item[this.path]) {
          item[this.path] = item[this.path].toString();
        }
        return item;
      });
    }
  
    getQueryConditions(dbPath) {
      return {
        ...this.equalityConditions(dbPath, mongoose.Types.Decimal128.fromString),
        ...this.orderingConditions(dbPath, mongoose.Types.Decimal128.fromString),
        ...this.inConditions(dbPath, mongoose.Types.Decimal128.fromString),
      };
    }
  }
  
  class KnexDecimalInterface extends KnexFieldAdapter {
    createColumn(table) {
      return table.decimal(this.path);
    }
    getQueryConditions(dbPath) {
      return {
        ...this.equalityConditions(dbPath),
        ...this.orderingConditions(dbPath),
        ...this.inConditions(dbPath),
      };
    }
  }
  
  module.exports = {
    Decimal,
    MongoDecimalInterface,
    KnexDecimalInterface,
  };
  