<commit_before># node modules

# Module dependencies
arangojs = require 'arangojs'
qb = require 'aqb'
url = require 'url'
merge = require 'extend'
async = require 'async'
_ = require 'underscore'
Connector = require('loopback-connector').Connector
GeoPoint = require('loopback-datasource-juggler').GeoPoint
debug = require('debug') 'loopback:connector:arango'


exports.generateConnObject = generateConnObject = (settings) ->
  u = {}
  u.protocol = settings.protocol or 'http:'
  u.hostname = settings.hostname or settings.host or '127.0.0.1'
  u.port = settings.port or 8529
  u.auth = "#{settings.username}:#{settings.password}" if settings.username and settings.password
  settings.databaseName = settings.database or settings.db or 'test'
  settings.promise = settings.promise or false
  return  url.format u

###
  Decide if field should be included
  @param {Object} fields
  @param {String} fieldName
  @returns {Boolean}
  @private
###
_fieldIncluded = (fields, fieldName) ->
  if !fields then return true

  if Array.isArray fields
    return fields.indexOf fieldName  >= 0

  if fields[fieldName]
    # Included
    return true

  if fieldName in fields and !fields[fieldName]
    # Excluded
    return false

  for f in fields
    return !fields[f]; # If the fields has exclusion

  return true


###
  Initialize the ArangoDB connector for the given data source
  @param dataSource [DataSource] The data source instance
  @param callback [Function] The callback function
###
initializeDataSource = (dataSource, callback) ->
  return if not arangojs

  s = dataSource.settings
  s.url = s.url or generateConnObject s
  dataSource.connector = new ArangoDBConnector s, dataSource
  dataSource.connector.connect callback if callback?

exports.initialize = initializeDataSource

###
  Loopback Arango Connector
  @extend Connector
###
class ArangoDBConnector extends Connector
  ###
    The constructor for ArangoDB connector
    @constructor
    @param dataSource [Object] Object to connect this connector to a data source
    @option settings host [String] The host/ip address to connect with
    @option settings port [Number] The port to connect with
    @option settings database/db [String] The database to connect with
    @option settings headers [Object] Object with header to include in every request
    @param dataSource [DataSource] The data source instance
  ###
  constructor: (settings, dataSource) ->
    super 'arangodb', settings
    # debug
    @debug = dataSource.settings.debug or debug.enabled
    # link to datasource
    @dataSource = dataSource
    # Query builder
    @qb = qb
    @returnVariable = 'result'

  ###
    Connect to ArangoDB
    @param callback [Function] The callback function, called the with created connection
  ###
  connect: (callback) ->
    debug "ArangoDB connection is called with settings: #{JSON.stringify @settings}" if @debug
    if not @db
      @db = arangojs @settings
      @api = @db.route '_api'
    process.nextTick () ->
      callback and callback null, @db

  ###
    Get the types of this connector
    @return [Array<String>] The types of connectors this connector belongs to
  ###
  getTypes: () ->
    return ['db', 'nosql', 'arangodb']

  ###
    The default Id type
    @return [Object] The class to build the Id Value with
  ###
  getDefaultIdType: () ->
    return String

  ###
    Get the model class for a certain model name
    @param model [String] The model name to lookup
    @return [Object] The model class of this model<commit_msg>Update api url from relative to absolute.<commit_after># node modules

# Module dependencies
arangojs = require 'arangojs'
qb = require 'aqb'
url = require 'url'
merge = require 'extend'
async = require 'async'
_ = require 'underscore'
Connector = require('loopback-connector').Connector
GeoPoint = require('loopback-datasource-juggler').GeoPoint
debug = require('debug') 'loopback:connector:arango'


exports.generateConnObject = generateConnObject = (settings) ->
  u = {}
  u.protocol = settings.protocol or 'http:'
  u.hostname = settings.hostname or settings.host or '127.0.0.1'
  u.port = settings.port or 8529
  u.auth = "#{settings.username}:#{settings.password}" if settings.username and settings.password
  settings.databaseName = settings.database or settings.db or 'test'
  settings.promise = settings.promise or false
  return  url.format u

###
  Decide if field should be included
  @param {Object} fields
  @param {String} fieldName
  @returns {Boolean}
  @private
###
_fieldIncluded = (fields, fieldName) ->
  if !fields then return true

  if Array.isArray fields
    return fields.indexOf fieldName  >= 0

  if fields[fieldName]
    # Included
    return true

  if fieldName in fields and !fields[fieldName]
    # Excluded
    return false

  for f in fields
    return !fields[f]; # If the fields has exclusion

  return true


###
  Initialize the ArangoDB connector for the given data source
  @param dataSource [DataSource] The data source instance
  @param callback [Function] The callback function
###
initializeDataSource = (dataSource, callback) ->
  return if not arangojs

  s = dataSource.settings
  s.url = s.url or generateConnObject s
  dataSource.connector = new ArangoDBConnector s, dataSource
  dataSource.connector.connect callback if callback?

exports.initialize = initializeDataSource

###
  Loopback Arango Connector
  @extend Connector
###
class ArangoDBConnector extends Connector
  ###
    The constructor for ArangoDB connector
    @constructor
    @param dataSource [Object] Object to connect this connector to a data source
    @option settings host [String] The host/ip address to connect with
    @option settings port [Number] The port to connect with
    @option settings database/db [String] The database to connect with
    @option settings headers [Object] Object with header to include in every request
    @param dataSource [DataSource] The data source instance
  ###
  constructor: (settings, dataSource) ->
    super 'arangodb', settings
    # debug
    @debug = dataSource.settings.debug or debug.enabled
    # link to datasource
    @dataSource = dataSource
    # Query builder
    @qb = qb
    @returnVariable = 'result'

  ###
    Connect to ArangoDB
    @param callback [Function] The callback function, called the with created connection
  ###
  connect: (callback) ->
    debug "ArangoDB connection is called with settings: #{JSON.stringify @settings}" if @debug
    if not @db
      @db = arangojs @settings
      @api = @db.route '/_api'
    process.nextTick () ->
      callback and callback null, @db

  ###
    Get the types of this connector
    @return [Array<String>] The types of connectors this connector belongs to
  ###
  getTypes: () ->
    return ['db', 'nosql', 'arangodb']

  ###
    The default Id type
    @return [Object] The class to build the Id Value with
  ###
  getDefaultIdType: () ->
    return String

  ###
    Get the model class for a certain model name
    @param model [String] The model name to lookup
    @return [Object] The model class of this model
{'fraction_non_alphanumeric': 0.09048494273999717, 'fraction_numerical': 0.003110419906687403, 'mean_word_length': 3.6631509558338826, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1027759', 'n_tokens_mistral': 2006, 'n_tokens_neox': 1957, 'n_words': 747}