-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(users): Add public.users table and new users to it
Add public.users table because supabase auth.users is private. Add users to it after sign up. Handle database migrations and queries with prisma. Add prisma (CLI) and @prisma/client dependencies.
- Loading branch information
Showing
14 changed files
with
321 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: deploy | ||
run-name: Reusable deploy workflow to deploy database changes to the target Supabase database. | ||
on: | ||
workflow_call: | ||
secrets: | ||
DATABASE_URL: | ||
required: true | ||
type: string | ||
DATABASE_DIRECT_URL: | ||
required: true | ||
type: string | ||
jobs: | ||
prisma-migrate-deploy: | ||
name: Run prisma migrations | ||
runs-on: ubuntu-latest | ||
env: | ||
DATABASE_URL: ${{ secrets.DATABASE_URL }} | ||
DATABASE_DIRECT_URL: ${{ secrets.DATABASE_DIRECT_URL }} | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
cache: 'npm' | ||
node-version-file: '.nvmrc' | ||
- run: npx [email protected] migrate deploy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name: staging-deploy | ||
run-name: Deploy database changes to Supabase's Staging database. | ||
on: | ||
push: | ||
branches-ignore: | ||
- 'main' | ||
jobs: | ||
call-deploy-workflow: | ||
name: Call reusable deploy workflow | ||
runs-on: ubuntu-latest | ||
environment: | ||
name: staging | ||
uses: ./.github/workflows/deploy.yml | ||
secrets: | ||
DATABASE_URL: ${{ secrets.DATABASE_URL }} | ||
DATABASE_DIRECT_URL: ${{ secrets.DATABASE_DIRECT_URL }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name: production-deploy | ||
run-name: Deploy database changes to Supabase's Production database. | ||
on: | ||
push: | ||
branches-ignore: | ||
- 'main' | ||
jobs: | ||
call-deploy-workflow: | ||
name: Call reusable deploy workflow | ||
runs-on: ubuntu-latest | ||
environment: | ||
name: production | ||
uses: ./.github/workflows/deploy.yml | ||
secrets: | ||
DATABASE_URL: ${{ secrets.DATABASE_URL }} | ||
DATABASE_DIRECT_URL: ${{ secrets.DATABASE_DIRECT_URL }} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
-- CreateTable | ||
CREATE TABLE "User" ( | ||
"id" UUID NOT NULL, | ||
"email" VARCHAR(254) NOT NULL, | ||
"name" VARCHAR(500), | ||
"provider" VARCHAR(100) NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3), | ||
|
||
CONSTRAINT "User_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "User_provider_key" ON "User"("provider"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Please do not edit this file manually | ||
# It should be added in your version-control system (i.e. Git) | ||
provider = "postgresql" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
directUrl = env("DATABASE_DIRECT_URL") | ||
} | ||
|
||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
// We use Supabase's authentication solution, which create users in a private "auth" schema. | ||
// So we create this User table in our "public" schema and add users to it after | ||
// they're created by Supabase's auth solution, using the same "id". | ||
model User { | ||
id String @id @db.Uuid | ||
email String @unique @db.VarChar(254) | ||
name String? @db.VarChar(500) | ||
provider String @unique @db.VarChar(100) | ||
createdAt DateTime @default(now()) | ||
updatedAt DateTime? @updatedAt | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { PrismaClient, User } from '@prisma/client'; | ||
|
||
export interface CreateUserData { | ||
readonly email: string; | ||
readonly id: string; | ||
readonly name?: string; | ||
readonly provider: string; | ||
} | ||
|
||
export const createUser = async (data: CreateUserData) => { | ||
const prisma = new PrismaClient(); | ||
|
||
const user = await prisma.user.findUnique({ where: { id: data.id } }); | ||
|
||
if (user) return user; | ||
|
||
const newUser = await prisma.user.create({ data }); | ||
return newUser; | ||
}; | ||
|
||
export const findUserById = async (id: string) => { | ||
const prisma = new PrismaClient(); | ||
return await prisma.user.findUnique({ where: { id } }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.