All files / FeatureServer/lib/helpers table-layer-metadata.js

100% Statements 57/57
90.24% Branches 37/41
100% Functions 15/15
100% Lines 57/57

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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 2592x       2x     2x             2x 2x 2x               16x             16x           16x   16x               114x   16x 2x     16x             37x                                                                                                                                   35x   35x   35x   35x   35x   35x   35x   35x   35x   35x   35x       35x 35x 35x         35x 35x   35x 8x         35x   35x 6x         35x 2x         35x 18x     17x 4x 4x     13x 1x         35x 2x         35x 1x         35x 1x                                 52x   52x                                 16x 4x           12x           2x  
const _ = require('lodash')
const {
  CURRENT_VERSION,
  FULL_VERSION
} = require('../constants')
const {
  LayerFields
} = require('../helpers/fields')
 
class TableLayerMetadata {
  static create (geojson = {}, options = {}) {
    const {
      geojson: normalizedGeojson,
      options: normalizedOptions
    } = TableLayerMetadata.normalizeInput(geojson, options)
    const tableMetadata = new TableLayerMetadata()
    return tableMetadata.mixinOverrides(normalizedGeojson, normalizedOptions)
  }
 
  static normalizeInput (geojson, req) {
    const {
      metadata = {},
      capabilities,
      ...normalizedGeojson
    } = geojson
 
    const {
      params: {
        layer: layerId
      } = {},
      query = {}
    } = req
 
    const {
      currentVersion,
      fullVersion,
      description
    } = _.get(req, 'app.locals.config.featureServer', {})
 
    const normalizedOptions = _.pickBy({
      currentVersion,
      fullVersion,
      description,
      layerId,
      ...query,
      ...metadata,
      capabilities: normalizeCapabilities(capabilities, metadata.capabilities)
    }, (value) => value)
 
    if (!normalizedGeojson.features) {
      normalizedGeojson.features = []
    }
 
    return {
      geojson: normalizedGeojson,
      options: normalizedOptions
    }
  }
 
  constructor () {
    Object.assign(this, {
      id: 0,
      name: 'Not Set',
      type: 'Table',
      description: 'This is a feature service powered by https://github.com/featureserver/featureserver',
      copyrightText: ' ',
      parentLayer: null,
      subLayers: null,
      defaultVisibility: true,
      hasAttachments: false,
      htmlPopupType: 'esriServerHTMLPopupTypeNone',
      displayField: 'OBJECTID',
      typeIdField: null,
      fields: [],
      relationships: [],
      capabilities: 'Query',
      maxRecordCount: 2000,
      supportsStatistics: true,
      supportsAdvancedQueries: true,
      supportedQueryFormats: 'JSON',
      ownershipBasedAccessControlForFeatures: {
        allowOthersToQuery: true
      },
      useStandardizedQueries: true,
      advancedQueryCapabilities: {
        useStandardizedQueries: true,
        supportsStatistics: true,
        supportsOrderBy: true,
        supportsDistinct: true,
        supportsPagination: true,
        supportsTrueCurve: false,
        supportsReturningQueryExtent: true,
        supportsQueryWithDistance: true
      },
      canModifyLayer: false,
      dateFieldsTimeReference: null,
      isDataVersioned: false,
      supportsRollbackOnFailureParameter: true,
      hasM: false,
      hasZ: false,
      allowGeometryUpdates: true,
      objectIdField: 'OBJECTID',
      globalIdField: '',
      types: [],
      templates: [],
      hasStaticData: false,
      timeInfo: {},
      uniqueIdField: {
        name: 'OBJECTID',
        isSystemMaintained: true
      },
      currentVersion: CURRENT_VERSION,
      fullVersion: FULL_VERSION
    })
  }
 
  mixinOverrides (geojson = {}, options = {}) {
    const {
      id,
      idField,
      displayField,
      capabilities,
      layerId,
      hasStaticData,
      supportsPagination,
      hasAttachments
    } = options
 
    this._setFields(geojson, options)
 
    this._setId(layerId, id)
 
    this._setDisplayField(displayField, idField)
 
    this._setHasStaticData(hasStaticData)
 
    this._setCapabilities(capabilities)
 
    this._setUniqueIdField(idField)
 
    this._setPagination(supportsPagination)
 
    this._setDirectOverrides(options)
 
    this._setHasAttachments(hasAttachments)
 
    return this
  }
 
  _setFields (data, options) {
    const fields = LayerFields.create({ ...data, ...options })
    Eif (fields) {
      this.fields = fields
    }
  }
 
  _setId (layerId, metadataId) {
    const requestPathLayerId = parseInt(layerId)
    const id = !isNaN(requestPathLayerId) ? requestPathLayerId : metadataId
 
    if (id) {
      this.id = id
    }
  }
 
  _setDisplayField (displayField, idField) {
    const overrideDisplayField = displayField || idField
 
    if (overrideDisplayField) {
      this.displayField = overrideDisplayField
    }
  }
 
  _setHasStaticData (hasStaticData) {
    if (typeof hasStaticData === 'boolean') {
      this.hasStaticData = hasStaticData
    }
  }
 
  _setCapabilities (capabilities) {
    if (!capabilities) {
      return
    }
 
    if (capabilities.list) {
      this.capabilities = capabilities.list
      return
    }
 
    if (_.has(capabilities, 'extract') && !this.capabilities.includes('Extract')) {
      this.capabilities = `${this.capabilities},Extract`
    }
  }
 
  _setUniqueIdField (idField) {
    if (idField) {
      this.uniqueIdField.name = idField
    }
  }
 
  _setPagination (supportsPagination) {
    if (typeof supportsPagination === 'boolean') {
      this.advancedQueryCapabilities.supportsPagination = supportsPagination
    }
  }
 
  _setHasAttachments (hasAttachments) {
    if (hasAttachments != null && typeof hasAttachments === 'boolean') {
      this.hasAttachments = hasAttachments
    }
  }
 
  _setDirectOverrides (options) {
    const {
      name,
      relationships,
      description,
      copyrightText,
      templates,
      idField,
      timeInfo,
      maxRecordCount,
      defaultVisibility,
      currentVersion,
      fullVersion
    } = options
 
    _.merge(this, {
      name,
      relationships,
      description,
      copyrightText,
      templates,
      objectIdField: idField,
      timeInfo,
      maxRecordCount,
      defaultVisibility,
      currentVersion,
      fullVersion
    })
  }
}
 
function normalizeCapabilities (capabilities, metadataCapabilites) {
  if (_.isString(metadataCapabilites)) {
    return {
      ...capabilities,
      list: metadataCapabilites
    }
  }
 
  return {
    ...(metadataCapabilites || {}),
    ...capabilities
  }
}
 
module.exports = TableLayerMetadata