diff --git a/src/__test__/storeBackend.test.js b/src/__test__/storeBackend.test.js index d8b9e73..925676a 100644 --- a/src/__test__/storeBackend.test.js +++ b/src/__test__/storeBackend.test.js @@ -31,14 +31,14 @@ jest.spyOn(global.Date, 'now').mockImplementation(() => { const backends = [ ['memory', MemoryBackend()], - //['NeDb', NeDBBackend({ filename: null, inMemoryOnly: true })], + ['NeDb', NeDBBackend({ filename: null, inMemoryOnly: true })], ]; if (MONGODB_DATABASE_TEST) { - /*backends.push([ + backends.push([ 'MongoDB', MongoDBBackend({ uri: MONGODB_URI, database: MONGODB_DATABASE_TEST }), - ]);*/ + ]); } describe.each(backends)( @@ -47,7 +47,12 @@ describe.each(backends)( let backend; beforeAll(async () => { - backend = wrapBackend(rawBackend, 'siteid', 'userid'); + backend = wrapBackend( + rawBackend, + 'siteid', + { clearAll: jest.fn() }, + 'userid' + ); if (backendName === 'MongoDB') { const { MongoClient, ServerApiVersion } = await import('mongodb'); diff --git a/src/fileStore/backends/index.js b/src/fileStore/backends/index.js index d9032d1..d8f2fbe 100644 --- a/src/fileStore/backends/index.js +++ b/src/fileStore/backends/index.js @@ -34,5 +34,12 @@ export const wrapBackend = (backend, siteId) => { async delete(boxId, resourceId, filename) { return await backend.delete(siteId, boxId, resourceId, filename); }, + async clearAll(boxId, id) { + return await Promise.all( + (await this.list(boxId, id)).map((filename) => { + return this.delete(boxId, id, filename); + }) + ); + }, }; }; diff --git a/src/middleware.js b/src/middleware.js index aab5902..de0e558 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -69,14 +69,15 @@ export const ricochetMiddleware = ({ remote({ context: (req) => { const { siteId, authenticatedUser } = req; - const wrappedBackend = wrapBackend( - storeBackend, + const wrappedFileBackend = wrapFileBackend( + fileStoreBackend, siteId, authenticatedUser ); - const wrappedFileBackend = wrapFileBackend( - fileStoreBackend, + const wrappedBackend = wrapBackend( + storeBackend, siteId, + wrappedFileBackend, authenticatedUser ); if (!functionsBySite[siteId]) { @@ -201,14 +202,15 @@ export const ricochetMiddleware = ({ execute({ context: (req) => { const { siteId, authenticatedUser } = req; - const wrappedBackend = wrapBackend( - storeBackend, + const wrappedFileBackend = wrapFileBackend( + fileStoreBackend, siteId, authenticatedUser ); - const wrappedFileBackend = wrapFileBackend( - fileStoreBackend, + const wrappedBackend = wrapBackend( + storeBackend, siteId, + wrappedFileBackend, authenticatedUser ); return { store: wrappedBackend, fileStore: wrappedFileBackend }; diff --git a/src/store/backends/utils.js b/src/store/backends/utils.js index faa1ae7..e700848 100644 --- a/src/store/backends/utils.js +++ b/src/store/backends/utils.js @@ -1,6 +1,6 @@ export const DEFAULT_BOX_OPTIONS = { security: 'private', personal: false }; -export const wrapBackend = (backend, siteId) => { +export const wrapBackend = (backend, siteId, fileBackend) => { const getBoxId = (userBoxId) => { return `_${siteId}__${userBoxId}`; }; @@ -81,7 +81,12 @@ export const wrapBackend = (backend, siteId) => { return await backend.update(getBoxId(boxId), id, data); }, async delete(boxId, id) { - return await backend.delete(getBoxId(boxId), id); + const result = await backend.delete(getBoxId(boxId), id); + if (result === 1) { + // Also delete the files related to this store + fileBackend.clearAll(boxId, id); + } + return result; }, }; }; diff --git a/src/store/index.js b/src/store/index.js index 84ef0bc..d218246 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -81,12 +81,17 @@ export const store = ({ const { boxId } = req.params; const { siteId, authenticatedUser } = req; - const wrappedBackend = wrapBackend(backend, siteId, authenticatedUser); const wrappedFileBackend = wrapFileBackend( fileBackend, siteId, authenticatedUser ); + const wrappedBackend = wrapBackend( + backend, + siteId, + wrappedFileBackend, + authenticatedUser + ); const { query, allow = false } = await applyHooks('before', req, { store: wrappedBackend, @@ -151,12 +156,17 @@ export const store = ({ const { siteId, authenticatedUser } = req; - const wrappedBackend = wrapBackend(backend, siteId, authenticatedUser); const wrappedFileBackend = wrapFileBackend( fileBackend, siteId, authenticatedUser ); + const wrappedBackend = wrapBackend( + backend, + siteId, + wrappedFileBackend, + authenticatedUser + ); if (boxId[0] === '_') { throwError( @@ -200,12 +210,17 @@ export const store = ({ authenticatedUser, } = req; - const wrappedBackend = wrapBackend(backend, siteId, authenticatedUser); - const wrappedFileBackend = wrapBackend( + const wrappedFileBackend = wrapFileBackend( fileBackend, siteId, authenticatedUser ); + const wrappedBackend = wrapBackend( + backend, + siteId, + wrappedFileBackend, + authenticatedUser + ); if (boxId[0] === '_') { throwError( @@ -243,12 +258,17 @@ export const store = ({ const { siteId, authenticatedUser } = req; - const wrappedBackend = wrapBackend(backend, siteId, authenticatedUser); const wrappedFileBackend = wrapFileBackend( fileBackend, siteId, authenticatedUser ); + const wrappedBackend = wrapBackend( + backend, + siteId, + wrappedFileBackend, + authenticatedUser + ); if (boxId[0] === '_') { throwError( @@ -286,12 +306,17 @@ export const store = ({ const { siteId, authenticatedUser } = req; - const wrappedBackend = wrapBackend(backend, siteId, authenticatedUser); const wrappedFileBackend = wrapFileBackend( fileBackend, siteId, authenticatedUser ); + const wrappedBackend = wrapBackend( + backend, + siteId, + wrappedFileBackend, + authenticatedUser + ); if (boxId[0] === '_') { throwError( @@ -314,15 +339,6 @@ export const store = ({ const result = await wrappedBackend.delete(boxId, resourceId); - if (result === 1) { - // Also delete the files - await Promise.all( - (await wrappedFileBackend.list(boxId, resourceId)).map((filename) => { - return wrappedFileBackend.delete(boxId, resourceId, filename); - }) - ); - } - await applyHooks('after', req, { store: wrappedBackend, fileStore: wrappedFileBackend, @@ -344,12 +360,17 @@ export const store = ({ const { siteId, authenticatedUser } = req; - const wrappedBackend = wrapBackend(backend, siteId, authenticatedUser); const wrappedFileBackend = wrapFileBackend( fileBackend, siteId, authenticatedUser ); + const wrappedBackend = wrapBackend( + backend, + siteId, + wrappedFileBackend, + authenticatedUser + ); // Just check the boxId exists await wrappedBackend.get(boxId, id); @@ -377,12 +398,18 @@ export const store = ({ fileStore(fileBackend, { prefix }), errorGuard(async (req, _, next) => { const { siteId, authenticatedUser } = req; - const wrappedBackend = wrapBackend(backend, siteId, authenticatedUser); + const wrappedFileBackend = wrapFileBackend( fileBackend, siteId, authenticatedUser ); + const wrappedBackend = wrapBackend( + backend, + siteId, + wrappedFileBackend, + authenticatedUser + ); await applyHooks('afterFile', req, { store: wrappedBackend,