Skip to content

Commit

Permalink
Move file deletion to backend (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmi authored Mar 13, 2023
1 parent 70b549f commit aee1401
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 31 deletions.
13 changes: 9 additions & 4 deletions src/__test__/storeBackend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)(
Expand All @@ -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');
Expand Down
7 changes: 7 additions & 0 deletions src/fileStore/backends/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
);
},
};
};
18 changes: 10 additions & 8 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down Expand Up @@ -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 };
Expand Down
9 changes: 7 additions & 2 deletions src/store/backends/utils.js
Original file line number Diff line number Diff line change
@@ -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}`;
};
Expand Down Expand Up @@ -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;
},
};
};
61 changes: 44 additions & 17 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit aee1401

Please sign in to comment.