<commit_before>Mesh can be used as the transport layer for models that represent data. Here's a simple example of how to setup this integration:

```javascript
class Model {

  /**
   * constructor
   * @param bus the transport for the model
   * @param the source of truth data for the model
   */

  constructor(collectionName, bus, data) {
    this.collectionName = collectionName;
    this.bus = bus;
    this.data = data || {};
  }

  /**
   * sets properties on the model data
   */

  setProperties(properties) {
    Object.assign(this.data, properties);
  }

  /**
   */

  getProperty(key) {
    return this.data[key];
  }

  /**
   * inserts the model
   */

  insert() {
    return this._sync("insert", {
      data: this.data
    })
  }

  /**
   */

  update() {
    return this._sync("update", {
      query : { _id: this.getProperty("_id") },
      data  : this.data
    })
  }

  /**
   */

  remove() {
    return this._sync("remove", {
      query: { _id: this.getProperty("_id") }
    });
  }

  /**
   * loads data from the source of truth
   */

  load() {
    return this._sync("load", {
      query: { _id: this.getProperty("_id") }
    })
  },

  /**
   * executes an operation against this model. Useful if you need to execute
   * any other operation against the model on the backend (user.resetPassword, user.logout)
   */

  fetch(action, operationProperties) {
    return this.bus.execute(Object.assign({
      action: action
    }, operationProperties));
  }

  /**
   * synchronizes data from the bus to this model
   */

  _sync(action, operationProperties) {
    var response = this.fetch(action, operationProperties);

    // the first chunk on the operation is the data returned from the
    // source of truth. We only want that.
    response.read().then((chunk) => {
      if (!chunk.done) this.data = chunk.value;
    });

    return response;
  }
}
```

This base model class is plain enough to be adaptable with just about any data source. Here's how you might use it with a mongodb bus:

```javascript
import { AttachDefaultsBus } from "mesh";

var bus = MongoDbBus.create();
// var bus = LocalStorageDbBus.create();
// var bus = MemoryDbBus.create();

// attach { collection: "people" } to all executed operations
var peopleDbCollectionBus = AttachDefaultsBus.create({
  collection: "people"
}, bus);

var model = new Model(peopleDbCollectionBus);

model.setProperties({
  name: "Tony"
});

model.insert().then(function() {
  console.log(model.data); // { _id: MongoId, name: "Tony" }
});
```
This example attaches a `collection` property for each operation since Mongodb needs to know where to create, read, update, and delete data from. By using the `AttachDefaultsBus` we can safely define this information *outside* of the model so that this adapter-specific code doesn't end up in our model base class. This pattern makes our base model, again, pretty adaptable many different use cases.

Creating a model base class that works with any data source is one thing. Making it testable is also important. Easy enough:


```javascript
import expect from "expect.js";
import Model from "./model";
import { BufferedResponse, AttachDefaultsBus } from "mesh";

// note - defining __filename in the describe block makes test files greppable. Super
// awesome for TDD.
describe(__filename + "#", function() {

  var ops = [];
  var bus;
<commit_msg>Update models.md<commit_after>Mesh can be used as the transport layer for models that represent data. Here's a simple example of how to setup this integration:

```javascript
class Model {

  /**
   * constructor
   * @param bus the transport for the model
   * @param the source of truth data for the model
   */

  constructor(collectionName, bus, data) {
    this.collectionName = collectionName;
    this.bus = bus;
    this.data = data || {};
  }

  /**
   * sets properties on the model data
   */

  setProperties(properties) {
    Object.assign(this.data, properties);
  }

  /**
   */

  getProperty(key) {
    return this.data[key];
  }

  /**
   * inserts the model
   */

  insert() {
    return this._sync("insert", {
      data: this.data
    })
  }

  /**
   */

  update() {
    return this._sync("update", {
      query : { _id: this.getProperty("_id") },
      data  : this.data
    })
  }

  /**
   */

  remove() {
    return this._sync("remove", {
      query: { _id: this.getProperty("_id") }
    });
  }

  /**
   * loads data from the source of truth
   */

  load() {
    return this._sync("load", {
      query: { _id: this.getProperty("_id") }
    })
  },

  /**
   * executes an operation against this model. Useful if you need to execute
   * any other operation against the model on the backend (user.resetPassword, user.logout)
   */

  fetch(action, operationProperties) {
    return this.bus.execute(Object.assign({
      action: action
    }, operationProperties));
  }

  /**
   * synchronizes data from the bus to this model
   */

  _sync(action, operationProperties) {
    var response = this.fetch(action, operationProperties);

    // the first chunk on the operation is the data returned from the
    // source of truth. We only want that.
    response.read().then((chunk) => {
      if (!chunk.done) this.data = chunk.value;
    });

    return response;
  }
}
```

This base model class is plain enough to be adaptable with just about any data source. Here's how you might use it with a mongodb bus:

```javascript
import { AttachDefaultsBus } from "mesh";

var bus = MongoDbBus.create();
// var bus = LocalStorageDbBus.create();
// var bus = MemoryDbBus.create();

// attach { collection: "people" } to all executed operations
var peopleDbCollectionBus = AttachDefaultsBus.create({
  collection: "people"
}, bus);

var model = new Model(peopleDbCollectionBus);

model.setProperties({
  name: "Tony"
});

model.insert().then(function() {
  console.log(model.data); // { _id: MongoId, name: "Tony" }
});
```
This example attaches a `collection` property for each model operation since Mongodb needs to know where to create, read, update, and delete data from. By using the `AttachDefaultsBus` we can safely define this information *outside* of the model so that this adapter-specific code doesn't end up in our model base class. This pattern makes our base model, again, pretty adaptable many different use cases.

Creating a model base class that works with any data source is one thing. Making it testable is also important. Easy enough:


```javascript
import expect from "expect.js";
import Model from "./model";
import { BufferedResponse, AttachDefaultsBus } from "mesh";

// note - defining __filename in the describe block makes test files greppable. Super
// awesome for TDD.
describe(__filename + "#", function() {

  var ops = [];
  var bus;

{'fraction_non_alphanumeric': 0.11756896297388995, 'fraction_numerical': 0.0, 'mean_word_length': 3.0942028985507246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1013953', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1922, 'n_words': 712}