@@ -10,7 +10,7 @@ The first step is to define your models. These will match the [resource objects]
10
10
11
11
Let's say we have a ` /api/v1/people ` endpoint:
12
12
13
- ``` es6
13
+ ``` js
14
14
// es6 import syntax
15
15
// vanilla JS would expose 'jsorm' as a global
16
16
import { Model , attr } from ' jsorm' ;
@@ -32,20 +32,20 @@ Alternatively, in Typescript:
32
32
``` ts
33
33
// typescript
34
34
class Person extends Model {
35
- static baseUrl: ' http://localhost:3000' ;
36
- static apiNamespace: ' /api/v1' ;
37
- static jsonapiType: ' people' ;
35
+ static baseUrl = ' http://localhost:3000' ;
36
+ static apiNamespace = ' /api/v1' ;
37
+ static jsonapiType = ' people' ;
38
38
39
- firstName: attr ();
40
- lastName: attr ();
39
+ firstName = attr ();
40
+ lastName = attr ();
41
41
}
42
42
```
43
43
44
44
** NB** : * Once your models are defined, you must call ` Config.setup() ` :
45
45
46
46
``` js
47
47
import { Config } from ' jsorm' ;
48
- Config .setup ();
48
+ Config .setup ();
49
49
```
50
50
51
51
## Basic Usage
@@ -61,7 +61,7 @@ Call `all()`, `first()`, or `find()` to actually fire the query.
61
61
62
62
All of the following examples can be chained together:
63
63
64
- ``` es6
64
+ ``` js
65
65
let scope = new Person ();
66
66
if (should_include_admins) {
67
67
scope = scope .where ({ admin: true });
@@ -81,7 +81,7 @@ scope.page(2).all().then((people) => {
81
81
82
82
Use ` per ` and ` page ` . To limit 10 per page, viewing the second page:
83
83
84
- ``` es6
84
+ ``` js
85
85
Person .per (10 ).page (2 ).all ();
86
86
```
87
87
@@ -95,15 +95,15 @@ Use `order`. Passing an attribute will default to ascending order.
95
95
96
96
Ascending:
97
97
98
- ``` es6
98
+ ``` js
99
99
Person .order (' name' );
100
100
```
101
101
102
102
> GET /people?sort=name
103
103
104
104
Descending:
105
105
106
- ``` es6
106
+ ``` js
107
107
Person .order ({ name: ' desc' });
108
108
```
109
109
@@ -115,23 +115,23 @@ Person.order({ name: 'desc' });
115
115
116
116
Use ` where ` :
117
117
118
- ``` es6
118
+ ``` js
119
119
Person .where ({ name: ' Joe' }).where ({ age: 30 }).all ();
120
120
```
121
121
122
122
> GET /people?filter[ name] =Joe&filter[ age] =30
123
123
124
124
Filters are based on swagger documentation, not object attributes. This means you can do stuff like:
125
125
126
- ``` es6
126
+ ``` js
127
127
Person .where ({ age_greater_than: 30 }).all ();
128
128
```
129
129
130
130
> GET /people?filter[ age_greater_than] =30
131
131
132
132
Arrays are supported automatically, defaulting to an OR clause:
133
133
134
- ``` es6
134
+ ``` js
135
135
Person .where ({ name: [' Joe' , ' Bill' ] }).all ();
136
136
```
137
137
@@ -143,7 +143,7 @@ Person.where({ name: ['Joe', 'Bill'] }).all();
143
143
144
144
Use ` select ` :
145
145
146
- ``` es6
146
+ ``` js
147
147
Person .select ({ people: [' name' , ' age' ] }).all ();
148
148
```
149
149
@@ -155,7 +155,7 @@ This functionality is enabled by [jsonapi_suite](https://jsonapi-suite.github.io
155
155
156
156
Use ` selectExtra ` :
157
157
158
- ``` es6
158
+ ``` js
159
159
Person .selectExtra ({ people: [' name' , ' age' ] }).all ();
160
160
```
161
161
@@ -167,7 +167,7 @@ Person.selectExtra({ people: ['name', 'age'] }).all();
167
167
168
168
Use ` includes ` . This can be a symbol, array, hash, or combination of all. In short - it works exactly like it works in ActiveRecord:
169
169
170
- ``` es6
170
+ ``` js
171
171
// a person has many tags, and has many pets
172
172
// a pet has many toys, and many tags
173
173
Person .includes ([' tags' , { pets: [' toys' , ' tags' ] }]);
@@ -177,7 +177,7 @@ Person.includes(['tags', { pets: ['toys', 'tags'] }]);
177
177
178
178
The included resources will now be present:
179
179
180
- ``` es6
180
+ ``` js
181
181
Person .includes (' tags' ).then ((person ) => {
182
182
person .tags .map ((t ) => { return t .name ; }); // #=> ['funny', 'smart']
183
183
});
@@ -189,13 +189,13 @@ Person.includes('tags').then((person) => {
189
189
190
190
` all ` , ` first ` , and ` find ` can be used in conjunction with scopes.
191
191
192
- ``` es6
192
+ ``` js
193
193
Person .all ();
194
194
```
195
195
196
196
> GET /people
197
197
198
- ``` es6
198
+ ``` js
199
199
scope = Person .where ({ name: ' Bill' }) # no query
200
200
scope .all (); # => fires query, returns a Promise that resolves to an array of Person objects
201
201
```
@@ -204,7 +204,7 @@ scope.all(); # => fires query, returns a Promise that resolves to an array of Pe
204
204
205
205
Use ` first ` to grab the first result:
206
206
207
- ``` es6
207
+ ``` js
208
208
// Limits per_page to 1, result is first element in the array
209
209
Person .where ({ name: ' Bill' }).first ().then ((person ) => {
210
210
// ...
@@ -219,7 +219,7 @@ Finally, use `find` to find a record by ID. This will hit the `show` action.
219
219
220
220
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 ) ):
221
221
222
- ``` es6
222
+ ``` js
223
223
import { Config } from ' jsorm' ;
224
224
let winston = require (' winston' );
225
225
0 commit comments