Repositories and models

Previously Foxx was heavily built around the concept of repositories and models, which provided complex but rarely necessary abstractions on top of ArangoDB collections and documents. In ArangoDB 3.0 these have been removed entirely.

Repositories vs collections

Repositories mostly wrapped methods that already existed on ArangoDB collection objects and primarily dealt with converting between plain ArangoDB documents and Foxx model instances. In ArangoDB 3.0 you can simply use these collections directly and treat documents as plain JavaScript objects.

Old:

  1. 'use strict';
  2. const Foxx = require('org/arangodb/foxx');
  3. const myRepo = new Foxx.Repository(
  4. applicationContext.collection('myCollection'),
  5. {model: Foxx.Model}
  6. );
  7. // ...
  8. const models = myRepo.byExample({color: 'green'});
  9. res.json(models.map(function (model) {
  10. return model.forClient();
  11. }));

New:

  1. 'use strict';
  2. const myDocs = module.context.collection('myCollection');
  3. // ...
  4. const docs = myDocs.byExample({color: 'green'});
  5. res.json(docs);

Schema validation

The main purpose of models in ArangoDB 2.x was to validate incoming data using joi schemas. In more recent versions of ArangoDB 2.x it was already possible to pass these schemas directly in most places where a model was expected as an argument. The only difference is that schemas should now be considered the default.

If you previously relied on the automatic validation of Foxx model instances when setting attributes or instantiating models from untrusted data, you can simply use the schema’s validate method directly.

Old:

  1. 'use strict';
  2. const joi = require('joi');
  3. const mySchema = {
  4. name: joi.string().required(),
  5. size: joi.number().required()
  6. };
  7. const Foxx = require('org/arangodb/foxx');
  8. const MyModel = Foxx.Model.extend({schema: mySchema});
  9. // ...
  10. const model = new MyModel(req.json());
  11. if (!model.isValid) {
  12. res.status(400);
  13. res.write('Bad request');
  14. return;
  15. }

New:

  1. 'use strict';
  2. const joi = require('joi');
  3. // Note this is now wrapped in a joi.object()
  4. const mySchema = joi.object({
  5. name: joi.string().required(),
  6. size: joi.number().required()
  7. }).required();
  8. // ...
  9. const result = mySchema.validate(req.body);
  10. if (result.errors) {
  11. res.status(400);
  12. res.write('Bad request');
  13. return;
  14. }

Migrating models

While most use cases for models can now be replaced with plain joi schemas, there is still the concept of a “model” in Foxx in ArangoDB 3.0 although it is quite different from Foxx models in ArangoDB 2.x.

A model in Foxx now refers to a plain JavaScript object with an optional schema attribute and the optional methods forClient and fromClient. Models can be used instead of plain joi schemas to define request and response bodies but there are no model “instances” in ArangoDB 3.0.

Old:

  1. 'use strict';
  2. const _ = require('underscore');
  3. const joi = require('joi');
  4. const Foxx = require('org/arangodb/foxx');
  5. const MyModel = Foxx.Model.extend({
  6. schema: {
  7. name: joi.string().required(),
  8. size: joi.number().required()
  9. },
  10. forClient () {
  11. return _.omit(this.attributes, ['_key', '_id', '_rev']);
  12. }
  13. });
  14. // ...
  15. ctrl.get(/* ... */)
  16. .bodyParam('body', {type: MyModel});

New:

  1. 'use strict';
  2. const _ = require('lodash');
  3. const joi = require('joi');
  4. const MyModel = {
  5. schema: joi.object({
  6. name: joi.string().required(),
  7. size: joi.number().required()
  8. }).required(),
  9. forClient (data) {
  10. return _.omit(data, ['_key', '_id', '_rev']);
  11. }
  12. };
  13. // ...
  14. router.get(/* ... */)
  15. .body(MyModel);

Triggers

When saving, updating, replacing or deleting models in ArangoDB 2.x using the repository methods the repository and model would fire events that could be subscribed to in order to perform side-effects.

Note that even in 2.x these events would not fire when using queries or manipulating documents in any other way than using the specific repository methods that operated on individual documents.

This behavior is no longer available in ArangoDB 3.0 but can be emulated by using an EventEmitter directly if it is not possible to solve the problem differently:

Old:

  1. 'use strict';
  2. const Foxx = require('org/arangodb/foxx');
  3. const MyModel = Foxx.Model.extend({
  4. // ...
  5. }, {
  6. afterRemove () {
  7. console.log(this.get('name'), 'was removed');
  8. }
  9. });
  10. // ...
  11. const model = myRepo.firstExample({name: 'myName'});
  12. myRepo.remove(model);
  13. // -> "myName was removed successfully"

New:

  1. 'use strict';
  2. const EventEmitter = require('events');
  3. const emitter = new EventEmitter();
  4. emitter.on('afterRemove', function (doc) {
  5. console.log(doc.name, 'was removed');
  6. });
  7. // ...
  8. const doc = myDocs.firstExample({name: 'myName'});
  9. myDocs.remove(doc);
  10. emitter.emit('afterRemove', doc);
  11. // -> "myName was removed successfully"

Or simply:

  1. 'use strict';
  2. function afterRemove(doc) {
  3. console.log(doc.name, 'was removed');
  4. }
  5. // ...
  6. const doc = myDocs.firstExample({name: 'myName'});
  7. myDocs.remove(doc);
  8. afterRemove(doc);
  9. // -> "myName was removed successfully"