@@ -99,29 +99,19 @@ export default (params?: LocalStorageDataProviderParams): DataProvider => {
9999 // update methods need to persist changes in localStorage
100100 update : < RecordType extends RaRecord = any > ( resource , params ) => {
101101 checkResource ( resource ) ;
102+ const resourceData = getResourceCollection ( data , resource ) ;
102103 try {
103- assertRecordsExist ( getResourceCollection ( data , resource ) , [
104- params . id ,
105- ] ) ;
104+ assertRecordsExist ( resourceData , [ params . id ] ) ;
106105 } catch ( error ) {
107106 return Promise . reject ( error ) ;
108107 }
109108 return baseDataProvider
110109 . update < RecordType > ( resource , params )
111110 . then ( response => {
112111 updateLocalStorage ( ( ) => {
113- const resourceData = getResourceCollection (
114- data ,
115- resource
116- ) ;
117112 const index = resourceData . findIndex (
118113 record => record . id == params . id
119114 ) ;
120-
121- if ( index === - 1 ) {
122- return ;
123- }
124-
125115 resourceData . splice ( index , 1 , {
126116 ...resourceData [ index ] ,
127117 ...params . data ,
@@ -133,11 +123,9 @@ export default (params?: LocalStorageDataProviderParams): DataProvider => {
133123 } ,
134124 updateMany : ( resource , params ) => {
135125 checkResource ( resource ) ;
126+ const resourceData = getResourceCollection ( data , resource ) ;
136127 try {
137- assertRecordsExist (
138- getResourceCollection ( data , resource ) ,
139- params . ids
140- ) ;
128+ assertRecordsExist ( resourceData , params . ids ) ;
141129 } catch ( error ) {
142130 return Promise . reject ( error ) ;
143131 }
@@ -146,19 +134,10 @@ export default (params?: LocalStorageDataProviderParams): DataProvider => {
146134 . updateMany ( resource , params )
147135 . then ( response => {
148136 updateLocalStorage ( ( ) => {
149- const resourceData = getResourceCollection (
150- data ,
151- resource
152- ) ;
153137 params . ids . forEach ( id => {
154138 const index = resourceData . findIndex (
155139 record => record . id == id
156140 ) ;
157-
158- if ( index === - 1 ) {
159- return ;
160- }
161-
162141 resourceData . splice ( index , 1 , {
163142 ...resourceData [ index ] ,
164143 ...params . data ,
@@ -190,29 +169,19 @@ export default (params?: LocalStorageDataProviderParams): DataProvider => {
190169 } ,
191170 delete : < RecordType extends RaRecord = any > ( resource , params ) => {
192171 checkResource ( resource ) ;
172+ const resourceData = getResourceCollection ( data , resource ) ;
193173 try {
194- assertRecordsExist ( getResourceCollection ( data , resource ) , [
195- params . id ,
196- ] ) ;
174+ assertRecordsExist ( resourceData , [ params . id ] ) ;
197175 } catch ( error ) {
198176 return Promise . reject ( error ) ;
199177 }
200178 return baseDataProvider
201179 . delete < RecordType > ( resource , params )
202180 . then ( response => {
203181 updateLocalStorage ( ( ) => {
204- const resourceData = getResourceCollection (
205- data ,
206- resource
207- ) ;
208182 const index = resourceData . findIndex (
209183 record => record . id == params . id
210184 ) ;
211-
212- if ( index === - 1 ) {
213- return ;
214- }
215-
216185 pullAt ( resourceData , [ index ] ) ;
217186 } ) ;
218187
@@ -221,11 +190,9 @@ export default (params?: LocalStorageDataProviderParams): DataProvider => {
221190 } ,
222191 deleteMany : ( resource , params ) => {
223192 checkResource ( resource ) ;
193+ const resourceData = getResourceCollection ( data , resource ) ;
224194 try {
225- assertRecordsExist (
226- getResourceCollection ( data , resource ) ,
227- params . ids
228- ) ;
195+ assertRecordsExist ( resourceData , params . ids ) ;
229196 } catch ( error ) {
230197 return Promise . reject ( error ) ;
231198 }
@@ -234,18 +201,9 @@ export default (params?: LocalStorageDataProviderParams): DataProvider => {
234201 . deleteMany ( resource , params )
235202 . then ( response => {
236203 updateLocalStorage ( ( ) => {
237- const resourceData = getResourceCollection (
238- data ,
239- resource
204+ const indexes = params . ids . map ( id =>
205+ resourceData . findIndex ( record => record . id == id )
240206 ) ;
241- const indexes = params . ids
242- . map ( id =>
243- resourceData . findIndex (
244- record => record . id == id
245- )
246- )
247- . filter ( index => index !== - 1 ) ;
248-
249207 pullAt ( resourceData , indexes ) ;
250208 } ) ;
251209
@@ -277,7 +235,7 @@ const getOrCreateResourceCollection = (
277235const checkResource = ( resource : string ) => {
278236 // Reject "__proto__" so dynamic writes like data[resource] = value don't
279237 // mutate Object.prototype instead of creating a normal resource collection.
280- if ( resource === '__proto__' ) {
238+ if ( [ '__proto__' , 'constructor' , 'prototype' ] . includes ( resource ) ) {
281239 throw new Error ( `Invalid resource key: ${ resource } ` ) ;
282240 }
283241} ;
0 commit comments