From 0e203338dca6736feabd67fb29598c2053b58cee Mon Sep 17 00:00:00 2001 From: Aaron Boodman Date: Sun, 4 Apr 2021 02:21:29 -1000 Subject: [PATCH] Add a "delete all" function. --- backend/data.ts | 6 ++++++ backend/rds.ts | 7 +++++++ frontend/data.ts | 9 +++++++++ frontend/nav.tsx | 32 +++++++++----------------------- pages/api/replicache-push.ts | 7 +++++++ 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/backend/data.ts b/backend/data.ts index 5c4dcc5..19e364d 100644 --- a/backend/data.ts +++ b/backend/data.ts @@ -79,3 +79,9 @@ export async function delObject( deleted: { booleanValue: true }, }); } + +export async function delAllObjects( + executor: ExecuteStatementFn +): Promise { + await executor(`CALL DeleteAllObjects()`, {}); +} diff --git a/backend/rds.ts b/backend/rds.ts index 89df6f5..b6b7fe5 100644 --- a/backend/rds.ts +++ b/backend/rds.ts @@ -132,6 +132,13 @@ export async function createDatabase() { INSERT INTO Object (K, V, Deleted, Version) VALUES (pK, pV, pDeleted, @version) ON DUPLICATE KEY UPDATE V = pV, Deleted = pDeleted, Version = @version; END`); + + await executeStatement(`CREATE PROCEDURE DeleteAllObjects () + BEGIN + SET @version = 0; + CALL NextVersion(@version); + UPDATE Object SET Deleted = True, Version = @version WHERE K LIKE 'shape-%'; + END`); } async function executeStatement( diff --git a/frontend/data.ts b/frontend/data.ts index 6637377..a6e9d18 100644 --- a/frontend/data.ts +++ b/frontend/data.ts @@ -123,6 +123,15 @@ export async function createData(rep: Replicache) { } ), + deleteAllShapes: rep.register( + "deleteAllShapes", + async (tx: WriteTransaction) => { + await Promise.all( + (await tx.scanAll({ prefix: `shape-` })).map(([k]) => tx.del(k)) + ); + } + ), + // subscriptions useShapeIDs: () => subscribe([], async (tx: ReadTransaction) => { diff --git a/frontend/nav.tsx b/frontend/nav.tsx index 8ae6631..5e49052 100644 --- a/frontend/nav.tsx +++ b/frontend/nav.tsx @@ -28,16 +28,12 @@ export function Nav({ data }: { data: Data | null }) { }); }; - const soon = () => { - alert("Coming soon… 😔"); - }; - return (
onRectangle()} className={styles.button} - title="Rectangle" + title="Square" >
-
soon()} className={styles.button} title="Ellipse"> - - - -
-
soon()} className={styles.button} title="Text"> +
data?.deleteAllShapes()} + > { case "selectShape": await selectShape(s, mutation.args); break; + case "deleteAllShapes": + await delAllObjects(executor); } lastMutationID = expectedMutationID;