24
loading...
This website collects cookies to deliver better user experience
yarn add prisma
npx prisma init
schema.prisma
in a new prisma
directory.// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = "<YOUR_DB_CONNECTION_STRING>"
}
postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA
prisma/schema.prisma
file....
model User {
id Int @id @default(autoincrement())
firstName String
lastName String
email String
}
User
model to get us started. npx prisma migrate dev --name init
migrations
folder created in the prisma
directory. If you navigate to your project in the Supabase UI, you will see the are now tables created! One is named User
and was created from our migration. (Another table is _prisma_migrations
and is used by Prisma to internally keep track of which migrations have been created or rolledback). yarn add @prisma/client
prisma
directory called prismaClient.js
with the following code.const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
module.exports = prisma
seedDb.js
in the prisma
directory that imports the prismaClient
we just created and seeds the DB with some dummy data. faker
to create some fake names for the dummy data.const prisma = require('./prismaClient.js')
const faker = require('faker')
const users = []
function createUsers(){
const num = 100;
let x = 0;
while(x < 100){
const user = {
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
email: faker.internet.email(),
}
users.push(user)
x++
}
}
async function seedDb(){
await prisma.user.createMany({data: users})
}
async function main() {
createUsers()
await seedDb()
}
main().catch((e) => {
throw e
}).finally(async () => {
await prisma.$disconnect()
})
components
in the src
directory. src/components
directory, we will create a new component called Users.jsx
, which will looks like this:import { useEffect, useState } from "react";
import supabase from "../supabase";
export default function () {
const [loading, setLoading] = useState(true);
const [users, setUsers] = useState([]);
async function getUsers() {
const { data, error } = await supabase.from("User").select();
setUsers(u => u= data);
}
useEffect(() => {
setLoading(true);
getUsers();
setLoading(false);
}, []);
return (
<>
<h2>Users</h2>
{loading ? (
<p>loading...</p>
) : (
<>
{users?.length ? (
<ul>
{users.map((user) => (
<li>
{user.email} : {user.firstName} {user.lastName}
</li>
))}
</ul>
) : (
<p>No users currently</p>
)}
</>
)}
</>
);
}
App.js
file to look like this:import "./App.css";
import supabase from "./supabase";
import { useState, useEffect } from "react";
import Users from "./components/Users";
function App() {
const [user, setUser] = useState(null);
supabase.auth.onAuthStateChange((event, session) => {
if (session?.user) {
setUser((u) => (u = session.user));
}
});
async function signInWithGithub() {
const { user, session, error } = await supabase.auth.signIn({
provider: "github",
});
}
async function signOut() {
const { error } = await supabase.auth.signOut();
setUser((u) => (u = null));
}
return (
<div className="App">
{!user ? (
<button onClick={signInWithGithub}>Sign In With Github</button>
) : (
<>
<button onClick={signOut}>Log Out, {user?.email}</button>
<Users />
</>
)}
</div>
);
}
export default App;