1
0
Fork 0

Updated docs for classes.

This commit is contained in:
Alan Plum 2015-08-11 13:26:05 +02:00
parent 5e4630d31d
commit c62573dc5c
3 changed files with 43 additions and 36 deletions

View File

@ -137,6 +137,8 @@ v2.7.0 (XXXX-XX-XX)
* added optional Foxx dependencies
* converted Foxx constructors to classes
* updated chai to 3.0.

View File

@ -8,8 +8,9 @@ your model file, export the model as **model**.
```js
var Foxx = require("org/arangodb/foxx");
var TodoModel = Foxx.Model.extend({
});
class TodoModel extends Foxx.Model {
// ...
}
exports.model = TodoModel;
```
@ -26,13 +27,14 @@ For more information on the syntax see [the official joi documentation](https://
var Foxx = require("org/arangodb/foxx");
var joi = require("joi");
var PersonModel = Foxx.Model.extend({
schema: {
class PersonModel extends Foxx.Model {
// ...
}
PersonModel.prototype.schema = {
name: joi.string().required(),
age: joi.number().integer(),
active: joi.boolean().default(true)
}
});
};
exports.model = PersonModel;
```
@ -40,12 +42,13 @@ exports.model = PersonModel;
You can also use `joi.object` schemas directly:
```js
var PersonModel = Foxx.Model.extend({
schema: joi.object().keys({
class PersonModel extends Foxx.Model {
// ...
}
PersonModel.prototype.schema = joi.object().keys({
name: joi.string().required(),
age: joi.number().integer(),
active: joi.boolean().default(true)
})
});
```

View File

@ -5,8 +5,9 @@ A repository is a gateway to the database. It gets data from the database, updat
```javascript
var Foxx = require("org/arangodb/foxx");
var TodosRepository = Foxx.Repository.extend({
});
class TodosRepository extends Foxx.Repository {
// ...
}
exports.repository = TodosRepository;
```
@ -27,8 +28,7 @@ Model lifecycle:
```js
var person = new PersonModel();
person.on('beforeCreate', function() {
var model = this;
model.fancyMethod(); // Do something fancy with the model
this.fancyMethod(); // Do something fancy with the model
});
var people = new Repository(appContext.collection("people"), { model: PersonModel });
@ -70,11 +70,12 @@ Making a simple query in the repository and using it from the controller:
// in the repository
var Foxx = require("org/arangodb/foxx");
var TodosRepository = Foxx.Repository.extend({
getPendingItems: Foxx.createQuery(
'FOR todo IN my_todos FILTER todo.completed == false RETURN todo'
)
class TodosRepository extends Foxx.Repository {
// ...
});
TodosRepository.prototype.getPendingItems = Foxx.createQuery(
'FOR todo IN my_todos FILTER todo.completed == false RETURN todo'
);
// in the controller
ctrl.get("/", function(req, res) {
@ -86,10 +87,10 @@ It is also possible to supply parameters to the query:
```js
// in the repository
getPendingItemById: Foxx.createQuery({
TodosRepository.prototype.getPendingItemById = Foxx.createQuery({
query: 'FOR todo IN my_todos FILTER todo.completed == false FILTER todo._key == @id RETURN todo',
params: ['id']
})
});
// in the controller
ctrl.get("/:id", function(req, res) {
@ -103,7 +104,7 @@ The list of results can also be transformed before returning it from the reposit
```js
// in the repository
getPendingItemById: Foxx.createQuery({
TodosRepository.prototype.getPendingItemById = Foxx.createQuery({
query: 'FOR todo IN my_todos FILTER todo.completed == false FILTER todo._key == @id RETURN todo',
params: ['id'],
transform: function(results, extra) {
@ -111,7 +112,7 @@ getPendingItemById: Foxx.createQuery({
results[i].extraProperty = extra;
}
}
})
});
// in the controller
ctrl.get("/:id", function(req, res) {
@ -152,15 +153,16 @@ The syntax for defining indexes is the same used in [*collection.ensureIndex*](.
```js
var Foxx = require('org/arangodb/foxx');
var FulltextRepository = Foxx.Repository.extend({
indexes: [
class FulltextRepository extends Foxx.Repository {
// ...
}
FulltextRepository.prototype.indexes = [
{
type: 'fulltext',
fields: ['text'],
minLength: 3
}
]
});
];
```
!SECTION Methods of a Repository