db sync userid with clerk

This commit is contained in:
2024-06-18 19:47:13 -04:00
parent 15d2b4113d
commit 0b89e8ec18
6 changed files with 54 additions and 383 deletions
+8 -1
View File
@@ -1,7 +1,14 @@
import { syncUsers } from "@/db/db";
import { SignInButton, SignedOut } from "@clerk/nextjs"; import { SignInButton, SignedOut } from "@clerk/nextjs";
import { auth, currentUser } from "@clerk/nextjs/server";
import React from "react"; import React from "react";
const page = () => { const page = async () => {
const user = await currentUser();
if (user) {
syncUsers(user);
}
return ( return (
<div> <div>
<SignedOut> <SignedOut>
+4 -1
View File
@@ -3,7 +3,7 @@ import { VariantProps, cva } from "class-variance-authority";
import React from "react"; import React from "react";
const CalenderItemVariants = cva( const CalenderItemVariants = cva(
"border border-neutral-700 rounded-full cursor-pointer bg-opacity-50 hover:bg-opacity-70 px-1", "rounded-sm cursor-pointer bg-opacity-30 hover:bg-opacity-70",
{ {
variants: { variants: {
variant: { variant: {
@@ -47,7 +47,10 @@ const CalendarItem = ({
"flex justify-between p-1 font-semibold" "flex justify-between p-1 font-semibold"
)} )}
> >
<div className="flex gap-1">
<div className="w-1 bg-violet-500 rounded-full" />
<p>{title}</p> <p>{title}</p>
</div>
<p className={cn(size == "sm" ? "hidden xl:block" : "", "")}>{time}</p> <p className={cn(size == "sm" ? "hidden xl:block" : "", "")}>{time}</p>
</div> </div>
{description && <p className="p-1 font-light">{description}</p>} {description && <p className="p-1 font-light">{description}</p>}
+40 -2
View File
@@ -2,7 +2,9 @@ import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres"; import postgres from "postgres";
import * as schema from "./schema"; import * as schema from "./schema";
import "dotenv/config"; import "dotenv/config";
import { sql } from "drizzle-orm"; import { eq, sql } from "drizzle-orm";
import { User } from "@clerk/nextjs/server";
import { UsersTable } from "./schema";
// const connectionString = process.env.DATABASE_URL as string; // const connectionString = process.env.DATABASE_URL as string;
const connectionString = const connectionString =
@@ -13,4 +15,40 @@ export const client = postgres(connectionString, { prepare: false });
export const db = drizzle(client, { schema }); export const db = drizzle(client, { schema });
// Query Calls // DB Calls
export const syncUsers = async ({
id,
firstName,
lastName,
primaryEmailAddress,
primaryPhoneNumber,
}: User) => {
const existingUser = await db.query.UsersTable.findFirst({
where: (UsersTable, { eq }) => eq(UsersTable.userId, id),
});
if (primaryEmailAddress == null) {
throw new Error("No email address for user");
}
if (!existingUser) {
await db
.insert(UsersTable)
.values({
userId: id,
firstName: firstName,
lastName: lastName,
emailAddress: primaryEmailAddress.emailAddress,
phoneNumber: primaryPhoneNumber?.phoneNumber,
})
.onConflictDoUpdate({
target: UsersTable.userId,
set: {
firstName: firstName,
lastName: lastName,
emailAddress: primaryEmailAddress.emailAddress,
phoneNumber: primaryPhoneNumber?.phoneNumber,
},
});
}
};
-69
View File
@@ -1,69 +0,0 @@
DO $$ BEGIN
CREATE TYPE "public"."eventVariant" AS ENUM('earnings', 'conference', 'split', 'dividend', 'econonmic', 'default');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
CREATE TYPE "public"."themeMode" AS ENUM('light', 'dark', 'system');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "dates" (
"date" timestamp with time zone PRIMARY KEY NOT NULL,
CONSTRAINT "dates_date_unique" UNIQUE("date")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "events" (
"eventId" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"title" text NOT NULL,
"companyName" text NOT NULL,
"variant" "eventVariant" NOT NULL,
"time" timestamp with time zone NOT NULL,
"date" timestamp NOT NULL,
CONSTRAINT "events_companyName_unique" UNIQUE("companyName")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "symbols" (
"symbol" text PRIMARY KEY NOT NULL,
"companyName" text NOT NULL,
CONSTRAINT "symbols_symbol_unique" UNIQUE("symbol"),
CONSTRAINT "symbols_companyName_unique" UNIQUE("companyName")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "userPreferences" (
"userId" text PRIMARY KEY NOT NULL,
"theme" "themeMode" DEFAULT 'dark' NOT NULL,
"timezone" text DEFAULT 'America/New_York' NOT NULL,
"currencyUnits" text DEFAULT 'USD' NOT NULL,
CONSTRAINT "userPreferences_userId_unique" UNIQUE("userId")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "users" (
"userId" text PRIMARY KEY NOT NULL,
"firstName" text,
"lastName" text,
"emailAddress" text NOT NULL,
"phoneNumber" text,
CONSTRAINT "users_userId_unique" UNIQUE("userId"),
CONSTRAINT "users_emailAddress_unique" UNIQUE("emailAddress")
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "events" ADD CONSTRAINT "events_companyName_symbols_symbol_fk" FOREIGN KEY ("companyName") REFERENCES "public"."symbols"("symbol") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "events" ADD CONSTRAINT "events_date_dates_date_fk" FOREIGN KEY ("date") REFERENCES "public"."dates"("date") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "userPreferences" ADD CONSTRAINT "userPreferences_userId_users_userId_fk" FOREIGN KEY ("userId") REFERENCES "public"."users"("userId") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
-296
View File
@@ -1,296 +0,0 @@
{
"id": "5f48754b-8b16-4f50-9273-9fa024fcbaf1",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.dates": {
"name": "dates",
"schema": "",
"columns": {
"date": {
"name": "date",
"type": "timestamp with time zone",
"primaryKey": true,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"dates_date_unique": {
"name": "dates_date_unique",
"nullsNotDistinct": false,
"columns": [
"date"
]
}
}
},
"public.events": {
"name": "events",
"schema": "",
"columns": {
"eventId": {
"name": "eventId",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"title": {
"name": "title",
"type": "text",
"primaryKey": false,
"notNull": true
},
"companyName": {
"name": "companyName",
"type": "text",
"primaryKey": false,
"notNull": true
},
"variant": {
"name": "variant",
"type": "eventVariant",
"typeSchema": "public",
"primaryKey": false,
"notNull": true
},
"time": {
"name": "time",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true
},
"date": {
"name": "date",
"type": "timestamp",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {
"events_companyName_symbols_symbol_fk": {
"name": "events_companyName_symbols_symbol_fk",
"tableFrom": "events",
"tableTo": "symbols",
"columnsFrom": [
"companyName"
],
"columnsTo": [
"symbol"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"events_date_dates_date_fk": {
"name": "events_date_dates_date_fk",
"tableFrom": "events",
"tableTo": "dates",
"columnsFrom": [
"date"
],
"columnsTo": [
"date"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"events_companyName_unique": {
"name": "events_companyName_unique",
"nullsNotDistinct": false,
"columns": [
"companyName"
]
}
}
},
"public.symbols": {
"name": "symbols",
"schema": "",
"columns": {
"symbol": {
"name": "symbol",
"type": "text",
"primaryKey": true,
"notNull": true
},
"companyName": {
"name": "companyName",
"type": "text",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"symbols_symbol_unique": {
"name": "symbols_symbol_unique",
"nullsNotDistinct": false,
"columns": [
"symbol"
]
},
"symbols_companyName_unique": {
"name": "symbols_companyName_unique",
"nullsNotDistinct": false,
"columns": [
"companyName"
]
}
}
},
"public.userPreferences": {
"name": "userPreferences",
"schema": "",
"columns": {
"userId": {
"name": "userId",
"type": "text",
"primaryKey": true,
"notNull": true
},
"theme": {
"name": "theme",
"type": "themeMode",
"typeSchema": "public",
"primaryKey": false,
"notNull": true,
"default": "'dark'"
},
"timezone": {
"name": "timezone",
"type": "text",
"primaryKey": false,
"notNull": true,
"default": "'America/New_York'"
},
"currencyUnits": {
"name": "currencyUnits",
"type": "text",
"primaryKey": false,
"notNull": true,
"default": "'USD'"
}
},
"indexes": {},
"foreignKeys": {
"userPreferences_userId_users_userId_fk": {
"name": "userPreferences_userId_users_userId_fk",
"tableFrom": "userPreferences",
"tableTo": "users",
"columnsFrom": [
"userId"
],
"columnsTo": [
"userId"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"userPreferences_userId_unique": {
"name": "userPreferences_userId_unique",
"nullsNotDistinct": false,
"columns": [
"userId"
]
}
}
},
"public.users": {
"name": "users",
"schema": "",
"columns": {
"userId": {
"name": "userId",
"type": "text",
"primaryKey": true,
"notNull": true
},
"firstName": {
"name": "firstName",
"type": "text",
"primaryKey": false,
"notNull": false
},
"lastName": {
"name": "lastName",
"type": "text",
"primaryKey": false,
"notNull": false
},
"emailAddress": {
"name": "emailAddress",
"type": "text",
"primaryKey": false,
"notNull": true
},
"phoneNumber": {
"name": "phoneNumber",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"users_userId_unique": {
"name": "users_userId_unique",
"nullsNotDistinct": false,
"columns": [
"userId"
]
},
"users_emailAddress_unique": {
"name": "users_emailAddress_unique",
"nullsNotDistinct": false,
"columns": [
"emailAddress"
]
}
}
}
},
"enums": {
"public.eventVariant": {
"name": "eventVariant",
"schema": "public",
"values": [
"earnings",
"conference",
"split",
"dividend",
"econonmic",
"default"
]
},
"public.themeMode": {
"name": "themeMode",
"schema": "public",
"values": [
"light",
"dark",
"system"
]
}
},
"schemas": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
+1 -13
View File
@@ -1,13 +1 @@
{ {"version":"7","dialect":"postgresql","entries":[]}
"version": "7",
"dialect": "postgresql",
"entries": [
{
"idx": 0,
"version": "7",
"when": 1718598866339,
"tag": "0000_cute_exiles",
"breakpoints": true
}
]
}