@@ -10,7 +10,7 @@ The first step is to define your models. These will match the [resource objects]
1010
1111Let's say we have a ` /api/v1/people ` endpoint:
1212
13- ``` es6
13+ ``` js
1414// es6 import syntax
1515// vanilla JS would expose 'jsorm' as a global
1616import { Model , attr } from ' jsorm' ;
@@ -32,20 +32,20 @@ Alternatively, in Typescript:
3232``` ts
3333// typescript
3434class 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' ;
3838
39- firstName: attr ();
40- lastName: attr ();
39+ firstName = attr ();
40+ lastName = attr ();
4141}
4242```
4343
4444** NB** : * Once your models are defined, you must call ` Config.setup() ` :
4545
4646``` js
4747import { Config } from ' jsorm' ;
48- Config .setup ();
48+ Config .setup ();
4949```
5050
5151## Basic Usage
@@ -61,7 +61,7 @@ Call `all()`, `first()`, or `find()` to actually fire the query.
6161
6262All of the following examples can be chained together:
6363
64- ``` es6
64+ ``` js
6565let scope = new Person ();
6666if (should_include_admins) {
6767 scope = scope .where ({ admin: true });
@@ -81,7 +81,7 @@ scope.page(2).all().then((people) => {
8181
8282Use ` per ` and ` page ` . To limit 10 per page, viewing the second page:
8383
84- ``` es6
84+ ``` js
8585Person .per (10 ).page (2 ).all ();
8686```
8787
@@ -95,15 +95,15 @@ Use `order`. Passing an attribute will default to ascending order.
9595
9696Ascending:
9797
98- ``` es6
98+ ``` js
9999Person .order (' name' );
100100```
101101
102102> GET /people?sort=name
103103
104104Descending:
105105
106- ``` es6
106+ ``` js
107107Person .order ({ name: ' desc' });
108108```
109109
@@ -115,23 +115,23 @@ Person.order({ name: 'desc' });
115115
116116Use ` where ` :
117117
118- ``` es6
118+ ``` js
119119Person .where ({ name: ' Joe' }).where ({ age: 30 }).all ();
120120```
121121
122122> GET /people?filter[ name] =Joe&filter[ age] =30
123123
124124Filters are based on swagger documentation, not object attributes. This means you can do stuff like:
125125
126- ``` es6
126+ ``` js
127127Person .where ({ age_greater_than: 30 }).all ();
128128```
129129
130130> GET /people?filter[ age_greater_than] =30
131131
132132Arrays are supported automatically, defaulting to an OR clause:
133133
134- ``` es6
134+ ``` js
135135Person .where ({ name: [' Joe' , ' Bill' ] }).all ();
136136```
137137
@@ -143,7 +143,7 @@ Person.where({ name: ['Joe', 'Bill'] }).all();
143143
144144Use ` select ` :
145145
146- ``` es6
146+ ``` js
147147Person .select ({ people: [' name' , ' age' ] }).all ();
148148```
149149
@@ -155,7 +155,7 @@ This functionality is enabled by [jsonapi_suite](https://jsonapi-suite.github.io
155155
156156Use ` selectExtra ` :
157157
158- ``` es6
158+ ``` js
159159Person .selectExtra ({ people: [' name' , ' age' ] }).all ();
160160```
161161
@@ -167,7 +167,7 @@ Person.selectExtra({ people: ['name', 'age'] }).all();
167167
168168Use ` includes ` . This can be a symbol, array, hash, or combination of all. In short - it works exactly like it works in ActiveRecord:
169169
170- ``` es6
170+ ``` js
171171// a person has many tags, and has many pets
172172// a pet has many toys, and many tags
173173Person .includes ([' tags' , { pets: [' toys' , ' tags' ] }]);
@@ -177,7 +177,7 @@ Person.includes(['tags', { pets: ['toys', 'tags'] }]);
177177
178178The included resources will now be present:
179179
180- ``` es6
180+ ``` js
181181Person .includes (' tags' ).then ((person ) => {
182182 person .tags .map ((t ) => { return t .name ; }); // #=> ['funny', 'smart']
183183});
@@ -189,13 +189,13 @@ Person.includes('tags').then((person) => {
189189
190190` all ` , ` first ` , and ` find ` can be used in conjunction with scopes.
191191
192- ``` es6
192+ ``` js
193193Person .all ();
194194```
195195
196196> GET /people
197197
198- ``` es6
198+ ``` js
199199scope = Person .where ({ name: ' Bill' }) # no query
200200scope .all (); # => fires query, returns a Promise that resolves to an array of Person objects
201201```
@@ -204,7 +204,7 @@ scope.all(); # => fires query, returns a Promise that resolves to an array of Pe
204204
205205Use ` first ` to grab the first result:
206206
207- ``` es6
207+ ``` js
208208// Limits per_page to 1, result is first element in the array
209209Person .where ({ name: ' Bill' }).first ().then ((person ) => {
210210 // ...
@@ -219,7 +219,7 @@ Finally, use `find` to find a record by ID. This will hit the `show` action.
219219
220220By 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 ) ):
221221
222- ``` es6
222+ ``` js
223223import { Config } from ' jsorm' ;
224224let winston = require (' winston' );
225225
0 commit comments