All files / lib/helpers/fields field-classes.js

98.03% Statements 50/51
85% Branches 17/20
100% Functions 9/9
98.03% Lines 50/51

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      1x                   1x       32x 32x       32x 32x       47x 42x 5x               25x 25x 25x 25x 25x 25x 25x           20x 20x 20x 20x 20x 20x 20x 20x           7x 7x 7x 7x 7x 7x 7x           5x 5x 5x           28x                 28x   28x 28x 28x 28x 28x 28x 28x   28x 27x             12x 12x 12x 12x       1x                
const {
  getEsriTypeFromDefinition,
  getEsriTypeFromValue
} = require('./esri-type-utils')
const {
  ESRI_FIELD_TYPE_OID,
  ESRI_FIELD_TYPE_STRING,
  ESRI_FIELD_TYPE_DATE,
  ESRI_FIELD_TYPE_DOUBLE,
  SQL_TYPE_INTEGER,
  SQL_TYPE_OTHER,
  SQL_TYPE_FLOAT,
  OBJECTID_DEFAULT_KEY
} = require('./constants')
 
class Field {
  setEditable (value = false) {
    this.editable = value
    return this
  }
 
  setNullable (value = false) {
    this.nullable = value
    return this
  }
 
  setLength () {
    if (this.type === ESRI_FIELD_TYPE_STRING) {
      this.length = 128
    } else Iif (this.type === ESRI_FIELD_TYPE_DATE) {
      this.length = 36
    }
  }
}
 
class ObjectIdField extends Field {
  constructor (key = OBJECTID_DEFAULT_KEY) {
    super()
    this.name = key
    this.type = ESRI_FIELD_TYPE_OID
    this.alias = key
    this.sqlType = SQL_TYPE_INTEGER
    this.domain = null
    this.defaultValue = null
  }
}
 
class FieldFromKeyValue extends Field {
  constructor (key, value) {
    super()
    this.name = key
    this.type = getEsriTypeFromValue(value)
    this.alias = key
    this.sqlType = SQL_TYPE_OTHER
    this.domain = null
    this.defaultValue = null
    this.setLength()
  }
}
 
class StatisticField extends Field {
  constructor (key) {
    super()
    this.name = key
    this.type = ESRI_FIELD_TYPE_DOUBLE
    this.sqlType = SQL_TYPE_FLOAT
    this.alias = key
    this.domain = null
    this.defaultValue = null
  }
}
 
class StatisticDateField extends StatisticField {
  constructor (key) {
    super(key)
    this.type = ESRI_FIELD_TYPE_DATE
    this.sqlType = SQL_TYPE_OTHER
  }
}
 
class FieldFromFieldDefinition extends Field {
  constructor (fieldDefinition) {
    super()
    const {
      name,
      type,
      alias,
      domain,
      sqlType,
      length,
      defaultValue
    } = fieldDefinition
 
    this.name = name
    this.type = getEsriTypeFromDefinition(type)
    this.alias = alias || name
    this.sqlType = sqlType || SQL_TYPE_OTHER
    this.domain = domain || null
    this.defaultValue = defaultValue || null
    this.length = length
 
    if (!this.length || !Number.isInteger(this.length)) {
      this.setLength()
    }
  }
}
 
class ObjectIdFieldFromDefinition extends FieldFromFieldDefinition {
  constructor (definition = {}) {
    super(definition)
    this.type = ESRI_FIELD_TYPE_OID
    this.sqlType = SQL_TYPE_INTEGER
    delete this.length
  }
}
 
module.exports = {
  ObjectIdField,
  ObjectIdFieldFromDefinition,
  FieldFromKeyValue,
  FieldFromFieldDefinition,
  StatisticField,
  StatisticDateField
}