<commit_before>mongoose = require 'mongoose'

schema = require './model'

module.exports = (url) ->
  db = mongoose.connect url
  Geo = db.model 'Geo', schema.Geo

  createGeo: (geo, callback) ->
    new Geo(geoToDoc geo).save callback

  createGeos: (geos, callback) ->
    result = []
    errs = undefined
    for geo in geos
      new Geo(geoToDoc geo).save (err, doc) ->
        result.push (if doc? then docToGeo doc else undefined)
        (if errs? then errs.push(err) else errs = [err]) if err?
        callback(errs, result) if result.length is geos.length

  getGeo: (id, callback) ->
    Geo.findById id, (err, doc) -> callback err, docToGeo doc

  getGeos: (callback, opt) ->
    {find, sort} = opt if opt?
    q = Geo.find find
    q = q.sort (if sort? then sort else 'created')
    q.exec (err, docs) -> callback err, (docToGeo doc for doc in docs)

exports.docToGeo = docToGeo = (doc) ->
  lnglat: doc.lnglat
  meta: doc.meta
  created: doc.created

exports.geoToDoc = geoToDoc = (geo) ->
  lnglat: geo.lnglat
  meta: geo.meta
<commit_msg>moved mongoose schemas out of model file into service<commit_after>mongoose = require 'mongoose'

GeoSchema = new mongoose.Schema
  type: String
  lnglats: []
  created: { type: Date, default: Date.now }
  meta: {}

module.exports = (opt) ->
  db = if opt.db? then opt.db else mongoose.connect opt.url
  Geo = db.model (opt.collectionName or 'Geo'), GeoSchema

  db: db
  createGeo: (geo, callback) ->
    new Geo(geoToDoc geo).save callback

  createGeos: (geos, callback) ->
    result = []
    errs = undefined
    for geo in geos
      new Geo(geoToDoc geo).save (err, doc) ->
        result.push (if doc? then docToGeo doc else undefined)
        (if errs? then errs.push(err) else errs = [err]) if err?
        callback(errs, result) if result.length is geos.length

  getGeo: (id, callback) ->
    Geo.findById id, (err, doc) -> callback err, docToGeo doc

  getGeos: (callback, opt) ->
    {find, sort, limit} = opt if opt?
    q = Geo.find find
    q = q.sort (if sort? then sort else 'created')
    if limit? then q = q.limit limit
    q.exec (err, docs) -> callback err, (docToGeo doc for doc in docs)

exports.docToGeo = docToGeo = (doc) ->
  type: doc.type
  lnglats: doc.lnglats
  meta: doc.meta
  created: doc.created

exports.geoToDoc = geoToDoc = (geo) ->
  type: geo.type
  lnglats: geo.lnglats
  meta: geo.meta

{'fraction_non_alphanumeric': 0.1266778523489933, 'fraction_numerical': 0.0, 'mean_word_length': 2.9034369885433717, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '3670616', 'n_tokens_mistral': 891, 'n_tokens_neox': 866, 'n_words': 271}