Skip to content

Commit

Permalink
add findAll by type to store
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Principe committed Sep 12, 2015
1 parent 7fcc043 commit 37fd751
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
components/
node_modules/
12 changes: 12 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ Retrieve a model by type and id. Constant-time lookup.

* **object** The corresponding model if present, and null otherwise.

## findAll(type)

Retrieve all models by type.

### Params:

* **string** *type* The type of the model.

### Return:

* **object** Array of the corresponding model if present, and empty array otherwise.

## reset()

Empty the store.
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ Just call the `.find(type, id)` method of your store.
```javascript
var article = store.find('article', 123);
```
or call the `.findAll(type)` method of your store to get all the models of that type.
```javascript
var articles = store.findAll('article');
```
All the attributes *and* relationships are accessible through the model as object properties.
```javascript
console.log(article.author.name);
Expand Down
13 changes: 13 additions & 0 deletions dist/jsonapi-datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ JsonApiDataStore.prototype.find = function(type, id) {
return this.graph[type][id];
};

/**
* Retrieve all models by type.
* @method findAll
* @param {string} type The type of the model.
* @return {object} Array of the corresponding model if present, and empty array otherwise.
*/
JsonApiDataStore.prototype.findAll = function(type) {
var self = this;

if (!this.graph[type]) return [];
return Object.keys(self.graph[type]).map(function(v) { return self.graph[type][v]; });
};

/**
* Empty the store.
* @method reset
Expand Down
2 changes: 1 addition & 1 deletion dist/jsonapi-datastore.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions dist/ng-jsonapi-datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@
return this.graph[type][id];
};

/**
* Retrieve all models by type.
* @method findAll
* @param {string} type The type of the model.
* @return {object} Array of the corresponding model if present, and empty array otherwise.
*/
JsonApiDataStore.prototype.findAll = function(type) {
var self = this;

if (!this.graph[type]) return [];
return Object.keys(self.graph[type]).map(function(v) {
return self.graph[type][v];
});
};

/**
* Empty the store.
* @method reset
Expand Down
2 changes: 1 addition & 1 deletion dist/ng-jsonapi-datastore.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions dist/node-jsonapi-datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@
return this.graph[type][id];
};

/**
* Retrieve all models by type.
* @method findAll
* @param {string} type The type of the model.
* @return {object} Array of the corresponding model if present, and empty array otherwise.
*/
JsonApiDataStore.prototype.findAll = function(type) {
var self = this;

if (!this.graph[type]) return [];
return Object.keys(self.graph[type]).map(function(v) {
return self.graph[type][v];
});
};

/**
* Empty the store.
* @method reset
Expand Down
2 changes: 1 addition & 1 deletion dist/node-jsonapi-datastore.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/jsonapi-datastore/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ JsonApiDataStore.prototype.find = function(type, id) {
return this.graph[type][id];
};

/**
* Retrieve all models by type.
* @method findAll
* @param {string} type The type of the model.
* @return {object} Array of the corresponding model if present, and empty array otherwise.
*/
JsonApiDataStore.prototype.findAll = function(type) {
var self = this;

if (!this.graph[type]) return [];
return Object.keys(self.graph[type]).map(function(v) { return self.graph[type][v]; });
};

/**
* Empty the store.
* @method reset
Expand Down
50 changes: 46 additions & 4 deletions test/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,16 @@ describe('JsonApiDataStore', function() {
describe('.find()', function() {
var store = new JsonApiDataStore(),
payload = {
data: {
type: 'article',
id: 1337
}
data: [
{
type: 'article',
id: 1337
},
{
type: 'article',
id: 1338
}
]
};

it('should find an existing model', function() {
Expand All @@ -250,6 +256,42 @@ describe('JsonApiDataStore', function() {
var article = store.find('article', 9999);
expect(article).to.eq(null);
});

it('should not find a non-existing model type', function() {
store.sync(payload);
var article = store.find('bad', 1337);
expect(article).to.eq(null);
});
});

describe('.findAll()', function() {
var store = new JsonApiDataStore(),
payload = {
data: [
{
type: 'article',
id: 1337
},
{
type: 'article',
id: 1338
}
]
};

it('should find all existing models', function() {
store.sync(payload);
var articles = store.findAll('article');
expect(articles.length).to.eq(2);
expect(articles[0].id).to.eq(1337);
expect(articles[1].id).to.eq(1338);
});

it('should not find a non-existing model', function() {
store.sync(payload);
var articles = store.findAll('bad');
expect(articles.length).to.eq(0);
});
});

describe('.destroy()', function() {
Expand Down

0 comments on commit 37fd751

Please sign in to comment.