Skip to content

Commit

Permalink
Merge pull request #39 from rocicorp/delete-all
Browse files Browse the repository at this point in the history
Add a "delete all" function.
  • Loading branch information
aboodman authored Apr 5, 2021
2 parents 3c12dc1 + 0e20333 commit 583f289
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
6 changes: 6 additions & 0 deletions backend/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,9 @@ export async function delObject<T extends JSONValue>(
deleted: { booleanValue: true },
});
}

export async function delAllObjects<T extends JSONValue>(
executor: ExecuteStatementFn
): Promise<void> {
await executor(`CALL DeleteAllObjects()`, {});
}
7 changes: 7 additions & 0 deletions backend/rds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions frontend/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
32 changes: 9 additions & 23 deletions frontend/nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@ export function Nav({ data }: { data: Data | null }) {
});
};

const soon = () => {
alert("Coming soon… 😔");
};

return (
<div className={styles.nav} style={{}}>
<div
onClick={() => onRectangle()}
className={styles.button}
title="Rectangle"
title="Square"
>
<svg
width="18"
Expand All @@ -52,30 +48,20 @@ export function Nav({ data }: { data: Data | null }) {
></path>
</svg>
</div>
<div onClick={() => soon()} className={styles.button} title="Ellipse">
<svg
width="18"
height="18"
viewBox="0 0 18 18"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M9 17c4.418 0 8-3.582 8-8 0-4.418-3.582-8-8-8-4.418 0-8 3.582-8 8 0 4.418 3.582 8 8 8zm0 1c4.97 0 9-4.03 9-9 0-4.97-4.03-9-9-9-4.97 0-9 4.03-9 9 0 4.97 4.03 9 9 9z"
fillRule="evenodd"
fillOpacity="1"
fill="white"
></path>
</svg>
</div>
<div onClick={() => soon()} className={styles.button} title="Text">
<div
className={styles.button}
title="Clear All"
onClick={() => data?.deleteAllShapes()}
>
<svg
width="18"
height="18"
viewBox="0 0 18 18"
viewBox="1 1 14 14"
xmlns="http://www.w3.org/2000/svg"
transform="rotate(45 0 0)"
>
<path
d="M2 5h1V2h5v14H5v1h7v-1H9V2h5v3h1V1H2v4z"
d="M15 8V7H9V1H8v6H2v1h6v6h1V8h6z"
fillRule="nonzero"
fillOpacity="1"
fill="white"
Expand Down
7 changes: 7 additions & 0 deletions pages/api/replicache-push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
getObject,
putObject,
delObject,
delAllObjects,
getLastMutationID,
setLastMutationID,
} from "../../backend/data";
Expand Down Expand Up @@ -99,6 +100,10 @@ const mutation = t.union([
shapeID: t.string,
}),
}),
t.type({
id: t.number,
name: t.literal("deleteAllShapes"),
}),
]);

const pushRequest = t.type({
Expand Down Expand Up @@ -196,6 +201,8 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
case "selectShape":
await selectShape(s, mutation.args);
break;
case "deleteAllShapes":
await delAllObjects(executor);
}

lastMutationID = expectedMutationID;
Expand Down

0 comments on commit 583f289

Please sign in to comment.