Skip to content

Commit 0fe1ca7

Browse files
committed
Add RecordProxy class
Similar to CollectionProxy, this acts as a container for a record retrieved by `Model.find(id)` and future similar methods.
1 parent d9066d3 commit 0fe1ca7

File tree

6 files changed

+108
-11
lines changed

6 files changed

+108
-11
lines changed

src/model.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Config from './configuration';
55
import Attribute from './attribute';
66
import deserialize from './util/deserialize';
77
import CollectionProxy from './collection-proxy';
8+
import RecordProxy from './record-proxy';
89
import _extend from './util/extend';
910
import { camelize } from './util/string';
1011

@@ -46,7 +47,7 @@ export default class Model {
4647
return this.scope().all();
4748
}
4849

49-
static find(id : string | number) : Promise<Model> {
50+
static find(id : string | number) : Promise<RecordProxy<Model>> {
5051
return this.scope().find(id);
5152
}
5253

src/record-proxy.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Model from './model';
2+
3+
class RecordProxy<T> {
4+
private _raw_json : japiDoc;
5+
private _model : T;
6+
7+
constructor (raw_json : japiDoc = { data: [] }) {
8+
this.setRaw(raw_json);
9+
}
10+
11+
get raw () : japiDoc {
12+
return this._raw_json;
13+
}
14+
15+
get data () : T {
16+
return this._model;
17+
}
18+
19+
get meta () : Object {
20+
return this.raw.meta || {};
21+
}
22+
23+
private setRaw = (json_payload : japiDoc) => {
24+
this._raw_json = json_payload;
25+
26+
27+
if (this.raw.data) {
28+
this._model = Model.fromJsonapi(this.raw.data, this.raw);
29+
}
30+
}
31+
}
32+
33+
export default RecordProxy;

src/scope.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Config from './configuration';
33
import parameterize from './util/parameterize';
44
import IncludeDirective from './util/include-directive';
55
import CollectionProxy from './collection-proxy';
6+
import RecordProxy from './record-proxy';
67
import Request from './request';
78
import colorize from './util/colorize';
89

@@ -28,9 +29,9 @@ export default class Scope {
2829
});
2930
}
3031

31-
find(id : string | number) : Promise<Model> {
32+
find(id : string | number) : Promise<RecordProxy<Model>> {
3233
return this._fetch(this.model.url(id)).then((json : japiDoc) => {
33-
return Model.fromJsonapi(json.data, json);
34+
return new RecordProxy(json)
3435
});
3536
}
3637

test/integration/finders-test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ after(function () {
88
});
99

1010
let resultData = function(promise) {
11-
return promise.then(function(collection) {
12-
return collection.data
11+
return promise.then(function(proxyObject) {
12+
return proxyObject.data
1313
})
1414
}
1515

@@ -28,13 +28,13 @@ describe('Model finders', function() {
2828
});
2929

3030
it('returns a promise that resolves the correct instance', function() {
31-
return expect(Person.find(1)).to.eventually
31+
return expect(resultData(Person.find(1))).to.eventually
3232
.be.instanceof(Person).and
3333
.have.property('id', '1');
3434
});
3535

3636
it('assigns attributes correctly', function() {
37-
return expect(Person.find(1)).to.eventually
37+
return expect(resultData(Person.find(1))).to.eventually
3838
.have.property('name', 'John')
3939
});
4040

@@ -50,7 +50,7 @@ describe('Model finders', function() {
5050
});
5151

5252
it('resolves to the correct class', function() {
53-
return expect(Person.find(1)).to.eventually
53+
return expect(resultData(Person.find(1))).to.eventually
5454
.be.instanceof(Author);
5555
});
5656
});

test/unit/collection-proxy-test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { sinon } from '../../test/test-helper';
21
import { Person } from '../fixtures';
32

43
import CollectionProxy from '../../src/collection-proxy'
@@ -15,7 +14,6 @@ describe('CollectionProxy', function() {
1514
},
1615
}
1716
],
18-
included: [],
1917
meta: {
2018
stats: {
2119
total: {
@@ -43,7 +41,7 @@ describe('CollectionProxy', function() {
4341
describe('#meta', function() {
4442
it('should get meta field from raw response', function() {
4543
let collection = new CollectionProxy(personData)
46-
expect(collection.meta).to.eq(personData.meta)
44+
expect(collection.meta).to.deep.eq(personData.meta)
4745
})
4846

4947
describe('meta is null', function() {

test/unit/record-proxy-test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { Person } from '../fixtures';
2+
3+
import RecordProxy from '../../src/record-proxy'
4+
5+
describe('RecordProxy', function() {
6+
let personData = {
7+
data: {
8+
id: '1',
9+
type: 'people',
10+
attributes: {
11+
firstName: 'Donald',
12+
lastName: 'Budge'
13+
},
14+
},
15+
meta: {
16+
stats: {
17+
total: {
18+
count: 3
19+
},
20+
average: {
21+
salary: "$100k"
22+
}
23+
}
24+
}
25+
}
26+
27+
describe('initialization', function() {
28+
it('should assign the response correctly', function() {
29+
let record = new RecordProxy(personData)
30+
expect(record.raw).to.deep.equal(personData)
31+
})
32+
33+
it('should assign the correct models to the data array', function() {
34+
let record = new RecordProxy(personData)
35+
expect(record.data).to.be.instanceof(Person)
36+
})
37+
})
38+
39+
describe('#meta', function() {
40+
it('should get meta field from raw response', function() {
41+
let record = new RecordProxy(personData)
42+
expect(record.meta).to.deep.eq(personData.meta)
43+
})
44+
45+
describe('meta is null', function() {
46+
let personData = {
47+
data: {
48+
id: '1',
49+
type: 'people',
50+
attributes: {
51+
firstName: 'Donald',
52+
lastName: 'Budge'
53+
},
54+
},
55+
}
56+
57+
it('should return an empty object', function() {
58+
let record = new RecordProxy(personData)
59+
expect(record.meta).to.deep.eq({})
60+
})
61+
})
62+
})
63+
})
64+

0 commit comments

Comments
 (0)