@@ -175,16 +175,28 @@ export default (params?: LocalForageDataProviderParams): DataProvider => {
175175 throw new Error ( 'The dataProvider is not initialized.' ) ;
176176 }
177177
178+ assertRecordsExist ( getResourceCollection ( data , resource ) , [
179+ params . id ,
180+ ] ) ;
181+ const response = await baseDataProvider . update < RecordType > (
182+ resource ,
183+ params
184+ ) ;
178185 const resourceData = getResourceCollection ( data , resource ) ;
179186 const index = resourceData . findIndex (
180187 ( record : { id : any } ) => record . id === params . id
181188 ) ;
189+
190+ if ( index === - 1 ) {
191+ return response ;
192+ }
193+
182194 resourceData . splice ( index , 1 , {
183195 ...resourceData [ index ] ,
184196 ...params . data ,
185197 } ) ;
186198 updateLocalForage ( resource ) ;
187- return baseDataProvider . update < RecordType > ( resource , params ) ;
199+ return response ;
188200 } ,
189201 updateMany : async ( resource : string , params : UpdateManyParams < any > ) => {
190202 checkResource ( resource ) ;
@@ -197,18 +209,28 @@ export default (params?: LocalForageDataProviderParams): DataProvider => {
197209 }
198210
199211 const resourceData = getResourceCollection ( data , resource ) ;
212+ assertRecordsExist ( resourceData , params . ids ) ;
213+ const response = await baseDataProvider . updateMany (
214+ resource ,
215+ params
216+ ) ;
200217
201218 params . ids . forEach ( ( id : Identifier ) => {
202219 const index = resourceData . findIndex (
203220 ( record : { id : Identifier } ) => record . id === id
204221 ) ;
222+
223+ if ( index === - 1 ) {
224+ return ;
225+ }
226+
205227 resourceData . splice ( index , 1 , {
206228 ...resourceData [ index ] ,
207229 ...params . data ,
208230 } ) ;
209231 } ) ;
210232 updateLocalForage ( resource ) ;
211- return baseDataProvider . updateMany ( resource , params ) ;
233+ return response ;
212234 } ,
213235 create : async < RecordType extends Omit < RaRecord , 'id' > = any > (
214236 resource : string ,
@@ -247,13 +269,25 @@ export default (params?: LocalForageDataProviderParams): DataProvider => {
247269 if ( ! data ) {
248270 throw new Error ( 'The dataProvider is not initialized.' ) ;
249271 }
272+ assertRecordsExist ( getResourceCollection ( data , resource ) , [
273+ params . id ,
274+ ] ) ;
275+ const response = await baseDataProvider . delete < RecordType > (
276+ resource ,
277+ params
278+ ) ;
250279 const resourceData = getResourceCollection ( data , resource ) ;
251280 const index = resourceData . findIndex (
252281 ( record : { id : any } ) => record . id === params . id
253282 ) ;
283+
284+ if ( index === - 1 ) {
285+ return response ;
286+ }
287+
254288 pullAt ( resourceData , [ index ] ) ;
255289 updateLocalForage ( resource ) ;
256- return baseDataProvider . delete < RecordType > ( resource , params ) ;
290+ return response ;
257291 } ,
258292 deleteMany : async ( resource : string , params : DeleteManyParams < any > ) => {
259293 checkResource ( resource ) ;
@@ -265,14 +299,22 @@ export default (params?: LocalForageDataProviderParams): DataProvider => {
265299 throw new Error ( 'The dataProvider is not initialized.' ) ;
266300 }
267301 const resourceData = getResourceCollection ( data , resource ) ;
268- const indexes = params . ids . map ( ( id : any ) => {
269- return resourceData . findIndex (
270- ( record : any ) => record . id === id
271- ) ;
272- } ) ;
302+ assertRecordsExist ( resourceData , params . ids ) ;
303+ const response = await baseDataProvider . deleteMany (
304+ resource ,
305+ params
306+ ) ;
307+ const indexes = params . ids
308+ . map ( ( id : any ) => {
309+ return resourceData . findIndex (
310+ ( record : any ) => record . id === id
311+ ) ;
312+ } )
313+ . filter ( index => index !== - 1 ) ;
314+
273315 pullAt ( resourceData , indexes ) ;
274316 updateLocalForage ( resource ) ;
275- return baseDataProvider . deleteMany ( resource , params ) ;
317+ return response ;
276318 } ,
277319 } ;
278320} ;
@@ -313,6 +355,18 @@ const checkResource = resource => {
313355 }
314356} ;
315357
358+ const assertRecordsExist = ( resourceData , ids ) => {
359+ ids . forEach ( id => {
360+ if (
361+ resourceData . findIndex (
362+ ( record : { id : Identifier } ) => record . id === id
363+ ) === - 1
364+ ) {
365+ throw new Error ( `No item with identifier ${ id } ` ) ;
366+ }
367+ } ) ;
368+ } ;
369+
316370export interface LocalForageDataProviderParams {
317371 defaultData ?: any ;
318372 prefixLocalForageKey ?: string ;
0 commit comments