<commit_before># lib
{castAll, convertComplexSelector} = require '../util'

###
@mixin: bongo "find" methods.
@description: Methods for selecting against the model's
  MongoDB collection.  You can mix this into constructors
  that have static methods with all of the following:
  @requiredSignature: SkinCollection obj.getCollection()
  @requiredSignature: obj.castValue(selector, constructor)
###
module.exports =

  drop:(callback)-> @getCollection().drop callback

  update:(selector, operation, options, callback)->
    [callback, options] = [options, callback]  unless callback
    options or= {}
    options.safe = yes
    callback ?= ->
    @getCollection().update selector, operation, options, callback
    @


  findAndModify:(rest...)->
    collection = @getCollection()
    collection.findAndModify rest...

  assure:(selectorOrInitializer, callback)->
    constructor = @
    @one selectorOrInitializer, (err, inst)->
      if err
        callback err
      else if inst
        callback null, inst
      else
        callback null, new constructor selectorOrInitializer
    @

  one:(uniqueSelector, options, callback)->
    [callback, options] = [options, callback]  unless callback
    options ?= {}
    constructor = @
    collection = constructor.getCollection()
    collection.findOne castAll(uniqueSelector, constructor), options, (err, doc)->
      model = if not err and doc then new constructor doc
      callback err, model
    @

  all:(selector, callback)->
    constructor = @
    collection = constructor.getCollection()
    collection.find castAll(selector, constructor), (err, cursor)->
      if err
        callback err
      else
        cursor.toArray (err, docs)->
          callback null, docs?.map (doc)->
            new constructor doc
    @

  remove:(selector, callback)-><commit_msg>bugfix: do not fail silently<commit_after># lib
{castAll, convertComplexSelector} = require '../util'

###
@mixin: bongo "find" methods.
@description: Methods for selecting against the model's
  MongoDB collection.  You can mix this into constructors
  that have static methods with all of the following:
  @requiredSignature: SkinCollection obj.getCollection()
  @requiredSignature: obj.castValue(selector, constructor)
###
module.exports =

  drop:(callback)-> @getCollection().drop callback

  update:(selector, operation, options, callback)->
    [callback, options] = [options, callback]  unless callback
    options or= {}
    options.safe = yes
    callback ?= ->
    @getCollection().update selector, operation, options, callback
    @


  findAndModify:(rest...)->
    collection = @getCollection()
    collection.findAndModify rest...

  assure:(selectorOrInitializer, callback)->
    constructor = @
    @one selectorOrInitializer, (err, inst)->
      if err
        callback err
      else if inst
        callback null, inst
      else
        callback null, new constructor selectorOrInitializer
    @

  one:(uniqueSelector, options, callback)->
    [callback, options] = [options, callback]  unless callback
    options ?= {}
    constructor = @
    collection = constructor.getCollection()
    collection.findOne castAll(uniqueSelector, constructor), options, (err, doc)->
      model = if not err and doc then new constructor doc
      callback err, model
    @

  all:(selector, callback)->
    constructor = @
    collection = constructor.getCollection()
    collection.find castAll(selector, constructor), (err, cursor)->
      if err
        callback err
      else
        cursor.toArray (err, docs)->
          if err then callback err
          else callback null, docs?.map (doc)->
            new constructor doc
    @

  remove:(selector, callback)->
{'fraction_non_alphanumeric': 0.10672791137530398, 'fraction_numerical': 0.0, 'mean_word_length': 3.154882154882155, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '3130483', 'n_tokens_mistral': 975, 'n_tokens_neox': 936, 'n_words': 311}