<commit_before>// Expects FOAM to already be loaded globally, and MongoDB's Node module to be installed.
var mongo = require('mongodb');

global.MongoDAO = FOAM({
  model_: 'Model',
  extendsModel: 'AbstractDAO',

  name: 'MongoDAO',
  label: 'MongoDB DAO',

  properties: [
    {
      name:  'model',
      label: 'Model',
      type:  'Model',
      required: true
    },
    {
      name:  'database',
      label: 'URL for the Database, as used by MongoClient, eg. "mongodb://localhost:27017/example"',
      type:  'String',
      required: true
    },
    {
      name:  'collection',
      label: 'Collection name',
      type:  'String',
      defaultValueFn: function() {
        return this.model.plural;
      }
    }
  ],

  methods: {
    init: function() {
      this.SUPER();

      this.withDB = amemo(this.openDB.bind(this));

      this.serialize = this.FOAMSerialize;
      this.deserialize = this.FOAMDeserialize;
    },

    FOAMDeserialize: function(json) {
      return JSONToObject.visitObject(json);
    },

    FOAMSerialize: function(obj) {
      return ObjectToJSON.visitObject(obj);
    },

    // NB: Returns the collection, not the database connection.
    openDB: function(cc) {
      var self = this;
      mongo.MongoClient.connect(this.database, function(err, db) {
        if (!err) {
          cc(db.collection(self.collection));
        } else {
          console.error(err);
        }
      });
    },

    withSink: function(sink, future) {
      var self = this;
      return function(err, result) {
        if (err) sink && sink.error && sink.error(err);
        else if (result == null) {
          sink && sink.eof && sink.eof();
          future.set(sink, err || undefined);
        }
        else sink && sink.put && sink.put(self.deserialize(result));
      };
    },

    put: function(value, sink) {
      var serialized = this.serialize(value);
      serialized._id = value.id;<commit_msg>Don't call future.set in MongoDAO's withSink unless it's defined<commit_after>// Expects FOAM to already be loaded globally, and MongoDB's Node module to be installed.
var mongo = require('mongodb');

global.MongoDAO = FOAM({
  model_: 'Model',
  extendsModel: 'AbstractDAO',

  name: 'MongoDAO',
  label: 'MongoDB DAO',

  properties: [
    {
      name:  'model',
      label: 'Model',
      type:  'Model',
      required: true
    },
    {
      name:  'database',
      label: 'URL for the Database, as used by MongoClient, eg. "mongodb://localhost:27017/example"',
      type:  'String',
      required: true
    },
    {
      name:  'collection',
      label: 'Collection name',
      type:  'String',
      defaultValueFn: function() {
        return this.model.plural;
      }
    }
  ],

  methods: {
    init: function() {
      this.SUPER();

      this.withDB = amemo(this.openDB.bind(this));

      this.serialize = this.FOAMSerialize;
      this.deserialize = this.FOAMDeserialize;
    },

    FOAMDeserialize: function(json) {
      return JSONToObject.visitObject(json);
    },

    FOAMSerialize: function(obj) {
      return ObjectToJSON.visitObject(obj);
    },

    // NB: Returns the collection, not the database connection.
    openDB: function(cc) {
      var self = this;
      mongo.MongoClient.connect(this.database, function(err, db) {
        if (!err) {
          cc(db.collection(self.collection));
        } else {
          console.error(err);
        }
      });
    },

    withSink: function(sink, future) {
      var self = this;
      return function(err, result) {
        if (err) sink && sink.error && sink.error(err);
        else if (result == null) {
          sink && sink.eof && sink.eof();
          future && future.set(sink, err || undefined);
        }
        else sink && sink.put && sink.put(self.deserialize(result));
      };
    },

    put: function(value, sink) {
      var serialized = this.serialize(value);
      serialized._id = value.id;
{'fraction_non_alphanumeric': 0.13752854605430093, 'fraction_numerical': 0.002537427048972342, 'mean_word_length': 2.2578512396694217, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6092129', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1232, 'n_words': 290}