11import { notifyPropertyChange } from '@ember/object' ;
2-
32import { Listener } from '@orbit/core' ;
43import { deepGet } from '@orbit/utils' ;
54import Orbit , {
65 buildQuery ,
76 RecordIdentity ,
87 QueryOrExpression ,
98 RecordOperation ,
10- Record
9+ Record ,
10+ KeyMap ,
11+ Schema ,
12+ TransformBuilder
1113} from '@orbit/data' ;
1214import { QueryResultData } from '@orbit/record-cache' ;
1315import { MemoryCache } from '@orbit/memory' ;
1416import IdentityMap from '@orbit/identity-map' ;
15-
1617import LiveQuery from './live-query' ;
1718import Model from './model' ;
1819import ModelFactory from './model-factory' ;
1920import recordIdentitySerializer from './utils/record-identity-serializer' ;
2021
21- const { deprecate } = Orbit ;
22+ const { assert , deprecate } = Orbit ;
2223
2324export 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 ,
0 commit comments