Skip to content

Commit

Permalink
Added mock DB structure
Browse files Browse the repository at this point in the history
  • Loading branch information
fooware committed Feb 22, 2024
1 parent 60ffb82 commit 10e5ff7
Show file tree
Hide file tree
Showing 6 changed files with 17,019 additions and 734 deletions.
102 changes: 94 additions & 8 deletions db/migrations/01-create_items_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,97 @@
* See https://electric-sql.com/docs/usage/data-modelling for more information.
*/

-- Create a simple items table.
CREATE TABLE IF NOT EXISTS items (
value TEXT PRIMARY KEY NOT NULL
);

--
-- Electrify the items table
ALTER TABLE items ENABLE ELECTRIC;

BEGIN;

CREATE TABLE
"house" (
"id" UUID NOT NULL,
"name" TEXT NOT NULL,
CONSTRAINT "house_pkey" PRIMARY KEY ("id")
);

CREATE TABLE
"room" (
"id" UUID NOT NULL,
"name" TEXT NOT NULL,
"house_id" UUID NOT NULL,
"owner_id" UUID,
CONSTRAINT "level_one_pkey" PRIMARY KEY ("id")
);

CREATE TABLE
"box" (
"id" UUID NOT NULL,
"name" TEXT NOT NULL,
"room_id" UUID NOT NULL,
CONSTRAINT "box_pkey" PRIMARY KEY ("id")
);

CREATE TABLE
"thing" (
"id" UUID NOT NULL,
"name" TEXT NOT NULL,
"box_id" UUID NOT NULL,
CONSTRAINT "thing_pkey" PRIMARY KEY ("id")
);

CREATE TABLE
"person" (
"id" UUID NOT NULL,
"name" TEXT NOT NULL,
CONSTRAINT "person_pkey" PRIMARY KEY ("id")
);

CREATE TABLE
"room_user" (
"id" UUID NOT NULL,
"person_id" UUID NOT NULL,
CONSTRAINT "room_user_pkey" PRIMARY KEY ("id", "person_id")
);

CREATE TABLE
"box_user" (
"id" UUID NOT NULL,
"person_id" UUID NOT NULL,
CONSTRAINT "box_user_pkey" PRIMARY KEY ("id", "person_id")
);

-- Create indexes
CREATE INDEX "room_user_person_id_idx" ON "room_user" ("person_id");

CREATE INDEX "box_user_person_id_idx" ON "box_user" ("person_id");

-- Setup foreign key constraints
ALTER TABLE "room" ADD CONSTRAINT "room_house_id_fkey" FOREIGN KEY ("house_id") REFERENCES "house" ("id") ON DELETE RESTRICT ON UPDATE CASCADE;

ALTER TABLE "room" ADD CONSTRAINT "room_owner_id_fkey" FOREIGN KEY ("owner_id") REFERENCES "person" ("id") ON DELETE SET NULL ON UPDATE CASCADE;

ALTER TABLE "box" ADD CONSTRAINT "box_room_id_fkey" FOREIGN KEY ("room_id") REFERENCES "room" ("id") ON DELETE RESTRICT ON UPDATE CASCADE;

ALTER TABLE "thing" ADD CONSTRAINT "thing_box_id_fkey" FOREIGN KEY ("box_id") REFERENCES "box" ("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "room_user" ADD CONSTRAINT "room_user_id_fkey" FOREIGN KEY ("id") REFERENCES "room" ("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "room_user" ADD CONSTRAINT "room_user_person_id_fkey" FOREIGN KEY ("person_id") REFERENCES "person" ("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "box_user" ADD CONSTRAINT "box_user_id_fkey" FOREIGN KEY ("id") REFERENCES "box" ("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "box_user" ADD CONSTRAINT "box_user_person_id_fkey" FOREIGN KEY ("person_id") REFERENCES "person" ("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- Electrify all the rebel_v1 tables
ALTER TABLE "house" ENABLE ELECTRIC;

ALTER TABLE "room" ENABLE ELECTRIC;

ALTER TABLE "box" ENABLE ELECTRIC;

ALTER TABLE "thing" ENABLE ELECTRIC;

ALTER TABLE "person" ENABLE ELECTRIC;

ALTER TABLE "box_user" ENABLE ELECTRIC;

ALTER TABLE "room_user" ENABLE ELECTRIC;

COMMIT;
32 changes: 22 additions & 10 deletions src/Example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { genUUID, uniqueTabId } from "electric-sql/util";
import { ElectricDatabase, electrify } from "electric-sql/wa-sqlite";

import { authToken } from "./auth";
import { Electric, Items as Item, schema } from "./generated/client";
import { Electric, House, schema } from "./generated/client";

import "./Example.css";

Expand Down Expand Up @@ -58,33 +58,45 @@ export const Example = () => {

const ExampleComponent = () => {
const { db } = useElectric()!;
const { results } = useLiveQuery(db.items.liveMany());
const { results } = useLiveQuery(db.house.liveMany());

useEffect(() => {
const syncItems = async () => {
// Resolves when the shape subscription has been established.
const shape = await db.items.sync();
const shape = await db.house.sync({
include: {
room: {
include: {
person: true,
room_user: true,
box: { include: { stuff: true, box_user: true } },
},
},
},
});

// Resolves when the data has been synced into the local database.
await shape.synced;
};

syncItems();
}, []);
}, [db.house]);

const addItem = async () => {
await db.items.create({
const id = genUUID();
await db.house.create({
data: {
value: genUUID(),
id: id,
name: "house " + id.toString(),
},
});
};

const clearItems = async () => {
await db.items.deleteMany();
await db.house.deleteMany();
};

const items: Item[] = results ?? [];
const items: House[] = results ?? [];

return (
<div>
Expand All @@ -106,9 +118,9 @@ const ExampleComponent = () => {
Remove everything
</button>
</div>
{items.map((item: Item, index: number) => (
{items.map((house: House, index: number) => (
<p key={index} className="item">
<code>{item.value}</code>
<code>{house.name}</code>
</p>
))}
</div>
Expand Down
Loading

0 comments on commit 10e5ff7

Please sign in to comment.