mirror of https://gitee.com/bigwinds/arangodb
Updated docs for classes.
This commit is contained in:
parent
5e4630d31d
commit
c62573dc5c
|
@ -137,6 +137,8 @@ v2.7.0 (XXXX-XX-XX)
|
||||||
|
|
||||||
* added optional Foxx dependencies
|
* added optional Foxx dependencies
|
||||||
|
|
||||||
|
* converted Foxx constructors to classes
|
||||||
|
|
||||||
* updated chai to 3.0.
|
* updated chai to 3.0.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,8 +8,9 @@ your model file, export the model as **model**.
|
||||||
```js
|
```js
|
||||||
var Foxx = require("org/arangodb/foxx");
|
var Foxx = require("org/arangodb/foxx");
|
||||||
|
|
||||||
var TodoModel = Foxx.Model.extend({
|
class TodoModel extends Foxx.Model {
|
||||||
});
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
exports.model = TodoModel;
|
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 Foxx = require("org/arangodb/foxx");
|
||||||
var joi = require("joi");
|
var joi = require("joi");
|
||||||
|
|
||||||
var PersonModel = Foxx.Model.extend({
|
class PersonModel extends Foxx.Model {
|
||||||
schema: {
|
// ...
|
||||||
|
}
|
||||||
|
PersonModel.prototype.schema = {
|
||||||
name: joi.string().required(),
|
name: joi.string().required(),
|
||||||
age: joi.number().integer(),
|
age: joi.number().integer(),
|
||||||
active: joi.boolean().default(true)
|
active: joi.boolean().default(true)
|
||||||
}
|
};
|
||||||
});
|
|
||||||
|
|
||||||
exports.model = PersonModel;
|
exports.model = PersonModel;
|
||||||
```
|
```
|
||||||
|
@ -40,12 +42,13 @@ exports.model = PersonModel;
|
||||||
You can also use `joi.object` schemas directly:
|
You can also use `joi.object` schemas directly:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var PersonModel = Foxx.Model.extend({
|
class PersonModel extends Foxx.Model {
|
||||||
schema: joi.object().keys({
|
// ...
|
||||||
|
}
|
||||||
|
PersonModel.prototype.schema = joi.object().keys({
|
||||||
name: joi.string().required(),
|
name: joi.string().required(),
|
||||||
age: joi.number().integer(),
|
age: joi.number().integer(),
|
||||||
active: joi.boolean().default(true)
|
active: joi.boolean().default(true)
|
||||||
})
|
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,9 @@ A repository is a gateway to the database. It gets data from the database, updat
|
||||||
```javascript
|
```javascript
|
||||||
var Foxx = require("org/arangodb/foxx");
|
var Foxx = require("org/arangodb/foxx");
|
||||||
|
|
||||||
var TodosRepository = Foxx.Repository.extend({
|
class TodosRepository extends Foxx.Repository {
|
||||||
});
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
exports.repository = TodosRepository;
|
exports.repository = TodosRepository;
|
||||||
```
|
```
|
||||||
|
@ -27,8 +28,7 @@ Model lifecycle:
|
||||||
```js
|
```js
|
||||||
var person = new PersonModel();
|
var person = new PersonModel();
|
||||||
person.on('beforeCreate', function() {
|
person.on('beforeCreate', function() {
|
||||||
var model = this;
|
this.fancyMethod(); // Do something fancy with the model
|
||||||
model.fancyMethod(); // Do something fancy with the model
|
|
||||||
});
|
});
|
||||||
var people = new Repository(appContext.collection("people"), { model: PersonModel });
|
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
|
// in the repository
|
||||||
var Foxx = require("org/arangodb/foxx");
|
var Foxx = require("org/arangodb/foxx");
|
||||||
|
|
||||||
var TodosRepository = Foxx.Repository.extend({
|
class TodosRepository extends Foxx.Repository {
|
||||||
getPendingItems: Foxx.createQuery(
|
// ...
|
||||||
'FOR todo IN my_todos FILTER todo.completed == false RETURN todo'
|
|
||||||
)
|
|
||||||
});
|
});
|
||||||
|
TodosRepository.prototype.getPendingItems = Foxx.createQuery(
|
||||||
|
'FOR todo IN my_todos FILTER todo.completed == false RETURN todo'
|
||||||
|
);
|
||||||
|
|
||||||
// in the controller
|
// in the controller
|
||||||
ctrl.get("/", function(req, res) {
|
ctrl.get("/", function(req, res) {
|
||||||
|
@ -86,10 +87,10 @@ It is also possible to supply parameters to the query:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// in the repository
|
// 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',
|
query: 'FOR todo IN my_todos FILTER todo.completed == false FILTER todo._key == @id RETURN todo',
|
||||||
params: ['id']
|
params: ['id']
|
||||||
})
|
});
|
||||||
|
|
||||||
// in the controller
|
// in the controller
|
||||||
ctrl.get("/:id", function(req, res) {
|
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
|
```js
|
||||||
// in the repository
|
// 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',
|
query: 'FOR todo IN my_todos FILTER todo.completed == false FILTER todo._key == @id RETURN todo',
|
||||||
params: ['id'],
|
params: ['id'],
|
||||||
transform: function(results, extra) {
|
transform: function(results, extra) {
|
||||||
|
@ -111,7 +112,7 @@ getPendingItemById: Foxx.createQuery({
|
||||||
results[i].extraProperty = extra;
|
results[i].extraProperty = extra;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
// in the controller
|
// in the controller
|
||||||
ctrl.get("/:id", function(req, res) {
|
ctrl.get("/:id", function(req, res) {
|
||||||
|
@ -152,15 +153,16 @@ The syntax for defining indexes is the same used in [*collection.ensureIndex*](.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var Foxx = require('org/arangodb/foxx');
|
var Foxx = require('org/arangodb/foxx');
|
||||||
var FulltextRepository = Foxx.Repository.extend({
|
class FulltextRepository extends Foxx.Repository {
|
||||||
indexes: [
|
// ...
|
||||||
|
}
|
||||||
|
FulltextRepository.prototype.indexes = [
|
||||||
{
|
{
|
||||||
type: 'fulltext',
|
type: 'fulltext',
|
||||||
fields: ['text'],
|
fields: ['text'],
|
||||||
minLength: 3
|
minLength: 3
|
||||||
}
|
}
|
||||||
]
|
];
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
!SECTION Methods of a Repository
|
!SECTION Methods of a Repository
|
||||||
|
|
Loading…
Reference in New Issue