<commit_before># OT storage for MongoDB
# Author: <NAME> (@jdzamfi)
#
# The mongodb database contains two collections:
#
# - 'docs': document snapshots, id set to document name.
# - 'ops': document ops with docName stored in 'd' and version stored in 'v'.
#
# This implementation isn't written to support multiple frontends
# talking to a single mongo backend.

mongodb = require 'mongodb'

defaultOptions =
  # Prefix for all database keys.
  db: 'sharejs'

  # Default options
  hostname: '127.0.0.1'
  port: 27017
  mongoOptions: {auto_reconnect: true}


# Valid options as above.
module.exports = MongoDb = (options) ->

  options ?= {}
  options[k] ?= v for k, v of defaultOptions

  client = new mongodb.Db(options.db, new mongodb.Server(options.hostname, options.port, options.mongoOptions))

  opsCollectionForDoc = (docName) -> 'ops.' + encodeURIComponent(docName).replace(/\./g, '%2E').replace(/-/g, '%2D')

  # Creates a new document.
  # data = {snapshot, type:typename, [meta]}
  @create = (docName, data, callback) ->
    # docName too long? http://www.mongodb.org/display/DOCS/Collections#Collections-Overview
    return callback? "Document name too long: #{docName}" if opsCollectionForDoc(docName).length > 90

    client.collection 'docs', (err, collection) ->
      return callback? err if err
      
      doc =
        _id: docName
        data: data
      
      collection.insert doc, safe: true, (err, doc) -><commit_msg>Added support for Mongo auth and passing in client<commit_after># OT storage for MongoDB
# Author: <NAME> (@jdzamfi)
#
# The mongodb database contains two collections:
#
# - 'docs': document snapshots, id set to document name.
# - 'ops': document ops with docName stored in 'd' and version stored in 'v'.
#
# This implementation isn't written to support multiple frontends
# talking to a single mongo backend.

mongodb = require 'mongodb'

defaultOptions =
  # Prefix for all database keys.
  db: 'sharejs'

  # Default options
  hostname: '127.0.0.1'
  port: 27017
  mongoOptions: {auto_reconnect: true}
  client: null    # an instance of mongodb.Db
  user: null      # an optional username for authentication
  password: null  # an optional password for authentication

# Valid options as above.
module.exports = MongoDb = (options) ->

  options ?= {}
  options[k] ?= v for k, v of defaultOptions

  client = options.client or new mongodb.Db(options.db, new mongodb.Server(options.hostname, options.port, options.mongoOptions))
  
  if options.user and options.password
    client.auth(options.user, options.password)

  opsCollectionForDoc = (docName) -> 'ops.' + encodeURIComponent(docName).replace(/\./g, '%2E').replace(/-/g, '%2D')

  # Creates a new document.
  # data = {snapshot, type:typename, [meta]}
  @create = (docName, data, callback) ->
    # docName too long? http://www.mongodb.org/display/DOCS/Collections#Collections-Overview
    return callback? "Document name too long: #{docName}" if opsCollectionForDoc(docName).length > 90

    client.collection 'docs', (err, collection) ->
      return callback? err if err
      
      doc =
        _id: docName
        data: data
      
      collection.insert doc, safe: true, (err, doc) ->
{'fraction_non_alphanumeric': 0.12125, 'fraction_numerical': 0.009375, 'mean_word_length': 3.5339943342776206, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5494932', 'n_tokens_mistral': 1020, 'n_tokens_neox': 972, 'n_words': 322}