mirror of https://gitee.com/bigwinds/arangodb
97 lines
2.9 KiB
Plaintext
97 lines
2.9 KiB
Plaintext
!CHAPTER Details on FoxxModel
|
|
|
|
The model doesn't know anything about the database. It is just a representation
|
|
of the data as an JavaScript object. You can add and overwrite the methods of
|
|
the prototype in your model prototype via the object you give to extend. In
|
|
your model file, export the model as *model*.
|
|
|
|
```js
|
|
var Foxx = require("org/arangodb/foxx");
|
|
|
|
var TodoModel = Foxx.Model.extend({
|
|
});
|
|
|
|
exports.model = TodoModel;
|
|
```
|
|
|
|
A Foxx Model can be initialized with an object of attributes and their values.
|
|
|
|
There's also the possibility of annotation: If you extend the model with a
|
|
*schema* property, the model's attributes will be validated against it.
|
|
|
|
You can define attributes in the schema using the bundled *joi* library.
|
|
For more information on the syntax see [the official joi documentation](https://github.com/spumko/joi).
|
|
|
|
```js
|
|
var Foxx = require("org/arangodb/foxx");
|
|
var joi = require("joi");
|
|
|
|
var PersonModel = Foxx.Model.extend({
|
|
schema: {
|
|
name: joi.string().required(),
|
|
age: joi.number().integer(),
|
|
active: joi.boolean().default(true)
|
|
}
|
|
});
|
|
|
|
exports.model = TodoModel;
|
|
```
|
|
|
|
This has two effects: On the one hand it provides documentation. If you annotated
|
|
your model, you can use it in the *bodyParam* method for documentation.
|
|
On the other hand it will influence the behavior of the constructor: If you provide
|
|
an object to the constructor, it will validate its attributes and set the special
|
|
*errors* property. This is especially useful if you want to to initialize
|
|
the Model from user input. On the other hand it will set the default value for all
|
|
attributes that have not been set by hand. An example:
|
|
|
|
```js
|
|
var person = new PersonModel({
|
|
name: "Pete",
|
|
admin: true
|
|
});
|
|
|
|
person.attributes // => { name: "Pete", admin: true, active: true }
|
|
person.errors // => {admin: [ValidationError: value is not allowed]}
|
|
```
|
|
|
|
!SUBSECTION Extend
|
|
<!-- js/server/modules/org/arangodb/foxx/model.js -->
|
|
@startDocuBlock JSF_foxx_model_extend
|
|
|
|
!SUBSECTION Initialize
|
|
<!-- js/server/modules/org/arangodb/foxx/model.js -->
|
|
@startDocuBlock JSF_foxx_model_initializer
|
|
|
|
!SUBSECTION Get
|
|
<!-- js/server/modules/org/arangodb/foxx/model.js -->
|
|
@startDocuBlock JSF_foxx_model_get
|
|
|
|
!SUBSECTION Set
|
|
<!-- js/server/modules/org/arangodb/foxx/model.js -->
|
|
@startDocuBlock JSF_foxx_model_set
|
|
|
|
!SUBSECTION Has
|
|
<!-- js/server/modules/org/arangodb/foxx/model.js -->
|
|
@startDocuBlock JSF_foxx_model_has
|
|
|
|
!SUBSECTION isValid
|
|
<!-- js/server/modules/org/arangodb/foxx/model.js -->
|
|
@startDocuBlock JSF_foxx_model_isvalid
|
|
|
|
!SUBSECTION Errors
|
|
<!-- js/server/modules/org/arangodb/foxx/model.js -->
|
|
@startDocuBlock JSF_foxx_model_errors
|
|
|
|
!SUBSECTION Attributes
|
|
<!-- js/server/modules/org/arangodb/foxx/model.js -->
|
|
@startDocuBlock JSF_foxx_model_attributes
|
|
|
|
!SUBSECTION forDB
|
|
<!-- js/server/modules/org/arangodb/foxx/model.js -->
|
|
@startDocuBlock JSF_foxx_model_forDB
|
|
|
|
!SUBSECTION forClient
|
|
<!-- js/server/modules/org/arangodb/foxx/model.js -->
|
|
@startDocuBlock JSF_foxx_model_forClient
|