Skip to content

Commit 8837244

Browse files
authored
Merge pull request #197 from orbitjs/update-store
Modernize and standardize Store + Cache interfaces
2 parents 7b7ad8a + 8c56008 commit 8837244

File tree

5 files changed

+516
-121
lines changed

5 files changed

+516
-121
lines changed

addon/-private/cache.ts

Lines changed: 112 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
import { notifyPropertyChange } from '@ember/object';
2-
32
import { Listener } from '@orbit/core';
43
import { deepGet } from '@orbit/utils';
54
import Orbit, {
65
buildQuery,
76
RecordIdentity,
87
QueryOrExpression,
98
RecordOperation,
10-
Record
9+
Record,
10+
KeyMap,
11+
Schema,
12+
TransformBuilder
1113
} from '@orbit/data';
1214
import { QueryResultData } from '@orbit/record-cache';
1315
import { MemoryCache } from '@orbit/memory';
1416
import IdentityMap from '@orbit/identity-map';
15-
1617
import LiveQuery from './live-query';
1718
import Model from './model';
1819
import ModelFactory from './model-factory';
1920
import recordIdentitySerializer from './utils/record-identity-serializer';
2021

21-
const { deprecate } = Orbit;
22+
const { assert, deprecate } = Orbit;
2223

2324
export interface CacheSettings {
2425
sourceCache: MemoryCache;
@@ -50,34 +51,127 @@ export default class Cache {
5051
this._sourceCache.on('reset', this._resetListener);
5152
}
5253

54+
get keyMap(): KeyMap | undefined {
55+
return this._sourceCache.keyMap;
56+
}
57+
58+
get schema(): Schema {
59+
return this._sourceCache.schema;
60+
}
61+
62+
get transformBuilder(): TransformBuilder {
63+
return this._sourceCache.transformBuilder;
64+
}
65+
66+
/**
67+
* @deprecated
68+
*/
5369
retrieveRecordData(type: string, id: string): Record | undefined {
70+
deprecate(
71+
'`Cache#retrieveRecordData(type, id)` is deprecated, use `Cache#peekRecordData(type, id)`.'
72+
);
73+
return this.peekRecordData(type, id);
74+
}
75+
76+
peekRecordData(type: string, id: string): Record | undefined {
5477
return this._sourceCache.getRecordSync({ type, id });
5578
}
5679

5780
includesRecord(type: string, id: string): boolean {
58-
return !!this.retrieveRecordData(type, id);
81+
return !!this.peekRecordData(type, id);
5982
}
6083

84+
/**
85+
* @deprecated
86+
*/
6187
retrieveRecord(type: string, id: string): Model | undefined {
88+
deprecate(
89+
'`Cache#retrieveRecord(type, id)` is deprecated, use `Cache#peekRecord(type, id)`.'
90+
);
91+
return this.peekRecord(type, id);
92+
}
93+
94+
peekRecord(type: string, id: string): Model | undefined {
6295
if (this.includesRecord(type, id)) {
6396
return this.lookup({ type, id }) as Model;
6497
}
6598
return undefined;
6699
}
67100

101+
peekRecords(type: string): Model[] {
102+
const identities = this._sourceCache.getRecordsSync(type);
103+
return this.lookup(identities) as Model[];
104+
}
105+
106+
peekRecordByKey(
107+
type: string,
108+
keyName: string,
109+
keyValue: string
110+
): Model | undefined {
111+
return this.peekRecord(type, this.recordIdFromKey(type, keyName, keyValue));
112+
}
113+
114+
recordIdFromKey(type: string, keyName: string, keyValue: string): string {
115+
let keyMap = this.keyMap as KeyMap;
116+
assert(
117+
'No `keyMap` has been assigned to the Cache, so `recordIdFromKey` can not work.',
118+
!!keyMap
119+
);
120+
let id = keyMap.keyToId(type, keyName, keyValue);
121+
if (!id) {
122+
id = this.schema.generateId(type);
123+
keyMap.pushRecord({ type, id, keys: { [keyName]: keyValue } });
124+
}
125+
return id;
126+
}
127+
128+
/**
129+
* @deprecated
130+
*/
68131
retrieveKey(identity: RecordIdentity, key: string): string | undefined {
132+
deprecate(
133+
'`Cache#retrieveKey(identity, key)` is deprecated, use `Cache#peekKey(identity, key)`.'
134+
);
135+
return this.peekKey(identity, key);
136+
}
137+
138+
peekKey(identity: RecordIdentity, key: string): string | undefined {
69139
const record = this._sourceCache.getRecordSync(identity);
70140
return record && deepGet(record, ['keys', key]);
71141
}
72142

143+
/**
144+
* @deprecated
145+
*/
73146
retrieveAttribute(identity: RecordIdentity, attribute: string): any {
147+
deprecate(
148+
'`Cache#retrieveAttribute(identity, attribute)` is deprecated, use `Cache#peekAttribute(identity, key)`.'
149+
);
74150
const record = this._sourceCache.getRecordSync(identity);
75151
return record && deepGet(record, ['attributes', attribute]);
76152
}
77153

154+
peekAttribute(identity: RecordIdentity, attribute: string): any {
155+
const record = this._sourceCache.getRecordSync(identity);
156+
return record && deepGet(record, ['attributes', attribute]);
157+
}
158+
159+
/**
160+
* @deprecated
161+
*/
78162
retrieveRelatedRecord(
79163
identity: RecordIdentity,
80164
relationship: string
165+
): Model | null | undefined {
166+
deprecate(
167+
'`Cache#retrieveRelatedRecord(identity, relationship)` is deprecated, use `Cache#peekRelatedRecord(identity, relationship)`.'
168+
);
169+
return this.peekRelatedRecord(identity, relationship);
170+
}
171+
172+
peekRelatedRecord(
173+
identity: RecordIdentity,
174+
relationship: string
81175
): Model | null | undefined {
82176
const relatedRecord = this._sourceCache.getRelatedRecordSync(
83177
identity,
@@ -90,9 +184,22 @@ export default class Cache {
90184
}
91185
}
92186

187+
/**
188+
* @deprecated
189+
*/
93190
retrieveRelatedRecords(
94191
identity: RecordIdentity,
95192
relationship: string
193+
): Model[] | undefined {
194+
deprecate(
195+
'`Cache#retrieveRelatedRecords(identity, relationship)` is deprecated, use `Cache#peekRelatedRecords(identity, relationship)`.'
196+
);
197+
return this.peekRelatedRecords(identity, relationship);
198+
}
199+
200+
peekRelatedRecords(
201+
identity: RecordIdentity,
202+
relationship: string
96203
): Model[] | undefined {
97204
const relatedRecords = this._sourceCache.getRelatedRecordsSync(
98205
identity,

addon/-private/model.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ export default class Model extends EmberObject {
4343
}
4444

4545
getData(): Record | undefined {
46-
return this.store.cache.retrieveRecordData(this.type, this.id);
46+
return this.store.cache.peekRecordData(this.type, this.id);
4747
}
4848

4949
getKey(field: string): string | undefined {
50-
return this.store.cache.retrieveKey(this.identity, field);
50+
return this.store.cache.peekKey(this.identity, field);
5151
}
5252

5353
async replaceKey(
@@ -62,7 +62,7 @@ export default class Model extends EmberObject {
6262
}
6363

6464
getAttribute(field: string): any {
65-
return this.store.cache.retrieveAttribute(this.identity, field);
65+
return this.store.cache.peekAttribute(this.identity, field);
6666
}
6767

6868
async replaceAttribute(
@@ -77,7 +77,7 @@ export default class Model extends EmberObject {
7777
}
7878

7979
getRelatedRecord(relationship: string): Record | null | undefined {
80-
return this.store.cache.retrieveRelatedRecord(this.identity, relationship);
80+
return this.store.cache.peekRelatedRecord(this.identity, relationship);
8181
}
8282

8383
async replaceRelatedRecord(
@@ -102,7 +102,7 @@ export default class Model extends EmberObject {
102102
if (!this._relatedRecords[relationship]) {
103103
this._relatedRecords[relationship] = HasMany.create({
104104
getContent: () =>
105-
this.store.cache.retrieveRelatedRecords(this.identity, relationship),
105+
this.store.cache.peekRelatedRecords(this.identity, relationship),
106106
addToContent: (record: Model): Promise<void> => {
107107
return this.addToRelatedRecords(relationship, record);
108108
},

0 commit comments

Comments
 (0)