Skip to content

Commit

Permalink
Add peekRecord and peekAll, caching records in the store
Browse files Browse the repository at this point in the history
  • Loading branch information
ghidoz committed Sep 4, 2016
1 parent 307e088 commit 43de815
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ You will get an array of objects where every mapped relationship will be automat
]
```

Use `peekAll()` to retrieve all of the records for a given type that are already loaded into the store, without making a network request:

```typescript
let posts = this.datastore.peekAll(Post);
```


#### Retrieving a Single Record

Use `findRecord()` to retrieve a record by its type and ID:
Expand All @@ -219,6 +226,12 @@ this.datastore.findRecord(Post, '1', {
);
```

Use `peekRecord()` to retrieve a record by its type and ID, without making a network request. This will return the record only if it is already present in the store:

```typescript
let post = this.datastore.peekRecord(Post, '1');
```

### Creating, Updating and Deleting

#### Creating Records
Expand Down
28 changes: 28 additions & 0 deletions src/services/json-api-datastore.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type ModelType = { new(datastore: JsonApiDatastore, data: any): JsonApiMo
export class JsonApiDatastore {

private _headers: Headers;
private _store: any = {};

constructor(private http: Http) { }

Expand Down Expand Up @@ -54,6 +55,16 @@ export class JsonApiDatastore {
.catch((res: any) => this.handleError(res));
}

peekRecord(modelType: ModelType, id: string) {
let type = Reflect.getMetadata('JsonApiModelConfig', modelType).type;
return this._store[type][id];
}

peekAll(modelType: ModelType) {
let type = Reflect.getMetadata('JsonApiModelConfig', modelType).type;
return _.values(this._store[type]);
}

set headers(headers: Headers){
this._headers = headers;
}
Expand Down Expand Up @@ -95,6 +106,7 @@ export class JsonApiDatastore {
}
models.push(model);
});
this.addToStore(models);
return models;
}

Expand All @@ -104,6 +116,7 @@ export class JsonApiDatastore {
if (body.included) {
model.syncRelationships(body.data, body.included);
}
this.addToStore(model);
return model;
}

Expand Down Expand Up @@ -155,4 +168,19 @@ export class JsonApiDatastore {
return encodedStr;
}

private addToStore(models: JsonApiModel | JsonApiModel[]) {
let model = models instanceof Array ? models[0] : models;
let type = Reflect.getMetadata('JsonApiModelConfig', model.constructor).type;
if (!this._store[type]) {
this._store[type] = {};
}
let hash = this.fromArrayToHash(models);
_.extend(this._store[type], hash);
}

private fromArrayToHash(models: JsonApiModel | JsonApiModel[]) {
let modelsArray: JsonApiModel[] = models instanceof Array ? models : [models];
return _.indexBy(modelsArray, 'id');
}

}

0 comments on commit 43de815

Please sign in to comment.