diff --git a/bun.lockb b/bun.lockb index 367c336..e35a745 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/drizzle/0000_thankful_sasquatch.sql b/drizzle/0000_watery_zaladane.sql similarity index 83% rename from drizzle/0000_thankful_sasquatch.sql rename to drizzle/0000_watery_zaladane.sql index 04b27b7..c2670ed 100644 --- a/drizzle/0000_thankful_sasquatch.sql +++ b/drizzle/0000_watery_zaladane.sql @@ -14,25 +14,29 @@ CREATE TABLE IF NOT EXISTS "account" ( ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "child" ( - "id" uuid PRIMARY KEY NOT NULL, + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "name" varchar(256), + "email" varchar(256), "phone" varchar(256), "key" varchar(256), - "parent_id" uuid + "parent_id" uuid NOT NULL ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "device" ( - "id" uuid PRIMARY KEY NOT NULL, - "child_id" uuid, + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "device_id" varchar NOT NULL, + "child_id" uuid NOT NULL, "api_key" varchar NOT NULL, "pin" integer NOT NULL, "expires" timestamp DEFAULT now () + interval '1 hour', + CONSTRAINT "device_device_id_unique" UNIQUE("device_id"), CONSTRAINT "device_api_key_unique" UNIQUE("api_key") ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "ping" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "device_id" uuid NOT NULL, "latitude" double precision NOT NULL, "longitude" double precision NOT NULL, @@ -46,7 +50,7 @@ CREATE TABLE IF NOT EXISTS "session" ( ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "user" ( - "id" uuid PRIMARY KEY NOT NULL, + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "name" text, "email" text NOT NULL, "emailVerified" timestamp, diff --git a/drizzle/meta/0000_snapshot.json b/drizzle/meta/0000_snapshot.json index 9dfc748..96196e9 100644 --- a/drizzle/meta/0000_snapshot.json +++ b/drizzle/meta/0000_snapshot.json @@ -1,5 +1,5 @@ { - "id": "876770f8-cae4-4913-b299-855783c0b37c", + "id": "7ac914a6-b952-4605-8341-0b7f522e85a5", "prevId": "00000000-0000-0000-0000-000000000000", "version": "5", "dialect": "pg", @@ -110,7 +110,8 @@ "name": "id", "type": "uuid", "primaryKey": true, - "notNull": true + "notNull": true, + "default": "gen_random_uuid()" }, "name": { "name": "name", @@ -118,6 +119,12 @@ "primaryKey": false, "notNull": false }, + "email": { + "name": "email", + "type": "varchar(256)", + "primaryKey": false, + "notNull": false + }, "phone": { "name": "phone", "type": "varchar(256)", @@ -134,7 +141,7 @@ "name": "parent_id", "type": "uuid", "primaryKey": false, - "notNull": false + "notNull": true } }, "indexes": {}, @@ -150,13 +157,20 @@ "name": "id", "type": "uuid", "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "device_id": { + "name": "device_id", + "type": "varchar", + "primaryKey": false, "notNull": true }, "child_id": { "name": "child_id", "type": "uuid", "primaryKey": false, - "notNull": false + "notNull": true }, "api_key": { "name": "api_key", @@ -182,6 +196,13 @@ "foreignKeys": {}, "compositePrimaryKeys": {}, "uniqueConstraints": { + "device_device_id_unique": { + "name": "device_device_id_unique", + "nullsNotDistinct": false, + "columns": [ + "device_id" + ] + }, "device_api_key_unique": { "name": "device_api_key_unique", "nullsNotDistinct": false, @@ -195,6 +216,13 @@ "name": "ping", "schema": "", "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, "device_id": { "name": "device_id", "type": "uuid", @@ -275,7 +303,8 @@ "name": "id", "type": "uuid", "primaryKey": true, - "notNull": true + "notNull": true, + "default": "gen_random_uuid()" }, "name": { "name": "name", diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json index 195b37c..9f2ac7a 100644 --- a/drizzle/meta/_journal.json +++ b/drizzle/meta/_journal.json @@ -5,8 +5,8 @@ { "idx": 0, "version": "5", - "when": 1703070994335, - "tag": "0000_thankful_sasquatch", + "when": 1703185817079, + "tag": "0000_watery_zaladane", "breakpoints": true } ] diff --git a/package.json b/package.json index 30f28d5..057ef9b 100644 --- a/package.json +++ b/package.json @@ -5,13 +5,14 @@ "scripts": { "dev2": "next dev", "dev": "NODE_ENV=development next dev -p 3002 & local-ssl-proxy --key /etc/letsencrypt/live/dev.fergl.ie/privkey.pem --cert /etc/letsencrypt/live/dev.fergl.ie/fullchain.pem --source 3000 --target 3002", + "drizzle:migrate": "node --loader esbuild-register/loader -r esbuild-register ./src/db/migrate.ts", "drizzle:seed": "node --loader esbuild-register/loader -r esbuild-register ./src/db/seed.ts", "build": "next build", "start": "next start", "lint": "next lint" }, "dependencies": { - "@auth/drizzle-adapter": "^0.3.11", + "@auth/drizzle-adapter": "^0.3.12", "@hookform/resolvers": "^3.3.2", "@microsoft/signalr": "^8.0.0", "@radix-ui/react-accordion": "^1.1.2", @@ -42,20 +43,21 @@ "@radix-ui/react-tooltip": "^1.0.7", "@t3-oss/env-core": "^0.7.1", "@t3-oss/env-nextjs": "^0.7.1", - "@tanstack/react-query": "^5.14.1", - "@tanstack/react-query-devtools": "^5.14.1", + "@tanstack/react-query": "^5.14.2", + "@tanstack/react-query-devtools": "^5.14.5", "axios": "^1.6.2", "class-variance-authority": "^0.7.0", "clsx": "^2.0.0", "cmdk": "^0.2.0", - "date-fns": "^3.0.0", + "date-fns": "^3.0.5", "dotenv": "^16.3.1", "drizzle-orm": "^0.29.1", + "esbuild-register": "^3.5.0", "generate-api-key": "^1.0.2", "http-status-codes": "^2.3.0", "leaflet": "^1.9.4", "local-ssl-proxy": "^2.0.5", - "lucide-react": "^0.298.0", + "lucide-react": "^0.299.0", "next": "14.0.4", "next-auth": "^4.24.5", "next-themes": "^0.2.1", diff --git a/scripts/reset.sh b/scripts/reset.sh index c07a717..eec76c7 100755 --- a/scripts/reset.sh +++ b/scripts/reset.sh @@ -3,7 +3,6 @@ export PGUSER=postgres export PGPASSWORD=hackme export PGHOST=localhost - echo Removing migrations rm -rf drizzle echo "Dropping db" @@ -11,9 +10,9 @@ echo "Dropping db" dropdb -f --if-exists parentgrine echo "Creating db" createdb parentgrine -echo "Exiting" -exit bunx drizzle-kit generate:pg --config=./drizzle.config.ts bunx drizzle-kit push:pg --config=./drizzle.config.ts -bunx tsx src/db/migrate.ts + +# bun run src/db/migrate.ts +bun run ./src/db/scripts/seed.ts diff --git a/src/app/api/child/route.ts b/src/app/api/child/route.ts index cb0f4c8..5dfcefb 100644 --- a/src/app/api/child/route.ts +++ b/src/app/api/child/route.ts @@ -15,7 +15,9 @@ export async function GET(request: Request) { const activeChildren = await db.query.child.findMany({ where: eq(child.parentId, session.user.id), with: { - devices: true, + devices: { + with: { pings: true }, + }, }, }); diff --git a/src/app/api/device/connect/route.ts b/src/app/api/device/connect/route.ts index b504f07..1ac78e9 100644 --- a/src/app/api/device/connect/route.ts +++ b/src/app/api/device/connect/route.ts @@ -49,7 +49,7 @@ const POST = async (req: Request, res: Response) => { .execute(); return Response.json( { childId, deviceId, pin, apiKey }, - { status: StatusCodes.CREATED }, + { status: StatusCodes.CREATED } ); } return badRequest('Invalid registration request'); diff --git a/src/components/header/auth-header.tsx b/src/components/header/auth-header.tsx index ca9cd2b..b536e8c 100644 --- a/src/components/header/auth-header.tsx +++ b/src/components/header/auth-header.tsx @@ -15,6 +15,7 @@ import { Icons } from '../icons'; const AuthHeader = () => { const { data: session, status } = useSession(); + if (status === 'loading') return null; return !session ? ( - - - {siteConfig.name} +
+ + + {siteConfig.name} {items?.length ? ( -