@@ -13,56 +13,58 @@ Let's say we have a `/api/v1/people` endpoint:
13
13
``` js
14
14
// es6 import syntax
15
15
// vanilla JS would expose 'jsorm' as a global
16
- import ' isomorphic-fetch' ;
17
- import { Model , attr } from ' jsorm' ;
16
+ import " isomorphic-fetch"
17
+ import { Model , attr } from " jsorm"
18
18
19
19
var Person = Model .extend ({
20
20
static: {
21
- baseUrl: ' http://localhost:3000' , // set to '' in browser for relative URLs
22
- apiNamespace: ' /api/v1' ,
23
- jsonapiType: ' people'
21
+ baseUrl: " http://localhost:3000" , // set to '' in browser for relative URLs
22
+ apiNamespace: " /api/v1" ,
23
+ jsonapiType: " people"
24
24
},
25
25
26
26
firstName: attr (),
27
27
lastName: attr ()
28
- });
28
+ })
29
29
```
30
30
31
31
Alternatively, in Typescript:
32
32
33
33
``` ts
34
34
// typescript
35
35
class Person extends Model {
36
- static baseUrl = ' http://localhost:3000' ;
37
- static apiNamespace = ' /api/v1' ;
38
- static jsonapiType = ' people' ;
36
+ static baseUrl = " http://localhost:3000"
37
+ static apiNamespace = " /api/v1"
38
+ static jsonapiType = " people"
39
39
40
- firstName = attr ();
41
- lastName = attr ();
40
+ firstName = attr ()
41
+ lastName = attr ()
42
42
}
43
43
```
44
44
45
- ** NOTE** : * Once your models are defined, you must call ` Config.setup() ` :
45
+ ** NOTE** : \ * Once your models are defined, you must call ` Config.setup() ` :
46
46
47
47
``` js
48
- import { Config } from ' jsorm' ;
49
- Config .setup ();
48
+ import { Config } from " jsorm"
49
+ Config .setup ()
50
50
```
51
51
52
52
### ES6/Typescript Classes
53
53
54
54
ES6 and TypeScript classes do not have an ` inherited ` hook. Because this hook provides critical functionality, you have three options:
55
55
56
56
* Edit your ` .tsconfig ` :
57
+
57
58
* Set ` target ` to ` es5 ` .
58
59
* Add ` noEmitHelpers: "true" `
59
-
60
+
60
61
* Call the inherited hook manually after each class definition:
62
+
61
63
``` ts
62
64
class Author extends Person { ... }
63
65
Person .inherited (Author );
64
66
```
65
-
67
+
66
68
* Use the ` let Person = Model.extend({ ... }) ` pattern shown above instead of native classes.
67
69
68
70
## Basic Usage
@@ -79,17 +81,24 @@ Call `all()`, `first()`, or `find()` to actually fire the query.
79
81
All of the following examples can be chained together:
80
82
81
83
``` js
82
- let scope = new Person ();
84
+ let scope = new Person ()
83
85
if (should_include_admins) {
84
- scope = scope .where ({ admin: true });
86
+ scope = scope .where ({ admin: true })
85
87
}
86
- scope .all ().then ((people ) => {
87
- people .data .map ((p ) => { return p .firstName ; }); // => ['Joe', 'Jane', 'Bill']
88
- });
89
-
90
- scope .page (2 ).all ().then ((people ) => {
91
- people .data .map ((p ) => { return p .firstName ; }); // => ['Chris', 'Sarah', 'Ben']
92
- });
88
+ scope .all ().then (people => {
89
+ people .data .map (p => {
90
+ return p .firstName
91
+ }) // => ['Joe', 'Jane', 'Bill']
92
+ })
93
+
94
+ scope
95
+ .page (2 )
96
+ .all ()
97
+ .then (people => {
98
+ people .data .map (p => {
99
+ return p .firstName
100
+ }) // => ['Chris', 'Sarah', 'Ben']
101
+ })
93
102
```
94
103
95
104
### Pagination
@@ -99,7 +108,9 @@ scope.page(2).all().then((people) => {
99
108
Use ` per ` and ` page ` . To limit 10 per page, viewing the second page:
100
109
101
110
``` js
102
- Person .per (10 ).page (2 ).all ();
111
+ Person .per (10 )
112
+ .page (2 )
113
+ .all ()
103
114
```
104
115
105
116
> GET /people?page[ number] =2&page[ size] =10
@@ -113,15 +124,15 @@ Use `order`. Passing an attribute will default to ascending order.
113
124
Ascending:
114
125
115
126
``` js
116
- Person .order (' name' );
127
+ Person .order (" name" )
117
128
```
118
129
119
130
> GET /people?sort=name
120
131
121
132
Descending:
122
133
123
134
``` js
124
- Person .order ({ name: ' desc' });
135
+ Person .order ({ name: " desc" })
125
136
```
126
137
127
138
> GET /people?sort=-name
@@ -133,23 +144,25 @@ Person.order({ name: 'desc' });
133
144
Use ` where ` :
134
145
135
146
``` js
136
- Person .where ({ name: ' Joe' }).where ({ age: 30 }).all ();
147
+ Person .where ({ name: " Joe" })
148
+ .where ({ age: 30 })
149
+ .all ()
137
150
```
138
151
139
152
> GET /people?filter[ name] =Joe&filter[ age] =30
140
153
141
154
Filters are based on swagger documentation, not object attributes. This means you can do stuff like:
142
155
143
156
``` js
144
- Person .where ({ age_greater_than: 30 }).all ();
157
+ Person .where ({ age_greater_than: 30 }).all ()
145
158
```
146
159
147
160
> GET /people?filter[ age_greater_than] =30
148
161
149
162
Arrays are supported automatically, defaulting to an OR clause:
150
163
151
164
``` js
152
- Person .where ({ name: [' Joe' , ' Bill' ] }).all ();
165
+ Person .where ({ name: [" Joe" , " Bill" ] }).all ()
153
166
```
154
167
155
168
> GET /people?&filter[ name] [ ] =Joe&filter[ name] [ ] =Bill
@@ -161,7 +174,7 @@ Person.where({ name: ['Joe', 'Bill'] }).all();
161
174
Use ` select ` :
162
175
163
176
``` js
164
- Person .select ({ people: [' name' , ' age' ] }).all ();
177
+ Person .select ({ people: [" name" , " age" ] }).all ()
165
178
```
166
179
167
180
> GET /people?fields[ people] =name,age
@@ -173,7 +186,7 @@ This functionality is enabled by [jsonapi_suite](https://jsonapi-suite.github.io
173
186
Use ` selectExtra ` :
174
187
175
188
``` js
176
- Person .selectExtra ({ people: [' name' , ' age' ] }).all ();
189
+ Person .selectExtra ({ people: [" name" , " age" ] }).all ()
177
190
```
178
191
179
192
> GET /people?extra_fields[ people] =name,age
@@ -187,17 +200,21 @@ Use `includes`. This can be a symbol, array, hash, or combination of all. In sho
187
200
``` js
188
201
// a person has many tags, and has many pets
189
202
// a pet has many toys, and many tags
190
- Person .includes ([' tags' , { pets: [' toys' , ' tags' ] }]);
203
+ Person .includes ([" tags" , { pets: [" toys" , " tags" ] }])
191
204
```
192
205
193
206
> GET /people?include=tags,pets.toys,pets.tags
194
207
195
208
The included resources will now be present:
196
209
197
210
``` js
198
- Person .includes (' tags' ).all ().then ((person ) => {
199
- person .data .tags .map ((t ) => { return t .name ; }); // #=> ['funny', 'smart']
200
- });
211
+ Person .includes (" tags" )
212
+ .all ()
213
+ .then (person => {
214
+ person .data .tags .map (t => {
215
+ return t .name
216
+ }) // #=> ['funny', 'smart']
217
+ })
201
218
```
202
219
203
220
> GET /people?include=tags
@@ -207,7 +224,7 @@ Person.includes('tags').all().then((person) => {
207
224
` all ` , ` first ` , and ` find ` can be used in conjunction with scopes.
208
225
209
226
``` js
210
- Person .all ();
227
+ Person .all ()
211
228
```
212
229
213
230
> GET /people
@@ -223,9 +240,11 @@ Use `first` to grab the first result:
223
240
224
241
``` js
225
242
// Limits per_page to 1, result is first element in the array
226
- Person .where ({ name: ' Bill' }).first ().then ((person ) => {
227
- // ...
228
- });
243
+ Person .where ({ name: " Bill" })
244
+ .first ()
245
+ .then (person => {
246
+ // ...
247
+ })
229
248
```
230
249
231
250
> GET /people?page[ size] =1&page[ number] =1&filter[ name] =Bill
@@ -237,11 +256,11 @@ Finally, use `find` to find a record by ID. This will hit the `show` action.
237
256
By default we will use ` console ` to log to STDOUT (or the browser's console log). If you are using node and want more in-depth options, inject another logger (we suggest [ winston] ( https://github.com/winstonjs/winston ) ):
238
257
239
258
``` js
240
- import { Config } from ' jsorm' ;
241
- let winston = require (' winston' );
259
+ import { Config } from " jsorm"
260
+ let winston = require (" winston" )
242
261
243
- winston .level = ' warn' ;
244
- Config .logger = winston;
262
+ winston .level = " warn"
263
+ Config .logger = winston
245
264
```
246
265
247
266
This will log colorized request/responses when the log_level is debug, and only the request when the log level is info.
0 commit comments