/
FoxxRepository.mdpp
177 lines (116 loc) 路 4.31 KB
/
FoxxRepository.mdpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
!CHAPTER Details on FoxxRepository
A repository is a gateway to the database. It gets data from the database, updates it or saves new data. It uses the given model when it returns a model and expects instances of the model for methods like save. In your repository file, export the repository as *repository*.
```javascript
var Foxx = require("org/arangodb/foxx");
var TodosRepository = Foxx.Repository.extend({
});
exports.repository = TodosRepository;
```
!SUBSECTION Initialize
@startDocuBlock JSF_foxx_repository_initializer
!SECTION Defining custom queries
You can define custom query methods using Foxx.createQuery and Foxx.Repository.extend.
For more details see the chapter on [Foxx Queries](../Foxx/FoxxQueries.md).
*Examples*
Making a simple query in the repository and using it from the controller:
```js
// 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'
)
});
// in the controller
ctrl.get("/", function(req, res) {
req.json(todosRepository.getPendingItems());
});
```
It is also possible to supply parameters to the query:
```js
// in the repository
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) {
var id = req.params("id");
var rv = todosRepository.getPendingItemById(id);
res.json(rv);
});
```
The list of results can also be transformed before returning it from the repository:
```js
// in the repository
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) {
for (var i = 0; i < results.length; i++) {
results[i].extraProperty = extra;
}
}
})
// in the controller
ctrl.get("/:id", function(req, res) {
var id = req.params("id");
var extra = req.params("extra");
var rv = todosRepository.getPendingItemById(id, extra);
res.json(rv);
});
```
!SECTION Attributes of a Repository
!SUBSECTION Collection
@startDocuBlock JSF_foxx_repository_collection
!SUBSECTION Model
@startDocuBlock JSF_foxx_repository_model
!SUBSECTION Model schema
@startDocuBlock JSF_foxx_repository_modelSchema
!SUBSECTION Prefix
@startDocuBlock JSF_foxx_repository_prefix
!SECTION Defining indexes
Repository can take care of ensuring the existence of collection indexes for you.
If you define indexes for a repository, instances of the repository will have
access to additional index-specific methods like *range* or *fulltext* (see below).
The syntax for defining indexes is the same used in [*collection.ensureIndex*](../IndexHandling/README.md).
@EXAMPLES
```js
var Foxx = require('org/arangodb/foxx');
var FulltextRepository = Foxx.Repository.extend({
indexes: [
{
type: 'fulltext',
fields: ['text'],
minLength: 3
}
]
});
```
!SECTION Methods of a Repository
!SUBSECTION Adding entries to the repository
@startDocuBlock JSF_foxx_repository_save
!SUBSECTION Finding entries in the repository
@startDocuBlock JSF_foxx_repository_byId
@startDocuBlock JSF_foxx_repository_byExample
@startDocuBlock JSF_foxx_repository_firstExample
@startDocuBlock JSF_foxx_repository_all
@startDocuBlock JSF_foxx_repository_any
!SUBSECTION Removing entries from the repository
@startDocuBlock JSF_foxx_repository_remove
@startDocuBlock JSF_foxx_repository_removeById
@startDocuBlock JSF_foxx_repository_removeByExample
!SUBSECTION Replacing entries in the repository
@startDocuBlock JSF_foxx_repository_replace
@startDocuBlock JSF_foxx_repository_replaceById
@startDocuBlock JSF_foxx_repository_replaceByExample
!SUBSECTION Updating entries in the repository
@startDocuBlock JSF_foxx_repository_updateById
@startDocuBlock JSF_foxx_repository_updateByExample
!SUBSECTION Counting entries in the repository
@startDocuBlock JSF_foxx_repository_count
!SUBSECTION Index-specific repository methods
@startDocuBlock JSF_foxx_repository_range
@startDocuBlock JSF_foxx_repository_near
@startDocuBlock JSF_foxx_repository_within
@startDocuBlock JSF_foxx_repository_fulltext